Search in sources :

Example 81 with HttpClient

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

the class AreaEnvironmentalEnvelope method onClick$filterDone.

public void onClick$filterDone(Event event) {
    ok = true;
    try {
        //create the layer
        StringBuilder sbProcessUrl = new StringBuilder();
        sbProcessUrl.append(CommonData.getSatServer()).append("/ws/envelope?area=").append(getWkt());
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(sbProcessUrl.toString());
        LOGGER.debug(sbProcessUrl.toString());
        post.addRequestHeader(StringConstants.ACCEPT, StringConstants.TEXT_PLAIN);
        client.executeMethod(post);
        String slist = post.getResponseBodyAsString();
        String[] list = slist.split("\n");
        String pid = list[0];
        String url = CommonData.getGeoServer() + "/wms?service=WMS&version=1.1.0&request=GetMap&layers=ALA:envelope_" + pid + "&FORMAT=image%2Fpng";
        String activeAreaExtent = list[1];
        activeAreaSize = list[2];
        //load the layer
        layerName = txtLayerName.getText();
        MapLayer ml = mc.addWMSLayer(pid, layerName, url, 0.75f, null, null, LayerUtilitiesImpl.ENVIRONMENTAL_ENVELOPE, null, null);
        //add colour!
        int colour = Util.nextColour();
        int r = (colour >> 16) & 0x000000ff;
        int g = (colour >> 8) & 0x000000ff;
        int b = (colour) & 0x000000ff;
        ml.setRedVal(r);
        ml.setGreenVal(g);
        ml.setBlueVal(b);
        ml.setDynamicStyle(true);
        ml.setPolygonLayer(true);
        getMapComposer().applyChange(ml);
        getMapComposer().updateLayerControls();
        //make the metadata?
        StringBuilder sb = new StringBuilder();
        StringBuilder sbLayerList = new StringBuilder();
        sb.append("Environmental Envelope<br>");
        for (int i = 0; i < selectedLayers.size(); i++) {
            JSONObject layer = selectedLayers.get(i);
            SPLFilter f = getSPLFilter(layer);
            sb.append(f.getLayername()).append(": ").append(f.getFilterString()).append("<br>");
            sbLayerList.append(f.getLayername());
            if (i < selectedLayers.size() - 1) {
                sbLayerList.append(":");
            }
        }
        String activeAreaMetadata = LayersUtil.getMetadata(sb.toString());
        getMapComposer().setAttribute("activeLayerName", sbLayerList.toString());
        getMapComposer().setAttribute("mappolygonlayer", sb.toString());
        String finalWkt = null;
        try {
            finalWkt = getWkt();
        } catch (Exception e) {
            LOGGER.error("failed to get WKT", e);
        }
        this.layerName = ml.getName();
        List<Double> bb = new ArrayList<Double>(4);
        String[] bs = activeAreaExtent.split(",");
        for (int i = 0; i < 4; i++) {
            bb.add(Double.parseDouble(bs[i]));
        }
        ml.getMapLayerMetadata().setBbox(bb);
        ml.getMapLayerMetadata().setMoreInfo(activeAreaMetadata);
        ml.setWKT(StringConstants.ENVELOPE + "(" + finalWkt + ")");
        //not the actual WKT
        ml.setEnvelope(finalWkt);
        try {
            double area = Double.parseDouble(activeAreaSize.replace(",", ""));
            activeAreaSize = String.format("%,.2f", area);
        } catch (NumberFormatException e) {
            LOGGER.error("failed to parse environmental envelope area for: " + activeAreaSize);
        }
        ml.setAreaSqKm(activeAreaSize);
        ml.setFacets(getFacets());
        //this also shows active area
        removeAllSelectedLayers(true);
    } catch (Exception e) {
        LOGGER.error("unable to create envelope layer: ", e);
    }
    //do detach after adding the new layer
    mc.updateLayerControls();
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) MapLayer(au.org.emii.portal.menu.MapLayer) ArrayList(java.util.ArrayList) JSONObject(org.json.simple.JSONObject) HttpClient(org.apache.commons.httpclient.HttpClient)

Example 82 with HttpClient

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

the class BiocacheQuery method retrieveCustomFacets.

/**
     * Retrieves a list of custom facets for the supplied query.
     *
     * @return
     */
private List<QueryField> retrieveCustomFacets() {
    List<QueryField> customFacets = new ArrayList<QueryField>();
    //custom fields can only be retrieved with specific query types
    String full = getFullQ(false);
    Matcher match = Pattern.compile("data_resource_uid:\"??dr[t][0-9]+\"??").matcher(full);
    if (!match.find()) {
        return customFacets;
    }
    //look up facets
    try {
        final String jsonUri = biocacheServer + "/upload/dynamicFacets?q=" + URLEncoder.encode(match.group(), "UTF-8");
        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();
            String facetDisplayName = jsonObject.get("displayName").toString();
            //TODO: remove this when _RNG fields work in legend &cm= parameter
            if (!(facetDisplayName.contains("(Range)") && facetName.endsWith("_RNG"))) {
                LOGGER.debug("Adding custom index : " + arrayElement);
                customFacets.add(new QueryField(facetName, facetDisplayName, QueryField.GroupType.CUSTOM, QueryField.FieldType.STRING));
            }
        }
    } catch (Exception e) {
        LOGGER.error("error loading custom facets for: " + getFullQ(false), e);
    }
    return customFacets;
}
Also used : QueryField(au.org.ala.legend.QueryField) JSONObject(org.json.simple.JSONObject) Matcher(java.util.regex.Matcher) 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 83 with HttpClient

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

