Search in sources :

Example 41 with PostMethod

use of org.apache.commons.httpclient.methods.PostMethod 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 42 with PostMethod

use of org.apache.commons.httpclient.methods.PostMethod 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 43 with PostMethod

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

the class Util method getDistributionsOrChecklists.

/**
     * Generates data for rendering of distributions table.
     *
     * @param type
     * @param wkt
     * @param lsids
     * @param geomIdx
     * @return
     */
public static String[] getDistributionsOrChecklists(String type, String wkt, String lsids, String geomIdx) {
    try {
        StringBuilder sbProcessUrl = new StringBuilder();
        sbProcessUrl.append("/").append(type);
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(CommonData.getLayersServer() + sbProcessUrl.toString());
        LOGGER.debug(CommonData.getLayersServer() + sbProcessUrl.toString());
        if (wkt != null) {
            post.addParameter(StringConstants.WKT, wkt);
        }
        if (lsids != null) {
            post.addParameter(StringConstants.LSIDS, lsids);
        }
        if (geomIdx != null) {
            post.addParameter(StringConstants.GEOM_IDX, geomIdx);
        }
        post.addRequestHeader(StringConstants.ACCEPT, StringConstants.JSON_JAVASCRIPT_ALL);
        int result = client.executeMethod(post);
        if (result == 200) {
            String txt = post.getResponseBodyAsString();
            JSONParser jp = new JSONParser();
            JSONArray ja = (JSONArray) jp.parse(txt);
            if (ja == null || ja.isEmpty()) {
                return new String[0];
            } else {
                String[] lines = new String[ja.size() + 1];
                lines[0] = "SPCODE,SCIENTIFIC_NAME,AUTHORITY_FULL,COMMON_NAME,FAMILY,GENUS_NAME,SPECIFIC_NAME,MIN_DEPTH,MAX_DEPTH,METADATA_URL,LSID,AREA_NAME,AREA_SQ_KM";
                for (int i = 0; i < ja.size(); i++) {
                    JSONObject jo = (JSONObject) ja.get(i);
                    String spcode = jo.containsKey(StringConstants.SPCODE) ? jo.get(StringConstants.SPCODE).toString() : "";
                    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 dataResourceUid = jo.containsKey(StringConstants.DATA_RESOURCE_UID) ? jo.get(StringConstants.DATA_RESOURCE_UID).toString() : "";
                    lines[i + 1] = spcode + "," + wrap(scientific) + "," + wrap(auth) + "," + wrap(common) + "," + wrap(family) + "," + wrap(genus) + "," + wrap(name) + "," + min + "," + max + "," + wrap(md) + "," + wrap(lsid) + "," + wrap(areaName) + "," + wrap(areaKm) + "," + wrap(dataResourceUid);
                }
                return lines;
            }
        }
    } catch (Exception e) {
        LOGGER.error("error building distribution or checklist csv", e);
    }
    return new String[0];
}
Also used : JSONObject(org.json.simple.JSONObject) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) ParseException(com.vividsolutions.jts.io.ParseException)

Example 44 with PostMethod

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

the class ScatterplotListComposer method onFinish.

