Search in sources :

Example 96 with GetMethod

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

Example 97 with GetMethod

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

the class SpeciesListUtil method getLists.

private static JSONObject getLists(String user, Integer offset, Integer max, String sort, String order, String searchTerm) {
    StringBuilder sb = new StringBuilder(CommonData.getSpeciesListServer());
    sb.append("/ws/speciesList");
    sb.append("?user=");
    if (user != null && !"guest@ala.org.au".equals(user)) {
        sb.append(user);
    }
    if (offset != null) {
        sb.append("&offset=").append(offset.toString());
    }
    if (max != null) {
        sb.append("&max=").append(max);
    }
    if (sort != null) {
        sb.append("&sort=").append(sort);
    }
    if (order != null) {
        sb.append("&order=").append(order);
    }
    if (searchTerm != null) {
        //sb.append("&listName=ilike:%25" + searchTerm + "%25");
        try {
            sb.append("&q=" + URLEncoder.encode(searchTerm, "UTF-8"));
        } catch (Exception e) {
        }
    }
    HttpClient client = new HttpClient();
    GetMethod get = new GetMethod(sb.toString());
    try {
        //permission to get private lists
        if (user != null) {
            get.addRequestHeader(StringConstants.COOKIE, "ALA-Auth=" + java.net.URLEncoder.encode(user, StringConstants.UTF_8));
        }
        get.addRequestHeader(StringConstants.CONTENT_TYPE, StringConstants.TEXT_PLAIN);
        int result = client.executeMethod(get);
        if (result == 200) {
            String rawJSON = get.getResponseBodyAsString();
            JSONParser jp = new JSONParser();
            return (JSONObject) jp.parse(rawJSON);
        } else {
            LOGGER.error("Unable to retrieve species list. " + result + " > " + get.getResponseBodyAsString());
        }
    } catch (Exception e) {
        LOGGER.error("Error retrieving public species list.", e);
    } finally {
        get.releaseConnection();
    }
    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 98 with GetMethod

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

the class LsidCountsDynamic method count.

private static int count(String lsid) {
    HttpClient client = new HttpClient();
    String url = null;
    try {
        url = CommonData.getBiocacheServer() + "/occurrences/search?facet=off&pageSize=0&fq=" + URLEncoder.encode("geospatial_kosher:*", StringConstants.UTF_8) + "&q=" + URLEncoder.encode("lsid:" + lsid, StringConstants.UTF_8) + CommonData.getBiocacheQc();
        LOGGER.debug(url);
        GetMethod get = new GetMethod(url);
        client.getHttpConnectionManager().getParams().setSoTimeout(30000);
        client.executeMethod(get);
        JSONParser jp = new JSONParser();
        JSONObject jo = (JSONObject) jp.parse(get.getResponseBodyAsString());
        return ((Long) jo.get("totalRecords")).intValue();
    } catch (Exception e) {
        LOGGER.error("error getting LSID count for : " + url, e);
    }
    return -1;
}
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 99 with GetMethod

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

the class LsidCountsDynamic method allSpecies.

private static void allSpecies() {
    Thread t = new Thread() {

        @Override
        public void run() {
            super.run();
            HttpClient client = new HttpClient();
            //get counts at genus, species, subspecies
            String url = null;
            try {
                String[] lsidLevels = new String[] { "genus_guid", "species_guid", "subspecies_guid" };
                for (String lsidLevel : lsidLevels) {
                    url = CommonData.getBiocacheServer() + "/occurrences/search?facet=on&facets=" + lsidLevel + "&pageSize=0&flimit=1000000&q=" + URLEncoder.encode("geospatial_kosher:*", StringConstants.UTF_8) + CommonData.getBiocacheQc();
                    LOGGER.debug(url);
                    GetMethod get = new GetMethod(url);
                    client.executeMethod(get);
                    JSONParser jp = new JSONParser();
                    org.json.simple.JSONObject jo = (org.json.simple.JSONObject) jp.parse(get.getResponseBodyAsString());
                    org.json.simple.JSONArray ja = (org.json.simple.JSONArray) ((org.json.simple.JSONObject) ((org.json.simple.JSONArray) jo.get("facetResults")).get(0)).get("fieldResult");
                    for (int i = 0; i < ja.size(); i++) {
                        String lsid = ((org.json.simple.JSONObject) ja.get(i)).get("label").toString();
                        int count = Integer.parseInt(((org.json.simple.JSONObject) ja.get(i)).get("count").toString());
                        counts.put(lsid, count);
                    }
                }
            } catch (Exception e) {
                LOGGER.error("error getting LSID count for : " + url, e);
            }
            //fill in zeros
            try {
                String[] lsidLevels = new String[] { "genus_guid", "species_guid", "subspecies_guid" };
                for (String lsidLevel : lsidLevels) {
                    url = CommonData.getBiocacheServer() + "/occurrences/search?facet=on&facets=" + lsidLevel + "&pageSize=0&flimit=1000000&q=*:*" + CommonData.getBiocacheQc();
                    LOGGER.debug(url);
                    GetMethod get = new GetMethod(url);
                    client.executeMethod(get);
                    JSONParser jp = new JSONParser();
                    org.json.simple.JSONObject jo = (org.json.simple.JSONObject) jp.parse(get.getResponseBodyAsString());
                    org.json.simple.JSONArray ja = (org.json.simple.JSONArray) ((org.json.simple.JSONObject) ((org.json.simple.JSONArray) jo.get("facetResults")).get(0)).get("fieldResult");
                    for (int i = 0; i < ja.size(); i++) {
                        String lsid = ((org.json.simple.JSONObject) ja.get(i)).get("label").toString();
                        if (!counts.containsKey(lsid)) {
                            counts.put(lsid, 0);
                        }
                    }
                }
            } catch (Exception e) {
                LOGGER.error("error getting LSID count for : " + url, e);
            }
        }
    };
    t.start();
}
Also used : JSONObject(org.json.simple.JSONObject) 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 100 with GetMethod

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

the class Util method getDistributionOrChecklist.

public static String[] getDistributionOrChecklist(String spcode) {
    try {
        StringBuilder sbProcessUrl = new StringBuilder();
        sbProcessUrl.append("/distribution/").append(spcode).append("?nowkt=true");
        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod(CommonData.getLayersServer() + sbProcessUrl.toString());
        LOGGER.debug(CommonData.getLayersServer() + sbProcessUrl.toString());
        get.addRequestHeader(StringConstants.ACCEPT, StringConstants.JSON_JAVASCRIPT_ALL);
        int result = client.executeMethod(get);
        if (result == 200) {
            String txt = get.getResponseBodyAsString();
            JSONParser jp = new JSONParser();
            JSONObject jo = (JSONObject) jp.parse(txt);
            if (jo == null) {
                return new String[0];
            } else {
                String[] output = new String[14];
                String scientific = jo.containsKey(StringConstants.SCIENTIFIC) ? jo.get(StringConstants.SCIENTIFIC).toString() : "";
                String auth = jo.containsKey(StringConstants.AUTHORITY) ? jo.get(StringConstants.AUTHORITY).toString() : "";
                String common = jo.containsKey(StringConstants.COMMON_NAM) ? jo.get(StringConstants.COMMON_NAM).toString() : "";
                String family = jo.containsKey(StringConstants.FAMILY) ? jo.get(StringConstants.FAMILY).toString() : "";
                String genus = jo.containsKey(StringConstants.GENUS) ? jo.get(StringConstants.GENUS).toString() : "";
                String name = jo.containsKey(StringConstants.SPECIFIC_N) ? jo.get(StringConstants.SPECIFIC_N).toString() : "";
                String min = jo.containsKey(StringConstants.MIN_DEPTH) ? jo.get(StringConstants.MIN_DEPTH).toString() : "";
                String max = jo.containsKey(StringConstants.MAX_DEPTH) ? jo.get(StringConstants.MAX_DEPTH).toString() : "";
                String md = jo.containsKey(StringConstants.METADATA_U) ? jo.get(StringConstants.METADATA_U).toString() : "";
                String lsid = jo.containsKey(StringConstants.LSID) ? jo.get(StringConstants.LSID).toString() : "";
                String areaName = jo.containsKey(StringConstants.AREA_NAME) ? jo.get(StringConstants.AREA_NAME).toString() : "";
                String areaKm = jo.containsKey(StringConstants.AREA_KM) ? jo.get(StringConstants.AREA_KM).toString() : "";
                String dataResourceId = jo.containsKey(StringConstants.DATA_RESOURCE_UID) ? jo.get(StringConstants.DATA_RESOURCE_UID).toString() : "";
                output[0] = spcode;
                output[1] = scientific;
                output[2] = auth;
                output[3] = common;
                output[4] = family;
                output[5] = genus;
                output[6] = name;
                output[7] = min;
                output[8] = max;
                output[9] = md;
                output[10] = lsid;
                output[11] = areaName;
                output[12] = areaKm;
                output[13] = dataResourceId;
                return output;
            }
        }
    } catch (Exception e) {
        LOGGER.error("error building distributions list", e);
    }
    return new String[0];
}
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) ParseException(com.vividsolutions.jts.io.ParseException)

Aggregations

GetMethod (org.apache.commons.httpclient.methods.GetMethod)357 HttpClient (org.apache.commons.httpclient.HttpClient)216 HttpMethod (org.apache.commons.httpclient.HttpMethod)93 IOException (java.io.IOException)82 Test (org.junit.Test)70 InputStream (java.io.InputStream)65 HttpException (org.apache.commons.httpclient.HttpException)41 Map (java.util.Map)39 ArrayList (java.util.ArrayList)37 HashMap (java.util.HashMap)36 JSONParser (org.json.simple.parser.JSONParser)34 JSONObject (org.json.simple.JSONObject)31 Header (org.apache.commons.httpclient.Header)22 Element (org.w3c.dom.Element)22 PostMethod (org.apache.commons.httpclient.methods.PostMethod)21 TypeToken (com.google.gson.reflect.TypeToken)20 List (java.util.List)19 HttpState (org.apache.commons.httpclient.HttpState)18 URI (java.net.URI)17 JSONArray (org.json.simple.JSONArray)17