Search in sources :

Example 46 with PostMethod

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

the class ScatterplotComposer 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);
    String name = getSelectedSpeciesName();
    String sbenvsel = getSelectedLayersWithDisplayNames();
    String[] layers = sbenvsel.split(":");
    if (layers.length > 2 || layers.length < 2) {
        getMapComposer().showMessage(sbenvsel.split(":").length + " layers selected.  Please select 2 environmental layers in step 4.");
        return false;
    }
    String[] layerNames = new String[2];
    String[] layerValues = new String[2];
    for (int i = 0; i < layers.length; i++) {
        String[] split = layers[i].split("\\|");
        layerValues[i] = split[0];
        layerNames[i] = split[1];
    }
    String pid = "";
    Query backgroundLsid = getSelectedSpeciesBk();
    if (backgroundLsid != null)
        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());
    }
    SelectedArea filterSa = getSelectedArea();
    if (filterSa == null) {
        LOGGER.error("scatterplot area is null");
        return false;
    }
    SelectedArea highlightSa = getSelectedAreaHighlight();
    boolean envGrid = chkShowEnvIntersection.isChecked();
    Query lsidQuery = QueryUtil.queryFromSelectedArea(lsid, filterSa, false, getGeospatialKosher());
    Query backgroundLsidQuery = null;
    if (backgroundLsid != null) {
        backgroundLsidQuery = QueryUtil.queryFromSelectedArea(backgroundLsid, filterSa, false, getGeospatialKosherBk());
    }
    ScatterplotDataDTO d = new ScatterplotDataDTO(lsidQuery, name, layerValues[0], layerNames[0], layerValues[1], layerNames[1], pid, null, true, highlightSa);
    try {
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(CommonData.getSatServer() + "/ws/scatterplot/new");
        //add data parameters
        String layerunits = "";
        try {
            String envUnits1 = ((JSONObject) CommonData.getLayer(layerValues[0]).get("layer")).get("environmentalvalueunits").toString();
            String envUnits2 = ((JSONObject) CommonData.getLayer(layerValues[1]).get("layer")).get("environmentalvalueunits").toString();
            layerunits = envUnits1 + "," + envUnits2;
        } catch (Exception e) {
        }
        //colon delimited
        post.addParameter("layers", layerValues[0] + ":" + layerValues[1]);
        //CSV format
        post.addParameter("layernames", "\"" + layerNames[0].replace("\"", "\"\"").replace("\\", "\\\\") + "\",\"" + layerNames[1].replace("\"", "\"\"").replace("\\", "\\\\") + "\"");
        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");
        }
        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 str = post.getResponseBodyAsString();
        JSONParser jp = new JSONParser();
        JSONObject jsonObject = (JSONObject) jp.parse(str);
        if (jsonObject != null && jsonObject.containsKey(StringConstants.ID)) {
            d.setId(jsonObject.get(StringConstants.ID).toString());
            d.setMissingCount(Integer.parseInt(jsonObject.get("missingCount").toString()));
        } else {
            LOGGER.error("error parsing scatterplot id from: " + str);
        }
    } catch (Exception e) {
        LOGGER.error("error getting a new scatterplot id", e);
    }
    getMapComposer().loadScatterplot(d, tToolName.getValue());
    this.detach();
    try {
        String extras = "";
        if (lsidQuery instanceof BiocacheQuery) {
            BiocacheQuery bq = (BiocacheQuery) lsidQuery;
            extras = bq.getWS() + "|" + bq.getBS() + "|" + bq.getFullQ(false) + "|" + extras;
            remoteLogger.logMapAnalysis(tToolName.getValue(), StringConstants.TOOL_SCATTERPLOT, filterSa.getWkt(), bq.getLsids(), layerValues[0] + ":" + layerValues[1], pid, extras, StringConstants.SUCCESSFUL);
        } else if (lsidQuery instanceof UserDataQuery) {
            remoteLogger.logMapAnalysis(tToolName.getValue(), StringConstants.TOOL_SCATTERPLOT, filterSa.getWkt(), lsidQuery.getQ(), "", pid, extras, StringConstants.SUCCESSFUL);
        } else {
            remoteLogger.logMapAnalysis(tToolName.getValue(), StringConstants.TOOL_SCATTERPLOT, filterSa.getWkt(), "", "", pid, extras, StringConstants.SUCCESSFUL);
        }
    } catch (Exception e) {
        LOGGER.error("logging error", e);
    }
    return true;
}
Also used : ScatterplotDataDTO(au.org.ala.spatial.dto.ScatterplotDataDTO) PostMethod(org.apache.commons.httpclient.methods.PostMethod) SelectedArea(au.org.emii.portal.menu.SelectedArea) 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 47 with PostMethod

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

