Search in sources :

Example 91 with GetMethod

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

the class RemoteLogger method getLogItem.

public JSONObject getLogItem(String logId) {
    init();
    try {
        if (Util.isLoggedIn()) {
            String url = loggerService + "/app/view/" + logId + ".json" + "?appid=" + URLEncoder.encode(appid, StringConstants.UTF_8) + "&api_key=" + URLEncoder.encode(CommonData.getSettings().getProperty("api_key"), StringConstants.UTF_8);
            HttpClient client = new HttpClient();
            GetMethod get = new GetMethod(url);
            get.addRequestHeader(StringConstants.ACCEPT, StringConstants.APPLICATION_JSON);
            client.executeMethod(get);
            LOGGER.debug("get: " + url + ", response: " + get.getResponseBodyAsString());
            JSONParser jp = new JSONParser();
            return (JSONObject) jp.parse(get.getResponseBodyAsString());
        }
    } catch (Exception e) {
        LOGGER.error("Error getting logging information from server:", 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 92 with GetMethod

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

the class Sampling method getDownloadUrl.

static String getDownloadUrl(String statusUrl) {
    String downloadUrl = null;
    try {
        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod(statusUrl);
        get.addRequestHeader(StringConstants.CONTENT_TYPE, StringConstants.APPLICATION_JSON);
        client.executeMethod(get);
        JSONParser jp = new JSONParser();
        JSONObject jo = (JSONObject) jp.parse(get.getResponseBodyAsString());
        if ("finished".equals(jo.get(StringConstants.STATUS))) {
            downloadUrl = jo.get("downloadUrl").toString();
        } else if ("cancelled".equals(jo.get(StringConstants.STATUS)) || "error".equals(jo.get(StringConstants.STATUS))) {
            downloadUrl = null;
        } else {
            downloadUrl = "";
        }
    } catch (Exception e) {
        LOGGER.error("error getting response from : " + statusUrl, e);
    }
    return downloadUrl;
}
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 93 with GetMethod

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

the class Sampling method getDownloadData.

static List<String[]> getDownloadData(String downloadUrl, int numberOfPoints) {
    List<String[]> output = new ArrayList<String[]>();
    try {
        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod(downloadUrl);
        get.addRequestHeader(StringConstants.CONTENT_TYPE, "application/zip");
        client.executeMethod(get);
        try {
            ZipInputStream zip = new ZipInputStream(get.getResponseBodyAsStream());
            zip.getNextEntry();
            CSVReader csv = new CSVReader(new InputStreamReader(zip));
            //read first line
            String[] line = csv.readNext();
            //setup
            for (int i = 2; i < line.length; i++) {
                output.add(new String[numberOfPoints]);
            }
            int row = 0;
            while ((line = csv.readNext()) != null && row < numberOfPoints) {
                for (int i = 2; i - 2 < output.size() && i < line.length; i++) {
                    output.get(i - 2)[row] = line[i];
                }
                row = row + 1;
            }
            zip.close();
        } catch (Exception e) {
            LOGGER.error("failed to read zipped stream from: " + downloadUrl, e);
        }
    } catch (Exception e) {
        LOGGER.error("error getting response from url: " + downloadUrl, e);
    }
    return output;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) InputStreamReader(java.io.InputStreamReader) CSVReader(au.com.bytecode.opencsv.CSVReader) HttpClient(org.apache.commons.httpclient.HttpClient) ArrayList(java.util.ArrayList) GetMethod(org.apache.commons.httpclient.methods.GetMethod)

Example 94 with GetMethod

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

the class SpeciesAutoComplete method getResults.

private JSONArray getResults(String nsurl) throws Exception {
    HttpClient client = new HttpClient();
    GetMethod get = new GetMethod(nsurl);
    get.addRequestHeader(StringConstants.CONTENT_TYPE, StringConstants.TEXT_PLAIN);
    client.executeMethod(get);
    String rawJSON = get.getResponseBodyAsString();
    //parse
    JSONParser jp = new JSONParser();
    JSONObject jo = (JSONObject) jp.parse(rawJSON);
    //support search and auto bie webservices
    if (jo.containsKey("searchResults")) {
        return (JSONArray) ((JSONObject) jo.get("searchResults")).get("results");
    } else {
        return (JSONArray) jo.get("autoCompleteList");
    }
}
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)

Example 95 with GetMethod

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

the class ProgressController method onClick$btnCancel.

public void onClick$btnCancel(Event e) {
    try {
        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod((CommonData.getSatServer() + "/ws/jobs/cancel?pid=") + pid);
        get.addRequestHeader(StringConstants.ACCEPT, StringConstants.TEXT_PLAIN);
        client.getHttpConnectionManager().getParams().setSoTimeout(timer.getDelay());
        client.executeMethod(get);
    } catch (Exception ex) {
        LOGGER.error("error getting updated job info pid=" + pid, ex);
    }
    this.detach();
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) SocketTimeoutException(java.net.SocketTimeoutException)

Aggregations

GetMethod (org.apache.commons.httpclient.methods.GetMethod)357 HttpClient (org.apache.commons.httpclient.HttpClient)216 HttpMethod (org.apache.commons.httpclient.HttpMethod)93 IOException (java.io.IOException)82 Test (org.junit.Test)70 InputStream (java.io.InputStream)65 HttpException (org.apache.commons.httpclient.HttpException)41 Map (java.util.Map)39 ArrayList (java.util.ArrayList)37 HashMap (java.util.HashMap)36 JSONParser (org.json.simple.parser.JSONParser)34 JSONObject (org.json.simple.JSONObject)31 Header (org.apache.commons.httpclient.Header)22 Element (org.w3c.dom.Element)22 PostMethod (org.apache.commons.httpclient.methods.PostMethod)21 TypeToken (com.google.gson.reflect.TypeToken)20 List (java.util.List)19 HttpState (org.apache.commons.httpclient.HttpState)18 URI (java.net.URI)17 JSONArray (org.json.simple.JSONArray)17