Search in sources :

Example 1 with Response

use of wslite.rest.Response in project axelor-open-suite by axelor.

the class MapService method getMapOsm.

@SuppressWarnings({ "rawtypes", "unchecked" })
public Map<String, Object> getMapOsm(String qString) {
    Map<String, Object> result = new HashMap<>();
    try {
        BigDecimal latitude = BigDecimal.ZERO;
        BigDecimal longitude = BigDecimal.ZERO;
        RESTClient restClient = new RESTClient("https://nominatim.openstreetmap.org/");
        Map<String, Object> mapQuery = new HashMap<>();
        mapQuery.put("q", qString);
        mapQuery.put("format", "xml");
        mapQuery.put("polygon", true);
        mapQuery.put("addressdetails", true);
        Map<String, Object> mapResponse = new HashMap<>();
        mapResponse.put("path", "/search");
        mapResponse.put("accept", ContentType.JSON);
        mapResponse.put("query", mapQuery);
        mapResponse.put("connectTimeout", 10000);
        mapResponse.put("readTimeout", 10000);
        mapResponse.put("followRedirects", false);
        mapResponse.put("useCaches", false);
        mapResponse.put("sslTrustAllCerts", true);
        Response restResponse = restClient.get(mapResponse);
        GPathResult searchresults = new XmlSlurper().parseText(restResponse.getContentAsString());
        Iterator<Node> iterator = searchresults.childNodes();
        if (iterator.hasNext()) {
            Node node = iterator.next();
            Map attributes = node.attributes();
            if (attributes.containsKey("lat") && attributes.containsKey("lon")) {
                if (BigDecimal.ZERO.compareTo(latitude) == 0)
                    latitude = new BigDecimal(node.attributes().get("lat").toString());
                if (BigDecimal.ZERO.compareTo(longitude) == 0)
                    longitude = new BigDecimal(node.attributes().get("lon").toString());
            }
        }
        LOG.debug("OSMap qString: {}, latitude: {}, longitude: {}", qString, latitude, longitude);
        if (BigDecimal.ZERO.compareTo(latitude) != 0 && BigDecimal.ZERO.compareTo(longitude) != 0) {
            result.put("url", "map/oneMarker.html?x=" + latitude + "&y=" + longitude + "&z=18");
            result.put("latitude", latitude);
            result.put("longitude", longitude);
            return result;
        }
    } catch (Exception e) {
        TraceBackService.trace(e);
    }
    return null;
}
Also used : RESTClient(wslite.rest.RESTClient) HashMap(java.util.HashMap) XmlSlurper(groovy.util.XmlSlurper) Node(groovy.util.slurpersupport.Node) BigDecimal(java.math.BigDecimal) AxelorException(com.axelor.exception.AxelorException) JSONException(wslite.json.JSONException) Response(wslite.rest.Response) JSONObject(wslite.json.JSONObject) GPathResult(groovy.util.slurpersupport.GPathResult) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Response

use of wslite.rest.Response in project axelor-open-suite by axelor.

the class MapService method testGMapService.

public void testGMapService() throws AxelorException, JSONException {
    RESTClient restClient = new RESTClient("https://maps.googleapis.com");
    Map<String, Object> responseQuery = new HashMap<>();
    responseQuery.put("address", "google");
    responseQuery.put("sensor", "false");
    responseQuery.put("key", getGoogleMapsApiKey());
    Map<String, Object> responseMap = new HashMap<>();
    responseMap.put("path", "/maps/api/geocode/json");
    responseMap.put("accept", ContentType.JSON);
    responseMap.put("query", responseQuery);
    responseMap.put("connectTimeout", 5000);
    responseMap.put("readTimeout", 10000);
    responseMap.put("followRedirects", false);
    responseMap.put("useCaches", false);
    responseMap.put("sslTrustAllCerts", true);
    Response response = restClient.get(responseMap);
    getJSON(response);
}
Also used : Response(wslite.rest.Response) RESTClient(wslite.rest.RESTClient) HashMap(java.util.HashMap) JSONObject(wslite.json.JSONObject)

Aggregations

HashMap (java.util.HashMap)2 JSONObject (wslite.json.JSONObject)2 RESTClient (wslite.rest.RESTClient)2 Response (wslite.rest.Response)2 AxelorException (com.axelor.exception.AxelorException)1 XmlSlurper (groovy.util.XmlSlurper)1 GPathResult (groovy.util.slurpersupport.GPathResult)1 Node (groovy.util.slurpersupport.Node)1 BigDecimal (java.math.BigDecimal)1 Map (java.util.Map)1 JSONException (wslite.json.JSONException)1