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