Example usage for org.apache.http.client.methods HttpEntityEnclosingRequestBase getEntity

List of usage examples for org.apache.http.client.methods HttpEntityEnclosingRequestBase getEntity

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpEntityEnclosingRequestBase getEntity.

Prototype

public HttpEntity getEntity() 

Source Link

Usage

From source file:com.cloverstudio.spika.couchdb.ConnectionHandler.java

public static void print(HttpEntityEnclosingRequestBase httpMethod) throws IOException {
    Logger.debug(httpMethod.getMethod(), httpMethod.getRequestLine().toString());

    print(httpMethod.getEntity());/*from  w w w  .j  av a2  s . co m*/

    Header[] headers = httpMethod.getAllHeaders();
    for (Header header : headers) {
        Logger.debug("headers", header.toString());
    }
}

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

@Test
public void testBodyNoType() throws Exception {
    final InputStream bodyStream = mock(InputStream.class);

    testBuilder.body(bodyStream).perform();

    final ArgumentCaptor<HttpRequestBase> requestCaptor = ArgumentCaptor.forClass(HttpRequestBase.class);
    verify(client).executeRequest(eq(uri), requestCaptor.capture());

    final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue();
    final HttpEntity bodyEntity = request.getEntity();
    assertEquals(bodyStream, bodyEntity.getContent());
    assertEquals("application/octet-stream", request.getFirstHeader(CONTENT_TYPE).getValue());
}

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

@Test
public void testWithBody() throws Exception {
    final InputStream bodyStream = mock(InputStream.class);

    testBuilder.body(bodyStream, "plain/text").digest("checksum").perform();

    final ArgumentCaptor<HttpRequestBase> requestCaptor = ArgumentCaptor.forClass(HttpRequestBase.class);
    verify(client).executeRequest(eq(uri), requestCaptor.capture());

    final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue();
    final HttpEntity bodyEntity = request.getEntity();
    assertEquals(bodyStream, bodyEntity.getContent());

    assertEquals("plain/text", request.getFirstHeader(CONTENT_TYPE).getValue());
    assertEquals("sha1=checksum", request.getFirstHeader(DIGEST).getValue());
}

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

@Test
public void testWithBody() throws Exception {
    final InputStream bodyStream = mock(InputStream.class);

    testBuilder.body(bodyStream, "plain/text").digest("checksum").filename("file.txt").slug("slug_value")
            .perform();//  w ww  .j  a v  a  2  s  . c  o  m

    final ArgumentCaptor<HttpRequestBase> requestCaptor = ArgumentCaptor.forClass(HttpRequestBase.class);
    verify(client).executeRequest(eq(uri), requestCaptor.capture());

    final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue();
    final HttpEntity bodyEntity = request.getEntity();
    assertEquals(bodyStream, bodyEntity.getContent());

    assertEquals("plain/text", request.getFirstHeader(CONTENT_TYPE).getValue());
    assertEquals("sha1=checksum", request.getFirstHeader(DIGEST).getValue());
    assertEquals("slug_value", request.getFirstHeader(SLUG).getValue());
    assertEquals("attachment; filename=\"file.txt\"", request.getFirstHeader(CONTENT_DISPOSITION).getValue());
}

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

@Test
public void testWithModificationHeaders() throws Exception {
    final InputStream bodyStream = mock(InputStream.class);

    final String etag = "123456";
    final String lastModified = "Mon, 19 May 2014 19:44:59 GMT";
    testBuilder.body(bodyStream, "plain/text").ifMatch(etag).ifUnmodifiedSince(lastModified).perform();

    final ArgumentCaptor<HttpRequestBase> requestCaptor = ArgumentCaptor.forClass(HttpRequestBase.class);
    verify(client).executeRequest(eq(uri), requestCaptor.capture());

    final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue();
    final HttpEntity bodyEntity = request.getEntity();
    assertEquals(bodyStream, bodyEntity.getContent());

    assertEquals("plain/text", request.getFirstHeader(CONTENT_TYPE).getValue());
    assertEquals(etag, request.getFirstHeader(IF_MATCH).getValue());
    assertEquals(lastModified, request.getFirstHeader(IF_UNMODIFIED_SINCE).getValue());
}

From source file:org.apache.juneau.rest.client.RestCallLogger.java

