diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2018-06-02 14:17:51 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2018-06-02 14:17:51 +1200 |
commit | 5a9811c3771d92832c9bba718b5ec935844978e8 (patch) | |
tree | 6c2f5024fb0297c66d9eb184fab259e7c3017927 /src/org/traccar/geolocation | |
parent | bfc7cb8efcaa9f9ec7bf24fd528b370a10c4f0ac (diff) | |
download | trackermap-server-5a9811c3771d92832c9bba718b5ec935844978e8.tar.gz trackermap-server-5a9811c3771d92832c9bba718b5ec935844978e8.tar.bz2 trackermap-server-5a9811c3771d92832c9bba718b5ec935844978e8.zip |
Remove async HTTP client dependency
Diffstat (limited to 'src/org/traccar/geolocation')
3 files changed, 56 insertions, 96 deletions
diff --git a/src/org/traccar/geolocation/OpenCellIdGeolocationProvider.java b/src/org/traccar/geolocation/OpenCellIdGeolocationProvider.java index d6e45b550..768aaf6a2 100644 --- a/src/org/traccar/geolocation/OpenCellIdGeolocationProvider.java +++ b/src/org/traccar/geolocation/OpenCellIdGeolocationProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2018 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,15 +15,12 @@ */ package org.traccar.geolocation; -import com.ning.http.client.AsyncCompletionHandler; -import com.ning.http.client.Response; import org.traccar.Context; import org.traccar.model.CellTower; import org.traccar.model.Network; -import javax.json.Json; import javax.json.JsonObject; -import javax.json.JsonReader; +import javax.ws.rs.client.InvocationCallback; public class OpenCellIdGeolocationProvider implements GeolocationProvider { @@ -45,25 +42,21 @@ public class OpenCellIdGeolocationProvider implements GeolocationProvider { String request = String.format(url, cellTower.getMobileCountryCode(), cellTower.getMobileNetworkCode(), cellTower.getLocationAreaCode(), cellTower.getCellId()); - Context.getAsyncHttpClient().prepareGet(request).execute(new AsyncCompletionHandler() { + Context.getClient().target(request).request().async().get(new InvocationCallback<JsonObject>() { @Override - public Object onCompleted(Response response) throws Exception { - try (JsonReader reader = Json.createReader(response.getResponseBodyAsStream())) { - JsonObject json = reader.readObject(); - if (json.containsKey("lat") && json.containsKey("lon")) { - callback.onSuccess( - json.getJsonNumber("lat").doubleValue(), - json.getJsonNumber("lon").doubleValue(), 0); - } else { - callback.onFailure(new GeolocationException("Coordinates are missing")); - } + public void completed(JsonObject json) { + if (json.containsKey("lat") && json.containsKey("lon")) { + callback.onSuccess( + json.getJsonNumber("lat").doubleValue(), + json.getJsonNumber("lon").doubleValue(), 0); + } else { + callback.onFailure(new GeolocationException("Coordinates are missing")); } - return null; } @Override - public void onThrowable(Throwable t) { - callback.onFailure(t); + public void failed(Throwable throwable) { + callback.onFailure(throwable); } }); diff --git a/src/org/traccar/geolocation/UniversalGeolocationProvider.java b/src/org/traccar/geolocation/UniversalGeolocationProvider.java index 6416b2633..f71620d8a 100644 --- a/src/org/traccar/geolocation/UniversalGeolocationProvider.java +++ b/src/org/traccar/geolocation/UniversalGeolocationProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2018 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,17 +15,13 @@ */ package org.traccar.geolocation; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.ning.http.client.AsyncCompletionHandler; -import com.ning.http.client.Response; import org.traccar.Context; import org.traccar.model.Network; -import javax.json.Json; import javax.json.JsonObject; -import javax.json.JsonReader; -import javax.ws.rs.core.HttpHeaders; -import javax.ws.rs.core.MediaType; +import javax.ws.rs.client.AsyncInvoker; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.InvocationCallback; public class UniversalGeolocationProvider implements GeolocationProvider { @@ -37,38 +33,26 @@ public class UniversalGeolocationProvider implements GeolocationProvider { @Override public void getLocation(Network network, final LocationProviderCallback callback) { - try { - String request = Context.getObjectMapper().writeValueAsString(network); - Context.getAsyncHttpClient().preparePost(url) - .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON) - .setHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(request.length())) - .setBody(request).execute(new AsyncCompletionHandler() { - @Override - public Object onCompleted(Response response) throws Exception { - try (JsonReader reader = Json.createReader(response.getResponseBodyAsStream())) { - JsonObject json = reader.readObject(); - if (json.containsKey("error")) { - callback.onFailure( - new GeolocationException(json.getJsonObject("error").getString("message"))); - } else { - JsonObject location = json.getJsonObject("location"); - callback.onSuccess( - location.getJsonNumber("lat").doubleValue(), - location.getJsonNumber("lng").doubleValue(), - json.getJsonNumber("accuracy").doubleValue()); - } - } - return null; + AsyncInvoker invoker = Context.getClient().target(url).request().async(); + invoker.post(Entity.json(network), new InvocationCallback<JsonObject>() { + @Override + public void completed(JsonObject json) { + if (json.containsKey("error")) { + callback.onFailure(new GeolocationException(json.getJsonObject("error").getString("message"))); + } else { + JsonObject location = json.getJsonObject("location"); + callback.onSuccess( + location.getJsonNumber("lat").doubleValue(), + location.getJsonNumber("lng").doubleValue(), + json.getJsonNumber("accuracy").doubleValue()); } + } - @Override - public void onThrowable(Throwable t) { - callback.onFailure(t); - } - }); - } catch (JsonProcessingException e) { - callback.onFailure(e); - } + @Override + public void failed(Throwable throwable) { + callback.onFailure(throwable); + } + }); } } diff --git a/src/org/traccar/geolocation/UnwiredGeolocationProvider.java b/src/org/traccar/geolocation/UnwiredGeolocationProvider.java index dcc5a4e7a..963bcb688 100644 --- a/src/org/traccar/geolocation/UnwiredGeolocationProvider.java +++ b/src/org/traccar/geolocation/UnwiredGeolocationProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 - 2018 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,21 +17,16 @@ package org.traccar.geolocation; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; -import com.ning.http.client.AsyncCompletionHandler; -import com.ning.http.client.Response; import org.traccar.Context; import org.traccar.model.CellTower; import org.traccar.model.Network; import org.traccar.model.WifiAccessPoint; -import javax.json.Json; import javax.json.JsonObject; -import javax.json.JsonReader; -import javax.ws.rs.core.HttpHeaders; -import javax.ws.rs.core.MediaType; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.InvocationCallback; import java.util.Collection; public class UnwiredGeolocationProvider implements GeolocationProvider { @@ -90,39 +85,27 @@ public class UnwiredGeolocationProvider implements GeolocationProvider { @Override public void getLocation(Network network, final LocationProviderCallback callback) { - try { - ObjectNode json = objectMapper.valueToTree(network); - json.put("token", key); - String request = objectMapper.writeValueAsString(json); - Context.getAsyncHttpClient().preparePost(url) - .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON) - .setHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(request.length())) - .setBody(request).execute(new AsyncCompletionHandler() { - @Override - public Object onCompleted(Response response) throws Exception { - try (JsonReader reader = Json.createReader(response.getResponseBodyAsStream())) { - JsonObject json = reader.readObject(); - if (json.getString("status").equals("error")) { - callback.onFailure( - new GeolocationException(json.getString("message"))); - } else { - callback.onSuccess( - json.getJsonNumber("lat").doubleValue(), - json.getJsonNumber("lon").doubleValue(), - json.getJsonNumber("accuracy").doubleValue()); - } - } - return null; - } + ObjectNode json = objectMapper.valueToTree(network); + json.put("token", key); - @Override - public void onThrowable(Throwable t) { - callback.onFailure(t); + Context.getClient().target(url).request().async().post(Entity.json(json), new InvocationCallback<JsonObject>() { + @Override + public void completed(JsonObject json) { + if (json.getString("status").equals("error")) { + callback.onFailure(new GeolocationException(json.getString("message"))); + } else { + callback.onSuccess( + json.getJsonNumber("lat").doubleValue(), + json.getJsonNumber("lon").doubleValue(), + json.getJsonNumber("accuracy").doubleValue()); } - }); - } catch (JsonProcessingException e) { - callback.onFailure(e); - } + } + + @Override + public void failed(Throwable throwable) { + callback.onFailure(throwable); + } + }); } } |