the class MaxentComposer method runmaxent.

public boolean runmaxent() {
    try {
        setupData();
        LOGGER.debug("Selected species: " + query.getName());
        LOGGER.debug("Selected species query: " + query.getQ());
        LOGGER.debug("Selected env vars");
        LOGGER.debug(sbenvsel);
        LOGGER.debug("Selected options: ");
        LOGGER.debug("Jackknife: " + chkJackknife.isChecked());
        LOGGER.debug("Response curves: " + chkRCurves.isChecked());
        LOGGER.debug("Test per: " + txtTestPercentage.getValue());
        StringBuilder sbProcessUrl = new StringBuilder();
        sbProcessUrl.append(CommonData.getSatServer()).append("/ws/maxent?");
        sbProcessUrl.append("taxonid=").append(URLEncoder.encode(query.getName(), StringConstants.UTF_8));
        sbProcessUrl.append("&taxonlsid=").append(URLEncoder.encode(query.getQ(), StringConstants.UTF_8));
        sbProcessUrl.append("&envlist=").append(URLEncoder.encode(sbenvsel, StringConstants.UTF_8));
        sbProcessUrl.append("&speciesq=").append(URLEncoder.encode(QueryUtil.queryFromSelectedArea(query, sa, false, getGeospatialKosher()).getQ(), StringConstants.UTF_8));
        sbProcessUrl.append("&bs=").append(URLEncoder.encode(query.getBS(), StringConstants.UTF_8));
        if (chkJackknife.isChecked()) {
            sbProcessUrl.append("&chkJackknife=on");
        }
        if (chkRCurves.isChecked()) {
            sbProcessUrl.append("&chkResponseCurves=on");
        }
        sbProcessUrl.append("&txtTestPercentage=").append(txtTestPercentage.getValue());
        HttpClient client = new HttpClient();
        PostMethod get = new PostMethod(sbProcessUrl.toString());
        String area;
        if (sa.getMapLayer() != null && sa.getMapLayer().getEnvelope() != null) {
            area = StringConstants.ENVELOPE + "(" + sa.getMapLayer().getEnvelope() + ")";
        } else {
            area = sa.getWkt();
        }
        if (getSelectedArea() != null) {
            get.addParameter(StringConstants.AREA, area);
        }
        LOGGER.debug("Getting species data");
        get.addRequestHeader(StringConstants.ACCEPT, StringConstants.TEXT_PLAIN);
        client.executeMethod(get);
        pid = get.getResponseBodyAsString();
        openProgressBar();
        try {
            String options = "";
            options += "Jackknife: " + chkJackknife.isChecked();
            options += ";Response curves: " + chkRCurves.isChecked();
            options += ";Test per: " + txtTestPercentage.getValue();
            if (query instanceof BiocacheQuery) {
                BiocacheQuery bq = (BiocacheQuery) query;
                options = bq.getWS() + "|" + bq.getBS() + "|" + bq.getFullQ(false) + "|" + options;
                remoteLogger.logMapAnalysis(tToolName.getValue(), "Tool - Prediction", area, bq.getLsids(), sbenvsel, pid, options, StringConstants.STARTED);
            } else {
                remoteLogger.logMapAnalysis(tToolName.getValue(), "Tool - Prediction", area, query.getName() + "__" + query.getQ(), sbenvsel, pid, options, StringConstants.STARTED);
            }
        } catch (Exception e) {
            LOGGER.error("error requesting maxent", e);
        }
        this.setVisible(false);
        return true;
    } catch (Exception e) {
        LOGGER.error("Maxent error: ", e);
        getMapComposer().showMessage("Unknown error.", this);
    }
    return false;
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient)

