Example usage for org.apache.commons.httpclient HttpClient executeMethod

List of usage examples for org.apache.commons.httpclient HttpClient executeMethod

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpClient executeMethod.

Prototype

public int executeMethod(HttpMethod paramHttpMethod) throws IOException, HttpException 

Source Link

Usage

From source file:it.geosolutions.mariss.wps.gs.DownloadProcess.java

private static boolean downloadVectorDataFromLocalhost(String url, File outDest, HttpConnectionManager manager)
        throws ProcessException {

    HttpClient client = new HttpClient(manager);
    HttpMethod method = new GetMethod(url);
    OutputStream out = null;/*from w ww  . ja  v  a2s.c  o m*/
    InputStream in = null;
    try {
        client.executeMethod(method);
        out = new FileOutputStream(outDest);
        in = method.getResponseBodyAsStream();
        IOUtils.copy(in, out);
    } catch (Exception e) {
        LOGGER.severe(e.getMessage());
        throw new ProcessException("error in vector data download...");
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                LOGGER.severe(e.getMessage());
            }
        }
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                LOGGER.severe(e.getMessage());
            }
        }
    }
    LOGGER.info("Vector Resource downloaded");
    return true;
}

From source file:com.pieframework.runtime.utils.CampfireNotifier.java

public static boolean notify(String msg, String url, String user, String password) {
    HttpClient client = new HttpClient();
    PostMethod post = new PostMethod(url);

    // stuff the Authorization request header
    byte[] encodedPassword = (user + ":" + password).getBytes();
    BASE64Encoder encoder = new BASE64Encoder();
    post.setRequestHeader("Authorization", "Basic " + encoder.encode(encodedPassword));

    try {/*from www.  j  a va2  s  .c om*/
        RequestEntity entity = new StringRequestEntity("<message><body>" + msg + "</body></message>",
                "application/xml", "UTF-8");
        post.setRequestEntity(entity);
        client.executeMethod(post);
        String responseMsg = "httpStatus: " + post.getStatusCode() + " " + "Content-type: "
                + post.getResponseHeader("Content-Type") + " " + post.getResponseBodyAsString();
        Configuration.log().info(responseMsg);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return true;
}

From source file:net.bioclipse.opentox.api.Dataset.java

@SuppressWarnings("serial")
public static List<String> getListOfAvailableDatasets(String service) throws IOException {
    HttpClient client = new HttpClient();
    GetMethod method = new GetMethod(service + "dataset");
    HttpMethodHelper.addMethodHeaders(method, new HashMap<String, String>() {
        {/*from  www .  j  a va  2s .c  om*/
            put("Accept", "text/uri-list");
        }
    });
    client.executeMethod(method);

    List<String> datasets = new ArrayList<String>();
    BufferedReader reader = new BufferedReader(new StringReader(method.getResponseBodyAsString()));
    String line;
    while ((line = reader.readLine()) != null) {
        line = line.trim();
        if (line.length() > 0)
            datasets.add(line);
    }
    reader.close();
    method.releaseConnection();
    return datasets;
}

From source file:net.bioclipse.opentox.api.Dataset.java

@SuppressWarnings("serial")
public static StringMatrix listPredictedFeatures(String datasetURI) throws Exception {
    logger.debug("Listing features for: " + datasetURI);
    datasetURI = datasetURI.replaceAll("\n", "");
    if (datasetURI.contains("feature_uris[]=")) {
        String baseURI = datasetURI.substring(0, datasetURI.indexOf("feature_uris[]="));
        String featureURIs = datasetURI.substring(datasetURI.indexOf("feature_uris[]=") + 15);
        featureURIs = URIUtil.decode(featureURIs);
        String fullURI = baseURI + "feature_uris[]=" + featureURIs;
        datasetURI = URIUtil.encodeQuery(fullURI);
    }// w w  w  .  j  a v a  2s  . c  o m
    HttpClient client = new HttpClient();
    GetMethod method = new GetMethod(datasetURI);
    HttpMethodHelper.addMethodHeaders(method, new HashMap<String, String>() {
        {
            put("Accept", "application/rdf+xml");
        }
    });
    client.executeMethod(method);
    String result = method.getResponseBodyAsString(); // without this things will fail??
    IRDFStore store = rdf.createInMemoryStore();
    rdf.importFromStream(store, method.getResponseBodyAsStream(), "RDF/XML", null);
    method.releaseConnection();
    String dump = rdf.asRDFN3(store);
    StringMatrix matrix = rdf.sparql(store, QUERY_PREDICTED_FEATURES);
    return matrix;
}

From source file:edu.stanford.epad.epadws.xnat.XNATCreationOperations.java

public static int createXNATProject(String xnatProjectLabel, String projectName, String description,
        String jsessionID) {//from w w w  .j a  va  2s .c o m
    String xnatProjectURL = XNATUtil.buildXNATProjectCreationURL(xnatProjectLabel, projectName, description);
    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod(xnatProjectURL);
    int xnatStatusCode;

    method.setRequestHeader("Cookie", "JSESSIONID=" + jsessionID);

    try {
        log.info("Invoking XNAT with URL " + xnatProjectURL);
        xnatStatusCode = client.executeMethod(method);
        if (XNATUtil.unexpectedXNATCreationStatusCode(xnatStatusCode))
            log.warning("Failure calling XNAT to create project; status code = " + xnatStatusCode);
        else
            eventTracker.recordProjectEvent(jsessionID, projectName);
    } catch (IOException e) {
        log.warning("IO exception calling XNAT to create project", e);
        xnatStatusCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
    } finally {
        method.releaseConnection();
    }
    if (xnatStatusCode == HttpServletResponse.SC_CONFLICT)
        xnatStatusCode = HttpServletResponse.SC_OK;

    return xnatStatusCode;
}

From source file:edu.uci.ics.asterix.test.aql.TestsUtils.java

private static int executeHttpMethod(HttpMethod method) throws Exception {
    HttpClient client = new HttpClient();
    int statusCode;
    try {//from www .  jav a 2  s .c  o m
        statusCode = client.executeMethod(method);
    } catch (Exception e) {
        GlobalConfig.ASTERIX_LOGGER.log(Level.SEVERE, e.getMessage(), e);
        e.printStackTrace();
        throw e;
    }
    if (statusCode != HttpStatus.SC_OK) {
        // QQQ For now, we are indeed assuming we get back JSON errors.
        // In future this may be changed depending on the requested
        // output format sent to the servlet.
        String errorBody = method.getResponseBodyAsString();
        JSONObject result = new JSONObject(errorBody);
        String[] errors = { result.getJSONArray("error-code").getString(0), result.getString("summary"),
                result.getString("stacktrace") };
        GlobalConfig.ASTERIX_LOGGER.log(Level.SEVERE, errors[2]);
        throw new Exception("HTTP operation failed: " + errors[0] + "\nSTATUS LINE: " + method.getStatusLine()
                + "\nSUMMARY: " + errors[1] + "\nSTACKTRACE: " + errors[2]);
    }
    return statusCode;
}

From source file:kevin.gvmsgarch.App.java

static String getInboxPage(String authToken, Worker.ListLocation location, int page) throws IOException {

    HttpClient client = new HttpClient();
    String retval = null;/*from  w w w  .ja v  a 2  s  .com*/

    GetMethod m = new GetMethod(location.getUri());
    if (page > 1) {
        m.setQueryString(new NameValuePair[] { new NameValuePair("page", "p" + page) });
    }
    m.setRequestHeader("Authorization", "GoogleLogin auth=" + authToken);
    int rcode;
    rcode = client.executeMethod(m);
    if (rcode != 200) {
        throw new RuntimeException("Received rcode: " + rcode);
    }
    retval = makeStringFromStream(m.getResponseBodyAsStream());
    return retval;
}

From source file:com.eucalyptus.storage.BlockStorageChecker.java

public static void checkWalrusConnection() {
    HttpClient httpClient = new HttpClient();
    GetMethod getMethod = null;//  www  .  jav  a2 s  .  c  om
    try {
        java.net.URI addrUri = new URL(StorageProperties.WALRUS_URL).toURI();
        String addrPath = addrUri.getPath();
        String addr = StorageProperties.WALRUS_URL.replaceAll(addrPath, "");
        getMethod = new GetMethod(addr);

        httpClient.executeMethod(getMethod);
        StorageProperties.enableSnapshots = true;
    } catch (Exception ex) {
        LOG.error("Could not connect to Walrus. Snapshot functionality disabled. Please check the Walrus url.");
        StorageProperties.enableSnapshots = false;
    } finally {
        if (getMethod != null)
            getMethod.releaseConnection();
    }
}

From source file:edu.stanford.epad.common.plugins.PluginFileUtil.java

public static int sendFileToRemoteEPAD(String username, String epadHost, String epadSessionID, String projectID,
        String subjectID, String studyUID, String seriesUID, File file, String description) throws Exception {
    String url = buildEPADBaseURL(epadHost, EPADConfig.epadPort,
            "/epad/v2/" + getAction(projectID, subjectID, studyUID, seriesUID, username));
    log.info("upload url " + url);
    HttpClient client = new HttpClient();
    PostMethod postMethod = new PostMethod(url);
    if (epadSessionID != null)
        postMethod.setRequestHeader("Cookie", "JSESSIONID=" + epadSessionID);
    try {/*from  w ww  . j  av  a2s  . c  om*/
        Part[] parts = { new FilePart(file.getName(), file), new StringPart("description", description) };

        postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));

        return client.executeMethod(postMethod);
    } catch (Exception e) {
        log.warning("Exception calling ePAD", e);
        return HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
    } finally {
        postMethod.releaseConnection();
    }
}

From source file:ec.edu.ucuenca.dcc.sld.HttpUtils.java

public static synchronized String Http2_(String s, Map<String, String> mp) throws SQLException, IOException {
    String resp = "";
    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod(s);
    method.getParams().setContentCharset("utf-8");
    //Add any parameter if u want to send it with Post req.
    for (Entry<String, String> mcc : mp.entrySet()) {
        method.addParameter(mcc.getKey(), mcc.getValue());
    }// w ww.  j  av a2s .c om
    int statusCode = client.executeMethod(method);
    if (statusCode != -1) {
        InputStream in = method.getResponseBodyAsStream();
        final Scanner reader = new Scanner(in, "UTF-8");
        while (reader.hasNextLine()) {
            final String line = reader.nextLine();
            resp += line + "\n";
        }
        reader.close();
    }
    return resp;
}