Search in sources :

Example 31 with JSONParser

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

the class RemoteLogger method getLogItem.

public JSONObject getLogItem(String logId) {
    init();
    try {
        if (Util.isLoggedIn()) {
            String url = loggerService + "/app/view/" + logId + ".json" + "?appid=" + URLEncoder.encode(appid, StringConstants.UTF_8) + "&api_key=" + URLEncoder.encode(CommonData.getSettings().getProperty("api_key"), StringConstants.UTF_8);
            HttpClient client = new HttpClient();
            GetMethod get = new GetMethod(url);
            get.addRequestHeader(StringConstants.ACCEPT, StringConstants.APPLICATION_JSON);
            client.executeMethod(get);
            LOGGER.debug("get: " + url + ", response: " + get.getResponseBodyAsString());
            JSONParser jp = new JSONParser();
            return (JSONObject) jp.parse(get.getResponseBodyAsString());
        }
    } catch (Exception e) {
        LOGGER.error("Error getting logging information from server:", e);
    }
    return null;
}
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 32 with JSONParser

use of org.json.simple.parser.JSONParser 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 33 with JSONParser

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

the class CommonData method initSpeciesListAdditionalColumns.

private static Map<String, Map<String, List<String>>> initSpeciesListAdditionalColumns() {
    Map<String, Map<String, List<String>>> map = new HashMap<String, Map<String, List<String>>>();
    String slac = settings.getProperty("species.list.additional.columns", "");
    //append dynamic columns
    slac += dynamicSpeciesListColumns();
    String[] columns = slac.split("\\|");
    for (String line : columns) {
        String[] parts = line.split(",");
        if (parts[0].equals("Conservation")) {
            speciesListThreatened = "species_list_uid:" + StringUtils.join(Arrays.copyOfRange(parts, 1, parts.length), " OR species_list_uid:");
        }
        if (parts[0].equals("Invasive")) {
            speciesListInvasive = "species_list_uid:" + StringUtils.join(Arrays.copyOfRange(parts, 1, parts.length), " OR species_list_uid:");
        }
        if (parts.length > 1) {
            String columnTitle = parts[0];
            for (int i = 1; i < parts.length; i++) {
                try {
                    JSONParser jp = new JSONParser();
                    InputStream is = new URL(CommonData.getSpeciesListServer() + "/ws/speciesList?druid=" + parts[i]).openStream();
                    String listName = ((JSONObject) jp.parse(IOUtils.toString(is))).get("listName").toString();
                    is.close();
                    Map<String, List<String>> m = map.get(columnTitle);
                    if (m == null)
                        m = new HashMap<String, List<String>>();
                    ArrayList<String> sp = new ArrayList<String>();
                    //fetch species list
                    Collection<SpeciesListItemDTO> list = SpeciesListUtil.getListItems(parts[i]);
                    for (SpeciesListItemDTO item : list) {
                        if (item.getLsid() != null && !item.getLsid().isEmpty())
                            sp.add(item.getLsid());
                    }
                    Collections.sort(sp);
                    m.put(listName, sp);
                    map.put(columnTitle, m);
                } catch (Exception e) {
                    LOGGER.error("error reading list: " + parts[i], e);
                }
            }
        }
    }
    return map;
}
Also used : Point(com.vividsolutions.jts.geom.Point) URL(java.net.URL) SpeciesListItemDTO(au.org.ala.spatial.dto.SpeciesListItemDTO) JSONParser(org.json.simple.parser.JSONParser)

Example 34 with JSONParser

use of org.json.simple.parser.JSONParser 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 35 with JSONParser

use of org.json.simple.parser.JSONParser 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)

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