Example usage for org.apache.commons.httpclient.methods PostMethod setRequestEntity

List of usage examples for org.apache.commons.httpclient.methods PostMethod setRequestEntity

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods PostMethod setRequestEntity.

Prototype

public void setRequestEntity(RequestEntity paramRequestEntity) 

Source Link

Usage

From source file:com.bigdata.rdf.sail.remoting.GraphRepositoryClient.java

/**
 * @see {@link GraphRepository#create(String)}
 *//*from w ww.j  av a2s.  c  o m*/
public void create(String rdfXml) throws Exception {

    // POST
    PostMethod post = new PostMethod(servletURL);
    try {

        // set the body
        if (rdfXml != null) {
            post.setRequestEntity(new StringRequestEntity(rdfXml, // the rdf/xml body
                    GraphRepositoryServlet.RDF_XML, // includes the encoding
                    null // so we don't need to say it here.
            ));
            post.setContentChunked(true);
        }

        // Execute the method.
        int sc = getHttpClient().executeMethod(post);
        if (sc != HttpStatus.SC_OK) {
            throw new IOException("HTTP-POST failed: " + post.getStatusLine());
        }

    } finally {
        // Release the connection.
        post.releaseConnection();
    }

}

From source file:net.morphbank.webclient.ProcessFiles.java

public InputStream post(String strURL, String strXMLFilename, PrintWriter out) throws Exception {
    InputStream response = null;//from  w w w . j a v a 2  s. com
    File input = new File(strXMLFilename);
    // Prepare HTTP post
    PostMethod post = new PostMethod(strURL);
    // Request content will be retrieved directly
    // from the input stream Part[] parts = {
    Part[] parts = { new FilePart("uploadFile", strXMLFilename, input) };
    RequestEntity entity = new MultipartRequestEntity(parts, post.getParams());
    // RequestEntity entity = new FileRequestEntity(input,
    // "text/xml;charset=utf-8");
    post.setRequestEntity(entity);
    // Get HTTP client
    HttpClient httpclient = new HttpClient();
    // Execute request
    try {
        int result = httpclient.executeMethod(post);
        // Display status code
        System.out.println("Response status code: " + result);
        // Display response
        response = post.getResponseBodyAsStream();
        for (int i = response.read(); i != -1; i = response.read()) {
            out.write(i);
        }
    } finally {
        // Release current connection to the connection pool once you are
        // done
        post.releaseConnection();
    }
    return response;
}

From source file:fr.unix_experience.owncloud_sms.engine.OCSMSOwnCloudClient.java

public PostMethod createPushRequest(JSONArray smsList) throws OCSyncException {
    JSONObject obj = createPushJSONObject(smsList);
    if (obj == null) {
        return null;
    }/*from ww w.  j a v  a 2 s.  c o m*/

    StringRequestEntity ent = createJSONRequestEntity(obj);
    if (ent == null) {
        return null;
    }

    PostMethod post = new PostMethod(_ocClient.getBaseUri() + OC_PUSH_ROUTE);
    post.addRequestHeader("OCS-APIREQUEST", "true");
    post.setRequestEntity(ent);

    return post;
}

From source file:com.epam.wilma.gepard.test.helper.ResourceUploaderDecorator.java

private String createRequest(final String fileName, final PostMethod httpPost) throws IOException {
    InputStream inputStream = new FileInputStream(new File(fileName).getAbsoluteFile());
    String content = getInputStreamAsString(inputStream);
    InputStream inputStream2 = new FileInputStream(new File(fileName).getAbsoluteFile());
    InputStreamRequestEntity entity = new InputStreamRequestEntity(inputStream2);
    httpPost.setRequestEntity(entity);
    return content;
}

From source file:com.uber.jenkins.phabricator.uberalls.UberallsClient.java

