|
1 | 1 | package com.osrm.client; |
2 | 2 |
|
3 | | -import okhttp3.OkHttpClient; |
4 | | -import okhttp3.MediaType; |
5 | | -import okhttp3.RequestBody; |
6 | | -import okhttp3.Request; |
7 | | -import okhttp3.Response; |
8 | | -import okhttp3.OkHttpClient.Builder; |
9 | | - |
10 | | -import java.io.IOException; |
11 | 3 | import java.util.ArrayList; |
12 | 4 | import java.util.List; |
13 | 5 | import java.util.concurrent.TimeUnit; |
| 6 | +import okhttp3.MediaType; |
| 7 | +import okhttp3.OkHttpClient; |
| 8 | +import okhttp3.OkHttpClient.Builder; |
| 9 | +import okhttp3.Request; |
| 10 | +import okhttp3.RequestBody; |
| 11 | +import okhttp3.Response; |
14 | 12 |
|
15 | 13 |
|
16 | 14 | public class OSRMClient { |
17 | | - private final String uri; |
| 15 | + private final String uri; |
18 | 16 |
|
19 | | - public OSRMClient(String uri) throws EmptyUrlException { |
20 | | - if (uri != null || !uri.isEmpty()) { |
21 | | - this.uri = uri; |
22 | | - } |
23 | | - else { |
24 | | - throw new EmptyUrlException("OSRMClient Constructor requires a OSRM http url"); |
25 | | - } |
| 17 | + public OSRMClient(String uri) throws EmptyUrlException { |
| 18 | + if (uri != null || !uri.isEmpty()) { |
| 19 | + this.uri = uri; |
| 20 | + } else { |
| 21 | + throw new EmptyUrlException("OSRMClient Constructor requires a OSRM http url"); |
26 | 22 | } |
| 23 | + } |
27 | 24 |
|
28 | 25 |
|
29 | | - public OSRMDistanceResponse getDistanceMatrix(List<GeoLocation> locations, double speedRate, String country, String token, String profile) throws OptimizationDistanceMatrixException { |
30 | | - OSRMDistanceResponse osrmDistanceResponse; |
| 26 | + public OSRMDistanceResponse getDistanceMatrix(List<GeoLocation> locations, double speedRate, String country, |
| 27 | + String token, String profile, |
| 28 | + String startTime) throws OptimizationDistanceMatrixException { |
| 29 | + Builder requestBuilder = new Builder(); |
31 | 30 |
|
32 | | - Builder requestBuilder = new Builder(); |
| 31 | + requestBuilder.readTimeout(900000, TimeUnit.MILLISECONDS); |
| 32 | + requestBuilder.writeTimeout(900000, TimeUnit.MILLISECONDS); |
33 | 33 |
|
34 | | - requestBuilder.readTimeout(900000, TimeUnit.MILLISECONDS); |
35 | | - requestBuilder.writeTimeout(900000, TimeUnit.MILLISECONDS); |
| 34 | + OkHttpClient client = requestBuilder.build(); |
36 | 35 |
|
37 | | - OkHttpClient client = requestBuilder.build(); |
| 36 | + MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); |
38 | 37 |
|
39 | | - MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded"); |
| 38 | + List<String> locationsCollection = new ArrayList<>(); |
40 | 39 |
|
41 | | - List<String> locationsCollection = new ArrayList<>(); |
42 | 40 |
|
| 41 | + for (GeoLocation geoloc : locations) { |
| 42 | + locationsCollection.add(geoloc.getLatLongString()); |
| 43 | + } |
43 | 44 |
|
44 | | - for (GeoLocation geoloc: locations) { |
45 | | - locationsCollection.add(geoloc.getLatLongString()); |
46 | | - } |
47 | | - |
48 | | - String paramsString = String.join("&loc=", locationsCollection); |
| 45 | + String paramsString = String.join("&loc=", locationsCollection); |
49 | 46 |
|
50 | | - paramsString += "&speedRate=" + speedRate; |
51 | | - paramsString += "&country=" + country; |
| 47 | + paramsString += "&speedRate=" + speedRate; |
| 48 | + paramsString += "&country=" + country; |
52 | 49 |
|
53 | | - RequestBody body = RequestBody.create(mediaType, "loc=" + paramsString); |
| 50 | + if (startTime != null && !startTime.isEmpty()) { |
| 51 | + paramsString += "&start_time=" + startTime; |
| 52 | + } |
54 | 53 |
|
55 | | - Request request = new Request.Builder() |
56 | | - .url(this.uri + "/table/" + profile) |
57 | | - .post(body) |
58 | | - .addHeader("Content-Type", "application/x-www-form-urlencoded") |
59 | | - .addHeader("Authorization", token) |
60 | | - .build(); |
| 54 | + RequestBody body = RequestBody.create(mediaType, "loc=" + paramsString); |
| 55 | + |
| 56 | + Request request = new Request.Builder() |
| 57 | + .url(this.uri + "/table/" + profile) |
| 58 | + .post(body) |
| 59 | + .addHeader("Content-Type", "application/x-www-form-urlencoded") |
| 60 | + .addHeader("Authorization", token) |
| 61 | + .build(); |
| 62 | + |
| 63 | + Response response; |
| 64 | + try { |
| 65 | + response = client.newCall(request).execute(); |
| 66 | + if (response.isSuccessful()) { |
| 67 | + return OSRMDistanceResponse.fromJSON(response.body().string()); |
| 68 | + } |
| 69 | + } catch (Exception e) { |
| 70 | + System.out.print(e.getMessage()); |
| 71 | + throw new OptimizationDistanceMatrixException("Error while connecting to OSRM Server"); |
| 72 | + } |
61 | 73 |
|
62 | | - try { |
63 | | - Response response = client.newCall(request).execute(); |
| 74 | + UnsuccessfulResponse unsuccessfulResponse = this.getUnsuccessfulResponse(response); |
| 75 | + if (unsuccessfulResponse != null && unsuccessfulResponse.getMessage() != null) { |
| 76 | + throw new DistanceMatrixResponseException("OSRM Error: " + unsuccessfulResponse.getMessage()); |
| 77 | + } |
64 | 78 |
|
65 | | - if (!response.isSuccessful()) { |
66 | | - UnsuccessfulResponse unsuccessfulResponse = UnsuccessfulResponse.fromJSON(response.body().string()); |
67 | | - throw new DistanceMatrixResponseException("OSRM Error: " + unsuccessfulResponse.getMessage()); |
68 | | - } |
| 79 | + throw new DistanceMatrixResponseException("OSRM Error: " + response); |
| 80 | + } |
69 | 81 |
|
70 | | - osrmDistanceResponse = OSRMDistanceResponse.fromJSON(response.body().string()); |
71 | | - } |
72 | | - catch(Exception e) { |
73 | | - System.out.print(e.getMessage()); |
74 | | - throw new OptimizationDistanceMatrixException("Error while connecting to OSRM Server"); |
| 82 | + private UnsuccessfulResponse getUnsuccessfulResponse(Response response){ |
| 83 | + try { |
| 84 | + if (response.body() != null) { |
| 85 | + String bodyResponse = response.body().string(); |
| 86 | + UnsuccessfulResponse unsuccessfulResponse = UnsuccessfulResponse.fromJSON(bodyResponse); |
| 87 | + if(unsuccessfulResponse.getMessage() == null){ |
| 88 | + return new UnsuccessfulResponse(bodyResponse,String.valueOf(response.code()),""); |
75 | 89 | } |
| 90 | + } |
| 91 | + } catch (Exception e) { |
| 92 | + System.out.print("Error getUnsuccessfulResponse.fromJSON: " + e.getMessage()); |
| 93 | + } |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
76 | 97 |
|
77 | | - return osrmDistanceResponse; |
78 | | - } |
79 | 98 | } |
0 commit comments