Example 48 with PostMethod

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

the class GDMComposer method rungdm.

public boolean rungdm() {
    try {
        StringBuilder sbProcessUrl = new StringBuilder();
        sbProcessUrl.append(CommonData.getSatServer()).append("/ws/gdm/step2?");
        sbProcessUrl.append("&pid=").append(pid);
        sbProcessUrl.append("&cutpoint=").append(cutpoint.getSelectedItem().getValue());
        sbProcessUrl.append("&useDistance=").append(rgdistance.getSelectedItem().getValue());
        sbProcessUrl.append("&weighting=").append(weighting.getSelectedItem().getValue());
        sbProcessUrl.append("&useSubSample=").append(useSubSample.isChecked() ? "1" : "0");
        sbProcessUrl.append("&sitePairsSize=").append(sitePairsSize.getValue());
        sbProcessUrl.append("&name=").append(query.getName());
        HttpClient client = new HttpClient();
        PostMethod get = new PostMethod(sbProcessUrl.toString());
        get.addRequestHeader(StringConstants.ACCEPT, StringConstants.TEXT_PLAIN);
        LOGGER.debug("calling gdm ws: " + sbProcessUrl.toString());
        client.executeMethod(get);
        step2Id = get.getResponseBodyAsString();
        getFellow("runningMsg2").setVisible(true);
        statusMsg = ((Label) getFellow("runningMsg2")).getValue();
        startTime = System.currentTimeMillis();
        Events.echoEvent("step2Status", this, null);
        return false;
    } catch (Exception e) {
        LOGGER.error("error finalizing GDM", e);
    }
    return false;
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient)

Example 49 with PostMethod

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

the class GDMComposer method runGDMStep1.

public boolean runGDMStep1() {
    try {
        SelectedArea sa = getSelectedArea();
        query = QueryUtil.queryFromSelectedArea(getSelectedSpecies(), sa, false, getGeospatialKosher());
        query = query.newFacet(new Facet("occurrence_status_s", "absent", false), false);
        sbenvsel = getSelectedLayersWithDisplayNames();
        if (query.getSpeciesCount() < 2) {
            getMapComposer().showMessage("An list of species with multiple occurrences for each species is required by GDM.", this);
            return false;
        }
        HttpClient client = new HttpClient();
        PostMethod get = new PostMethod(CommonData.getSatServer() + "/ws/gdm/step1?" + "envlist=" + URLEncoder.encode(sbenvsel, StringConstants.UTF_8) + "&taxacount=" + query.getSpeciesCount() + "&speciesq=" + URLEncoder.encode(QueryUtil.queryFromSelectedArea(query, sa, false, getGeospatialKosher()).getQ(), StringConstants.UTF_8) + "&bs=" + URLEncoder.encode(query.getBS(), StringConstants.UTF_8));
        if (sa.getMapLayer() != null && sa.getMapLayer().getEnvelope() != null) {
            area = StringConstants.ENVELOPE + "(" + sa.getMapLayer().getEnvelope() + ")";
        } else {
            area = sa.getWkt();
        }
        if (getSelectedArea() != null) {
            get.addParameter(StringConstants.AREA, area);
        }
        get.addRequestHeader(StringConstants.ACCEPT, StringConstants.TEXT_PLAIN);
        LOGGER.debug("calling gdm ws step 1");
        client.executeMethod(get);
        step1Id = get.getResponseBodyAsString();
        //wait for step 1
        LOGGER.debug(step1Id);
        getFellow("runningMsg1").setVisible(true);
        statusMsg = ((Label) getFellow("runningMsg1")).getValue();
        startTime = System.currentTimeMillis();
        Events.echoEvent("step1Status", this, null);
        btnOk.setDisabled(true);
        return true;
    } catch (Exception e) {
        LOGGER.error("GDM error: ", e);
        getMapComposer().showMessage("Unknown error.", this);
    }
    return false;
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) SelectedArea(au.org.emii.portal.menu.SelectedArea) HttpClient(org.apache.commons.httpclient.HttpClient) Facet(au.org.ala.legend.Facet)

