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