]>
Commit | Line | Data |
---|---|---|
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 | /* | |
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 | */ | |
44 | public 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; | |
70 | /** | |
71 | * Server URL path | |
72 | */ | |
73 | private final String path; | |
74 | /** | |
75 | * Response code from the server | |
76 | */ | |
77 | private int responseCode=0; | |
78 | ||
79 | /** | |
80 | * Constructs a SendHttpMessageAsyncTask which sends a string-based message and does not broadcast the response. | |
81 | * | |
82 | * @param path URL path | |
83 | * @param headers the extra headers | |
84 | * @param context the context instance | |
85 | */ | |
86 | public SendHttpMessageAsyncTask(final String path, final Collection<Header> headers, final Context context){ | |
87 | super(); | |
88 | this.path=path; | |
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 | * | |
98 | * @param path URL path | |
99 | * @param headers the extra headers | |
100 | * @param context the context instance | |
101 | * @param data the message to send | |
102 | */ | |
103 | public SendHttpMessageAsyncTask(final String path, final Collection<Header> headers, final Context context, final byte[] data){//NOPMD array is supposed to be immutable. | |
104 | super(); | |
105 | this.path=path; | |
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 | * | |
115 | * @param path URL path | |
116 | * @param headers the extra headers | |
117 | * @param broadcast the broadcast to send | |
118 | * @param context the context instance | |
119 | */ | |
120 | public SendHttpMessageAsyncTask(final String path, final Collection<Header> headers, final String broadcast, final Context context){ | |
121 | super(); | |
122 | this.path=path; | |
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; | |
135 | if(data==null && args.length > 0) | |
136 | msg=Utils.join(" ", args).getBytes(); | |
137 | else if(data!=null) | |
138 | msg=data; | |
139 | else | |
140 | msg=null; | |
141 | ||
142 | try { | |
143 | final URL url=Utils.getServerURL(toNonNull(context),toNonNull(path)); | |
144 | final HttpURLConnection conn=(HttpURLConnection) url.openConnection(); | |
145 | if(msg!=null){ | |
146 | conn.setDoOutput(true); | |
147 | conn.setFixedLengthStreamingMode(msg.length); | |
148 | } | |
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)); | |
154 | ||
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); | |
159 | conn.connect(); | |
160 | if(msg!=null){ | |
161 | final OutputStream stream=conn.getOutputStream(); | |
162 | stream.write(msg); | |
163 | stream.close(); | |
164 | } | |
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) { | |
173 | e.printStackTrace(); | |
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 | } |