Commit | Line | Data |
---|---|---|
8dfb76c9 MG |
1 | package ro.ieval.fonbot; |
2 | ||
3 | import static ro.ieval.fonbot.R.string.*; | |
4 | ||
5 | import static ro.ieval.fonbot.Utils.toNonNull; | |
6 | ||
7 | import java.io.OutputStream; | |
8 | import java.net.HttpURLConnection; | |
9 | import java.net.URL; | |
10 | import java.util.Collection; | |
11 | ||
12 | import org.eclipse.jdt.annotation.Nullable; | |
13 | ||
14 | import android.content.Context; | |
15 | import android.content.Intent; | |
16 | import android.os.AsyncTask; | |
957f7118 | 17 | import android.preference.PreferenceManager; |
8dfb76c9 MG |
18 | import android.util.Log; |
19 | ||
20 | /* | |
21 | * Copyright © 2013 Marius Gavrilescu | |
22 | * | |
23 | * This file is part of FonBot. | |
24 | * | |
25 | * FonBot is free software: you can redistribute it and/or modify | |
26 | * it under the terms of the GNU General Public License as published by | |
27 | * the Free Software Foundation, either version 3 of the License, or | |
28 | * (at your option) any later version. | |
29 | * | |
30 | * FonBot is distributed in the hope that it will be useful, | |
31 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
32 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
33 | * GNU General Public License for more details. | |
34 | * | |
35 | * You should have received a copy of the GNU General Public License | |
36 | * along with FonBot. If not, see <http://www.gnu.org/licenses/>. | |
37 | */ | |
38 | ||
39 | /** | |
40 | * AsyncTask that sends a HTTP request to the server and broadcasts back the response message & response code | |
41 | * | |
42 | * @author Marius Gavrilescu <marius@ieval.ro> | |
43 | */ | |
44 | public final class SendHttpMessageAsyncTask extends AsyncTask<String, Void, String> { | |
45 | /** | |
46 | * Extra: the response message | |
47 | */ | |
48 | public static final String EXTRA_RESPONSE_MESSAGE="response_message"; | |
49 | /** | |
50 | * Extra: the response code | |
51 | */ | |
52 | public static final String EXTRA_RESPONSE_CODE="response_code"; | |
53 | ||
54 | /** | |
55 | * List of extra request headers. | |
56 | */ | |
57 | private final Collection<Header> headers; | |
58 | /** | |
59 | * Action to send back in the response broadcast | |
60 | */ | |
61 | private final String broadcast; | |
62 | /** | |
63 | * Context instance used by this class | |
64 | */ | |
65 | private final Context context; | |
66 | /** | |
67 | * Data to send to the server | |
68 | */ | |
69 | private final byte[] data; | |
70 | /** | |
71 | * Response code from the server | |
72 | */ | |
73 | private int responseCode; | |
74 | ||
75 | /** | |
76 | * Constructs a SendHttpMessageAsyncTask which sends a string-based message and does not broadcast the response. | |
77 | * | |
78 | * @param headers the extra headers | |
79 | * @param context the context instance | |
80 | */ | |
81 | public SendHttpMessageAsyncTask(final Collection<Header> headers, final Context context){ | |
82 | super(); | |
83 | this.headers=headers; | |
84 | this.broadcast=null;//NOPMD final field | |
85 | this.context=context; | |
86 | this.data=null;//NOPMD final field | |
87 | } | |
88 | ||
89 | /** | |
90 | * Constructs a SendHttpMessageAsyncTask which sends a binary message and does not broadcast the response. | |
91 | * | |
92 | * @param headers the extra headers | |
93 | * @param context the context instance | |
94 | * @param data the message to send | |
95 | */ | |
96 | public SendHttpMessageAsyncTask(final Collection<Header> headers, final Context context, final byte[] data){//NOPMD array is supposed to be immutable. | |
97 | super(); | |
98 | this.headers=headers; | |
99 | this.broadcast=null;//NOPMD final field | |
100 | this.context=context; | |
101 | this.data=data; | |
102 | } | |
103 | ||
104 | /** | |
105 | * Constructs a SendHttpMessageAsyncTask which sends a string-based message and broadcasts the response. | |
106 | * | |
107 | * @param headers the extra headers | |
108 | * @param broadcast the broadcast to send | |
109 | * @param context the context instance | |
110 | */ | |
111 | public SendHttpMessageAsyncTask(final Collection<Header> headers, final String broadcast, final Context context){ | |
112 | super(); | |
113 | this.headers=headers; | |
114 | this.broadcast=broadcast; | |
115 | this.context=context; | |
116 | this.data=null;//NOPMD final field | |
117 | } | |
118 | ||
119 | @Override | |
120 | protected String doInBackground(final @Nullable String... args) { | |
121 | if(args==null) | |
122 | return ""; | |
123 | ||
124 | final byte[] msg; | |
125 | if(data==null) | |
126 | msg=Utils.join(" ", args).getBytes(); | |
127 | else | |
128 | msg=data; | |
129 | ||
130 | try { | |
957f7118 MG |
131 | final String hostname=PreferenceManager.getDefaultSharedPreferences(context).getString("hostname", "ieval.ro"); |
132 | final int port=Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(context).getString("port", "7777")); | |
133 | final URL url=new URL("http", hostname, port, "/"); | |
8dfb76c9 MG |
134 | final HttpURLConnection conn=(HttpURLConnection) url.openConnection(); |
135 | conn.setDoOutput(true); | |
136 | conn.setFixedLengthStreamingMode(msg.length); | |
137 | conn.setRequestProperty("X-ID", FonBotApplication.instance.regID); | |
138 | for (Header header : headers) | |
139 | conn.setRequestProperty(header.name, header.value); | |
140 | conn.connect(); | |
141 | final OutputStream stream=conn.getOutputStream(); | |
142 | stream.write(msg); | |
143 | stream.close(); | |
144 | Log.d(getClass().getName(),"HTTP Response: "+conn.getResponseCode()+" "+conn.getResponseMessage()); | |
145 | conn.disconnect(); | |
146 | final String message=conn.getResponseMessage(); | |
147 | responseCode=conn.getResponseCode(); | |
148 | if(message==null) | |
149 | return toNonNull(context.getString(no_response_returned_from_server)); | |
150 | return Utils.parseHttpMessage(message); | |
151 | } catch (Exception e) { | |
152 | return toNonNull(context.getString(connection_error)); | |
153 | } | |
154 | } | |
155 | ||
156 | @Override | |
157 | protected void onPostExecute(final @Nullable String result) { | |
158 | super.onPostExecute(result); | |
159 | if(broadcast!=null){ | |
160 | final Intent intent=new Intent(broadcast); | |
161 | intent.putExtra(EXTRA_RESPONSE_MESSAGE, result); | |
162 | intent.putExtra(EXTRA_RESPONSE_CODE, responseCode); | |
163 | context.sendBroadcast(intent); | |
164 | } | |
165 | } | |
166 | } |