1 package ro
.ieval
.fonbot
;
3 import static ro
.ieval
.fonbot
.R
.string
.*;
4 import static ro
.ieval
.fonbot
.Utils
.toNonNull
;
6 import java
.io
.OutputStream
;
7 import java
.net
.HttpURLConnection
;
9 import java
.util
.Collection
;
11 import org
.eclipse
.jdt
.annotation
.Nullable
;
13 import android
.content
.Context
;
14 import android
.content
.Intent
;
15 import android
.os
.AsyncTask
;
16 import android
.preference
.PreferenceManager
;
17 import android
.util
.Base64
;
18 import android
.util
.Log
;
20 import com
.google
.android
.gcm
.GCMRegistrar
;
23 * Copyright © 2013 Marius Gavrilescu
25 * This file is part of FonBot.
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.
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.
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/>.
42 * AsyncTask that sends a HTTP request to the server and broadcasts back the response message & response code
44 * @author Marius Gavrilescu <marius@ieval.ro>
46 public final class SendHttpMessageAsyncTask
extends AsyncTask
<String
, Void
, String
> {
48 * Extra: the response message
50 public static final String EXTRA_RESPONSE_MESSAGE
="response_message";
52 * Extra: the response code
54 public static final String EXTRA_RESPONSE_CODE
="response_code";
57 * List of extra request headers.
59 private final Collection
<Header
> headers
;
61 * Action to send back in the response broadcast
63 private final String broadcast
;
65 * Context instance used by this class
67 private final Context context
;
69 * Data to send to the server
71 private final byte[] data
;
75 private final String path
;
77 * Response code from the server
79 private int responseCode
=0;
82 * Constructs a SendHttpMessageAsyncTask which sends a string-based message and does not broadcast the response.
84 * @param path URL path
85 * @param headers the extra headers
86 * @param context the context instance
88 public SendHttpMessageAsyncTask(final String path
, final Collection
<Header
> headers
, final Context context
){
92 this.broadcast
=null;//NOPMD final field
94 this.data
=null;//NOPMD final field
98 * Constructs a SendHttpMessageAsyncTask which sends a binary message and does not broadcast the response.
100 * @param path URL path
101 * @param headers the extra headers
102 * @param context the context instance
103 * @param data the message to send
105 public SendHttpMessageAsyncTask(final String path
, final Collection
<Header
> headers
, final Context context
, final byte[] data
){//NOPMD array is supposed to be immutable.
108 this.headers
=headers
;
109 this.broadcast
=null;//NOPMD final field
110 this.context
=context
;
115 * Constructs a SendHttpMessageAsyncTask which sends a string-based message and broadcasts the response.
117 * @param path URL path
118 * @param headers the extra headers
119 * @param broadcast the broadcast to send
120 * @param context the context instance
122 public SendHttpMessageAsyncTask(final String path
, final Collection
<Header
> headers
, final String broadcast
, final Context context
){
125 this.headers
=headers
;
126 this.broadcast
=broadcast
;
127 this.context
=context
;
128 this.data
=null;//NOPMD final field
132 protected String
doInBackground(final @Nullable String
... args
) {
137 if(data
==null && args
.length
> 0)
138 msg
=Utils
.join(" ", args
).getBytes();
145 final URL url
=Utils
.getServerURL(toNonNull(context
),toNonNull(path
));
146 final HttpURLConnection conn
=(HttpURLConnection
) url
.openConnection();
148 conn
.setDoOutput(true);
149 conn
.setFixedLengthStreamingMode(msg
.length
);
151 conn
.setRequestProperty("X-ID", GCMRegistrar
.getRegistrationId(context
));
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
));
157 conn
.setRequestProperty("Authorization", "Basic "+Base64
.encodeToString(
158 (user
+':'+password
).getBytes(), Base64
.NO_WRAP
));
159 for (Header header
: headers
)
160 conn
.setRequestProperty(header
.name
, header
.value
);
163 final OutputStream stream
=conn
.getOutputStream();
167 Log
.d(getClass().getName(),"HTTP Response: "+conn
.getResponseCode()+" "+conn
.getResponseMessage());
169 final String message
=conn
.getResponseMessage();
170 responseCode
=conn
.getResponseCode();
172 return toNonNull(context
.getString(no_response_returned_from_server
));
173 return Utils
.parseHttpMessage(message
);
174 } catch (Exception e
) {
176 return toNonNull(context
.getString(connection_error
));
181 protected void onPostExecute(final @Nullable String result
) {
182 super.onPostExecute(result
);
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
);