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
;
21 * Copyright © 2013 Marius Gavrilescu
23 * This file is part of FonBot.
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.
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.
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/>.
40 * AsyncTask that sends a HTTP request to the server and broadcasts back the response message & response code
42 * @author Marius Gavrilescu <marius@ieval.ro>
44 public final class SendHttpMessageAsyncTask
extends AsyncTask
<String
, Void
, String
> {
46 * Extra: the response message
48 public static final String EXTRA_RESPONSE_MESSAGE
="response_message";
50 * Extra: the response code
52 public static final String EXTRA_RESPONSE_CODE
="response_code";
55 * List of extra request headers.
57 private final Collection
<Header
> headers
;
59 * Action to send back in the response broadcast
61 private final String broadcast
;
63 * Context instance used by this class
65 private final Context context
;
67 * Data to send to the server
69 private final byte[] data
;
73 private final String path
;
75 * Response code from the server
77 private int responseCode
=0;
80 * Constructs a SendHttpMessageAsyncTask which sends a string-based message and does not broadcast the response.
82 * @param path URL path
83 * @param headers the extra headers
84 * @param context the context instance
86 public SendHttpMessageAsyncTask(final String path
, final Collection
<Header
> headers
, final Context context
){
90 this.broadcast
=null;//NOPMD final field
92 this.data
=null;//NOPMD final field
96 * Constructs a SendHttpMessageAsyncTask which sends a binary message and does not broadcast the response.
98 * @param path URL path
99 * @param headers the extra headers
100 * @param context the context instance
101 * @param data the message to send
103 public SendHttpMessageAsyncTask(final String path
, final Collection
<Header
> headers
, final Context context
, final byte[] data
){//NOPMD array is supposed to be immutable.
106 this.headers
=headers
;
107 this.broadcast
=null;//NOPMD final field
108 this.context
=context
;
113 * Constructs a SendHttpMessageAsyncTask which sends a string-based message and broadcasts the response.
115 * @param path URL path
116 * @param headers the extra headers
117 * @param broadcast the broadcast to send
118 * @param context the context instance
120 public SendHttpMessageAsyncTask(final String path
, final Collection
<Header
> headers
, final String broadcast
, final Context context
){
123 this.headers
=headers
;
124 this.broadcast
=broadcast
;
125 this.context
=context
;
126 this.data
=null;//NOPMD final field
130 protected String
doInBackground(final @Nullable String
... args
) {
135 if(data
==null && args
.length
> 0)
136 msg
=Utils
.join(" ", args
).getBytes();
143 final URL url
=Utils
.getServerURL(toNonNull(context
),toNonNull(path
));
144 final HttpURLConnection conn
=(HttpURLConnection
) url
.openConnection();
146 conn
.setDoOutput(true);
147 conn
.setFixedLengthStreamingMode(msg
.length
);
149 conn
.setRequestProperty("X-ID", FonBotApplication
.instance
.regID
);
150 final String user
=PreferenceManager
.getDefaultSharedPreferences(context
).getString("username", null);
151 final String password
=PreferenceManager
.getDefaultSharedPreferences(context
).getString("password", null);
152 if(user
== null || password
== null)
153 return toNonNull(context
.getString(user_or_password_not_set
));
155 conn
.setRequestProperty("Authorization", "Basic "+Base64
.encodeToString(
156 (user
+':'+password
).getBytes(), Base64
.NO_WRAP
));
157 for (Header header
: headers
)
158 conn
.setRequestProperty(header
.name
, header
.value
);
161 final OutputStream stream
=conn
.getOutputStream();
165 Log
.d(getClass().getName(),"HTTP Response: "+conn
.getResponseCode()+" "+conn
.getResponseMessage());
167 final String message
=conn
.getResponseMessage();
168 responseCode
=conn
.getResponseCode();
170 return toNonNull(context
.getString(no_response_returned_from_server
));
171 return Utils
.parseHttpMessage(message
);
172 } catch (Exception e
) {
174 return toNonNull(context
.getString(connection_error
));
179 protected void onPostExecute(final @Nullable String result
) {
180 super.onPostExecute(result
);
182 final Intent intent
=new Intent(broadcast
);
183 intent
.putExtra(EXTRA_RESPONSE_MESSAGE
, result
);
184 intent
.putExtra(EXTRA_RESPONSE_CODE
, responseCode
);
185 context
.sendBroadcast(intent
);