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

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

Introduction

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

Prototype

public void setContentChunked(boolean paramBoolean) 

Source Link

Usage

From source file:org.apache.axis2.transport.http.HTTPSender.java

/**
 * Used to send a request via HTTP Post Method
 *
 * @param msgContext       - The MessageContext of the message
 * @param url              - The target URL
 * @param soapActionString - The soapAction string of the request
 * @throws AxisFault - Thrown in case an exception occurs
 *//*from  w  w w.jav  a2s  .co  m*/
private void sendViaPost(MessageContext msgContext, URL url, String soapActionString) throws AxisFault {

    HttpClient httpClient = getHttpClient(msgContext);

    /*  What's up with this, it never gets used anywhere?? --Glen
            String charEncoding =
        (String) msgContext.getProperty(Constants.Configuration.CHARACTER_SET_ENCODING);
            
            if (charEncoding == null) {
    charEncoding = MessageContext.DEFAULT_CHAR_SET_ENCODING;
            }
    */

    PostMethod postMethod = new PostMethod();
    if (log.isTraceEnabled()) {
        log.trace(Thread.currentThread() + " PostMethod " + postMethod + " / " + httpClient);
    }
    MessageFormatter messageFormatter = populateCommonProperties(msgContext, url, postMethod, httpClient,
            soapActionString);

    postMethod.setRequestEntity(new AxisRequestEntity(messageFormatter, msgContext, format, soapActionString,
            chunked, isAllowedRetry));

    if (!httpVersion.equals(HTTPConstants.HEADER_PROTOCOL_10) && chunked) {
        postMethod.setContentChunked(true);
    }

    String soapAction = messageFormatter.formatSOAPAction(msgContext, format, soapActionString);

    if (soapAction != null && !msgContext.isDoingREST()) {
        postMethod.setRequestHeader(HTTPConstants.HEADER_SOAP_ACTION, soapAction);
    }

    /*
     *   main excecution takes place..
     */
    try {
        executeMethod(httpClient, msgContext, url, postMethod);
        handleResponse(msgContext, postMethod);
    } catch (IOException e) {
        log.info("Unable to sendViaPost to url[" + url + "]", e);
        throw AxisFault.makeFault(e);
    } finally {
        cleanup(msgContext, postMethod);
    }
}

From source file:org.apache.axis2.transport.http.impl.httpclient3.HTTPSenderImpl.java

/**
 * Used to send a request via HTTP Post Method
 * /*from ww w.j a va2s  . c  o  m*/
 * @param msgContext
 *            - The MessageContext of the message
 * @param url
 *            - The target URL
 * @param soapActionString
 *            - The soapAction string of the request
 * @throws AxisFault
 *             - Thrown in case an exception occurs
 */
protected void sendViaPost(MessageContext msgContext, URL url, String soapActionString) throws AxisFault {

    HttpClient httpClient = getHttpClient(msgContext);

    /*
     * What's up with this, it never gets used anywhere?? --Glen String
     * charEncoding = (String)
     * msgContext.getProperty(Constants.Configuration
     * .CHARACTER_SET_ENCODING);
     * 
     * if (charEncoding == null) { charEncoding =
     * MessageContext.DEFAULT_CHAR_SET_ENCODING; }
     */

    PostMethod postMethod = new PostMethod();
    if (log.isTraceEnabled()) {
        log.trace(Thread.currentThread() + " PostMethod " + postMethod + " / " + httpClient);
    }
    MessageFormatter messageFormatter = populateCommonProperties(msgContext, url, postMethod, httpClient,
            soapActionString);

    postMethod.setRequestEntity(new AxisRequestEntityImpl(messageFormatter, msgContext, format,
            soapActionString, chunked, isAllowedRetry));

    if (!httpVersion.equals(HTTPConstants.HEADER_PROTOCOL_10) && chunked) {
        postMethod.setContentChunked(true);
    }

    String soapAction = messageFormatter.formatSOAPAction(msgContext, format, soapActionString);

    if (soapAction != null) {
        postMethod.setRequestHeader(HTTPConstants.HEADER_SOAP_ACTION, soapAction);
    }

    /*
     * main excecution takes place..
     */
    try {
        executeMethod(httpClient, msgContext, url, postMethod);
        handleResponse(msgContext, postMethod);
    } catch (IOException e) {
        log.info("Unable to sendViaPost to url[" + url + "]", e);
        throw AxisFault.makeFault(e);
    } finally {
        cleanup(msgContext, postMethod);
    }
}

From source file:org.apache.wink.itest.contentencoding.ContentEncodingTest.java

/**
 * Tests sending in small bits of gzip encoded content.
 * //from w w  w  . jav a  2  s.  c  o m
 * @throws HttpException
 * @throws IOException
 */
