Upgrade to the latest server protocol.
[fonbot.git] / src / ro / ieval / fonbot / SendHttpMessageAsyncTask.java
CommitLineData
8dfb76c9
MG
1package ro.ieval.fonbot;
2
3import static ro.ieval.fonbot.R.string.*;
8dfb76c9
MG
4import static ro.ieval.fonbot.Utils.toNonNull;
5
6import java.io.OutputStream;
7import java.net.HttpURLConnection;
8import java.net.URL;
9import java.util.Collection;
10
11import org.eclipse.jdt.annotation.Nullable;
12
13import android.content.Context;
14import android.content.Intent;
15import android.os.AsyncTask;
d13f1533
MG
16import android.preference.PreferenceManager;
17import android.util.Base64;
8dfb76c9
MG
18import 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 */
44public 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;
d13f1533
MG
70 /**
71 * Server URL path
72 */
73 private final String path;
8dfb76c9
MG
74 /**
75 * Response code from the server
76 */
d13f1533 77 private int responseCode=0;
8dfb76c9
MG
78
79 /**
80 * Constructs a SendHttpMessageAsyncTask which sends a string-based message and does not broadcast the response.
81 *
d13f1533 82 * @param path URL path
8dfb76c9
MG
83 * @param headers the extra headers
84 * @param context the context instance
85 */
d13f1533 86 public SendHttpMessageAsyncTask(final String path, final Collection<Header> headers, final Context context){
8dfb76c9 87 super();
d13f1533 88 this.path=path;
8dfb76c9
MG
89 this.headers=headers;
90 this.broadcast=null;//NOPMD final field
91 this.context=context;
92 this.data=null;//NOPMD final field
93 }
94
95 /**
96 * Constructs a SendHttpMessageAsyncTask which sends a binary message and does not broadcast the response.
97 *
d13f1533 98 * @param path URL path
8dfb76c9
MG
99 * @param headers the extra headers
100 * @param context the context instance
101 * @param data the message to send
102 */
d13f1533 103 public SendHttpMessageAsyncTask(final String path, final Collection<Header> headers, final Context context, final byte[] data){//NOPMD array is supposed to be immutable.
8dfb76c9 104 super();
d13f1533 105 this.path=path;
8dfb76c9
MG
106 this.headers=headers;
107 this.broadcast=null;//NOPMD final field
108 this.context=context;
109 this.data=data;
110 }
111
112 /**
113 * Constructs a SendHttpMessageAsyncTask which sends a string-based message and broadcasts the response.
114 *
d13f1533 115 * @param path URL path
8dfb76c9
MG
116 * @param headers the extra headers
117 * @param broadcast the broadcast to send
118 * @param context the context instance
119 */
d13f1533 120 public SendHttpMessageAsyncTask(final String path, final Collection<Header> headers, final String broadcast, final Context context){
8dfb76c9 121 super();
d13f1533 122 this.path=path;
8dfb76c9
MG
123 this.headers=headers;
124 this.broadcast=broadcast;
125 this.context=context;
126 this.data=null;//NOPMD final field
127 }
128
129 @Override
130 protected String doInBackground(final @Nullable String... args) {
131 if(args==null)
132 return "";
133
134 final byte[] msg;
d13f1533 135 if(data==null && args.length > 0)
8dfb76c9 136 msg=Utils.join(" ", args).getBytes();
d13f1533 137 else if(data!=null)
8dfb76c9 138 msg=data;
d13f1533
MG
139 else
140 msg=null;
8dfb76c9
MG
141
142 try {
d13f1533 143 final URL url=Utils.getServerURL(toNonNull(context),toNonNull(path));
8dfb76c9 144 final HttpURLConnection conn=(HttpURLConnection) url.openConnection();
d13f1533
MG
145 if(msg!=null){
146 conn.setDoOutput(true);
147 conn.setFixedLengthStreamingMode(msg.length);
148 }
8dfb76c9 149 conn.setRequestProperty("X-ID", FonBotApplication.instance.regID);
d13f1533
MG
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));
154
155 conn.setRequestProperty("Authorization", "Basic "+Base64.encodeToString(
156 (user+':'+password).getBytes(), Base64.NO_WRAP));
8dfb76c9
MG
157 for (Header header : headers)
158 conn.setRequestProperty(header.name, header.value);
159 conn.connect();
d13f1533
MG
160 if(msg!=null){
161 final OutputStream stream=conn.getOutputStream();
162 stream.write(msg);
163 stream.close();
164 }
8dfb76c9
MG
165 Log.d(getClass().getName(),"HTTP Response: "+conn.getResponseCode()+" "+conn.getResponseMessage());
166 conn.disconnect();
167 final String message=conn.getResponseMessage();
168 responseCode=conn.getResponseCode();
169 if(message==null)
170 return toNonNull(context.getString(no_response_returned_from_server));
171 return Utils.parseHttpMessage(message);
172 } catch (Exception e) {
a97d31fb 173 e.printStackTrace();
8dfb76c9
MG
174 return toNonNull(context.getString(connection_error));
175 }
176 }
177
178 @Override
179 protected void onPostExecute(final @Nullable String result) {
180 super.onPostExecute(result);
181 if(broadcast!=null){
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);
186 }
187 }
188}
This page took 0.021204 seconds and 4 git commands to generate.