11package com .osrm .client ;
22
33import com .fasterxml .jackson .databind .ObjectMapper ;
4- import java .net .URLEncoder ;
5- import java .nio .charset .StandardCharsets ;
4+
65import java .util .ArrayList ;
76import java .util .List ;
8- import java .util .Map ;
97import java .util .concurrent .TimeUnit ;
108
11- import com .osrm .client .exception .DistanceMatrixResponseException ;
129import com .osrm .client .exception .EmptyUrlException ;
13- import com .osrm .client .exception .OptimizationDistanceMatrixException ;
10+ import com .osrm .client .exception .OSRMClientException ;
11+ import com .osrm .client .request .CostMatricesRequest ;
12+ import com .osrm .client .request .GeoLocation ;
1413import okhttp3 .MediaType ;
1514import okhttp3 .OkHttpClient ;
1615import okhttp3 .OkHttpClient .Builder ;
1918import okhttp3 .Response ;
2019
2120
22- public class OSRMClient {
21+ public class OSRMClient implements CostService {
2322 private final String uri ;
23+ private static final String ENDPOINT_V2_TABLE = "/v2/table/" ;
2424
2525 public OSRMClient (String uri ) throws EmptyUrlException {
2626 if (uri != null || !uri .isEmpty ()) {
@@ -30,10 +30,8 @@ public OSRMClient(String uri) throws EmptyUrlException {
3030 }
3131 }
3232
33-
34- public OSRMCostMatrixResponse getDistanceMatrix (List <GeoLocation > locations , double speedRate , String country ,
35- String token , String profile ,
36- String options ) throws OptimizationDistanceMatrixException {
33+ @ Override
34+ public CostMatrices getCostMatrices (CostMatricesRequest request ) {
3735 Builder requestBuilder = new Builder ();
3836
3937 requestBuilder .readTimeout (900000 , TimeUnit .MILLISECONDS );
@@ -45,76 +43,39 @@ public OSRMCostMatrixResponse getDistanceMatrix(List<GeoLocation> locations, dou
4543
4644 List <String > locationsCollection = new ArrayList <>();
4745
48-
49- for (GeoLocation geoloc : locations ) {
50- locationsCollection .add (geoloc .getLatLongString ());
46+ for (GeoLocation geolocation : request .getLocations ()) {
47+ locationsCollection .add (geolocation .getLatLongString ());
5148 }
5249
5350 String paramsString = String .join ("&loc=" , locationsCollection );
5451
55- paramsString += "&speedRate=" + speedRate ;
56- paramsString += "&country=" + country ;
57- paramsString += encodeJsonToUrlParams (options );
52+ paramsString = addParamString (paramsString , "speedRate" , Double .toString (request .getSpeedRate ()));
53+ paramsString = addParamString (paramsString , "country" , request .getCountry ());
54+ paramsString = addParamString (paramsString , "start_time" , request .getStartTime ());
55+ paramsString = addParamString (paramsString , "vehicleSubType" , request .getVehicleSubType ());
56+ paramsString = addParamString (paramsString , "metrics" , request .getMetrics ());
57+ //TODO:423 pending implement vehicular restriction
5858
5959 RequestBody body = RequestBody .create (mediaType , "loc=" + paramsString );
6060
61- Request request = new Request .Builder ()
62- .url (this .uri + "/table/" + profile )
63- .post (body )
64- .addHeader ("Content-Type" , "application/x-www-form-urlencoded" )
65- .addHeader ("Authorization" , token )
66- .build ();
61+ Request osrmRequest = new Request .Builder ()
62+ .url (this .uri + ENDPOINT_V2_TABLE + request . getProfile () )
63+ .post (body )
64+ .addHeader ("Content-Type" , "application/x-www-form-urlencoded" )
65+ .addHeader ("Authorization" , request . getToken () )
66+ .build ();
6767
68- Response response ;
6968 try {
70- response = client .newCall (request ).execute ();
71- if (response .isSuccessful ()) {
72- return OSRMCostMatrixResponse .fromJSON (response .body ().string ());
73- }
74- } catch (Exception e ) {
75- System .out .print (e .getMessage ());
76- throw new OptimizationDistanceMatrixException ("Error while connecting to OSRM Server" );
77- }
78-
79- UnsuccessfulResponse unsuccessfulResponse = this .getUnsuccessfulResponse (response );
80- if (unsuccessfulResponse != null && unsuccessfulResponse .getMessage () != null ) {
81- throw new DistanceMatrixResponseException ("OSRM Error: " + unsuccessfulResponse .getMessage ());
82- }
69+ Response response = client .newCall (osrmRequest ).execute ();
8370
84- throw new DistanceMatrixResponseException ("OSRM Error: " + response );
85- }
86-
87- public static String encodeJsonToUrlParams (String options ) {
88- ObjectMapper objectMapper = new ObjectMapper ();
89- StringBuilder urlParams = new StringBuilder ();
90- try {
91- Map <String , Object > map = objectMapper .readValue (options , Map .class );
92-
93- for (Map .Entry <String , Object > entry : map .entrySet ()) {
94- String key = URLEncoder .encode (entry .getKey (), StandardCharsets .UTF_8 .toString ());
95- String value = URLEncoder .encode (String .valueOf (entry .getValue ()), StandardCharsets .UTF_8 .toString ());
96- urlParams .append ("&" ).append (key ).append ("=" ).append (value );
97- }
98- }catch (Exception e ) {
99- System .out .print ("Error getUnsuccessfulResponse.fromJSON: " + e .getMessage ());
71+ ObjectMapper mapper = new ObjectMapper ();
72+ return mapper .readValue (response .body ().string (), CostMatrices .class );
73+ } catch (Exception e ) {
74+ throw new OSRMClientException ("Error while connecting to OSRM Server" );
10075 }
101- return urlParams .toString ();
10276 }
10377
104- private UnsuccessfulResponse getUnsuccessfulResponse (Response response ){
105- try {
106- if (response .body () != null ) {
107- String bodyResponse = response .body ().string ();
108- UnsuccessfulResponse unsuccessfulResponse = UnsuccessfulResponse .fromJSON (bodyResponse );
109- if (unsuccessfulResponse .getMessage () == null ){
110- return new UnsuccessfulResponse (bodyResponse ,String .valueOf (response .code ()),"" );
111- }
112- }
113- } catch (Exception e ) {
114- System .out .print ("Error getUnsuccessfulResponse.fromJSON: " + e .getMessage ());
115- }
116- return null ;
78+ private String addParamString (String paramString , String key , String value ) {
79+ return (value != null ) && (!value .equals ("" )) ? paramString .concat ("&" + key + "=" + value ) : paramString ;
11780 }
118-
119-
12081}
0 commit comments