Search in sources :

Example 21 with JSONParser

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

the class BiocacheQuery method getAutoComplete.

/**
     * Returns the "autocomplete" values for the query based on the supplied facet field. Extensible so that we can add autocomplete
     * based on queries in other areas.
     * <p/>
     * NC 20131126 - added to support an autocomplete of raw taxon name for a fix associated with Fungi
     *
     * @param facet The facet to autocomplete on
     * @param value The prefix for the autocomplete
     * @param limit The maximum number of values to return
     * @return
     */
public String getAutoComplete(String facet, String value, int limit) {
    HttpClient client = new HttpClient();
    StringBuilder slist = new StringBuilder();
    if (value.length() >= 3 && StringUtils.isNotBlank(facet)) {
        try {
            String url = biocacheServer + QUERY_TITLE_URL + "q=" + getQ() + getQc() + "&facets=" + facet + "&fprefix=" + URLEncoder.encode(value, StringConstants.UTF_8) + "&pageSize=0&flimit=" + limit;
            GetMethod get = new GetMethod(url);
            get.addRequestHeader(StringConstants.CONTENT_TYPE, StringConstants.TEXT_PLAIN);
            int result = client.executeMethod(get);
            if (result == 200) {
                //success
                String rawJSON = get.getResponseBodyAsString();
                //parse
                JSONParser jp = new JSONParser();
                JSONObject jo = (JSONObject) jp.parse(rawJSON);
                JSONArray ja = (JSONArray) jo.get("facetResults");
                for (int i = 0; i < ja.size(); i++) {
                    JSONObject o = (JSONObject) ja.get(i);
                    if (o.get("fieldName").equals(facet)) {
                        //process the values in the list
                        JSONArray values = (JSONArray) o.get("fieldResult");
                        for (int j = 0; j < values.size(); j++) {
                            JSONObject vo = (JSONObject) values.get(j);
                            if (slist.length() > 0) {
                                slist.append("\n");
                            }
                            slist.append(vo.get("label")).append("//found ").append(vo.get(StringConstants.COUNT).toString());
                        }
                    }
                }
            } else {
                LOGGER.warn("There was an issue performing the autocomplete from the biocache: " + result);
            }
        } catch (Exception e) {
            LOGGER.error("failed to get autocomplete facet=" + facet + ", value=" + value, e);
        }
    }
    return slist.toString();
}
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 22 with JSONParser

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

the class BiocacheQuery method getGuid.

/**
     * Performs a scientific name or common name lookup and returns the guid if it exists in the BIE
     * <p/>
     * TODO Move getGuid and getClassification to BIE Utilities...
     *
     * @param name
     * @return
     */
public static String getGuid(String name) {
    if ("true".equalsIgnoreCase(CommonData.getSettings().getProperty("new.bie"))) {
        String url = CommonData.getBieServer() + "/ws/species/lookup/bulk";
        try {
            HttpClient client = new HttpClient();
            PostMethod get = new PostMethod(url);
            get.addRequestHeader(StringConstants.CONTENT_TYPE, StringConstants.APPLICATION_JSON);
            get.setRequestEntity(new StringRequestEntity("{\"names\":[\"" + name.replace("\"", "\\\"") + "\"]}"));
            client.executeMethod(get);
            String body = get.getResponseBodyAsString();
            JSONParser jp = new JSONParser();
            JSONArray ja = (JSONArray) jp.parse(body);
            if (ja != null && !ja.isEmpty()) {
                JSONObject jo = (JSONObject) ja.get(0);
                if (jo != null && jo.containsKey("acceptedIdentifier") && jo.get("acceptedIdentifier") != null) {
                    return jo.get("acceptedIdentifier").toString();
                } else if (jo != null && jo.containsKey("acceptedIdentifierGuid") && jo.get("acceptedIdentifierGuid") != null) {
                    return jo.get("acceptedIdentifierGuid").toString();
                } else if (jo != null && jo.containsKey("acceptedConceptID") && jo.get("acceptedConceptID") != null) {
                    return jo.get("acceptedConceptID").toString();
                } else if (jo != null && jo.containsKey("guid") && jo.get("guid") != null) {
                    return jo.get("guid").toString();
                } else {
                    return null;
                }
            } else {
                return null;
            }
        } catch (Exception e) {
            LOGGER.error("error getting guid at: " + url, e);
            return null;
        }
    } else {
        String url = CommonData.getBieServer() + "/ws/guid/" + name.replaceAll(" ", "%20");
        try {
            HttpClient client = new HttpClient();
            GetMethod get = new GetMethod(url);
            get.addRequestHeader(StringConstants.CONTENT_TYPE, StringConstants.APPLICATION_JSON);
            client.executeMethod(get);
            String body = get.getResponseBodyAsString();
            JSONParser jp = new JSONParser();
            JSONArray ja = (JSONArray) jp.parse(body);
            if (ja != null && !ja.isEmpty()) {
                JSONObject jo = (JSONObject) ja.get(0);
                if (jo != null && jo.containsKey("acceptedIdentifier") && jo.get("acceptedIdentifier") != null) {
                    return jo.get("acceptedIdentifier").toString();
                } else {
                    return null;
                }
            } else {
                return null;
            }
        } catch (Exception e) {
            LOGGER.error("error getting guid at: " + url, e);
            return null;
        }
    }
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) JSONObject(org.json.simple.JSONObject) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) JSONArray(org.json.simple.JSONArray) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JSONParser(org.json.simple.parser.JSONParser)

