Remove the global FonBotApplication instance.
[fonbot.git] / src / ro / ieval / fonbot / SendHttpMessageAsyncTask.java
1 package ro.ieval.fonbot;
2
3 import static ro.ieval.fonbot.R.string.*;
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;
16 import android.preference.PreferenceManager;
17 import android.util.Base64;
18 import android.util.Log;
19
20 import com.google.android.gcm.GCMRegistrar;
21
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;
72 /**
73 * Server URL path
74 */
75 private final String path;
76 /**
77 * Response code from the server
78 */
79 private int responseCode=0;
80
81 /**
82 * Constructs a SendHttpMessageAsyncTask which sends a string-based message and does not broadcast the response.
83 *
84 * @param path URL path
85 * @param headers the extra headers
86 * @param context the context instance
87 */
88 public SendHttpMessageAsyncTask(final String path, final Collection<Header> headers, final Context context){
89 super();
90 this.path=path;
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 *
100 * @param path URL path
101 * @param headers the extra headers
102 * @param context the context instance
103 * @param data the message to send
104 */
105 public SendHttpMessageAsyncTask(final String path, final Collection<Header> headers, final Context context, final byte[] data){//NOPMD array is supposed to be immutable.
106 super();
107 this.path=path;
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 *
117 * @param path URL path
118 * @param headers the extra headers
119 * @param broadcast the broadcast to send
120 * @param context the context instance
121 */
122 public SendHttpMessageAsyncTask(final String path, final Collection<Header> headers, final String broadcast, final Context context){
123 super();
124 this.path=path;
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;
137 if(data==null && args.length > 0)
138 msg=Utils.join(" ", args).getBytes();
139 else if(data!=null)
140 msg=data;
141 else
142 msg=null;
143
144 try {
145 final URL url=Utils.getServerURL(toNonNull(context),toNonNull(path));
146 final HttpURLConnection conn=(HttpURLConnection) url.openConnection();
147 if(msg!=null){
148 conn.setDoOutput(true);
149 conn.setFixedLengthStreamingMode(msg.length);
150 }
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));
156
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);
161 conn.connect();
162 if(msg!=null){
163 final OutputStream stream=conn.getOutputStream();
164 stream.write(msg);
165 stream.close();
166 }
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) {
175 e.printStackTrace();
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 }
This page took 0.026772 seconds and 4 git commands to generate.