Example 50 with PostMethod

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

the class ALOCComposer method getEstimate.

@Override
public long getEstimate() {
    try {
        String sbenvsel = getSelectedLayers();
        if (sbenvsel.split(":").length > 50) {
            getMapComposer().showMessage(sbenvsel.split(":").length + " layers selected.  Please select fewer than 50 environmental layers in step 1.");
            return -1;
        }
        if (groupCount.getValue() <= 1 || groupCount.getValue() > 200) {
            getMapComposer().showMessage("Please enter the number of groups to generate (2 to 200) in step 2.");
            //highlight step 2
            return -1;
        }
        SelectedArea sa = getSelectedArea();
        //estimate analysis size in bytes
        double[][] bbox = null;
        List<Double> bb;
        if (sa.getMapLayer() != null) {
            bb = sa.getMapLayer().getMapLayerMetadata().getBbox();
        } else {
            bb = Util.getBoundingBox(sa.getWkt());
        }
        bbox = new double[][] { { bb.get(0), bb.get(1) }, { bb.get(2), bb.get(3) } };
        long cellsInBBox = (long) ((bbox[1][0] - bbox[0][0]) / 0.01 * (bbox[1][1] - bbox[0][1]) / 0.01);
        long size = (groupCount.getValue() + sbenvsel.split(":").length + 2) * cellsInBBox * 4;
        LOGGER.debug("ALOC estimate size in MB, cells=" + cellsInBBox + ", bbox=" + bbox[0][0] + "," + bbox[0][1] + "," + bbox[1][0] + "," + bbox[1][1] + ", groups=" + groupCount.getValue() + ", layers=" + sbenvsel.split(":").length + ", size=" + size / 1024 / 1024 + ", max size=" + CommonData.getSettings().getProperty("aloc_size_limit_in_mb"));
        if (size / 1024 / 1024 > Integer.parseInt(CommonData.getSettings().getProperty("aloc_size_limit_in_mb"))) {
            getMapComposer().showMessage("Analysis is too large.  Reduce the number of groups, number of layers or area.", this);
            return -1;
        }
        HttpClient client = new HttpClient();
        PostMethod get = new PostMethod((CommonData.getSatServer() + "/ws/aloc/estimate?") + "gc=" + URLEncoder.encode(String.valueOf(groupCount.getValue()), StringConstants.UTF_8) + "&envlist=" + URLEncoder.encode(sbenvsel, StringConstants.UTF_8));
        String area;
        if (sa.getMapLayer() != null && sa.getMapLayer().getEnvelope() != null) {
            area = StringConstants.ENVELOPE + "(" + sa.getMapLayer().getEnvelope() + ")";
        } else {
            area = sa.getWkt();
        }
        get.addParameter(StringConstants.AREA, area);
        get.addRequestHeader(StringConstants.ACCEPT, StringConstants.TEXT_PLAIN);
        client.executeMethod(get);
        String estimate = get.getResponseBodyAsString();
        return Long.valueOf(estimate);
    } catch (Exception e) {
        LOGGER.error("Unable to get estimates", e);
    }
    return -1;
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) SelectedArea(au.org.emii.portal.menu.SelectedArea) HttpClient(org.apache.commons.httpclient.HttpClient)

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