Search in sources :

Example 96 with HttpClient

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

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

the class AreaReportController method getGazPoints.

public static JSONArray getGazPoints(String wkt) {
    try {
        int limit = Integer.MAX_VALUE;
        String url = CommonData.getLayersServer() + "/objects/inarea/" + CommonData.getSettings().get("area_report_gaz_field") + "?limit=" + limit;
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(url);
        LOGGER.debug(url);
        if (wkt != null) {
            post.addParameter(StringConstants.WKT, wkt);
        }
        post.addRequestHeader(StringConstants.ACCEPT, StringConstants.JSON_JAVASCRIPT_ALL);
        int result = client.executeMethod(post);
        if (result == 200) {
            String txt = post.getResponseBodyAsString();
            JSONParser jp = new JSONParser();
            return (JSONArray) jp.parse(txt);
        } else {
            LOGGER.debug(result + ", " + post.getResponseBodyAsString());
        }
    } catch (Exception e) {
        LOGGER.error("error getting number of gaz points in an area: " + wkt, e);
    }
    return null;
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser)

Example 98 with HttpClient

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

the class MapComposer method loadDistributionMap.

private void loadDistributionMap(String lsids, String wkt) {
    String newWkt = wkt;
    if (CommonData.WORLD_WKT.equals(newWkt)) {
        newWkt = null;
    }
    //test for a valid lsid match
    String[] wmsNames = CommonData.getSpeciesDistributionWMS(lsids);
    String[] spcode = CommonData.getSpeciesDistributionSpcode(lsids);
    MapLayer ml;
    JSONParser jp = new JSONParser();
    if (wmsNames.length > 0 && (newWkt == null || newWkt.equals(CommonData.WORLD_WKT))) {
        //add all
        for (int i = 0; i < wmsNames.length; i++) {
            if (getMapLayerWMS(wmsNames[i]) == null) {
                //map this layer with its recorded scientific name
                try {
                    JSONObject jo = ((JSONObject) jp.parse(Util.readUrl(CommonData.getLayersServer() + "/distribution/" + spcode[i] + "?nowkt=true")));
                    String scientific = jo.get(StringConstants.SCIENTIFIC).toString();
                    String distributionAreaName = jo.get("area_name").toString();
                    String layerName = getNextAreaLayerName(scientific);
                    String html = Util.getMetadataHtmlForDistributionOrChecklist(spcode[i], null, layerName);
                    ml = addWMSLayer(layerName, getNextAreaLayerName(distributionAreaName), wmsNames[i], 0.35f, html, null, LayerUtilitiesImpl.WKT, null, null);
                    ml.setSPCode(spcode[i]);
                    setupMapLayerAsDistributionArea(ml);
                } catch (Exception e) {
                    LOGGER.error("failed to parse for distribution: " + spcode[i]);
                }
            }
        }
    } else if (wmsNames.length > 0 && newWkt != null && !newWkt.equals(CommonData.WORLD_WKT)) {
        String url = CommonData.getLayersServer() + "/distributions";
        try {
            HttpClient client = new HttpClient();
            PostMethod post = new PostMethod(url);
            post.addParameter(StringConstants.WKT, newWkt);
            post.addParameter(StringConstants.LSIDS, lsids);
            post.addRequestHeader(StringConstants.ACCEPT, StringConstants.JSON_JAVASCRIPT_ALL);
            int result = client.executeMethod(post);
            if (result == 200) {
                String txt = post.getResponseBodyAsString();
                JSONArray ja = (JSONArray) jp.parse(txt);
                List<String> found = new ArrayList();
                for (int i = 0; i < ja.size(); i++) {
                    JSONObject jo = (JSONObject) ja.get(i);
                    if (jo.containsKey(StringConstants.WMSURL)) {
                        found.add(jo.get(StringConstants.WMSURL).toString());
                    }
                }
                for (int i = 0; i < wmsNames.length; i++) {
                    if (getMapLayerWMS(wmsNames[i]) == null) {
                        String scientific = ((JSONObject) jp.parse(Util.readUrl(CommonData.getLayersServer() + "/distribution/" + spcode[i] + "?nowkt=true"))).get(StringConstants.SCIENTIFIC).toString();
                        String layerName = getNextAreaLayerName(scientific + " area " + (i + 1));
                        String html = Util.getMetadataHtmlForDistributionOrChecklist(spcode[i], null, layerName);
                        ml = addWMSLayer(layerName, getNextAreaLayerName("Expert distribution: " + scientific), found.get(i), 0.35f, html, null, LayerUtilitiesImpl.WKT, null, null);
                        ml.setSPCode(spcode[i]);
                        setupMapLayerAsDistributionArea(ml);
                    }
                }
            }
        } catch (Exception e) {
            LOGGER.error("error posting distributions: " + url);
        }
    }
    openChecklistSpecies(lsids, newWkt, true);
}
Also used : JSONObject(org.json.simple.JSONObject) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HasMapLayer(au.org.emii.portal.menu.HasMapLayer) MapLayer(au.org.emii.portal.menu.MapLayer) HttpClient(org.apache.commons.httpclient.HttpClient) JSONArray(org.json.simple.JSONArray) XmlArrayList(com.thoughtworks.xstream.persistence.XmlArrayList) JSONParser(org.json.simple.parser.JSONParser) XmlArrayList(com.thoughtworks.xstream.persistence.XmlArrayList) List(java.util.List) ParseException(org.json.simple.parser.ParseException)

Example 99 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 100 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)

Aggregations

HttpClient (org.apache.commons.httpclient.HttpClient)389 GetMethod (org.apache.commons.httpclient.methods.GetMethod)217 PostMethod (org.apache.commons.httpclient.methods.PostMethod)115 HttpMethod (org.apache.commons.httpclient.HttpMethod)98 Test (org.junit.Test)86 IOException (java.io.IOException)79 InputStream (java.io.InputStream)65 JSONParser (org.json.simple.parser.JSONParser)44 HttpException (org.apache.commons.httpclient.HttpException)43 JSONObject (org.json.simple.JSONObject)40 Map (java.util.Map)32 ArrayList (java.util.ArrayList)27 HttpState (org.apache.commons.httpclient.HttpState)25 ZMailbox (com.zimbra.client.ZMailbox)23 Account (com.zimbra.cs.account.Account)22 HashMap (java.util.HashMap)22 ServiceException (com.zimbra.common.service.ServiceException)21 JSONArray (org.json.simple.JSONArray)21 ByteArrayRequestEntity (org.apache.commons.httpclient.methods.ByteArrayRequestEntity)20 Element (org.w3c.dom.Element)20