Search in sources :

Example 36 with PostMethod

use of org.apache.commons.httpclient.methods.PostMethod in project intellij-community by JetBrains.

the class LighthouseRepository method doREST.

private HttpMethod doREST(String request, boolean post, HttpClient client) throws Exception {
    String uri = getUrl() + request;
    HttpMethod method = post ? new PostMethod(uri) : new GetMethod(uri);
    configureHttpMethod(method);
    client.executeMethod(method);
    return method;
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpMethod(org.apache.commons.httpclient.HttpMethod)

Example 37 with PostMethod

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

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

the class BiocacheQuery method getGuid.

/**
     * Performs a scientific name or common name lookup and returns the guid if it exists in the BIE
     * <p/>
     * TODO Move getGuid and getClassification to BIE Utilities...
     *
     * @param name
     * @return
     */
public static String getGuid(String name) {
    if ("true".equalsIgnoreCase(CommonData.getSettings().getProperty("new.bie"))) {
        String url = CommonData.getBieServer() + "/ws/species/lookup/bulk";
        try {
            HttpClient client = new HttpClient();
            PostMethod get = new PostMethod(url);
            get.addRequestHeader(StringConstants.CONTENT_TYPE, StringConstants.APPLICATION_JSON);
            get.setRequestEntity(new StringRequestEntity("{\"names\":[\"" + name.replace("\"", "\\\"") + "\"]}"));
            client.executeMethod(get);
            String body = get.getResponseBodyAsString();
            JSONParser jp = new JSONParser();
            JSONArray ja = (JSONArray) jp.parse(body);
            if (ja != null && !ja.isEmpty()) {
                JSONObject jo = (JSONObject) ja.get(0);
                if (jo != null && jo.containsKey("acceptedIdentifier") && jo.get("acceptedIdentifier") != null) {
                    return jo.get("acceptedIdentifier").toString();
                } else if (jo != null && jo.containsKey("acceptedIdentifierGuid") && jo.get("acceptedIdentifierGuid") != null) {
                    return jo.get("acceptedIdentifierGuid").toString();
                } else if (jo != null && jo.containsKey("acceptedConceptID") && jo.get("acceptedConceptID") != null) {
                    return jo.get("acceptedConceptID").toString();
                } else if (jo != null && jo.containsKey("guid") && jo.get("guid") != null) {
                    return jo.get("guid").toString();
                } else {
                    return null;
                }
            } else {
                return null;
            }
        } catch (Exception e) {
            LOGGER.error("error getting guid at: " + url, e);
            return null;
        }
    } else {
        String url = CommonData.getBieServer() + "/ws/guid/" + name.replaceAll(" ", "%20");
        try {
            HttpClient client = new HttpClient();
            GetMethod get = new GetMethod(url);
            get.addRequestHeader(StringConstants.CONTENT_TYPE, StringConstants.APPLICATION_JSON);
            client.executeMethod(get);
            String body = get.getResponseBodyAsString();
            JSONParser jp = new JSONParser();
            JSONArray ja = (JSONArray) jp.parse(body);
            if (ja != null && !ja.isEmpty()) {
                JSONObject jo = (JSONObject) ja.get(0);
                if (jo != null && jo.containsKey("acceptedIdentifier") && jo.get("acceptedIdentifier") != null) {
                    return jo.get("acceptedIdentifier").toString();
                } else {
                    return null;
                }
            } else {
                return null;
            }
        } catch (Exception e) {
            LOGGER.error("error getting guid at: " + url, 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) JSONArray(org.json.simple.JSONArray) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JSONParser(org.json.simple.parser.JSONParser)

Example 39 with PostMethod

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

the class SandboxPasteController method upload.

private boolean upload(byte[] bytes, String filename, String contentType) throws Exception {
    //create tmp file
    File tmp = File.createTempFile("pointsUpload", "_" + filename);
    FileUtils.writeByteArrayToFile(tmp, bytes);
    String url = CommonData.getSettings().getProperty("sandbox.url") + "upload/uploadFile";
    HttpClient httpClient = new HttpClient();
    PostMethod filePost = new PostMethod(url);
    Part[] parts = { new FilePart("myFile", filename, tmp, contentType, null) };
    filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
    int status = httpClient.executeMethod(filePost);
    if (status == 302) {
        String responseText = filePost.getResponseHeader("Location").getValue();
        uploadId = responseText.substring(responseText.indexOf("preview/") + "preview/".length(), responseText.lastIndexOf('?'));
        uploadFn = responseText.substring(responseText.indexOf('?') + 4);
        System.out.println(responseText);
        return true;
    }
    return false;
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part) HttpClient(org.apache.commons.httpclient.HttpClient) File(java.io.File) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart)

Example 40 with PostMethod

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

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