]> iEval git - fonbot.git/blob - src/ro/ieval/fonbot/SendHttpMessageAsyncTask.java
Make custom server work when polling
[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.util.Log;
17
18 /*
19 * Copyright © 2013 Marius Gavrilescu
20 *
21 * This file is part of FonBot.
22 *
23 * FonBot is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
27 *
28 * FonBot is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with FonBot. If not, see <http://www.gnu.org/licenses/>.
35 */
36
37 /**
38 * AsyncTask that sends a HTTP request to the server and broadcasts back the response message & response code
39 *
40 * @author Marius Gavrilescu <marius@ieval.ro>
41 */
42 public final class SendHttpMessageAsyncTask extends AsyncTask<String, Void, String> {
43 /**
44 * Extra: the response message
45 */
46 public static final String EXTRA_RESPONSE_MESSAGE="response_message";
47 /**
48 * Extra: the response code
49 */
50 public static final String EXTRA_RESPONSE_CODE="response_code";
51
52 /**
53 * List of extra request headers.
54 */
55 private final Collection<Header> headers;
56 /**
57 * Action to send back in the response broadcast
58 */
59 private final String broadcast;
60 /**
61 * Context instance used by this class
62 */
63 private final Context context;
64 /**
65 * Data to send to the server
66 */
67 private final byte[] data;
68 /**
69 * Response code from the server
70 */
71 private int responseCode;
72
73 /**
74 * Constructs a SendHttpMessageAsyncTask which sends a string-based message and does not broadcast the response.
75 *
76 * @param headers the extra headers
77 * @param context the context instance
78 */
79 public SendHttpMessageAsyncTask(final Collection<Header> headers, final Context context){
80 super();
81 this.headers=headers;
82 this.broadcast=null;//NOPMD final field
83 this.context=context;
84 this.data=null;//NOPMD final field
85 }
86
87 /**
88 * Constructs a SendHttpMessageAsyncTask which sends a binary message and does not broadcast the response.
89 *
90 * @param headers the extra headers
91 * @param context the context instance
92 * @param data the message to send
93 */
94 public SendHttpMessageAsyncTask(final Collection<Header> headers, final Context context, final byte[] data){//NOPMD array is supposed to be immutable.
95 super();
96 this.headers=headers;
97 this.broadcast=null;//NOPMD final field
98 this.context=context;
99 this.data=data;
100 }
101
102 /**
103 * Constructs a SendHttpMessageAsyncTask which sends a string-based message and broadcasts the response.
104 *
105 * @param headers the extra headers
106 * @param broadcast the broadcast to send
107 * @param context the context instance
108 */
109 public SendHttpMessageAsyncTask(final Collection<Header> headers, final String broadcast, final Context context){
110 super();
111 this.headers=headers;
112 this.broadcast=broadcast;
113 this.context=context;
114 this.data=null;//NOPMD final field
115 }
116
117 @Override
118 protected String doInBackground(final @Nullable String... args) {
119 if(args==null)
120 return "";
121
122 final byte[] msg;
123 if(data==null)
124 msg=Utils.join(" ", args).getBytes();
125 else
126 msg=data;
127
128 try {
129 final URL url=Utils.getServerURL(toNonNull(context),"/");
130 final HttpURLConnection conn=(HttpURLConnection) url.openConnection();
131 conn.setDoOutput(true);
132 conn.setFixedLengthStreamingMode(msg.length);
133 conn.setRequestProperty("X-ID", FonBotApplication.instance.regID);
134 for (Header header : headers)
135 conn.setRequestProperty(header.name, header.value);
136 conn.connect();
137 final OutputStream stream=conn.getOutputStream();
138 stream.write(msg);
139 stream.close();
140 Log.d(getClass().getName(),"HTTP Response: "+conn.getResponseCode()+" "+conn.getResponseMessage());
141 conn.disconnect();
142 final String message=conn.getResponseMessage();
143 responseCode=conn.getResponseCode();
144 if(message==null)
145 return toNonNull(context.getString(no_response_returned_from_server));
146 return Utils.parseHttpMessage(message);
147 } catch (Exception e) {
148 e.printStackTrace();
149 return toNonNull(context.getString(connection_error));
150 }
151 }
152
153 @Override
154 protected void onPostExecute(final @Nullable String result) {
155 super.onPostExecute(result);
156 if(broadcast!=null){
157 final Intent intent=new Intent(broadcast);
158 intent.putExtra(EXTRA_RESPONSE_MESSAGE, result);
159 intent.putExtra(EXTRA_RESPONSE_CODE, responseCode);
160 context.sendBroadcast(intent);
161 }
162 }
163 }
This page took 0.051535 seconds and 4 git commands to generate.