Search in sources :

Example 96 with JsonObject

use of org.json.simple.JsonObject in project spatial-portal by AtlasOfLivingAustralia.

the class Sampling method getDownloadUrl.

static String getDownloadUrl(String statusUrl) {
    String downloadUrl = null;
    try {
        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod(statusUrl);
        get.addRequestHeader(StringConstants.CONTENT_TYPE, StringConstants.APPLICATION_JSON);
        client.executeMethod(get);
        JSONParser jp = new JSONParser();
        JSONObject jo = (JSONObject) jp.parse(get.getResponseBodyAsString());
        if ("finished".equals(jo.get(StringConstants.STATUS))) {
            downloadUrl = jo.get("downloadUrl").toString();
        } else if ("cancelled".equals(jo.get(StringConstants.STATUS)) || "error".equals(jo.get(StringConstants.STATUS))) {
            downloadUrl = null;
        } else {
            downloadUrl = "";
        }
    } catch (Exception e) {
        LOGGER.error("error getting response from : " + statusUrl, e);
    }
    return downloadUrl;
}
Also used : JSONObject(org.json.simple.JSONObject) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JSONParser(org.json.simple.parser.JSONParser)

Example 97 with JsonObject

use of org.json.simple.JsonObject in project spatial-portal by AtlasOfLivingAustralia.

the class PhylogeneticDiversityListResults method evalArea.

private void evalArea(SelectedArea sa) {
    try {
        Query sq = QueryUtil.queryFromSelectedArea(selectedQuery, sa, null, false, null);
        CSVReader r = new CSVReader(new StringReader(sq.speciesList()));
        JSONArray ja = new JSONArray();
        for (String[] s : r.readAll()) {
            ja.add(s[1]);
        }
        //call pd with specieslist=ja.toString()
        String url = CommonData.getSettings().getProperty(CommonData.PHYLOLIST_URL) + "/phylo/getPD";
        NameValuePair[] params = new NameValuePair[2];
        params[0] = new NameValuePair("noTreeText", StringConstants.TRUE);
        params[1] = new NameValuePair("speciesList", ja.toString());
        JSONParser jp = new JSONParser();
        JSONArray pds = (JSONArray) jp.parse(Util.readUrlPost(url, params));
        Map<String, String> pdrow = new HashMap<String, String>();
        Map<String, JSONArray> speciesRow = new HashMap<String, JSONArray>();
        for (int j = 0; j < pds.size(); j++) {
            String tree = "" + ((JSONObject) pds.get(j)).get(StringConstants.STUDY_ID);
            pdrow.put(tree, ((JSONObject) pds.get(j)).get("pd").toString());
            speciesRow.put(tree, (JSONArray) ((JSONObject) pds.get(j)).get("taxaRecognised"));
            //maxPD retrieval
            String maxPd = ((JSONObject) pds.get(j)).get("maxPd").toString();
            for (int k = 0; k < selectedTrees.size(); k++) {
                if (((Map<String, String>) selectedTrees.get(k)).get(StringConstants.STUDY_ID).equals(tree)) {
                    ((Map<String, String>) selectedTrees.get(k)).put("maxPd", maxPd);
                }
            }
        }
        areaPds.add(pdrow);
        areaSpeciesMatches.add(speciesRow);
    } catch (Exception e) {
        LOGGER.error("failed processing a pd for a selected area.", e);
    }
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) Query(au.org.ala.spatial.util.Query) CSVReader(au.com.bytecode.opencsv.CSVReader) JSONArray(org.json.simple.JSONArray) JSONObject(org.json.simple.JSONObject) StringReader(java.io.StringReader) JSONParser(org.json.simple.parser.JSONParser)

Example 98 with JsonObject

use of org.json.simple.JsonObject in project spatial-portal by AtlasOfLivingAustralia.

the class AreaReportController method onMapPointsOfInterest.