public void testSendLargeGzipContentEncoded() throws HttpException, IOException {
    PostMethod postMethod = new PostMethod(BASE_URI + "/bigbook/mirror");
    postMethod.setContentChunked(true);
    postMethod.addRequestHeader("Accept", "text/plain");
    postMethod.setRequestHeader("Content-Encoding", "gzip");
    // postMethod.setRequestHeader("Content-)
    ByteArrayOutputStream originalContent = new ByteArrayOutputStream();
    for (int c = 0; c < 5000000; ++c) {
        originalContent.write(c);
    }

    /*
     * gzip the contents
     */
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzipOut = new GZIPOutputStream(baos);
    originalContent.writeTo(gzipOut);
    gzipOut.finish();
    byte[] content = baos.toByteArray();

    postMethod.setRequestEntity(new ByteArrayRequestEntity(content, "text/plain; charset=utf-8"));
    try {
        int result = client.executeMethod(postMethod);
        assertEquals(200, result);
        InputStream responseStream = postMethod.getResponseBodyAsStream();
        for (int c = 0; c < 5000000; ++c) {
            assertEquals(c % 256, responseStream.read());
        }
    } finally {
        postMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.contentencoding.ContentEncodingTest.java

/**
 * Tests sending in small bits of gzip encoded content.
 * //from  w ww.j ava 2s.c  o  m
 * @throws HttpException
 * @throws IOException
 */
public void testSendLargeGzipContentEncodedAndReceiveContentEncoded() throws HttpException, IOException {
    PostMethod postMethod = new PostMethod(BASE_URI + "/bigbook/mirror");
    postMethod.setContentChunked(true);
    postMethod.setRequestHeader("Accept", "text/plain");
    postMethod.setRequestHeader("Accept-Encoding", "gzip");
    postMethod.setRequestHeader("Content-Encoding", "gzip");
    ByteArrayOutputStream originalContent = new ByteArrayOutputStream();
    for (int c = 0; c < 5000000; ++c) {
        originalContent.write(c);
    }

    /*
     * gzip the contents
     */
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzipOut = new GZIPOutputStream(baos);
    originalContent.writeTo(gzipOut);
    gzipOut.finish();
    byte[] content = baos.toByteArray();

    postMethod.setRequestEntity(new ByteArrayRequestEntity(content, "text/plain; charset=utf-8"));
    try {
        int result = client.executeMethod(postMethod);
        assertEquals(200, result);
        InputStream responseStream = new GZIPInputStream(postMethod.getResponseBodyAsStream());
        for (int c = 0; c < 5000000; ++c) {
            assertEquals(c % 256, responseStream.read());
        }
    } finally {
        postMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.largeentity.LargeEntityTest.java

/**
 * Tests sending a large string. Possible failures including the servlet
 * request buffer being too small, so the status headers do not get set
 * correctly since the message body will have to be flushed out of the
 * servlet response buffer.//from  w w  w.  ja  v  a 2  s.c  o  m
 * 
 * @throws Exception
 */
public void testSendLargeStringChunked() throws Exception {
    PostMethod postMethod = new PostMethod(getBaseURI() + "/large");
    postMethod.setContentChunked(true);
    try {

        ByteArrayOutputStream originalContent = new ByteArrayOutputStream();
        for (int c = 0; c < 5000000; ++c) {
            originalContent.write(c);
        }
        byte[] entity = originalContent.toByteArray();
        postMethod.setRequestEntity(new ByteArrayRequestEntity(entity, "text/xml"));
        client.executeMethod(postMethod);
        assertEquals(277, postMethod.getStatusCode());

        InputStream respStream = postMethod.getResponseBodyAsStream();
        for (int c = 0; c < entity.length; ++c) {
            int respByte = respStream.read();
            assertEquals(entity[c] % 256, (byte) respByte);
        }

        // int headerLength = (entity.length < 2048) ? entity.length : 2048;
        // byte[] headerBytes = new byte[headerLength];
        // for (int c = 0; c < headerLength; ++c) {
        // headerBytes[c] = entity[c];
        // }

        StringBuffer sb = new StringBuffer();
        for (int c = 0; c < 50; ++c) {
            sb.append("abcdefghijklmnopqrstuvwxyz");
        }
        // String expectedHeaderValue = new String(headerBytes, "UTF-8");
        Header header = postMethod.getResponseHeader("appendStringsHeader");
        assertNotNull(header);
        assertEquals(sb.toString(), header.getValue());
    } finally {
        postMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.largeentity.LargeEntityTest.java

/**
 * Tests sending a JAR file in chunked transfer format.
 * //from   w ww .j  a  va 2  s.  c o  m
 * @throws Exception
 */
public void testSendJARChunked() throws Exception {
    PostMethod postMethod = new PostMethod(getBaseURI() + "/large/zip");
    postMethod.setContentChunked(true);
    try {
        System.out.println(
                new File(ServerEnvironmentInfo.getWorkDir() + "/wink-itest-targeting-1.4.0-SNAPSHOT.war")
                        .getAbsoluteFile().getAbsolutePath());
        postMethod.setRequestEntity(new FileRequestEntity(
                new File(ServerEnvironmentInfo.getWorkDir() + "/wink-itest-targeting-1.4.0-SNAPSHOT.war"),
                "application/jar"));
        client.executeMethod(postMethod);
        assertEquals(290, postMethod.getStatusCode());
        String resp = postMethod.getResponseBodyAsString();
        assertEquals("META-INF/DEPENDENCIES", resp);
    } finally {
        postMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.transferencoding.TransferEncodingTest.java

/**
 * Tests sending in small bits of chunked content.
 * /*from  ww w.  java  2  s .  c om*/
 * @throws HttpException
 * @throws IOException
 */
public void testSendSmallGzipContentEncoded() throws HttpException, IOException {
    PostMethod postMethod = new PostMethod(BASE_URI + "/chunkedbook");
    postMethod.setContentChunked(true);

    postMethod.addRequestHeader("Accept", "text/plain");
    ByteArrayOutputStream originalContent = new ByteArrayOutputStream();
    originalContent.write("Hello world".getBytes("UTF-8"));

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    originalContent.writeTo(baos);
    byte[] content = baos.toByteArray();

    postMethod.setRequestEntity(new ByteArrayRequestEntity(content, "text/plain; charset=utf-8"));
    try {
        int result = client.executeMethod(postMethod);
        assertEquals(200, result);
        String response = postMethod.getResponseBodyAsString();
        assertEquals("Hello world", response);
    } finally {
        postMethod.releaseConnection();
    }
}

From source file:org.elasticsearch.hadoop.rest.RestClient.java

public void bulk(String index, byte[] buffer, int bufferSize) {
    PostMethod post = new PostMethod(index + "/_bulk");
    post.setRequestEntity(new JsonByteArrayRequestEntity(buffer, bufferSize));
    post.setContentChunked(false);
    execute(post);/*ww  w.  java 2  s . co m*/
}

From source file:org.fcrepo.client.FedoraClient.java

/**
 * Upload the given file to Fedora's upload interface via HTTP POST.
 *
 * @return the temporary id which can then be passed to API-M requests as a
 *         URL. It will look like uploaded://123
 *//*from   w  ww.  ja  v a  2 s.  c o  m*/
public String uploadFile(File file) throws IOException {
    PostMethod post = null;
    try {
        // prepare the post method
        post = new PostMethod(getUploadURL());
        post.setDoAuthentication(true);
        post.getParams().setParameter("Connection", "Keep-Alive");

        // chunked encoding is not required by the Fedora server,
        // but makes uploading very large files possible
        post.setContentChunked(true);

        // add the file part
        Part[] parts = { new FilePart("file", file) };
        post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));

        // execute and get the response
        int responseCode = getHttpClient().executeMethod(post);
        String body = null;
        try {
            body = post.getResponseBodyAsString();
        } catch (Exception e) {
            logger.warn("Error reading response body", e);
        }
        if (body == null) {
            body = "[empty response body]";
        }
        body = body.trim();
        if (responseCode != HttpStatus.SC_CREATED) {
            throw new IOException("Upload failed: " + HttpStatus.getStatusText(responseCode) + ": "
                    + replaceNewlines(body, " "));
        } else {
            return replaceNewlines(body, "");
        }
    } finally {
        if (post != null) {
            post.releaseConnection();
        }
    }
}

From source file:org.geoserver.gss.HTTPGSSClient.java

public GetDiffResponseType getDiff(GetDiffType getDiff) throws IOException {
    // prepare the encoder
    Encoder encoder = new Encoder(configuration, configuration.getXSD().getSchema());
    QName layerName = getDiff.getTypeName();
    encoder.getNamespaces().declarePrefix(layerName.getPrefix(), layerName.getNamespaceURI());
    encoder.setEncoding(Charset.forName("UTF-8"));

    // prepare POST request
    PostMethod method = new PostMethod(address.toExternalForm());
    method.setContentChunked(true);
    method.setRequestEntity(new XMLEntity(getDiff, GSS.GetDiff, encoder));

    // execute the request and interpret the response
    Object response = executeMethod(method);
    if (response instanceof GetDiffResponseType) {
        return (GetDiffResponseType) response;
    } else {//from www.  java  2s.  c om
        if (response == null) {
            throw new IOException("The response was parsed to a null object");
        }
        throw new IOException("The response was parsed to an unrecognized object type: " + response.getClass());
    }

}