Search in sources :

Example 91 with HttpClient

use of org.apache.commons.httpclient.HttpClient in project spatial-portal by AtlasOfLivingAustralia.

the class BiocacheQuery method retrieveCustomFields.

private List<String> retrieveCustomFields() {
    List<String> customFields = new ArrayList<String>();
    //look up facets
    final String jsonUri = biocacheServer + "/upload/dynamicFacets?q=" + getFullQ(true) + "&qc=" + getQc();
    try {
        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod(jsonUri);
        get.addRequestHeader(StringConstants.CONTENT_TYPE, StringConstants.APPLICATION_JSON);
        client.executeMethod(get);
        String slist = get.getResponseBodyAsString();
        JSONParser jp = new JSONParser();
        JSONArray ja = (JSONArray) jp.parse(slist);
        for (Object arrayElement : ja) {
            JSONObject jsonObject = (JSONObject) arrayElement;
            String facetName = jsonObject.get(StringConstants.NAME).toString();
            if (!facetName.endsWith("_RNG")) {
                customFields.add(facetName);
            }
        }
    } catch (Exception e) {
        LOGGER.error("error loading custom facets for: " + jsonUri, e);
    }
    return customFields;
}
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) JSONObject(org.json.simple.JSONObject) LegendObject(au.org.ala.legend.LegendObject)

Example 92 with HttpClient

use of org.apache.commons.httpclient.HttpClient in project spatial-portal by AtlasOfLivingAustralia.

the class BiocacheQuery method getBBox.

@Override
public List<Double> getBBox() {
    if (bbox != null) {
        return bbox;
    }
    bbox = new ArrayList<Double>();
    HttpClient client = new HttpClient();
    String url = biocacheServer + BOUNDING_BOX_CSV + DEFAULT_ROWS + "&q=" + getQ() + getQc();
    GetMethod get = new GetMethod(url);
    try {
        client.executeMethod(get);
        String[] s = get.getResponseBodyAsString().split(",");
        for (int i = 0; i < 4; i++) {
            bbox.add(Double.parseDouble(s[i]));
        }
    } catch (Exception e) {
        //default to 'world' bb
        bbox = Util.getBoundingBox(CommonData.WORLD_WKT);
        LOGGER.error("error getting species layer bounding box from biocache:" + url, e);
    }
    return bbox;
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod)

Example 93 with HttpClient

use of org.apache.commons.httpclient.HttpClient in project spatial-portal by AtlasOfLivingAustralia.

the class BiocacheQuery method endemicSpeciesList.

public String endemicSpeciesList() {
    if (endemicSpeciesList != null) {
        return endemicSpeciesList;
    }
    if (CommonData.getSettings().containsKey("endemic.sp.method") && CommonData.getSettings().getProperty("endemic.sp.method").equals("true")) {
        String speciesList = speciesList();
        //can get species list counts as "kosher:true" or "kosher:*" only
        Map speciesCounts;
        if (getGeospatialKosher()[1]) {
            //[1] is 'include kosher:false'
            speciesCounts = CommonData.getSpeciesListCounts(false);
        } else {
            speciesCounts = CommonData.getSpeciesListCountsKosher(false);
        }
        StringBuilder sb = new StringBuilder();
        int speciesCol = 0;
        int countCol = 11;
        try {
            CSVReader csv = new CSVReader(new StringReader(speciesList));
            String[] row;
            int currentPos = 0;
            int nextPos = speciesList.indexOf('\n', currentPos + 1);
            //header
            sb.append(speciesList.substring(currentPos, nextPos));
            //header
            csv.readNext();
            while ((row = csv.readNext()) != null) {
                //add if species is not present elsewhere
                Long c = (Long) speciesCounts.get(row[speciesCol]);
                if (c != null && c <= Long.parseLong(row[countCol])) {
                    if (nextPos > speciesList.length()) {
                        nextPos = speciesList.length();
                    }
                    sb.append(speciesList.substring(currentPos, nextPos));
                } else if (c == null) {
                    LOGGER.error("failed to find species_guid: " + row[speciesCol] + " in CommonData.getSpeciesListCounts()");
                }
                currentPos = nextPos;
                nextPos = speciesList.indexOf('\n', currentPos + 1);
            }
        } catch (Exception e) {
            LOGGER.error("failed generating endemic species list", e);
        }
        endemicSpeciesList = sb.toString();
    } else {
        forMapping = true;
        if (paramId == null)
            makeParamId();
        HttpClient client = new HttpClient();
        String url = biocacheServer + ENDEMIC_LIST + paramId + "?facets=names_and_lsid";
        LOGGER.debug(url);
        GetMethod get = new GetMethod(url);
        try {
            client.executeMethod(get);
            JSONParser jp = new JSONParser();
            JSONArray ja = (JSONArray) jp.parse(get.getResponseBodyAsString());
            //extract endemic matches from the species list
            String speciesList = speciesList();
            StringBuilder sb = new StringBuilder();
            int idx = speciesList.indexOf('\n');
            if (idx > 0) {
                sb.append(speciesList.substring(0, idx));
            }
            for (int j = 0; j < ja.size(); j++) {
                JSONObject jo = (JSONObject) ja.get(j);
                if (jo.containsKey("label")) {
                    idx = speciesList.indexOf("\n" + jo.get("label") + ",");
                    if (idx > 0) {
                        int lineEnd = speciesList.indexOf('\n', idx + 1);
                        if (lineEnd < 0)
                            lineEnd = speciesList.length();
                        sb.append(speciesList.substring(idx, lineEnd));
                    }
                }
            }
            endemicSpeciesList = sb.toString();
        } catch (Exception e) {
            LOGGER.error("error getting endemic species result", e);
        }
    }
    return endemicSpeciesList;
}
Also used : CSVReader(au.com.bytecode.opencsv.CSVReader) JSONArray(org.json.simple.JSONArray) 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 94 with HttpClient

use of org.apache.commons.httpclient.HttpClient in project spatial-portal by AtlasOfLivingAustralia.

the class RemoteLogger method getLogCSV.

public JSONObject getLogCSV() {
    init();
    try {
        if (Util.isLoggedIn()) {
            String url = loggerService + "/app/types/tool.json?" + "email=" + URLEncoder.encode(Util.getUserEmail(), StringConstants.UTF_8) + "&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 95 with HttpClient

use of org.apache.commons.httpclient.HttpClient 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)

Aggregations

HttpClient (org.apache.commons.httpclient.HttpClient)399 GetMethod (org.apache.commons.httpclient.methods.GetMethod)221 PostMethod (org.apache.commons.httpclient.methods.PostMethod)120 HttpMethod (org.apache.commons.httpclient.HttpMethod)98 Test (org.junit.Test)87 IOException (java.io.IOException)82 InputStream (java.io.InputStream)70 HttpException (org.apache.commons.httpclient.HttpException)44 JSONParser (org.json.simple.parser.JSONParser)44 JSONObject (org.json.simple.JSONObject)40 Map (java.util.Map)35 ArrayList (java.util.ArrayList)27 HashMap (java.util.HashMap)25 HttpState (org.apache.commons.httpclient.HttpState)25 ZMailbox (com.zimbra.client.ZMailbox)23 Account (com.zimbra.cs.account.Account)22 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)22 ServiceException (com.zimbra.common.service.ServiceException)21 JSONArray (org.json.simple.JSONArray)21 ByteArrayRequestEntity (org.apache.commons.httpclient.methods.ByteArrayRequestEntity)20