Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
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;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

import okhttp3.ResponseBody;
import retrofit2.Call;
Expand Down Expand Up @@ -60,8 +60,6 @@
public abstract class MapboxDirections extends
MapboxService<DirectionsResponse, DirectionsService> {

private static final Logger LOGGER = Logger.getLogger(MapboxDirections.class.getName());

protected MapboxDirections() {
super(DirectionsService.class);
}
Expand Down Expand Up @@ -107,11 +105,26 @@ protected GsonBuilder getGsonBuilder() {
@Override
public Response<DirectionsResponse> executeCall() throws IOException {
Response<DirectionsResponse> 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;
}

/**
Expand All @@ -129,15 +142,31 @@ public void enqueueCall(final Callback<DirectionsResponse> callback) {
public void onResponse(Call<DirectionsResponse> call, Response<DirectionsResponse> 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<DirectionsResponse> 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
Expand All @@ -153,13 +182,16 @@ private void errorDidOccur(@Nullable Callback<DirectionsResponse> callback,
Converter<ResponseBody, DirectionsError> 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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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))
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,22 @@
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;
import java.util.List;
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)
Expand Down Expand Up @@ -102,14 +104,26 @@ protected Call<MapMatchingResponse> initializeCall() {
public Response<MapMatchingResponse> executeCall() throws IOException {

Response<MapMatchingResponse> 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;
}

/**
Expand All @@ -128,22 +142,34 @@ public void onResponse(Call<MapMatchingResponse> call,
Response<MapMatchingResponse> 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<MapMatchingResponse> 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<MapMatchingResponse> call, Throwable throwable) {
callback.onFailure(call, throwable);
Expand All @@ -158,15 +184,16 @@ private void errorDidOccur(@Nullable Callback<MapMatchingResponse> callback,
Converter<ResponseBody, MapMatchingError> 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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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()
Expand Down