Example 23 with JSONParser

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

the class CommonData method getDataTypes.

/**
     * Extracts the biocache data types from the webservice so that they can be used to dynamically load the facets
     *
     * @return
     */
private static Map<String, QueryField.FieldType> getDataTypes() throws Exception {
    Map<String, QueryField.FieldType> map = new HashMap<String, QueryField.FieldType>();
    //get the JSON from the WS
    JSONParser jp = new JSONParser();
    JSONArray values = (JSONArray) jp.parse(Util.readUrl(CommonData.getBiocacheServer() + "/index/fields"));
    for (Object mvalues : values) {
        String name = ((JSONObject) mvalues).get(StringConstants.NAME).toString();
        String dtype = "string";
        if (((JSONObject) mvalues).containsKey("dataType"))
            dtype = ((JSONObject) mvalues).get("dataType").toString();
        if ("string".equals(dtype) || "textgen".equals(dtype)) {
            map.put(name, QueryField.FieldType.STRING);
        } else if ("int".equals(dtype) || "tint".equals(dtype) || "tdate".equals(dtype)) {
            map.put(name, QueryField.FieldType.INT);
        } else if ("double".equals(dtype) || "tdouble".equals(dtype)) {
            map.put(name, QueryField.FieldType.DOUBLE);
        } else {
            map.put(name, QueryField.FieldType.STRING);
        }
    }
    return map;
}
Also used : QueryField(au.org.ala.legend.QueryField) JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject)

Example 24 with JSONParser

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

the class Sampling method sampling.

public static List<String[]> sampling(List<String> facetIds, double[][] points) {
    try {
        long start = System.currentTimeMillis();
        URL url = new URL(CommonData.getLayersServer() + "/intersect/batch");
        URLConnection c = url.openConnection();
        c.setDoOutput(true);
        OutputStreamWriter out = new OutputStreamWriter(c.getOutputStream());
        out.write("fids=");
        for (int i = 0; i < facetIds.size(); i++) {
            if (i > 0) {
                out.write(",");
            }
            out.write(facetIds.get(i));
        }
        out.write("&points=");
        for (int i = 0; i < points.length; i++) {
            if (i > 0) {
                out.write(",");
            }
            out.write(String.valueOf(points[i][1]));
            out.write(",");
            out.write(String.valueOf(points[i][0]));
        }
        out.write("&pw=");
        out.write(CommonData.getSettings().getProperty("batch.sampling.pw"));
        out.close();
        JSONParser jp = new JSONParser();
        JSONObject jo = (JSONObject) jp.parse(IOUtils.toString(c.getInputStream()));
        String statusUrl = jo.get("statusUrl").toString();
        //wait until done, or until it fails
        String downloadUrl = null;
        int count = 0;
        int retryMax = 10;
        int retry = 0;
        while (retry < retryMax) {
            Thread.sleep(2000);
            while ((downloadUrl = getDownloadUrl(statusUrl)) != null) {
                retry = 0;
                if (!downloadUrl.isEmpty() || downloadUrl == null) {
                    retry = retryMax;
                    break;
                }
                count++;
            }
            retry++;
        }
        if (downloadUrl != null) {
            return getDownloadData(downloadUrl, points.length);
        }
    } catch (Exception e) {
        LOGGER.error("error with sampling", e);
    }
    return new ArrayList();
}
Also used : JSONObject(org.json.simple.JSONObject) ArrayList(java.util.ArrayList) OutputStreamWriter(java.io.OutputStreamWriter) JSONParser(org.json.simple.parser.JSONParser) URL(java.net.URL) URLConnection(java.net.URLConnection)

Example 25 with JSONParser

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

the class CommonData method initDownloadReasons.

public static void initDownloadReasons() {
    copyDownloadReasons = null;
    LOGGER.debug("CommonData::initDownloadReasons()");
    String url = CommonData.getSettings().getProperty("logger.url") + "/service/logger/reasons";
    try {
        LOGGER.debug(url);
        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod(url);
        int result = client.executeMethod(get);
        if (result == 200) {
            JSONParser jp = new JSONParser();
            copyDownloadReasons = (JSONArray) jp.parse(get.getResponseBodyAsString());
        }
    } catch (Exception e) {
        copyDownloadReasons = null;
        LOGGER.error("error getting reasons: " + url, e);
    }
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JSONParser(org.json.simple.parser.JSONParser) Point(com.vividsolutions.jts.geom.Point)

Aggregations

JSONParser (org.json.simple.parser.JSONParser)146 JSONObject (org.json.simple.JSONObject)136 JSONArray (org.json.simple.JSONArray)61 HttpClient (org.apache.commons.httpclient.HttpClient)44 ParseException (org.json.simple.parser.ParseException)40 GetMethod (org.apache.commons.httpclient.methods.GetMethod)34 Test (org.junit.Test)18 IOException (java.io.IOException)15 File (java.io.File)13 URL (java.net.URL)13 InputStreamReader (java.io.InputStreamReader)11 PostMethod (org.apache.commons.httpclient.methods.PostMethod)11 HashMap (java.util.HashMap)10 Map (java.util.Map)10 MapLayer (au.org.emii.portal.menu.MapLayer)9 Point (com.vividsolutions.jts.geom.Point)9 FileReader (java.io.FileReader)9 ArrayList (java.util.ArrayList)9 InputStream (java.io.InputStream)7 Facet (au.org.ala.legend.Facet)6