]>
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.InputStream; | |
7 | import java.io.OutputStream; | |
8 | import java.net.URL; | |
9 | import java.util.Collection; | |
10 | ||
11 | import java.net.HttpURLConnection; | |
12 | import org.eclipse.jdt.annotation.Nullable; | |
13 | ||
14 | import android.content.Context; | |
15 | import android.preference.PreferenceManager; | |
16 | import android.util.Base64; | |
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 | * ExecutableRunnable that makes a HTTP(S) call to the server and hands the response to a callback | |
40 | * | |
41 | * @author Marius Gavrilescu <marius@ieval.ro> | |
42 | */ | |
43 | public final class HttpCallExecutableRunnable extends ExecutableRunnable{ | |
44 | ||
45 | /** | |
46 | * Callback which is run after a HTTP call. | |
47 | * | |
48 | * @author Marius Gavrilescu | |
49 | */ | |
50 | public interface ResultCallback{ | |
51 | /** | |
52 | * Callback invoked if the HTTP call is successful. | |
53 | * | |
54 | * @param responseCode HTTP response code | |
55 | * @param responseMessage HTTP response message | |
56 | * @param inputStream HTTP content InputStream | |
57 | */ | |
58 | void onResult(final int responseCode, final String responseMessage, final InputStream inputStream); | |
59 | /** | |
60 | * Callback invoked if the HTTP call is unsuccessful. | |
61 | * | |
62 | * @param error localized error message | |
63 | */ | |
64 | void onError(final String error); | |
65 | } | |
66 | ||
67 | /** | |
68 | * List of extra request headers. | |
69 | */ | |
70 | private final Collection<Header> headers; | |
71 | /** | |
72 | * Context instance used by this class | |
73 | */ | |
74 | private final Context context; | |
75 | /** | |
76 | * Data to send to the server | |
77 | */ | |
78 | private final byte[] data; | |
79 | /** | |
80 | * Server URL path | |
81 | */ | |
82 | private final String path; | |
83 | /** | |
84 | * Callback to run after the request returns | |
85 | */ | |
86 | private final ResultCallback callback; | |
87 | /** If true, the task should be retried if it fails */ | |
88 | private final boolean mustRetryTask; | |
89 | ||
90 | /** | |
91 | * Constructs a SendHttpMessageAsyncTask which sends a binary message. | |
92 | * | |
93 | * @param path URL path | |
94 | * @param headers the extra headers | |
95 | * @param context the context instance | |
96 | * @param resultCallback {@link ResultCallback} instance | |
97 | * @param mustRetryTask true if this task should be retried if it fails | |
98 | * @param data the message to send | |
99 | */ | |
100 | 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. | |
101 | this.path=path; | |
102 | this.headers=headers; | |
103 | this.context=context; | |
104 | this.callback=resultCallback; | |
105 | this.mustRetryTask=mustRetryTask; | |
106 | this.data=data; | |
107 | } | |
108 | ||
109 | /** | |
110 | * Constructs a SendHttpMessageAsyncTask which sends a text message. | |
111 | * | |
112 | * @param path URL path | |
113 | * @param headers the extra headers | |
114 | * @param context the context instance | |
115 | * @param resultCallback {@link ResultCallback} instance | |
116 | * @param mustRetryTask true if this task should be retried if it fails | |
117 | * @param message message to send | |
118 | */ | |
119 | public HttpCallExecutableRunnable(final String path, final @Nullable Collection<Header> headers, final Context context, final @Nullable ResultCallback resultCallback, final boolean mustRetryTask, final String... message){ | |
120 | this.path=path; | |
121 | this.headers=headers; | |
122 | this.context=context; | |
123 | this.callback=resultCallback; | |
124 | this.mustRetryTask=mustRetryTask; | |
125 | if(message.length == 0) | |
126 | this.data=null;//NOPMD final field | |
127 | else | |
128 | this.data=Utils.join(" ", message).getBytes(); | |
129 | } | |
130 | ||
131 | @Override | |
132 | public void run() { | |
133 | try { | |
134 | final URL url=Utils.getServerURL(toNonNull(context),toNonNull(path)); | |
135 | final HttpURLConnection conn=(HttpURLConnection) url.openConnection(); | |
136 | if(data!=null){ | |
137 | conn.setDoOutput(true); | |
138 | conn.setFixedLengthStreamingMode(data.length); | |
139 | } | |
140 | final String user=PreferenceManager.getDefaultSharedPreferences(context).getString("username", null); | |
141 | final String password=PreferenceManager.getDefaultSharedPreferences(context).getString("password", null); | |
142 | if(user == null || password == null){ | |
143 | if(callback!=null) | |
144 | callback.onError(toNonNull(context.getString(user_or_password_not_set))); | |
145 | return; | |
146 | } | |
147 | ||
148 | conn.setRequestProperty("Authorization", "Basic "+Base64.encodeToString( | |
149 | (user+':'+password).getBytes(), Base64.NO_WRAP)); | |
150 | if(headers != null) | |
151 | for (final Header header : headers) | |
152 | conn.setRequestProperty(header.name, header.value); | |
153 | conn.connect(); | |
154 | if(data!=null){ | |
155 | final OutputStream stream=conn.getOutputStream(); | |
156 | stream.write(data); | |
157 | stream.close(); | |
158 | } | |
159 | Log.d(getClass().getName(),"HTTP Response: "+conn.getResponseCode()+" "+conn.getResponseMessage()); | |
160 | String message=conn.getResponseMessage(); | |
161 | if(message==null && callback != null) | |
162 | callback.onError(toNonNull(context.getString(no_response_returned_from_server))); | |
163 | else if(message != null && callback != null){ | |
164 | if(message.charAt(message.length()-1) == ')')//message is (something) | |
165 | message=message.substring(1, message.length()-1); | |
166 | else if(message.charAt(0) == '(')//message is (something) something else | |
167 | message=message.substring(message.indexOf(')')+2); | |
168 | callback.onResult(conn.getResponseCode(), message, conn.getResponseCode() == 200 ? conn.getInputStream() : null); | |
169 | } | |
170 | conn.disconnect(); | |
171 | } catch (Exception e) { | |
172 | e.printStackTrace(); | |
173 | if(callback != null) | |
174 | callback.onError(toNonNull(context.getString(connection_error))); | |
175 | if(mustRetryTask) | |
176 | retry(); | |
177 | } | |
178 | } | |
179 | } |