Search in sources :

Example 61 with HttpClient

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

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

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

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

the class Util method readUrlPost.

public static String readUrlPost(String url, NameValuePair[] params) {
    try {
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(url);
        post.setRequestBody(params);
        int result = client.executeMethod(post);
        // Get the response
        if (result == 200) {
            return post.getResponseBodyAsString();
        }
    } catch (Exception e) {
        LOGGER.error("failed to read url: " + url, e);
    }
    return null;
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) ParseException(com.vividsolutions.jts.io.ParseException)

Example 65 with HttpClient

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

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