Search in sources :

Example 1 with RESTClient

use of wslite.rest.RESTClient 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 RESTClient

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

the class MapService method geocodeGoogle.

public JSONObject geocodeGoogle(String qString) throws AxelorException, JSONException {
    if (StringUtils.isBlank(qString)) {
        return null;
    }
    // http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true_or_false
    // TODO inject the rest client, or better, run it in the browser
    RESTClient restClient = new RESTClient("https://maps.googleapis.com");
    Map<String, Object> responseQuery = new HashMap<>();
    responseQuery.put("address", qString.trim());
    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);
    JSONObject restResponse = getJSON(restClient.get(responseMap));
    LOG.debug("Gmap response: {}", restResponse);
    if (restResponse.containsKey("results")) {
        JSONArray results = (JSONArray) restResponse.get("results");
        if (CollectionUtils.isNotEmpty(results)) {
            JSONObject result = (JSONObject) results.iterator().next();
            if (result != null && result.containsKey("geometry")) {
                return (JSONObject) ((JSONObject) result.get("geometry")).get("location");
            }
        }
    }
    throw new AxelorException(appBaseService.getAppBase(), TraceBackRepository.CATEGORY_NO_VALUE, I18n.get(IExceptionMessage.MAP_RESPONSE_ERROR), restResponse);
/*
     * log.debug("restResponse = {}", restResponse)
     * log.debug("restResponse.parsedResponseContent.text = {}",
     * restResponse.parsedResponseContent.text)
     */
// def searchresults = new
// JsonSlurper().parseText(restResponse.parsedResponseContent.text);
/*
     * LOG.debug("searchresults.status = {}", searchresults.status); if
     * (searchresults.status == "OK") { /*
     * log.debug("searchresults.results.size() = {}", searchresults.results.size())
     * log.debug("searchresults.results[0] = {}", searchresults.results[0])
     * log.debug("searchresults.results[0].address_components = {}",
     * searchresults.results[0].address_components)
     * log.debug("searchresults.results[0].geometry.location = {}",
     * searchresults.results[0].geometry.location)
     */
/*
     * def results = searchresults.results;
     *
     * if (results.size() > 1) { response.put("multiple", true); } def
     * firstPlaceFound = results[0];
     *
     * if (firstPlaceFound) { BigDecimal lat = new
     * BigDecimal(firstPlaceFound.geometry.location.lat); BigDecimal lng = new
     * BigDecimal(firstPlaceFound.geometry.location.lng);
     *
     * response.put("lat", lat.setScale(10, RoundingMode.HALF_UP));
     * response.put("lng", lng.setScale(10, RoundingMode.HALF_UP)); }
     */
// }
}
Also used : AxelorException(com.axelor.exception.AxelorException) RESTClient(wslite.rest.RESTClient) JSONObject(wslite.json.JSONObject) HashMap(java.util.HashMap) JSONArray(wslite.json.JSONArray) JSONObject(wslite.json.JSONObject)

Example 3 with RESTClient

use of wslite.rest.RESTClient 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)3 JSONObject (wslite.json.JSONObject)3 RESTClient (wslite.rest.RESTClient)3 AxelorException (com.axelor.exception.AxelorException)2 Response (wslite.rest.Response)2 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 JSONArray (wslite.json.JSONArray)1 JSONException (wslite.json.JSONException)1