aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/geocoder/JsonGeocoder.java
diff options
context:
space:
mode:
authorShinryuken <watertext@hotmail.it>2017-11-19 11:22:31 +0100
committerGitHub <noreply@github.com>2017-11-19 11:22:31 +0100
commita5b273f8f8afd1a67613517c6487642601070456 (patch)
treebabc63f5d50977fc91b3946b1da2e86ec74c51a5 /src/org/traccar/geocoder/JsonGeocoder.java
parentc7928ef4a722cb40e9911c8e76e308fc48c85567 (diff)
parentfb9cbb2b74dc0060bd0dc5d0b3bfeb958ed6f3b5 (diff)
downloadtrackermap-server-a5b273f8f8afd1a67613517c6487642601070456.tar.gz
trackermap-server-a5b273f8f8afd1a67613517c6487642601070456.tar.bz2
trackermap-server-a5b273f8f8afd1a67613517c6487642601070456.zip
Merge branch 'master' into payload-as-form-param
Diffstat (limited to 'src/org/traccar/geocoder/JsonGeocoder.java')
-rw-r--r--src/org/traccar/geocoder/JsonGeocoder.java85
1 files changed, 57 insertions, 28 deletions
diff --git a/src/org/traccar/geocoder/JsonGeocoder.java b/src/org/traccar/geocoder/JsonGeocoder.java
index 6d1380729..82a6ee604 100644
--- a/src/org/traccar/geocoder/JsonGeocoder.java
+++ b/src/org/traccar/geocoder/JsonGeocoder.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2015 Anton Tananaev (anton@traccar.org)
+ * Copyright 2015 - 2017 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.
@@ -18,23 +18,29 @@ 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 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 {
private final String url;
+ private final AddressFormat addressFormat;
private Map<Map.Entry<Double, Double>, String> cache;
- public JsonGeocoder(String url, final int cacheSize) {
+ public JsonGeocoder(String url, final int cacheSize, AddressFormat addressFormat) {
this.url = url;
+ this.addressFormat = addressFormat;
if (cacheSize > 0) {
this.cache = Collections.synchronizedMap(new LinkedHashMap<Map.Entry<Double, Double>, String>() {
@Override
@@ -45,43 +51,66 @@ 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");
+ }
+ }
+ return null;
+ }
+
@Override
- public void getAddress(
- final AddressFormat format, final double latitude,
- final double longitude, final ReverseGeocoderCallback callback) {
+ public String getAddress(
+ final double latitude, final double longitude, final ReverseGeocoderCallback callback) {
if (cache != null) {
String cachedAddress = cache.get(new AbstractMap.SimpleImmutableEntry<>(latitude, longitude));
if (cachedAddress != null) {
- callback.onSuccess(cachedAddress);
- return;
+ if (callback != null) {
+ callback.onSuccess(cachedAddress);
+ }
+ return cachedAddress;
}
}
- Context.getAsyncHttpClient().prepareGet(String.format(url, latitude, longitude))
- .execute(new AsyncCompletionHandler() {
- @Override
- public Object onCompleted(Response response) throws Exception {
- try (JsonReader reader = Json.createReader(response.getResponseBodyAsStream())) {
- Address address = parseAddress(reader.readObject());
- if (address != null) {
- String formattedAddress = format.format(address);
- if (cache != null) {
- cache.put(new AbstractMap.SimpleImmutableEntry<>(latitude, longitude), formattedAddress);
- }
- callback.onSuccess(formattedAddress);
- } else {
- callback.onFailure(new GeocoderException("Empty address"));
- }
+ if (callback != null) {
+ Context.getAsyncHttpClient().prepareGet(String.format(url, latitude, longitude))
+ .execute(new AsyncCompletionHandler() {
+ @Override
+ public Object onCompleted(Response response) throws Exception {
+ return handleResponse(latitude, longitude, response, callback);
}
- return null;
- }
- @Override
- public void onThrowable(Throwable t) {
- callback.onFailure(t);
+ @Override
+ public void onThrowable(Throwable t) {
+ callback.onFailure(t);
+ }
+ });
+ } 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 null;
}
public abstract Address parseAddress(JsonObject json);