@Override
public boolean onFinish() {
    LOGGER.debug("Area: " + getSelectedArea());
    LOGGER.debug("Species: " + getSelectedSpecies());
    Query lsid = getSelectedSpecies();
    if (lsid == null) {
        getMapComposer().showMessage("There was a problem selecting the species.  Try to select the species again", this);
        return false;
    }
    lsid = lsid.newFacet(new Facet("occurrence_status_s", "absent", false), false);
    SelectedArea filterSa = getSelectedArea();
    SelectedArea highlightSa = getSelectedAreaHighlight();
    Query lsidQuery = QueryUtil.queryFromSelectedArea(lsid, filterSa, false, getGeospatialKosher());
    if (lsidQuery == null || lsidQuery.getOccurrenceCount() == 0) {
        getMapComposer().showMessage("No occurrences found for the selected species in the selected area.");
        return false;
    }
    String pid = "";
    Query backgroundLsid = getSelectedSpeciesBk();
    backgroundLsid = backgroundLsid.newFacet(new Facet("occurrence_status_s", "absent", false), false);
    if (bgSearchSpeciesACComp.hasValidAnnotatedItemSelected()) {
        backgroundLsid = bgSearchSpeciesACComp.getQuery((Map) getMapComposer().getSession().getAttribute(StringConstants.USERPOINTS), false, getGeospatialKosher());
    }
    boolean envGrid = chkShowEnvIntersection.isChecked();
    Query backgroundLsidQuery = null;
    if (backgroundLsid != null) {
        backgroundLsidQuery = QueryUtil.queryFromSelectedArea(backgroundLsid, filterSa, false, getGeospatialKosherBk());
    }
    String sbenvsel = getSelectedLayersWithDisplayNames();
    String[] layers = sbenvsel.split(":");
    if (layers.length > 20) {
        getMapComposer().showMessage(sbenvsel.split(":").length + " layers selected.  Please select fewer than 20 environmental layers in step 1.");
        return false;
    }
    StringBuilder layernames = new StringBuilder();
    String layerunits = "";
    for (int i = 0; i < layers.length; i++) {
        String[] split = layers[i].split("\\|");
        if (layernames.length() > 0) {
            layernames.append(",");
            layerunits += ",";
        }
        layers[i] = split[0];
        String layerDisplayName = split[1];
        layernames.append("\"").append(layerDisplayName.replace("\"", "\"\"").replace("\\", "\\\\")).append("\"");
        String units = "";
        try {
            units = String.valueOf(((JSONObject) CommonData.getLayer(layers[i]).get("layer")).get("environmentalvalueunits"));
        } catch (Exception e) {
        }
        layerunits += units;
    }
    try {
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(CommonData.getSatServer() + "/ws/scatterplotlist");
        //add data parameters
        post.addParameter("layers", getSelectedLayers());
        post.addParameter("layernames", layernames.toString());
        post.addParameter("layerunits", layerunits);
        post.addParameter("foregroundOccurrencesQs", lsidQuery.getQ());
        post.addParameter("foregroundOccurrencesBs", lsidQuery.getBS());
        post.addParameter("foregroundName", lsidQuery.getName());
        if (backgroundLsidQuery != null) {
            post.addParameter("backgroundOccurrencesQs", backgroundLsidQuery.getQ());
            post.addParameter("backgroundOccurrencesBs", backgroundLsidQuery.getBS());
            post.addParameter("backgroundName", backgroundLsidQuery.getName());
        }
        if (envGrid) {
            post.addParameter("gridDivisions", "20");
        }
        if (filterSa != null) {
            post.addParameter("filterWkt", filterSa.getWkt());
        }
        //add style parameters (highlight area)
        if (highlightSa != null) {
            post.addParameter(StringConstants.HIGHLIGHT_WKT, highlightSa.getWkt());
        }
        post.addRequestHeader(StringConstants.ACCEPT, StringConstants.APPLICATION_JSON);
        client.executeMethod(post);
        String hasId = post.getResponseBodyAsString();
        JSONParser jp = new JSONParser();
        JSONObject jo = (JSONObject) jp.parse(hasId);
        String htmlUrl = null;
        String downloadUrl = null;
        if (jo.containsKey(StringConstants.ID)) {
            pid = jo.get(StringConstants.ID).toString();
        }
        if (jo.containsKey("htmlUrl")) {
            htmlUrl = jo.get("htmlUrl").toString();
        }
        if (jo.containsKey("downloadUrl")) {
            downloadUrl = jo.get("downloadUrl").toString();
        }
        if (htmlUrl != null && downloadUrl != null) {
            Events.echoEvent(StringConstants.OPEN_URL, getMapComposer(), htmlUrl);
            try {
                Filedownload.save(new URL(downloadUrl).openStream(), "application/zip", tToolName.getValue().replaceAll(" ", "_") + ".zip");
            } catch (Exception e) {
                LOGGER.error("error preparing download for scatterplot data", e);
            }
            try {
                String extras = "";
                if (highlightSa != null) {
                    extras += "highlight=" + highlightSa.getWkt();
                }
                if (backgroundLsid instanceof BiocacheQuery) {
                    extras += "background=" + ((BiocacheQuery) backgroundLsid).getLsids();
                } else if (backgroundLsid instanceof UserDataQuery) {
                    extras += "background=" + backgroundLsid.getQ();
                } else {
                    extras += "background=none";
                }
                if (lsidQuery instanceof BiocacheQuery) {
                    BiocacheQuery bq = (BiocacheQuery) lsidQuery;
                    extras = bq.getWS() + "|" + bq.getBS() + "|" + bq.getFullQ(false) + "|" + extras;
                    remoteLogger.logMapAnalysis(tToolName.getValue(), "Tool - Scatterplot List", filterSa.getWkt(), bq.getLsids(), sbenvsel, pid, extras, StringConstants.SUCCESSFUL);
                } else if (lsidQuery instanceof UserDataQuery) {
                    remoteLogger.logMapAnalysis(tToolName.getValue(), "Tool - Scatterplot List", filterSa.getWkt(), lsidQuery.getQ(), sbenvsel, pid, extras, StringConstants.SUCCESSFUL);
                } else {
                    remoteLogger.logMapAnalysis(tToolName.getValue(), "Tool - Scatterplot List", filterSa.getWkt(), "", sbenvsel, pid, extras, StringConstants.SUCCESSFUL);
                }
            } catch (Exception e) {
                LOGGER.error("failed to produce a scatterplot list.", e);
            }
        } else {
            LOGGER.error("failed to produce a scatterplot list.  response: " + jo);
        }
    } catch (Exception e) {
        LOGGER.error("error getting a new scatterplot id", e);
    }
    this.detach();
    return true;
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) SelectedArea(au.org.emii.portal.menu.SelectedArea) URL(java.net.URL) JSONObject(org.json.simple.JSONObject) HttpClient(org.apache.commons.httpclient.HttpClient) JSONParser(org.json.simple.parser.JSONParser) Map(java.util.Map) Facet(au.org.ala.legend.Facet)