public boolean recordCoverage(String sha, CodeCoverageMetrics codeCoverageMetrics) {
    if (codeCoverageMetrics != null && codeCoverageMetrics.isValid()) {
        JSONObject params = new JSONObject();
        params.put("sha", sha);
        params.put("branch", branch);
        params.put("repository", repository);
        params.put(PACKAGE_COVERAGE_KEY, codeCoverageMetrics.getPackageCoveragePercent());
        params.put(FILES_COVERAGE_KEY, codeCoverageMetrics.getFilesCoveragePercent());
        params.put(CLASSES_COVERAGE_KEY, codeCoverageMetrics.getClassesCoveragePercent());
        params.put(METHOD_COVERAGE_KEY, codeCoverageMetrics.getMethodCoveragePercent());
        params.put(LINE_COVERAGE_KEY, codeCoverageMetrics.getLineCoveragePercent());
        params.put(CONDITIONAL_COVERAGE_KEY, codeCoverageMetrics.getConditionalCoveragePercent());

        try {/*from  ww w .  j a va 2 s.  c o m*/
            HttpClient client = getClient();
            PostMethod request = new PostMethod(getBuilder().build().toString());
            request.addRequestHeader("Content-Type", "application/json");
            StringRequestEntity requestEntity = new StringRequestEntity(params.toString(),
                    ContentType.APPLICATION_JSON.toString(), "UTF-8");
            request.setRequestEntity(requestEntity);
            int statusCode = client.executeMethod(request);

            if (statusCode != HttpStatus.SC_OK) {
                logger.info(TAG, "Call failed: " + request.getStatusLine());
                return false;
            }
            return true;
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (HttpResponseException e) {
            // e.g. 404, pass
            logger.info(TAG, "HTTP Response error recording metrics: " + e);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return false;
}

From source file:de.mpg.escidoc.services.edoc.PubManImport.java

private String submitItem(String id, String lastModificationDate) throws Exception {
    HttpClient httpClient = new HttpClient();
    PostMethod postMethod = new PostMethod(CORESERVICES_URL + "/ir/item/" + id + "/submit");
    postMethod.addRequestHeader("Cookie", "escidocCookie=" + userHandle);
    String body = "<param last-modification-date=\"" + lastModificationDate
            + "\"><comment>Submit comment.</comment></param>";

    postMethod.setRequestEntity(new StringRequestEntity(body));
    ProxyHelper.executeMethod(httpClient, postMethod);

    String response = postMethod.getResponseBodyAsString();

    if (postMethod.getStatusCode() != 200) {
        throw new RuntimeException(response);
    }/*from  ww w  .j  a  v a 2  s .c om*/

    return response;
}

From source file:de.mpg.escidoc.services.edoc.PubManImport.java

private String releaseItem(String id, String lastModificationDate) throws Exception {
    HttpClient httpClient = new HttpClient();
    PostMethod postMethod = new PostMethod(CORESERVICES_URL + "/ir/item/" + id + "/release");
    postMethod.addRequestHeader("Cookie", "escidocCookie=" + userHandle);
    String body = "<param last-modification-date=\"" + lastModificationDate
            + "\"><comment>Release comment.</comment></param>";

    postMethod.setRequestEntity(new StringRequestEntity(body));
    ProxyHelper.executeMethod(httpClient, postMethod);

    String response = postMethod.getResponseBodyAsString();

    if (postMethod.getStatusCode() != 200) {
        throw new RuntimeException(response);
    }//from   ww w.  ja v  a 2 s .  c  o  m

    return response;
}

From source file:javaapplicationclientrest.ControllerCliente.java

public void cadastrarNoticia(String titulo, String autor, String conteudo) {
    PostMethod p = new PostMethod(Constants.POST_NOTICIA);
    String xml = "<?xml version=\"1.0\"?>";
    xml += "<noticia><titulo>" + titulo + "</titulo><autor>" + autor + "</autor><conteudo>" + conteudo
            + "</conteudo></noticia>";
    StringRequestEntity requestEntity;//from w ww.j av a  2s.  c  om
    try {
        requestEntity = new StringRequestEntity(xml, "application/xml", "UTF-8");

        p.setRequestEntity(requestEntity);

        try {

            http.executeMethod(p);
            System.out.println("status : " + p.getStatusText());
            //                System.out.println("status: "+ p.getStatusText() + "\n");
            // System.out.println("message: "+ p.getResponseBodyAsString());
        } catch (IOException ex) {
            Logger.getLogger(ControllerCliente.class.getName()).log(Level.SEVERE, null, ex);
        }

    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(ControllerCliente.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:com.atlassian.jira.web.action.setup.SetupProductBundleHelper.java

public void authenticateUser(final String username, final String password) {
    final HttpClient httpClient = prepareClient();
    final PostMethod method = new PostMethod(getAuthenticationUrl());

    try {/*www.  j  av  a2s .co m*/
        final StringRequestEntity requestEntity = new StringRequestEntity(
                prepareJSON(ImmutableMap.of("username", username, "password", password)), "application/json",
                "UTF-8");

        method.setRequestEntity(requestEntity);

        final int status = httpClient.executeMethod(method);
        if (status == 200) {
            saveCookies(httpClient.getState().getCookies());
        } else {
            log.warn("Problem authenticating user during product bundle license installation, status code: "
                    + status);
        }
    } catch (final IOException e) {
        log.warn("Problem authenticating user during product bundle license installation", e);
    } finally {
        method.releaseConnection();
    }
}

From source file:com.honnix.yaacs.adapter.http.ACHttpClient.java

private PostMethod buildPostMethod(String url, String request) {
    StringBuilder sb = new StringBuilder("http://").append(host).append(':').append(port).append(url);
    PostMethod postMethod = new PostMethod(sb.toString());

    sb = new StringBuilder(ACHttpConstant.HTTP_CONTENT_TYPE).append(" charset=").append(CHARSET);
    postMethod.setRequestHeader("Content-Type", sb.toString());

    try {/*from ww  w .j  a  v a  2  s  .  co m*/
        StringRequestEntity requestEntity = new StringRequestEntity(request, null, CHARSET);

        postMethod.setRequestEntity(requestEntity);
    } catch (UnsupportedEncodingException e) {
        postMethod = null;
    }

    return postMethod;
}