Skip to content
Open
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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies {
compile 'com.squareup.okhttp3:okhttp:3.12.0'
compile group: 'com.google.code.gson', name: 'gson', version: '2.7'
implementation 'com.google.guava:guava:28.1-jre'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.5'

testCompile group: 'junit', name: 'junit', version: '4.12'
}
2 changes: 1 addition & 1 deletion src/main/java/com/osrm/client/DistanceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import java.util.List;

public interface DistanceService {
DistanceMatrix buildDistanceMatrix(List<GeoLocation> coordinates, double speedRate, String country, String token, String profile, String startTime);
DistanceMatrix buildDistanceMatrix(List<GeoLocation> coordinates, double speedRate, String country, String token, String profile, String options);
}
29 changes: 17 additions & 12 deletions src/main/java/com/osrm/client/OSRMClient.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.osrm.client;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
Expand All @@ -28,7 +30,7 @@ public OSRMClient(String uri) throws EmptyUrlException {

public OSRMDistanceResponse getDistanceMatrix(List<GeoLocation> locations, double speedRate, String country,
String token, String profile,
String startTime) throws OptimizationDistanceMatrixException {
String options) throws OptimizationDistanceMatrixException {
Builder requestBuilder = new Builder();

requestBuilder.readTimeout(900000, TimeUnit.MILLISECONDS);
Expand All @@ -49,7 +51,7 @@ public OSRMDistanceResponse getDistanceMatrix(List<GeoLocation> locations, doubl

paramsString += "&speedRate=" + speedRate;
paramsString += "&country=" + country;
paramsString += encodeStartTime(startTime);
paramsString += encodeJsonToUrlParams(options);

RequestBody body = RequestBody.create(mediaType, "loc=" + paramsString);

Expand Down Expand Up @@ -79,18 +81,21 @@ public OSRMDistanceResponse getDistanceMatrix(List<GeoLocation> locations, doubl
throw new DistanceMatrixResponseException("OSRM Error: " + response);
}

private String encodeStartTime(String startTime) {
String paramsString = "";
if (startTime == null || startTime.isEmpty()) {
return paramsString;
}
public static String encodeJsonToUrlParams(String options) {
ObjectMapper objectMapper = new ObjectMapper();
StringBuilder urlParams = new StringBuilder();
try {
String encodedStartTime = URLEncoder.encode(startTime, StandardCharsets.UTF_8.toString());
paramsString += "&start_time=" + encodedStartTime;
} catch (Exception e){
throw new OptimizationDistanceMatrixException("Error while encoding startTime parameter");
Map<String, Object> map = objectMapper.readValue(options, Map.class);

for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8.toString());
String value = URLEncoder.encode(String.valueOf(entry.getValue()), StandardCharsets.UTF_8.toString());
urlParams.append("&").append(key).append("=").append(value);
}
}catch (Exception e) {
System.out.print("Error getUnsuccessfulResponse.fromJSON: " + e.getMessage());
}
return paramsString;
return urlParams.toString();
}

private UnsuccessfulResponse getUnsuccessfulResponse(Response response){
Expand Down