Example 45 with PostMethod

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

the class ToolComposer method processAdhoc.

JSONObject processAdhoc(String scientificName) {
    String url = CommonData.getBiocacheServer() + "/process/adhoc";
    try {
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(url);
        StringRequestEntity sre = new StringRequestEntity("{ \"scientificName\": \"" + scientificName.replace("\"", "'") + "\" } ", StringConstants.APPLICATION_JSON, StringConstants.UTF_8);
        post.setRequestEntity(sre);
        int result = client.executeMethod(post);
        if (result == 200) {
            JSONParser jp = new JSONParser();
            return (JSONObject) jp.parse(post.getResponseBodyAsString());
        }
    } catch (Exception e) {
        LOGGER.error("error processing species request: " + url + ", scientificName=" + scientificName, e);
    }
    return null;
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) JSONObject(org.json.simple.JSONObject) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) JSONParser(org.json.simple.parser.JSONParser) IOException(java.io.IOException)

Aggregations

PostMethod (org.apache.commons.httpclient.methods.PostMethod)203 HttpClient (org.apache.commons.httpclient.HttpClient)114 Test (org.junit.Test)55 IOException (java.io.IOException)32 MultipartRequestEntity (org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity)28 Part (org.apache.commons.httpclient.methods.multipart.Part)25 NameValuePair (org.apache.commons.httpclient.NameValuePair)24 FilePart (org.apache.commons.httpclient.methods.multipart.FilePart)23 Map (java.util.Map)21 GetMethod (org.apache.commons.httpclient.methods.GetMethod)21 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)21 StringPart (org.apache.commons.httpclient.methods.multipart.StringPart)21 HttpMethod (org.apache.commons.httpclient.HttpMethod)17 Header (org.apache.commons.httpclient.Header)16 Note (org.apache.zeppelin.notebook.Note)13 HttpException (org.apache.commons.httpclient.HttpException)12 File (java.io.File)11 InputStream (java.io.InputStream)11 ByteArrayRequestEntity (org.apache.commons.httpclient.methods.ByteArrayRequestEntity)11 JSONParser (org.json.simple.parser.JSONParser)11