diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2018-06-08 19:34:42 +1200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-08 19:34:42 +1200 |
commit | a169ff4eb6c1de110ddcedebdec77fb7aae05a33 (patch) | |
tree | f2a3482f0214d04ec31bd898c375d38c7b89a9be /src/org/traccar/geocoder/JsonGeocoder.java | |
parent | ae1b5bd74b69a7e8222af39e3c62aa217ba94d29 (diff) | |
parent | 5a9811c3771d92832c9bba718b5ec935844978e8 (diff) | |
download | trackermap-server-a169ff4eb6c1de110ddcedebdec77fb7aae05a33.tar.gz trackermap-server-a169ff4eb6c1de110ddcedebdec77fb7aae05a33.tar.bz2 trackermap-server-a169ff4eb6c1de110ddcedebdec77fb7aae05a33.zip |
Merge pull request #3920 from traccar/remove-http-client
Remove async HTTP client dependency
Diffstat (limited to 'src/org/traccar/geocoder/JsonGeocoder.java')
-rw-r--r-- | src/org/traccar/geocoder/JsonGeocoder.java | 67 |
1 files changed, 28 insertions, 39 deletions
diff --git a/src/org/traccar/geocoder/JsonGeocoder.java b/src/org/traccar/geocoder/JsonGeocoder.java index 82a6ee604..e06663689 100644 --- a/src/org/traccar/geocoder/JsonGeocoder.java +++ b/src/org/traccar/geocoder/JsonGeocoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2017 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,21 +15,16 @@ */ package org.traccar.geocoder; -import com.ning.http.client.AsyncCompletionHandler; -import com.ning.http.client.Response; import org.traccar.Context; import org.traccar.helper.Log; -import javax.json.Json; import javax.json.JsonObject; -import javax.json.JsonReader; - -import java.io.IOException; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.client.InvocationCallback; import java.util.AbstractMap; import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; -import java.util.concurrent.ExecutionException; public abstract class JsonGeocoder implements Geocoder { @@ -51,25 +46,24 @@ public abstract class JsonGeocoder implements Geocoder { } } - private String handleResponse(double latitude, double longitude, Response response, - ReverseGeocoderCallback callback) throws IOException { - try (JsonReader reader = Json.createReader(response.getResponseBodyAsStream())) { - Address address = parseAddress(reader.readObject()); - if (address != null) { - String formattedAddress = addressFormat.format(address); - if (cache != null) { - cache.put(new AbstractMap.SimpleImmutableEntry<>(latitude, longitude), formattedAddress); - } - if (callback != null) { - callback.onSuccess(formattedAddress); - } - return formattedAddress; - } else { - if (callback != null) { - callback.onFailure(new GeocoderException("Empty address")); - } - Log.warning("Empty address"); + private String handleResponse( + double latitude, double longitude, JsonObject json, ReverseGeocoderCallback callback) { + + Address address = parseAddress(json); + if (address != null) { + String formattedAddress = addressFormat.format(address); + if (cache != null) { + cache.put(new AbstractMap.SimpleImmutableEntry<>(latitude, longitude), formattedAddress); + } + if (callback != null) { + callback.onSuccess(formattedAddress); + } + return formattedAddress; + } else { + if (callback != null) { + callback.onFailure(new GeocoderException("Empty address")); } + Log.warning("Empty address"); } return null; } @@ -88,27 +82,22 @@ public abstract class JsonGeocoder implements Geocoder { } } + Invocation.Builder request = Context.getClient().target(String.format(url, latitude, longitude)).request(); + if (callback != null) { - Context.getAsyncHttpClient().prepareGet(String.format(url, latitude, longitude)) - .execute(new AsyncCompletionHandler() { + request.async().get(new InvocationCallback<JsonObject>() { @Override - public Object onCompleted(Response response) throws Exception { - return handleResponse(latitude, longitude, response, callback); + public void completed(JsonObject json) { + callback.onSuccess(handleResponse(latitude, longitude, json, callback)); } @Override - public void onThrowable(Throwable t) { - callback.onFailure(t); + public void failed(Throwable throwable) { + callback.onFailure(throwable); } }); } else { - try { - Response response = Context.getAsyncHttpClient() - .prepareGet(String.format(url, latitude, longitude)).execute().get(); - return handleResponse(latitude, longitude, response, null); - } catch (InterruptedException | ExecutionException | IOException error) { - Log.warning("Geocoding failed", error); - } + return handleResponse(latitude, longitude, request.get(JsonObject.class), null); } return null; } |