]>
Commit | Line | Data |
---|---|---|
8dfb76c9 MG |
1 | package ro.ieval.fonbot; |
2 | ||
3 | import static ro.ieval.fonbot.R.string.*; | |
8dfb76c9 MG |
4 | import static ro.ieval.fonbot.Utils.toNonNull; |
5 | ||
2e5049c9 | 6 | import java.io.InputStream; |
8dfb76c9 | 7 | import java.io.OutputStream; |
8dfb76c9 MG |
8 | import java.net.URL; |
9 | import java.util.Collection; | |
10 | ||
9717b83e | 11 | import javax.net.ssl.HttpsURLConnection; |
8dfb76c9 MG |
12 | import org.eclipse.jdt.annotation.Nullable; |
13 | ||
14 | import android.content.Context; | |
d13f1533 MG |
15 | import android.preference.PreferenceManager; |
16 | import android.util.Base64; | |
8dfb76c9 MG |
17 | import android.util.Log; |
18 | ||
8bfdcf7a MG |
19 | import com.google.android.gcm.GCMRegistrar; |
20 | ||
8dfb76c9 MG |
21 | /* |
22 | * Copyright © 2013 Marius Gavrilescu | |
23 | * | |
24 | * This file is part of FonBot. | |
25 | * | |
26 | * FonBot is free software: you can redistribute it and/or modify | |
27 | * it under the terms of the GNU General Public License as published by | |
28 | * the Free Software Foundation, either version 3 of the License, or | |
29 | * (at your option) any later version. | |
30 | * | |
31 | * FonBot is distributed in the hope that it will be useful, | |
32 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
33 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
34 | * GNU General Public License for more details. | |
35 | * | |
36 | * You should have received a copy of the GNU General Public License | |
37 | * along with FonBot. If not, see <http://www.gnu.org/licenses/>. | |
38 | */ | |
39 | ||
40 | /** | |
9717b83e | 41 | * ExecutableRunnable that makes a HTTPS call to the server and hands the response to a callback |
8dfb76c9 MG |
42 | * |
43 | * @author Marius Gavrilescu <marius@ieval.ro> | |
44 | */ | |
2e5049c9 | 45 | public final class HttpCallExecutableRunnable extends ExecutableRunnable{ |
9717b83e | 46 | |
8dfb76c9 | 47 | /** |
2e5049c9 MG |
48 | * Callback which is run after a HTTP call. |
49 | * | |
50 | * @author Marius Gavrilescu | |
8dfb76c9 | 51 | */ |
561cd6dc | 52 | public interface ResultCallback{ |
2e5049c9 MG |
53 | /** |
54 | * Callback invoked if the HTTP call is successful. | |
55 | * | |
56 | * @param responseCode HTTP response code | |
57 | * @param responseMessage HTTP response message | |
58 | * @param inputStream HTTP content InputStream | |
59 | */ | |
561cd6dc | 60 | void onResult(final int responseCode, final String responseMessage, final InputStream inputStream); |
2e5049c9 MG |
61 | /** |
62 | * Callback invoked if the HTTP call is unsuccessful. | |
63 | * | |
64 | * @param error localized error message | |
65 | */ | |
561cd6dc | 66 | void onError(final String error); |
2e5049c9 | 67 | } |
8dfb76c9 MG |
68 | |
69 | /** | |
70 | * List of extra request headers. | |
71 | */ | |
72 | private final Collection<Header> headers; | |
8dfb76c9 MG |
73 | /** |
74 | * Context instance used by this class | |
75 | */ | |
76 | private final Context context; | |
77 | /** | |
78 | * Data to send to the server | |
79 | */ | |
80 | private final byte[] data; | |
d13f1533 MG |
81 | /** |
82 | * Server URL path | |
83 | */ | |
84 | private final String path; | |
8dfb76c9 | 85 | /** |
2e5049c9 | 86 | * Callback to run after the request returns |
8dfb76c9 | 87 | */ |
2e5049c9 | 88 | private final ResultCallback callback; |
b26f32d6 MG |
89 | /** If true, the task should be retried if it fails */ |
90 | private final boolean mustRetryTask; | |
8dfb76c9 MG |
91 | |
92 | /** | |
2e5049c9 | 93 | * Constructs a SendHttpMessageAsyncTask which sends a binary message. |
8dfb76c9 | 94 | * |
d13f1533 | 95 | * @param path URL path |
8dfb76c9 MG |
96 | * @param headers the extra headers |
97 | * @param context the context instance | |
2e5049c9 | 98 | * @param resultCallback {@link ResultCallback} instance |
b26f32d6 | 99 | * @param mustRetryTask true if this task should be retried if it fails |
8dfb76c9 MG |
100 | * @param data the message to send |
101 | */ | |
b26f32d6 | 102 | public HttpCallExecutableRunnable(final String path, final @Nullable Collection<Header> headers, final Context context, final @Nullable ResultCallback resultCallback, final boolean mustRetryTask, final byte[] data){//NOPMD array is supposed to be immutable. |
d13f1533 | 103 | this.path=path; |
8dfb76c9 | 104 | this.headers=headers; |
8dfb76c9 | 105 | this.context=context; |
2e5049c9 | 106 | this.callback=resultCallback; |
b26f32d6 | 107 | this.mustRetryTask=mustRetryTask; |
8dfb76c9 MG |
108 | this.data=data; |
109 | } | |
110 | ||
111 | /** | |
2e5049c9 | 112 | * Constructs a SendHttpMessageAsyncTask which sends a text message. |
8dfb76c9 | 113 | * |
d13f1533 | 114 | * @param path URL path |
8dfb76c9 | 115 | * @param headers the extra headers |
8dfb76c9 | 116 | * @param context the context instance |
2e5049c9 | 117 | * @param resultCallback {@link ResultCallback} instance |
b26f32d6 | 118 | * @param mustRetryTask true if this task should be retried if it fails |
2e5049c9 | 119 | * @param message message to send |
8dfb76c9 | 120 | */ |
b26f32d6 | 121 | public HttpCallExecutableRunnable(final String path, final @Nullable Collection<Header> headers, final Context context, final @Nullable ResultCallback resultCallback, final boolean mustRetryTask, final String... message){ |
d13f1533 | 122 | this.path=path; |
8dfb76c9 | 123 | this.headers=headers; |
8dfb76c9 | 124 | this.context=context; |
2e5049c9 | 125 | this.callback=resultCallback; |
b26f32d6 | 126 | this.mustRetryTask=mustRetryTask; |
2e5049c9 MG |
127 | if(message.length == 0) |
128 | this.data=null;//NOPMD final field | |
129 | else | |
130 | this.data=Utils.join(" ", message).getBytes(); | |
8dfb76c9 MG |
131 | } |
132 | ||
133 | @Override | |
2e5049c9 | 134 | public void run() { |
8dfb76c9 | 135 | try { |
d13f1533 | 136 | final URL url=Utils.getServerURL(toNonNull(context),toNonNull(path)); |
9717b83e | 137 | final HttpsURLConnection conn=(HttpsURLConnection) url.openConnection(); |
2e5049c9 | 138 | if(data!=null){ |
d13f1533 | 139 | conn.setDoOutput(true); |
2e5049c9 | 140 | conn.setFixedLengthStreamingMode(data.length); |
d13f1533 | 141 | } |
8bfdcf7a | 142 | conn.setRequestProperty("X-ID", GCMRegistrar.getRegistrationId(context)); |
d13f1533 MG |
143 | final String user=PreferenceManager.getDefaultSharedPreferences(context).getString("username", null); |
144 | final String password=PreferenceManager.getDefaultSharedPreferences(context).getString("password", null); | |
2e5049c9 MG |
145 | if(user == null || password == null){ |
146 | if(callback!=null) | |
147 | callback.onError(toNonNull(context.getString(user_or_password_not_set))); | |
148 | return; | |
149 | } | |
d13f1533 MG |
150 | |
151 | conn.setRequestProperty("Authorization", "Basic "+Base64.encodeToString( | |
152 | (user+':'+password).getBytes(), Base64.NO_WRAP)); | |
2e5049c9 MG |
153 | if(headers != null) |
154 | for (final Header header : headers) | |
155 | conn.setRequestProperty(header.name, header.value); | |
8dfb76c9 | 156 | conn.connect(); |
2e5049c9 | 157 | if(data!=null){ |
d13f1533 | 158 | final OutputStream stream=conn.getOutputStream(); |
2e5049c9 | 159 | stream.write(data); |
d13f1533 MG |
160 | stream.close(); |
161 | } | |
8dfb76c9 | 162 | Log.d(getClass().getName(),"HTTP Response: "+conn.getResponseCode()+" "+conn.getResponseMessage()); |
8dfb76c9 | 163 | final String message=conn.getResponseMessage(); |
2e5049c9 MG |
164 | if(message==null && callback != null) |
165 | callback.onError(toNonNull(context.getString(no_response_returned_from_server))); | |
166 | else if(message != null && callback != null) | |
e65b28b4 | 167 | callback.onResult(conn.getResponseCode(), message, |
2e5049c9 MG |
168 | toNonNull(conn.getInputStream())); |
169 | conn.disconnect(); | |
8dfb76c9 | 170 | } catch (Exception e) { |
a97d31fb | 171 | e.printStackTrace(); |
c33b4990 MG |
172 | if(callback != null) |
173 | callback.onError(toNonNull(context.getString(connection_error))); | |
b26f32d6 MG |
174 | if(mustRetryTask) |
175 | retry(); | |
8dfb76c9 MG |
176 | } |
177 | } | |
178 | } |