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