diff --git a/services-directions/src/main/java/com/mapbox/api/directions/v5/MapboxDirections.java b/services-directions/src/main/java/com/mapbox/api/directions/v5/MapboxDirections.java index b27c610d7..0dd49b5c4 100644 --- a/services-directions/src/main/java/com/mapbox/api/directions/v5/MapboxDirections.java +++ b/services-directions/src/main/java/com/mapbox/api/directions/v5/MapboxDirections.java @@ -24,6 +24,7 @@ import com.mapbox.core.utils.MapboxUtils; import com.mapbox.core.utils.TextUtils; import com.mapbox.geojson.Point; +import com.sun.xml.internal.ws.spi.db.BindingContextFactory; import java.io.IOException; import java.lang.annotation.Annotation; @@ -31,7 +32,6 @@ import java.util.List; import java.util.Locale; import java.util.logging.Level; -import java.util.logging.Logger; import okhttp3.ResponseBody; import retrofit2.Call; @@ -60,8 +60,6 @@ public abstract class MapboxDirections extends MapboxService { - private static final Logger LOGGER = Logger.getLogger(MapboxDirections.class.getName()); - protected MapboxDirections() { super(DirectionsService.class); } @@ -107,11 +105,26 @@ protected GsonBuilder getGsonBuilder() { @Override public Response executeCall() throws IOException { Response response = super.executeCall(); - if (!response.isSuccessful()) { + if (response.isSuccessful()) { + if (response.body() != null && !response.body().routes().isEmpty()) { + return Response.success( + response.body() + .toBuilder() + .routes(generateRouteOptions(response)) + .build(), + new okhttp3.Response.Builder() + .code(200) + .message("OK") + .protocol(response.raw().protocol()) + .headers(response.headers()) + .request(response.raw().request()) + .build()); + } + } else { errorDidOccur(null, response); } - return Response.success(response.body().toBuilder().routes( - generateRouteOptions(response)).build()); + + return response; } /** @@ -129,15 +142,31 @@ public void enqueueCall(final Callback callback) { public void onResponse(Call call, Response response) { if (!response.isSuccessful()) { errorDidOccur(callback, response); - return; - } else if (response.body() == null || response.body().routes().isEmpty()) { - // If null just pass the original object back since there's nothing to modify. - callback.onResponse(call, response); - return; + + } else if (callback != null) { + if (response.body() == null || response.body().routes().isEmpty()) { + // If null just pass the original object back since there's nothing to modify. + callback.onResponse(call, response); + + } else { + Response newResponse = + Response.success( + response + .body() + .toBuilder() + .routes(generateRouteOptions(response)) + .build(), + new okhttp3.Response.Builder() + .code(200) + .message("OK") + .protocol(response.raw().protocol()) + .headers(response.headers()) + .request(response.raw().request()) + .build()); + + callback.onResponse(call, newResponse); + } } - DirectionsResponse newResponse = response.body().toBuilder().routes( - generateRouteOptions(response)).build(); - callback.onResponse(call, Response.success(newResponse)); } @Override @@ -153,13 +182,16 @@ private void errorDidOccur(@Nullable Callback callback, Converter errorConverter = getRetrofit().responseBodyConverter(DirectionsError.class, new Annotation[0]); if (callback == null) { - return; - } - try { - callback.onFailure(getCall(), - new Throwable(errorConverter.convert(response.errorBody()).message())); - } catch (IOException ioException) { - LOGGER.log(Level.WARNING, "Failed to complete your request. ", ioException); + BindingContextFactory.LOGGER.log( + Level.WARNING, "Failed to complete your request and callback is null"); + } else { + try { + callback.onFailure(getCall(), + new Throwable(errorConverter.convert(response.errorBody()).message())); + } catch (IOException ioException) { + BindingContextFactory.LOGGER.log( + Level.WARNING, "Failed to complete your request. ", ioException); + } } } diff --git a/services-directions/src/test/java/com/mapbox/api/directions/v5/MapboxDirectionsTest.java b/services-directions/src/test/java/com/mapbox/api/directions/v5/MapboxDirectionsTest.java index 57987a565..747405648 100644 --- a/services-directions/src/test/java/com/mapbox/api/directions/v5/MapboxDirectionsTest.java +++ b/services-directions/src/test/java/com/mapbox/api/directions/v5/MapboxDirectionsTest.java @@ -26,6 +26,8 @@ import java.util.List; import java.util.Locale; +import javax.swing.plaf.basic.BasicInternalFrameTitlePane; + public class MapboxDirectionsTest extends TestUtils { private static final String DIRECTIONS_V5_FIXTURE = "directions_v5.json"; @@ -304,7 +306,7 @@ public void radiuses_doesGetFormattedInUrlCorrectly() throws Exception { } @Test - public void clientAppName_doesGetAddedToRequestHeader() throws Exception { + public void clientAppName_doesGetAddedToRequestHeader1() throws Exception { MapboxDirections directions = MapboxDirections.builder() .destination(Point.fromLngLat(13.4930, 9.958)) .origin(Point.fromLngLat(1.234, 2.345)) @@ -314,6 +316,17 @@ public void clientAppName_doesGetAddedToRequestHeader() throws Exception { assertTrue(directions.cloneCall().request().header("User-Agent").contains("APP")); } + @Test + public void clientAppName_doesGetAddedToRequestHeader2() throws Exception { + MapboxDirections directions = MapboxDirections.builder() + .destination(Point.fromLngLat(13.4930, 9.958)) + .origin(Point.fromLngLat(1.234, 2.345)) + .accessToken(ACCESS_TOKEN) + .clientAppName("APP") + .build(); + assertTrue(directions.executeCall().raw().request().header("User-Agent").contains("APP")); + } + @Test public void accessToken_doesGetFormattedInUrlCorrectly() throws Exception { MapboxDirections directions = MapboxDirections.builder() diff --git a/services-matching/src/main/java/com/mapbox/api/matching/v5/MapboxMapMatching.java b/services-matching/src/main/java/com/mapbox/api/matching/v5/MapboxMapMatching.java index 52d58d9ee..efecd8803 100644 --- a/services-matching/src/main/java/com/mapbox/api/matching/v5/MapboxMapMatching.java +++ b/services-matching/src/main/java/com/mapbox/api/matching/v5/MapboxMapMatching.java @@ -25,8 +25,15 @@ import com.mapbox.core.utils.MapboxUtils; import com.mapbox.core.utils.TextUtils; import com.mapbox.geojson.Point; + import com.sun.xml.internal.ws.spi.db.BindingContextFactory; +import okhttp3.ResponseBody; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Converter; +import retrofit2.Response; + import java.io.IOException; import java.lang.annotation.Annotation; import java.util.ArrayList; @@ -34,11 +41,6 @@ import java.util.Locale; import java.util.logging.Level; -import okhttp3.ResponseBody; -import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Converter; -import retrofit2.Response; /** * The Mapbox map matching interface (v5) @@ -102,14 +104,26 @@ protected Call initializeCall() { public Response executeCall() throws IOException { Response response = getCall().execute(); - if (!response.isSuccessful()) { + if (response.isSuccessful()) { + if (response.body() != null && !response.body().matchings().isEmpty()) { + return Response.success( + response.body() + .toBuilder() + .matchings(generateRouteOptions(response)) + .build(), + new okhttp3.Response.Builder() + .code(200) + .message("OK") + .protocol(response.raw().protocol()) + .headers(response.headers()) + .request(response.raw().request()) + .build()); + } + } else { errorDidOccur(null, response); } - return Response.success(response.body() - .toBuilder() - .matchings(generateRouteOptions(response)) - .build()); + return response; } /** @@ -128,22 +142,34 @@ public void onResponse(Call call, Response response) { if (!response.isSuccessful()) { errorDidOccur(callback, response); - return; - } else if (response.body() == null || response.body().matchings().isEmpty()) { - // If null just pass the original object back since there's nothing to modify. - callback.onResponse(call, response); - return; + } else if (callback != null) { + if (response.body() == null || response.body().matchings().isEmpty()) { + // If null just pass the original object back since there's nothing to modify. + callback.onResponse(call, response); + + } else { + Response newResponse = + Response.success( + response + .body() + .toBuilder() + .matchings(generateRouteOptions(response)) + .build(), + new okhttp3.Response.Builder() + .code(200) + .message("OK") + .protocol(response.raw().protocol()) + .headers(response.headers()) + .request(response.raw().request()) + .build()); + + callback.onResponse(call, newResponse); + } } - MapMatchingResponse newResponse = - response - .body() - .toBuilder() - .matchings(generateRouteOptions(response)) - .build(); - callback.onResponse(call, Response.success(newResponse)); } + @Override public void onFailure(Call call, Throwable throwable) { callback.onFailure(call, throwable); @@ -158,15 +184,16 @@ private void errorDidOccur(@Nullable Callback callback, Converter errorConverter = getRetrofit().responseBodyConverter(MapMatchingError.class, new Annotation[0]); if (callback == null) { - return; - } - try { - callback.onFailure(getCall(), - new Throwable(errorConverter.convert(response.errorBody()).message())); - } catch (IOException ioException) { BindingContextFactory.LOGGER.log( - Level.WARNING, "Failed to complete your request. ", ioException - ); + Level.WARNING, "Failed to complete your request and callback is null"); + } else { + try { + callback.onFailure(getCall(), + new Throwable(errorConverter.convert(response.errorBody()).message())); + } catch (IOException ioException) { + BindingContextFactory.LOGGER.log( + Level.WARNING, "Failed to complete your request. ", ioException); + } } } diff --git a/services-matching/src/test/java/com/mapbox/api/matching/v5/MapboxMapMatchingTest.java b/services-matching/src/test/java/com/mapbox/api/matching/v5/MapboxMapMatchingTest.java index 4d7f25924..b6b642530 100644 --- a/services-matching/src/test/java/com/mapbox/api/matching/v5/MapboxMapMatchingTest.java +++ b/services-matching/src/test/java/com/mapbox/api/matching/v5/MapboxMapMatchingTest.java @@ -164,7 +164,7 @@ public void build_throwsExceptionWhenNoValidAccessTokenProvided() throws Excepti } @Test - public void clientAppName_doesSetInHeaderCorrectly() throws Exception { + public void clientAppName_doesSetInHeaderCorrectly1() throws Exception { MapboxMapMatching mapMatching = MapboxMapMatching.builder() .coordinates(coordinates) .baseUrl(mockUrl.toString()) @@ -174,6 +174,18 @@ public void clientAppName_doesSetInHeaderCorrectly() throws Exception { assertTrue(mapMatching.cloneCall().request().header("User-Agent").contains("APP")); } + @Test + public void clientAppName_doesSetInHeaderCorrectly2() throws Exception { + MapboxMapMatching mapMatching = MapboxMapMatching.builder() + .coordinates(coordinates) + .baseUrl(mockUrl.toString()) + .clientAppName("APP") + .accessToken(ACCESS_TOKEN) + .build(); + + assertTrue(mapMatching.executeCall().raw().request().header("User-Agent").contains("APP")); + } + @Test public void mapMatchingToDirectionsRoute() throws Exception { MapboxMapMatching mapMatching = MapboxMapMatching.builder()