@Override /* RestCallInterceptor */
public void onClose(RestCall restCall) throws RestCallException {
    try {/*from w  w  w .ja  v  a 2 s .  c  o  m*/
        if (log.isLoggable(level)) {
            String output = restCall.getCapturedResponse();
            StringBuilder sb = new StringBuilder();
            HttpUriRequest req = restCall.getRequest();
            HttpResponse res = restCall.getResponse();
            if (req != null) {
                sb.append("\n=== HTTP Call (outgoing) =======================================================");

                sb.append("\n=== REQUEST ===\n").append(req);
                sb.append("\n---request headers---");
                for (Header h : req.getAllHeaders())
                    sb.append("\n\t").append(h);
                if (req instanceof HttpEntityEnclosingRequestBase) {
                    sb.append("\n---request entity---");
                    HttpEntityEnclosingRequestBase req2 = (HttpEntityEnclosingRequestBase) req;
                    HttpEntity e = req2.getEntity();
                    if (e == null)
                        sb.append("\nEntity is null");
                    else {
                        if (e.getContentType() != null)
                            sb.append("\n").append(e.getContentType());
                        if (e.getContentEncoding() != null)
                            sb.append("\n").append(e.getContentEncoding());
                        if (e.isRepeatable()) {
                            try {
                                sb.append("\n---request content---\n").append(EntityUtils.toString(e));
                            } catch (Exception ex) {
                                throw new RuntimeException(ex);
                            }
                        }
                    }
                }
            }
            if (res != null) {
                sb.append("\n=== RESPONSE ===\n").append(res.getStatusLine());
                sb.append("\n---response headers---");
                for (Header h : res.getAllHeaders())
                    sb.append("\n\t").append(h);
                sb.append("\n---response content---\n").append(output);
                sb.append("\n=== END ========================================================================");
            }
            log.log(level, sb.toString());
        }
    } catch (IOException e) {
        log.log(Level.SEVERE, e.getLocalizedMessage(), e);
    }
}

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

@Test
public void testPostNoBody() throws Exception {
    testBuilder.perform();//from  w ww .j  av a 2  s  .c o  m

    final ArgumentCaptor<HttpRequestBase> requestCaptor = ArgumentCaptor.forClass(HttpRequestBase.class);
    verify(client).executeRequest(eq(uri), requestCaptor.capture());

    final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue();
    assertNull("Request body should not be set", request.getEntity());
    assertEquals(0, request.getAllHeaders().length);
}

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

@Test
public void testPutNoBody() throws Exception {
    testBuilder.perform();//from   w w w .jav a2 s .  c  o m

    final ArgumentCaptor<HttpRequestBase> requestCaptor = ArgumentCaptor.forClass(HttpRequestBase.class);
    verify(client).executeRequest(eq(uri), requestCaptor.capture());

    final HttpEntityEnclosingRequestBase request = (HttpEntityEnclosingRequestBase) requestCaptor.getValue();
    assertNull("Request body should not be set", request.getEntity());
    assertEquals(0, request.getAllHeaders().length);
}

From source file:org.apache.sling.testing.tools.http.RequestDocumentor.java

protected void documentRequest(PrintWriter pw, RequestExecutor executor, String[] metadataArray)
        throws IOException {
    // Convert metadata to more convenient Map 
    final Map<String, String> m = new HashMap<String, String>();
    if (metadataArray.length % 2 != 0) {
        throw new IllegalArgumentException("Metadata array must be of even size, got " + metadataArray.length);
    }/*w ww  .j a v a2 s  .  c  om*/
    for (int i = 0; i < metadataArray.length; i += 2) {
        m.put(metadataArray[i], metadataArray[i + 1]);
    }

    // TODO use velocity or other templates? Just a rough prototype for now
    // Also need to filter overly long input/output, binary etc.
    pw.println();
    pw.println("====================================================================================");
    pw.print("=== ");
    pw.print(m.get("title"));
    pw.println(" ===");
    pw.println(m.get("description"));

    pw.print("\n=== ");
    pw.print("REQUEST");
    pw.println(" ===");

    pw.print("Method: ");
    pw.println(executor.getRequest().getMethod());
    pw.print("URI: ");
    pw.println(executor.getRequest().getURI());

    final Header[] allHeaders = executor.getRequest().getAllHeaders();
    if (allHeaders != null && allHeaders.length > 0) {
        pw.println("Headers:");
        for (Header h : allHeaders) {
            pw.print(h.getName());
            pw.print(":");
            pw.println(h.getValue());
        }
    }

    if (executor.getRequest() instanceof HttpEntityEnclosingRequestBase) {
        final HttpEntityEnclosingRequestBase heb = (HttpEntityEnclosingRequestBase) executor.getRequest();
        if (heb.getEntity() != null) {
            pw.print("Content-Type:");
            pw.println(heb.getEntity().getContentType().getValue());
            pw.println("Content:");
            final InputStream is = heb.getEntity().getContent();
            final byte[] buffer = new byte[16384];
            int count = 0;
            while ((count = is.read(buffer, 0, buffer.length)) > 0) {
                // TODO encoding??
                pw.write(new String(buffer, 0, count));
            }
            pw.println();
        }
    }

    pw.print("\n=== ");
    pw.print("RESPONSE");
    pw.println(" ===");
    pw.print("Content-Type:");
    pw.println(executor.getResponse().getEntity().getContentType().getValue());
    pw.println("Content:");
    pw.println(executor.getContent());

    pw.println("====================================================================================");
}