public void onMapPointsOfInterest(Event event) {
    try {
        String activeAreaLayerName = "Points of interest in " + areaDisplayName;
        StringBuilder sb = new StringBuilder();
        sb.append("MULTIPOINT(");
        if (pointsOfInterest == null) {
            String wkt = selectedArea.getWkt();
            if (wkt.contains(StringConstants.ENVELOPE) && selectedArea.getMapLayer() != null) {
                // use boundingbox
                List<Double> bbox = selectedArea.getMapLayer().getMapLayerMetadata().getBbox();
                double long1 = bbox.get(0);
                double lat1 = bbox.get(1);
                double long2 = bbox.get(2);
                double lat2 = bbox.get(3);
                wkt = StringConstants.POLYGON + "((" + long1 + " " + lat1 + "," + long1 + " " + lat2 + "," + long2 + " " + lat2 + "," + long2 + " " + lat1 + "," + long1 + " " + lat1 + "))";
            } else {
            //wkt = selectedArea.getReducedWkt();
            }
            pointsOfInterest = getPointsOfInterest(wkt);
        }
        for (int i = 0; i < pointsOfInterest.size(); i++) {
            JSONObject jsonObjPoi = (JSONObject) pointsOfInterest.get(i);
            double latitude = Double.parseDouble(jsonObjPoi.get(StringConstants.LATITUDE).toString());
            double longitude = Double.parseDouble(jsonObjPoi.get(StringConstants.LONGITUDE).toString());
            sb.append(longitude);
            sb.append(" ");
            sb.append(latitude);
            if (i < pointsOfInterest.size() - 1) {
                sb.append(",");
            }
        }
        sb.append(")");
        getMapComposer().mapPointsOfInterest(sb.toString(), activeAreaLayerName, activeAreaLayerName);
    } catch (Exception e) {
        LOGGER.error("error mapping points of interest", e);
    }
}
Also used : JSONObject(org.json.simple.JSONObject)

Example 99 with JsonObject

use of org.json.simple.JsonObject in project spatial-portal by AtlasOfLivingAustralia.

the class SpeciesAutoComplete method getResults.

private JSONArray getResults(String nsurl) throws Exception {
    HttpClient client = new HttpClient();
    GetMethod get = new GetMethod(nsurl);
    get.addRequestHeader(StringConstants.CONTENT_TYPE, StringConstants.TEXT_PLAIN);
    client.executeMethod(get);
    String rawJSON = get.getResponseBodyAsString();
    //parse
    JSONParser jp = new JSONParser();
    JSONObject jo = (JSONObject) jp.parse(rawJSON);
    //support search and auto bie webservices
    if (jo.containsKey("searchResults")) {
        return (JSONArray) ((JSONObject) jo.get("searchResults")).get("results");
    } else {
        return (JSONArray) jo.get("autoCompleteList");
    }
}
Also used : JSONObject(org.json.simple.JSONObject) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser)

Example 100 with JsonObject

use of org.json.simple.JsonObject in project spatial-portal by AtlasOfLivingAustralia.

the class ProgressController method get.

JSONObject get() {
    try {
        StringBuilder sbProcessUrl = new StringBuilder();
        sbProcessUrl.append(CommonData.getSatServer()).append("/ws/job?pid=").append(pid);
        LOGGER.debug("checking status every '" + timer.getDelay() + "' sec: " + sbProcessUrl.toString());
        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod(sbProcessUrl.toString());
        get.addRequestHeader(StringConstants.ACCEPT, StringConstants.APPLICATION_JSON);
        client.getHttpConnectionManager().getParams().setSoTimeout(timer.getDelay());
        int result = client.executeMethod(get);
        if (result == 200) {
            JSONParser jp = new JSONParser();
            return (JSONObject) jp.parse(get.getResponseBodyAsString());
        }
    } catch (SocketTimeoutException e) {
        LOGGER.debug("progress timeout exception, will be trying again.");
    } catch (Exception e) {
        LOGGER.error("error getting updated job info pid=" + pid, e);
    }
    return null;
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) JSONObject(org.json.simple.JSONObject) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JSONParser(org.json.simple.parser.JSONParser) SocketTimeoutException(java.net.SocketTimeoutException)

Aggregations

JSONObject (org.json.simple.JSONObject)424 JSONParser (org.json.simple.parser.JSONParser)133 JSONArray (org.json.simple.JSONArray)123 Test (org.junit.Test)75 IOException (java.io.IOException)48 ParseException (org.json.simple.parser.ParseException)46 HashMap (java.util.HashMap)44 Map (java.util.Map)43 HttpClient (org.apache.commons.httpclient.HttpClient)39 ArrayList (java.util.ArrayList)33 GetMethod (org.apache.commons.httpclient.methods.GetMethod)30 File (java.io.File)26 List (java.util.List)25 HttpURLConnection (java.net.HttpURLConnection)22 URL (java.net.URL)18 MapLayer (au.org.emii.portal.menu.MapLayer)17 InputStreamReader (java.io.InputStreamReader)16 FileReader (java.io.FileReader)13 Date (java.util.Date)13 InputStream (java.io.InputStream)11