Merge pull request #1915 from jmakovicka/remove-apache-http
Avoid using deprecated org.apache.http
This commit is contained in:
commit
9b0ca931f1
2 changed files with 105 additions and 79 deletions
|
@ -1,14 +1,20 @@
|
||||||
package net.osmand.plus.monitoring;
|
package net.osmand.plus.monitoring;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.net.ssl.HttpsURLConnection;
|
||||||
|
import javax.net.ssl.HostnameVerifier;
|
||||||
|
import javax.net.ssl.SSLSession;
|
||||||
|
|
||||||
import net.osmand.PlatformUtil;
|
import net.osmand.PlatformUtil;
|
||||||
import net.osmand.plus.OsmAndLocationProvider;
|
import net.osmand.plus.OsmAndLocationProvider;
|
||||||
import net.osmand.plus.OsmandApplication;
|
import net.osmand.plus.OsmandApplication;
|
||||||
|
@ -17,15 +23,6 @@ import net.osmand.plus.OsmandSettings;
|
||||||
import net.osmand.plus.R;
|
import net.osmand.plus.R;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.http.HttpResponse;
|
|
||||||
import org.apache.http.client.methods.HttpGet;
|
|
||||||
import org.apache.http.client.methods.HttpRequestBase;
|
|
||||||
import org.apache.http.impl.client.DefaultHttpClient;
|
|
||||||
import org.apache.http.params.BasicHttpParams;
|
|
||||||
import org.apache.http.params.HttpConnectionParams;
|
|
||||||
import org.apache.http.params.HttpParams;
|
|
||||||
import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
|
|
||||||
import org.apache.http.conn.ssl.SSLSocketFactory;
|
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
|
@ -134,35 +131,37 @@ public class LiveMonitoringHelper {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
String url = MessageFormat.format(st, prm.toArray());
|
String urlStr = MessageFormat.format(st, prm.toArray());
|
||||||
try {
|
try {
|
||||||
|
|
||||||
HttpParams params = new BasicHttpParams();
|
|
||||||
HttpConnectionParams.setConnectionTimeout(params, 15000);
|
|
||||||
DefaultHttpClient httpclient = new DefaultHttpClient(params);
|
|
||||||
//allow certificates where hostnames doesn't match CN
|
|
||||||
SSLSocketFactory sf = (SSLSocketFactory) httpclient.getConnectionManager().getSchemeRegistry().getScheme("https").getSocketFactory();
|
|
||||||
sf.setHostnameVerifier(new AllowAllHostnameVerifier());
|
|
||||||
// Parse the URL and let the URI constructor handle proper encoding of special characters such as spaces
|
// Parse the URL and let the URI constructor handle proper encoding of special characters such as spaces
|
||||||
URL u = new URL(url);
|
URL url = new URL(urlStr);
|
||||||
URI uri = new URI(u.getProtocol(), u.getUserInfo(), u.getHost(), u.getPort(), u.getPath(), u.getQuery(), u.getRef());
|
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
|
||||||
HttpRequestBase method = new HttpGet(uri);
|
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(),
|
||||||
|
url.getPath(), url.getQuery(), url.getRef());
|
||||||
|
|
||||||
|
urlConnection.setConnectTimeout(15000);
|
||||||
|
urlConnection.setReadTimeout(15000);
|
||||||
|
|
||||||
|
// allow certificates where hostnames doesn't match CN
|
||||||
|
if (url.getProtocol() == "https") {
|
||||||
|
((HttpsURLConnection) urlConnection).setHostnameVerifier(
|
||||||
|
new HostnameVerifier() {
|
||||||
|
public boolean verify(String host, SSLSession session) {
|
||||||
|
return (true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
log.info("Monitor " + uri);
|
log.info("Monitor " + uri);
|
||||||
HttpResponse response = httpclient.execute(method);
|
|
||||||
|
if (urlConnection.getResponseCode() != 200) {
|
||||||
if(response.getStatusLine() == null ||
|
|
||||||
response.getStatusLine().getStatusCode() != 200){
|
String msg = urlConnection.getResponseCode() + " : " + //$NON-NLS-1$//$NON-NLS-2$
|
||||||
|
urlConnection.getResponseMessage();
|
||||||
String msg;
|
|
||||||
if(response.getStatusLine() == null){
|
|
||||||
msg = ctx.getString(R.string.failed_op); //$NON-NLS-1$
|
|
||||||
} else {
|
|
||||||
msg = response.getStatusLine().getStatusCode() + " : " + //$NON-NLS-1$//$NON-NLS-2$
|
|
||||||
response.getStatusLine().getReasonPhrase();
|
|
||||||
}
|
|
||||||
log.error("Error sending monitor request: " + msg);
|
log.error("Error sending monitor request: " + msg);
|
||||||
} else {
|
} else {
|
||||||
InputStream is = response.getEntity().getContent();
|
InputStream is = urlConnection.getInputStream();
|
||||||
StringBuilder responseBody = new StringBuilder();
|
StringBuilder responseBody = new StringBuilder();
|
||||||
if (is != null) {
|
if (is != null) {
|
||||||
BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8")); //$NON-NLS-1$
|
BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8")); //$NON-NLS-1$
|
||||||
|
@ -173,13 +172,13 @@ public class LiveMonitoringHelper {
|
||||||
}
|
}
|
||||||
is.close();
|
is.close();
|
||||||
}
|
}
|
||||||
httpclient.getConnectionManager().shutdown();
|
log.info("Monitor response (" + urlConnection.getHeaderField("Content-Type") + "): " + responseBody.toString());
|
||||||
log.info("Monitor response (" + response.getFirstHeader("Content-Type") + "): " + responseBody.toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
urlConnection.disconnect();
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Failed connect to " + url + ": " + e.getMessage(), e);
|
log.error("Failed connect to " + urlStr + ": " + e.getMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,26 +23,26 @@ import net.osmand.plus.R;
|
||||||
import net.osmand.plus.Version;
|
import net.osmand.plus.Version;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.http.HttpResponse;
|
|
||||||
import org.apache.http.NameValuePair;
|
|
||||||
import org.apache.http.client.ClientProtocolException;
|
|
||||||
import org.apache.http.client.HttpClient;
|
|
||||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
||||||
import org.apache.http.client.methods.HttpPost;
|
|
||||||
import org.apache.http.impl.client.DefaultHttpClient;
|
|
||||||
import org.apache.http.message.BasicNameValuePair;
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
|
||||||
public class OsMoService implements OsMoReactor {
|
public class OsMoService implements OsMoReactor {
|
||||||
private static final String HTTP_API_PREPARE = "http://api.osmo.mobi/prepare";
|
private static final String HTTP_API_PREPARE = "http://api.osmo.mobi/prepare";
|
||||||
|
@ -70,6 +70,31 @@ public class OsMoService implements OsMoReactor {
|
||||||
public final static String OSMO_REGISTER_AGAIN = "OSMO_REGISTER_AGAIN"; //$NON-NLS-1$
|
public final static String OSMO_REGISTER_AGAIN = "OSMO_REGISTER_AGAIN"; //$NON-NLS-1$
|
||||||
private final static int SIMPLE_NOTFICATION_ID = 5;
|
private final static int SIMPLE_NOTFICATION_ID = 5;
|
||||||
|
|
||||||
|
private class HttpPostWriter {
|
||||||
|
BufferedWriter writer;
|
||||||
|
boolean first;
|
||||||
|
|
||||||
|
HttpPostWriter(OutputStream outputStream) {
|
||||||
|
this.writer = new BufferedWriter(new OutputStreamWriter(outputStream));
|
||||||
|
this.first = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void addPair(String key, String value) throws IOException {
|
||||||
|
if (this.first)
|
||||||
|
this.first = false;
|
||||||
|
else
|
||||||
|
this.writer.write("&");
|
||||||
|
|
||||||
|
this.writer.write(URLEncoder.encode(key, "UTF-8"));
|
||||||
|
this.writer.write("=");
|
||||||
|
this.writer.write(URLEncoder.encode(value, "UTF-8"));
|
||||||
|
}
|
||||||
|
|
||||||
|
void flush() throws IOException {
|
||||||
|
this.writer.flush();
|
||||||
|
this.writer.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public OsMoService(final OsmandApplication app, OsMoPlugin plugin) {
|
public OsMoService(final OsmandApplication app, OsMoPlugin plugin) {
|
||||||
|
@ -185,27 +210,29 @@ public class OsMoService implements OsMoReactor {
|
||||||
|
|
||||||
|
|
||||||
public String registerOsmoDeviceKey() throws IOException {
|
public String registerOsmoDeviceKey() throws IOException {
|
||||||
HttpClient httpclient = new DefaultHttpClient();
|
URL url = new URL(plugin.useHttps()? HTTPS_AUTH : HTTP_AUTH);
|
||||||
HttpPost httppost = new HttpPost(plugin.useHttps()? HTTPS_AUTH : HTTP_AUTH);
|
|
||||||
try {
|
try {
|
||||||
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
|
conn.setDoOutput(true);
|
||||||
|
|
||||||
// Add your data
|
// Add your data
|
||||||
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
|
HttpPostWriter postWriter = new HttpPostWriter(conn.getOutputStream());
|
||||||
nameValuePairs.add(new BasicNameValuePair("android_id",
|
postWriter.addPair("android_id", Secure.getString(app.getContentResolver(),
|
||||||
Secure.getString(app.getContentResolver(),
|
Secure.ANDROID_ID));
|
||||||
Secure.ANDROID_ID)));
|
|
||||||
nameValuePairs.add(new BasicNameValuePair("android_model", Build.MODEL));
|
postWriter.addPair("android_model", Build.MODEL);
|
||||||
nameValuePairs.add(new BasicNameValuePair("imei", "0"));
|
postWriter.addPair("imei", "0");
|
||||||
nameValuePairs.add(new BasicNameValuePair("android_product", Build.PRODUCT));
|
postWriter.addPair("android_product", Build.PRODUCT);
|
||||||
nameValuePairs.add(new BasicNameValuePair("client", Version.getFullVersion(app)));
|
postWriter.addPair("client", Version.getFullVersion(app));
|
||||||
nameValuePairs.add(new BasicNameValuePair("osmand", Version.getFullVersion(app)));
|
postWriter.addPair("osmand", Version.getFullVersion(app));
|
||||||
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
|
|
||||||
|
|
||||||
// Execute HTTP Post Request
|
// Execute HTTP Post Request
|
||||||
HttpResponse response = httpclient.execute(httppost);
|
postWriter.flush();
|
||||||
InputStream cm = response.getEntity().getContent();
|
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(cm));
|
|
||||||
String r = reader.readLine();
|
String r = reader.readLine();
|
||||||
reader.close();
|
reader.close();
|
||||||
|
conn.disconnect();
|
||||||
log.info("Authorization key : " + r);
|
log.info("Authorization key : " + r);
|
||||||
final JSONObject obj = new JSONObject(r);
|
final JSONObject obj = new JSONObject(r);
|
||||||
if(obj.has("error")) {
|
if(obj.has("error")) {
|
||||||
|
@ -271,50 +298,52 @@ public class OsMoService implements OsMoReactor {
|
||||||
|
|
||||||
public SessionInfo prepareSessionToken() throws IOException {
|
public SessionInfo prepareSessionToken() throws IOException {
|
||||||
String deviceKey = app.getSettings().OSMO_DEVICE_KEY.get();
|
String deviceKey = app.getSettings().OSMO_DEVICE_KEY.get();
|
||||||
if(deviceKey.length() == 0) {
|
if (deviceKey.length() == 0) {
|
||||||
deviceKey = registerOsmoDeviceKey();
|
deviceKey = registerOsmoDeviceKey();
|
||||||
}
|
}
|
||||||
HttpClient httpclient = new DefaultHttpClient();
|
|
||||||
HttpPost httppost = new HttpPost(plugin.useHttps()? HTTPS_API_PREPARE : HTTP_API_PREPARE);
|
URL url = new URL(plugin.useHttps() ? HTTPS_API_PREPARE : HTTP_API_PREPARE);
|
||||||
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
try {
|
try {
|
||||||
|
conn.setDoOutput(true);
|
||||||
|
HttpPostWriter postWriter = new HttpPostWriter(conn.getOutputStream());
|
||||||
|
|
||||||
// Add your data
|
// Add your data
|
||||||
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
|
postWriter.addPair("app", Version.getFullVersion(app));
|
||||||
nameValuePairs.add(new BasicNameValuePair("app", Version.getFullVersion(app)));
|
postWriter.addPair("key", deviceKey);
|
||||||
nameValuePairs.add(new BasicNameValuePair("key", deviceKey));
|
if (app.getSettings().OSMO_USER_PWD.get() != null) {
|
||||||
if(app.getSettings().OSMO_USER_PWD.get() != null) {
|
postWriter.addPair("auth", app.getSettings().OSMO_USER_PWD.get());
|
||||||
nameValuePairs.add(new BasicNameValuePair("auth", app.getSettings().OSMO_USER_PWD.get()));
|
|
||||||
}
|
}
|
||||||
nameValuePairs.add(new BasicNameValuePair("protocol", "1"));
|
postWriter.addPair("protocol", "1");
|
||||||
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
|
|
||||||
|
|
||||||
// Execute HTTP Post Request
|
// Execute HTTP Post Request
|
||||||
HttpResponse response = httpclient.execute(httppost);
|
postWriter.flush();
|
||||||
InputStream cm = response.getEntity().getContent();
|
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(cm));
|
|
||||||
String r = reader.readLine();
|
String r = reader.readLine();
|
||||||
reader.close();
|
reader.close();
|
||||||
|
conn.disconnect();
|
||||||
log.info("Authorization key : " + r);
|
log.info("Authorization key : " + r);
|
||||||
final JSONObject obj = new JSONObject(r);
|
final JSONObject obj = new JSONObject(r);
|
||||||
if(obj.has("error")) {
|
if (obj.has("error")) {
|
||||||
lastRegistrationError = obj.getString("error");
|
lastRegistrationError = obj.getString("error");
|
||||||
runNotification(lastRegistrationError);
|
runNotification(lastRegistrationError);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if(!obj.has("address")) {
|
if (!obj.has("address")) {
|
||||||
lastRegistrationError = "Host name not specified";
|
lastRegistrationError = "Host name not specified";
|
||||||
throw new RuntimeException("Host name not specified");
|
throw new RuntimeException("Host name not specified");
|
||||||
}
|
}
|
||||||
if(!obj.has("token")) {
|
if (!obj.has("token")) {
|
||||||
lastRegistrationError = "Token not specified by server";
|
lastRegistrationError = "Token not specified by server";
|
||||||
throw new RuntimeException("Token not specified by server");
|
throw new RuntimeException("Token not specified by server");
|
||||||
}
|
}
|
||||||
|
|
||||||
SessionInfo si = new SessionInfo();
|
SessionInfo si = new SessionInfo();
|
||||||
String a = obj.getString("address");
|
String a = obj.getString("address");
|
||||||
if(obj.has("name")) {
|
if (obj.has("name")) {
|
||||||
si.username = obj.getString("name");
|
si.username = obj.getString("name");
|
||||||
}
|
}
|
||||||
if(obj.has("uid")) {
|
if (obj.has("uid")) {
|
||||||
si.uid = obj.getString("uid");
|
si.uid = obj.getString("uid");
|
||||||
}
|
}
|
||||||
int i = a.indexOf(':');
|
int i = a.indexOf(':');
|
||||||
|
@ -322,8 +351,6 @@ public class OsMoService implements OsMoReactor {
|
||||||
si.port = a.substring(i + 1);
|
si.port = a.substring(i + 1);
|
||||||
si.token = obj.getString("token");
|
si.token = obj.getString("token");
|
||||||
return si;
|
return si;
|
||||||
} catch (ClientProtocolException e) {
|
|
||||||
throw new IOException(e);
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw e;
|
throw e;
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
|
|
Loading…
Reference in a new issue