summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HTTPRequest.java
diff options
context:
space:
mode:
authorCameron Mace <cameron@mapbox.com>2016-12-16 16:19:15 -0500
committerGitHub <noreply@github.com>2016-12-16 16:19:15 -0500
commit20b958301eb208fe9ed0ae8edfb14b6f3741d8f2 (patch)
tree94ae0ce250cda159be13f9a21cc70c92d4908974 /platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HTTPRequest.java
parentf95b4838ea816b9da0c151a953a1f98f97c79a39 (diff)
downloadqtlocation-mapboxgl-20b958301eb208fe9ed0ae8edfb14b6f3741d8f2.tar.gz
Adds checkstyle to CI (#7442)
* adds checkstyle to CI * fixed gradlew path * resolved testapp checkstyle violations * added back mapboxMap variable for test * checkstyle annotations * checkstyle SDK round 1 * maps package checkstyle * rest of SDK checkstyle * checkstyle gesture library * checkstyle test * finished rest of test checkstyle * resolved all checkstyle errors * fixed class name * removed old test file * fixed camera postion test * fixed native crash
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HTTPRequest.java')
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HTTPRequest.java306
1 files changed, 155 insertions, 151 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HTTPRequest.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HTTPRequest.java
index 991bb49ab3..ef6b9670db 100644
--- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HTTPRequest.java
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/http/HTTPRequest.java
@@ -5,6 +5,7 @@ import android.content.Context;
import android.content.pm.PackageInfo;
import android.os.Build;
import android.text.TextUtils;
+
import timber.log.Timber;
import com.mapbox.mapboxsdk.BuildConfig;
@@ -31,172 +32,175 @@ import okhttp3.internal.Util;
class HTTPRequest implements Callback {
- private static OkHttpClient mClient = new OkHttpClient();
- private String USER_AGENT_STRING = null;
-
- private static final int CONNECTION_ERROR = 0;
- private static final int TEMPORARY_ERROR = 1;
- private static final int PERMANENT_ERROR = 2;
-
- // Reentrancy is not needed, but "Lock" is an
- // abstract class.
- private ReentrantLock mLock = new ReentrantLock();
-
- private long mNativePtr = 0;
-
- private Call mCall;
- private Request mRequest;
-
- private native void nativeOnFailure(int type, String message);
-
- private native void nativeOnResponse(int code, String etag, String modified, String cacheControl, String expires, String retryAfter, String xRateLimitReset, byte[] body);
-
- private HTTPRequest(long nativePtr, String resourceUrl, String etag, String modified) {
- mNativePtr = nativePtr;
-
- try {
- // Don't try a request if we aren't connected
- if (!MapboxAccountManager.getInstance().isConnected()) {
- throw new NoRouteToHostException("No Internet connection available.");
- }
-
- HttpUrl httpUrl = HttpUrl.parse(resourceUrl);
- final String host = httpUrl.host().toLowerCase(MapboxConstants.MAPBOX_LOCALE);
- if (host.equals("mapbox.com") || host.endsWith(".mapbox.com") || host.equals("mapbox.cn") || host.endsWith(".mapbox.cn")) {
- if (httpUrl.querySize() == 0) {
- resourceUrl = resourceUrl + "?";
- } else {
- resourceUrl = resourceUrl + "&";
- }
- resourceUrl = resourceUrl + "events=true";
- }
-
- Request.Builder builder = new Request.Builder()
- .url(resourceUrl)
- .tag(resourceUrl.toLowerCase(MapboxConstants.MAPBOX_LOCALE))
- .addHeader("User-Agent", getUserAgent());
- if (etag.length() > 0) {
- builder = builder.addHeader("If-None-Match", etag);
- } else if (modified.length() > 0) {
- builder = builder.addHeader("If-Modified-Since", modified);
- }
- mRequest = builder.build();
- mCall = mClient.newCall(mRequest);
- mCall.enqueue(this);
- } catch (Exception e) {
- onFailure(e);
- }
- }
+ private static OkHttpClient mClient = new OkHttpClient();
+ private String USER_AGENT_STRING = null;
- public void cancel() {
- // mCall can be null if the constructor gets aborted (e.g, under a NoRouteToHostException).
- if (mCall != null) {
- mCall.cancel();
- }
+ private static final int CONNECTION_ERROR = 0;
+ private static final int TEMPORARY_ERROR = 1;
+ private static final int PERMANENT_ERROR = 2;
- // TODO: We need a lock here because we can try
- // to cancel at the same time the request is getting
- // answered on the OkHTTP thread. We could get rid of
- // this lock by using Runnable when we move Android
- // implementation of mbgl::RunLoop to Looper.
- mLock.lock();
- mNativePtr = 0;
- mLock.unlock();
- }
+ // Reentrancy is not needed, but "Lock" is an
+ // abstract class.
+ private ReentrantLock mLock = new ReentrantLock();
- @Override
- public void onResponse(Call call, Response response) throws IOException {
- if (response.isSuccessful()) {
- Timber.v(String.format("[HTTP] Request was successful (code = %d).", response.code()));
- } else {
- // We don't want to call this unsuccessful because a 304 isn't really an error
- String message = !TextUtils.isEmpty(response.message()) ? response.message() : "No additional information";
- Timber.d(String.format(
- "[HTTP] Request with response code = %d: %s",
- response.code(), message));
- }
+ private long mNativePtr = 0;
- byte[] body;
- try {
- body = response.body().bytes();
- } catch (IOException e) {
- onFailure(e);
- //throw e;
- return;
- } finally {
- response.body().close();
- }
+ private Call mCall;
+ private Request mRequest;
- mLock.lock();
- if (mNativePtr != 0) {
- nativeOnResponse(response.code(),
- response.header("ETag"),
- response.header("Last-Modified"),
- response.header("Cache-Control"),
- response.header("Expires"),
- response.header("Retry-After"),
- response.header("x-rate-limit-reset"),
- body);
- }
- mLock.unlock();
- }
+ private native void nativeOnFailure(int type, String message);
- @Override
- public void onFailure(Call call, IOException e) {
- onFailure(e);
- }
+ private native void nativeOnResponse(int code, String etag, String modified, String cacheControl, String expires,
+ String retryAfter, String xRateLimitReset, byte[] body);
- private void onFailure(Exception e) {
- int type = PERMANENT_ERROR;
- if ((e instanceof NoRouteToHostException) || (e instanceof UnknownHostException) || (e instanceof SocketException) || (e instanceof ProtocolException) || (e instanceof SSLException)) {
- type = CONNECTION_ERROR;
- } else if ((e instanceof InterruptedIOException)) {
- type = TEMPORARY_ERROR;
- }
+ private HTTPRequest(long nativePtr, String resourceUrl, String etag, String modified) {
+ mNativePtr = nativePtr;
- String errorMessage = e.getMessage() != null ? e.getMessage() : "Error processing the request";
+ try {
+ // Don't try a request if we aren't connected
+ if (!MapboxAccountManager.getInstance().isConnected()) {
+ throw new NoRouteToHostException("No Internet connection available.");
+ }
- if (type == TEMPORARY_ERROR) {
- Timber.d(String.format(MapboxConstants.MAPBOX_LOCALE,
- "Request failed due to a temporary error: %s", errorMessage));
- } else if (type == CONNECTION_ERROR) {
- Timber.i(String.format(MapboxConstants.MAPBOX_LOCALE,
- "Request failed due to a connection error: %s", errorMessage));
+ HttpUrl httpUrl = HttpUrl.parse(resourceUrl);
+ final String host = httpUrl.host().toLowerCase(MapboxConstants.MAPBOX_LOCALE);
+ if (host.equals("mapbox.com") || host.endsWith(".mapbox.com") || host.equals("mapbox.cn")
+ || host.endsWith(".mapbox.cn")) {
+ if (httpUrl.querySize() == 0) {
+ resourceUrl = resourceUrl + "?";
} else {
- // PERMANENT_ERROR
- Timber.w(String.format(MapboxConstants.MAPBOX_LOCALE,
- "Request failed due to a permanent error: %s", errorMessage));
+ resourceUrl = resourceUrl + "&";
}
+ resourceUrl = resourceUrl + "events=true";
+ }
+
+ Request.Builder builder = new Request.Builder()
+ .url(resourceUrl)
+ .tag(resourceUrl.toLowerCase(MapboxConstants.MAPBOX_LOCALE))
+ .addHeader("User-Agent", getUserAgent());
+ if (etag.length() > 0) {
+ builder = builder.addHeader("If-None-Match", etag);
+ } else if (modified.length() > 0) {
+ builder = builder.addHeader("If-Modified-Since", modified);
+ }
+ mRequest = builder.build();
+ mCall = mClient.newCall(mRequest);
+ mCall.enqueue(this);
+ } catch (Exception exception) {
+ onFailure(exception);
+ }
+ }
- mLock.lock();
- if (mNativePtr != 0) {
- nativeOnFailure(type, errorMessage);
- }
- mLock.unlock();
+ public void cancel() {
+ // mCall can be null if the constructor gets aborted (e.g, under a NoRouteToHostException).
+ if (mCall != null) {
+ mCall.cancel();
}
- private String getUserAgent() {
- if (USER_AGENT_STRING == null) {
- return USER_AGENT_STRING = Util.toHumanReadableAscii(
- String.format("%s %s (%s) Android/%s (%s)",
- getApplicationIdentifier(),
- BuildConfig.MAPBOX_VERSION_STRING,
- BuildConfig.GIT_REVISION_SHORT,
- Build.VERSION.SDK_INT,
- Build.CPU_ABI)
- );
- } else {
- return USER_AGENT_STRING;
- }
+ // TODO: We need a lock here because we can try
+ // to cancel at the same time the request is getting
+ // answered on the OkHTTP thread. We could get rid of
+ // this lock by using Runnable when we move Android
+ // implementation of mbgl::RunLoop to Looper.
+ mLock.lock();
+ mNativePtr = 0;
+ mLock.unlock();
+ }
+
+ @Override
+ public void onResponse(Call call, Response response) throws IOException {
+ if (response.isSuccessful()) {
+ Timber.v(String.format("[HTTP] Request was successful (code = %d).", response.code()));
+ } else {
+ // We don't want to call this unsuccessful because a 304 isn't really an error
+ String message = !TextUtils.isEmpty(response.message()) ? response.message() : "No additional information";
+ Timber.d(String.format(
+ "[HTTP] Request with response code = %d: %s",
+ response.code(), message));
}
- private String getApplicationIdentifier() {
- try {
- Context context = MapboxAccountManager.getInstance().getApplicationContext();
- PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
- return String.format("%s/%s (%s)", context.getPackageName(), packageInfo.versionName, packageInfo.versionCode);
- } catch (Exception e) {
- return "";
- }
+ byte[] body;
+ try {
+ body = response.body().bytes();
+ } catch (IOException ioException) {
+ onFailure(ioException);
+ //throw ioException;
+ return;
+ } finally {
+ response.body().close();
+ }
+
+ mLock.lock();
+ if (mNativePtr != 0) {
+ nativeOnResponse(response.code(),
+ response.header("ETag"),
+ response.header("Last-Modified"),
+ response.header("Cache-Control"),
+ response.header("Expires"),
+ response.header("Retry-After"),
+ response.header("x-rate-limit-reset"),
+ body);
+ }
+ mLock.unlock();
+ }
+
+ @Override
+ public void onFailure(Call call, IOException e) {
+ onFailure(e);
+ }
+
+ private void onFailure(Exception e) {
+ int type = PERMANENT_ERROR;
+ if ((e instanceof NoRouteToHostException) || (e instanceof UnknownHostException) || (e instanceof SocketException)
+ || (e instanceof ProtocolException) || (e instanceof SSLException)) {
+ type = CONNECTION_ERROR;
+ } else if ((e instanceof InterruptedIOException)) {
+ type = TEMPORARY_ERROR;
+ }
+
+ String errorMessage = e.getMessage() != null ? e.getMessage() : "Error processing the request";
+
+ if (type == TEMPORARY_ERROR) {
+ Timber.d(String.format(MapboxConstants.MAPBOX_LOCALE,
+ "Request failed due to a temporary error: %s", errorMessage));
+ } else if (type == CONNECTION_ERROR) {
+ Timber.i(String.format(MapboxConstants.MAPBOX_LOCALE,
+ "Request failed due to a connection error: %s", errorMessage));
+ } else {
+ // PERMANENT_ERROR
+ Timber.w(String.format(MapboxConstants.MAPBOX_LOCALE,
+ "Request failed due to a permanent error: %s", errorMessage));
+ }
+
+ mLock.lock();
+ if (mNativePtr != 0) {
+ nativeOnFailure(type, errorMessage);
+ }
+ mLock.unlock();
+ }
+
+ private String getUserAgent() {
+ if (USER_AGENT_STRING == null) {
+ return USER_AGENT_STRING = Util.toHumanReadableAscii(
+ String.format("%s %s (%s) Android/%s (%s)",
+ getApplicationIdentifier(),
+ BuildConfig.MAPBOX_VERSION_STRING,
+ BuildConfig.GIT_REVISION_SHORT,
+ Build.VERSION.SDK_INT,
+ Build.CPU_ABI)
+ );
+ } else {
+ return USER_AGENT_STRING;
+ }
+ }
+
+ private String getApplicationIdentifier() {
+ try {
+ Context context = MapboxAccountManager.getInstance().getApplicationContext();
+ PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
+ return String.format("%s/%s (%s)", context.getPackageName(), packageInfo.versionName, packageInfo.versionCode);
+ } catch (Exception exception) {
+ return "";
}
+ }
}