the class BiocacheQuery method getQidDetails.

private JSONObject getQidDetails(String qidTerm) {
    HttpClient client = new HttpClient();
    String url = biocacheServer + QID_DETAILS + qidTerm.replace("qid:", "");
    GetMethod get = new GetMethod(url);
    try {
        int result = client.executeMethod(get);
        String response = get.getResponseBodyAsString();
        if (result == 200) {
            JSONParser jp = new JSONParser();
            JSONObject jo = (JSONObject) jp.parse(response);
            return jo;
        } else {
            LOGGER.debug("error with url:" + url + " getting qid details for " + qidTerm + " > response_code:" + result + " response:" + response);
        }
    } catch (Exception e) {
        LOGGER.error("error getting biocache param details from " + url, 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 84 with HttpClient

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

the class BiocacheQuery method getLegend.

/**
     * Get legend for a facet field.
     *
     * @param colourmode
     * @return
     */
@Override
public LegendObject getLegend(String colourmode) {
    if ("-1".equals(colourmode) || StringConstants.GRID.equals(colourmode)) {
        return null;
    }
    LegendObject lo = legends.get(colourmode);
    if (lo != null && lo.getColourMode() != null && !lo.getColourMode().equals(colourmode)) {
        LOGGER.debug("lo not empty and lo=" + lo);
        lo = legends.get(lo.getColourMode());
    }
    if (lo == null && getOccurrenceCount() > 0) {
        HttpClient client = new HttpClient();
        String facetToColourBy = StringConstants.OCCURRENCE_YEAR_DECADE.equals(colourmode) ? StringConstants.OCCURRENCE_YEAR : translateFieldForSolr(colourmode);
        try {
            String url = biocacheServer + LEGEND_SERVICE_CSV + DEFAULT_ROWS + "&q=" + getQ() + "&cm=" + URLEncoder.encode(facetToColourBy, StringConstants.UTF_8) + getQc();
            LOGGER.debug(url);
            GetMethod get = new GetMethod(url);
            //NQ: Set the header type to JSON so that we can parse JSON instead of CSV (CSV has issue with quoted field where a quote is the escape character)
            get.addRequestHeader(StringConstants.ACCEPT, StringConstants.APPLICATION_JSON);
            client.executeMethod(get);
            String s = get.getResponseBodyAsString();
            //in the first line do field name replacement
            String t = translateFieldForSolr(colourmode);
            if (!colourmode.equals(t)) {
                s = s.replaceFirst(t, colourmode);
            }
            lo = new BiocacheLegendObject(colourmode, s);
            //test for exceptions
            if (!colourmode.contains(",") && (StringConstants.UNCERTAINTY.equals(colourmode) || StringConstants.DECADE.equals(colourmode) || StringConstants.OCCURRENCE_YEAR.equals(colourmode) || StringConstants.COORDINATE_UNCERTAINTY.equals(colourmode))) {
                lo = ((BiocacheLegendObject) lo).getAsIntegerLegend();
                //apply cutpoints to colourMode string
                Legend l = lo.getNumericLegend();
                if (l == null || l.getCutoffFloats() == null) {
                    return null;
                }
                float[] cutpoints = l.getCutoffFloats();
                float[] cutpointmins = l.getCutoffMinFloats();
                StringBuilder sb = new StringBuilder();
                //NQ 20140109: use the translated SOLR field as the colour mode so that Constants.DECADE does not cause an issue
                String newFacet = StringConstants.DECADE.equals(colourmode) ? StringConstants.OCCURRENCE_YEAR : colourmode;
                sb.append(newFacet);
                int i = 0;
                while (i < cutpoints.length) {
                    if (i == cutpoints.length - 1 || cutpoints[i] != cutpoints[i + 1]) {
                        if (i > 0) {
                            sb.append(",").append(cutpointmins[i]);
                            if (StringConstants.OCCURRENCE_YEAR.equals(colourmode) || StringConstants.DECADE.equals(colourmode)) {
                                sb.append(StringConstants.DATE_TIME_BEGINNING_OF_YEAR);
                            }
                        } else {
                            sb.append(",*");
                        }
                        sb.append(",").append(cutpoints[i]);
                        if (StringConstants.OCCURRENCE_YEAR.equals(colourmode) || StringConstants.DECADE.equals(colourmode)) {
                            sb.append(StringConstants.DATE_TIME_END_OF_YEAR);
                        }
                    } else if (i < cutpoints.length - 1 && cutpoints[i] == cutpoints[i + 1]) {
                        cutpointmins[i + 1] = cutpointmins[i];
                    }
                    i++;
                }
                String newColourMode = sb.toString();
                if (StringConstants.OCCURRENCE_YEAR.equals(colourmode) || StringConstants.DECADE.equals(colourmode)) {
                    newColourMode = newColourMode.replace(".0", "");
                }
                lo.setColourMode(newColourMode);
                legends.put(colourmode, lo);
                LegendObject newlo = getLegend(newColourMode);
                newlo.setColourMode(newColourMode);
                newlo.setNumericLegend(lo.getNumericLegend());
                legends.put(newColourMode, newlo);
                lo = newlo;
            } else if (StringConstants.MONTH.equals(colourmode)) {
                String newColourMode = "month,00,00,01,01,02,02,03,03,04,04,05,05,06,06,07,07,08,08,09,09,10,10,11,11,12,12";
                lo.setColourMode(newColourMode);
                legends.put(colourmode, lo);
                LegendObject newlo = getLegend(newColourMode);
                newlo.setColourMode(newColourMode);
                newlo.setNumericLegend(lo.getNumericLegend());
                legends.put(newColourMode, newlo);
                lo = newlo;
            } else if (!colourmode.contains(",") && (StringConstants.OCCURRENCE_YEAR_DECADE.equals(colourmode) || StringConstants.DECADE.equals(colourmode))) {
                Set<Integer> decades = new TreeSet<Integer>();
                for (double d : ((BiocacheLegendObject) lo).categoriesNumeric.keySet()) {
                    decades.add((int) (d / 10));
                }
                List<Integer> d = new ArrayList<Integer>(decades);
                StringBuilder sb = new StringBuilder();
                sb.append(StringConstants.OCCURRENCE_YEAR);
                for (int i = !d.isEmpty() && d.get(0) > 0 ? 0 : 1; i < d.size(); i++) {
                    if (i > 0) {
                        sb.append(",").append(d.get(i));
                        sb.append("0-01-01T00:00:00Z");
                    } else {
                        sb.append(",*");
                    }
                    sb.append(",").append(d.get(i));
                    sb.append("9-12-31T00:00:00Z");
                }
                String newColourMode = sb.toString();
                lo.setColourMode(newColourMode);
                legends.put(colourmode, lo);
                LegendObject newlo = getLegend(newColourMode);
                newlo.setColourMode(newColourMode);
                newlo.setNumericLegend(lo.getNumericLegend());
                legends.put(newColourMode, newlo);
                lo = newlo;
            } else {
                legends.put(colourmode, lo);
            }
        } catch (Exception e) {
            LOGGER.error("error getting legend for : " + colourmode, e);
        }
    }
    return lo;
}
Also used : Legend(au.org.ala.legend.Legend) LegendObject(au.org.ala.legend.LegendObject) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod)

Example 85 with HttpClient

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

the class BiocacheQuery method getAutoComplete.

/**
     * Returns the "autocomplete" values for the query based on the supplied facet field. Extensible so that we can add autocomplete
     * based on queries in other areas.
     * <p/>
     * NC 20131126 - added to support an autocomplete of raw taxon name for a fix associated with Fungi
     *
     * @param facet The facet to autocomplete on
     * @param value The prefix for the autocomplete
     * @param limit The maximum number of values to return
     * @return
     */
public String getAutoComplete(String facet, String value, int limit) {
    HttpClient client = new HttpClient();
    StringBuilder slist = new StringBuilder();
    if (value.length() >= 3 && StringUtils.isNotBlank(facet)) {
        try {
            String url = biocacheServer + QUERY_TITLE_URL + "q=" + getQ() + getQc() + "&facets=" + facet + "&fprefix=" + URLEncoder.encode(value, StringConstants.UTF_8) + "&pageSize=0&flimit=" + limit;
            GetMethod get = new GetMethod(url);
            get.addRequestHeader(StringConstants.CONTENT_TYPE, StringConstants.TEXT_PLAIN);
            int result = client.executeMethod(get);
            if (result == 200) {
                //success
                String rawJSON = get.getResponseBodyAsString();
                //parse
                JSONParser jp = new JSONParser();
                JSONObject jo = (JSONObject) jp.parse(rawJSON);
                JSONArray ja = (JSONArray) jo.get("facetResults");
                for (int i = 0; i < ja.size(); i++) {
                    JSONObject o = (JSONObject) ja.get(i);
                    if (o.get("fieldName").equals(facet)) {
                        //process the values in the list
                        JSONArray values = (JSONArray) o.get("fieldResult");
                        for (int j = 0; j < values.size(); j++) {
                            JSONObject vo = (JSONObject) values.get(j);
                            if (slist.length() > 0) {
                                slist.append("\n");
                            }
                            slist.append(vo.get("label")).append("//found ").append(vo.get(StringConstants.COUNT).toString());
                        }
                    }
                }
            } else {
                LOGGER.warn("There was an issue performing the autocomplete from the biocache: " + result);
            }
        } catch (Exception e) {
            LOGGER.error("failed to get autocomplete facet=" + facet + ", value=" + value, e);
        }
    }
    return slist.toString();
}
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

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