Search in sources :

Example 71 with HttpClient

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

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

Example 73 with HttpClient

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

the class TabulationComposer method afterCompose.

@Override
public void afterCompose() {
    super.afterCompose();
    this.selectedMethod = StringConstants.TABULATION;
    this.totalSteps = 1;
    this.updateWindowTitle();
    cbTabLayers1.setFocus(true);
    tabLayers = new HashMap<FieldDTO, List<FieldDTO>>();
    tabLayerDisplayNames = new HashMap<String, List<String>>();
    try {
        int i;
        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod(CommonData.getLayersServer() + "/tabulations.json");
        get.addRequestHeader(StringConstants.ACCEPT, StringConstants.APPLICATION_JSON);
        client.executeMethod(get);
        String tlayers = get.getResponseBodyAsString();
        JSONParser jp = new JSONParser();
        JSONObject joTop = (JSONObject) jp.parse(tlayers);
        JSONArray joarr = (JSONArray) joTop.get("tabulations");
        for (i = 0; i < joarr.size(); i++) {
            JSONObject jo = (JSONObject) joarr.get(i);
            FieldDTO f1 = new FieldDTO(jo.get("fid1").toString(), jo.get("name1").toString(), "");
            FieldDTO f2 = new FieldDTO(jo.get("fid2").toString(), jo.get("name2").toString(), "");
            load(f1, f2);
            load(f2, f1);
        }
        Set keySet = (Set) tabLayerDisplayNames.keySet();
        List keyList = new ArrayList(keySet);
        Collections.sort(keyList);
        LOGGER.debug("keyList1=" + keyList);
        for (int j = 0; j < keyList.size(); j++) {
            String temp = (String) keyList.get(j);
            LOGGER.debug("temp=" + temp);
            Comboitem ci = new Comboitem(temp);
            ci.setValue(temp);
            ci.setParent(cbTabLayers1);
        }
        cbTabLayers1.addEventListener("onChange", new EventListener() {

            public void onEvent(Event event) throws Exception {
                onChange$cbTabLayer1(event);
            }
        });
        cbTabLayers1.setSelectedIndex(0);
        onChange$cbTabLayer1(null);
        cbTabLayers2.setSelectedIndex(0);
        cbTabType.setSelectedIndex(0);
    } catch (Exception e) {
        LOGGER.debug("Unable to call tabulation service for a list of layers", e);
    }
}
Also used : JSONArray(org.json.simple.JSONArray) JSONObject(org.json.simple.JSONObject) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Event(org.zkoss.zk.ui.event.Event) JSONParser(org.json.simple.parser.JSONParser) Comboitem(org.zkoss.zul.Comboitem) EventListener(org.zkoss.zk.ui.event.EventListener) FieldDTO(au.org.ala.spatial.dto.FieldDTO)

Example 74 with HttpClient

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

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

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