Example usage for org.apache.http.client.methods CloseableHttpResponse setEntity

List of usage examples for org.apache.http.client.methods CloseableHttpResponse setEntity

Introduction

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

Prototype

void setEntity(HttpEntity httpEntity);

Source Link

Usage

From source file:org.esigate.HttpErrorPage.java

public static CloseableHttpResponse generateHttpResponse(int statusCode, String statusText) {
    CloseableHttpResponse result = BasicCloseableHttpResponse
            .adapt(new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, statusCode, statusText)));
    result.setEntity(toMemoryEntity(statusText));
    return result;
}

From source file:com.polydeucesys.eslogging.testutils.MockCloseableHttpClient.java

public static CloseableHttpResponse responseWithBody(final int statusCode, final String body) {
    CloseableHttpResponse resp = new BasicCloseableHttpResponse(new StatusLine() {
        @Override//from w ww  . ja v a 2s .  c  o  m
        public int getStatusCode() {
            // TODO Auto-generated method stub
            return statusCode;
        }

        @Override
        public ProtocolVersion getProtocolVersion() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public String getReasonPhrase() {
            // TODO Auto-generated method stub
            return null;
        }
    });
    resp.setEntity(new AbstractHttpEntity() {

        @Override
        public boolean isRepeatable() {
            // TODO Auto-generated method stub
            return true;
        }

        @Override
        public long getContentLength() {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public InputStream getContent() throws IOException, IllegalStateException {
            return new ByteArrayInputStream(body.getBytes());
        }

        @Override
        public void writeTo(OutputStream outstream) throws IOException {
            // TODO Auto-generated method stub

        }

        @Override
        public boolean isStreaming() {
            // TODO Auto-generated method stub
            return false;
        }

    });
    resp.setHeader(new BasicHeader("Content-Type", "text"));
    return resp;
}

From source file:bit.changepurse.wdk.http.TestHTTPResponse.java

private void testGetBody(HttpEntity entity) throws UnsupportedEncodingException {
    CloseableHttpResponse rawResponse = new MockedApacheResponse();
    rawResponse.setStatusCode(HTTPStatusCode.SUCCESS.toInt());
    rawResponse.setEntity(entity);
    HTTPResponse response = new HTTPResponse(rawResponse);
    assertThat(response.getBody(), equalTo(CheckedExceptionMethods.toString(entity)));
    assertThat(response.toString(), notNullValue());
}

From source file:org.esigate.servlet.impl.ResponseSenderTest.java

public void testSendResponseAlreadySent() throws Exception {
    MockHttpServletResponse httpServletResponse = new MockHttpServletResponse();
    PrintWriter writer = httpServletResponse.getWriter();
    writer.write("Test");
    writer.close();/*  w  w w .j ava2 s . co  m*/
    CloseableHttpResponse httpClientResponse = BasicCloseableHttpResponse
            .adapt(new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK")));
    httpClientResponse.setEntity(new StringEntity("Abcdefg"));
    renderer.sendResponse(httpClientResponse, null, httpServletResponse);
}

From source file:org.callimachusproject.client.AutoClosingHttpClient.java

@Override
protected CloseableHttpResponse doExecute(final HttpHost host, final HttpRequest request, HttpContext ctx)
        throws IOException, ClientProtocolException {
    if (++numberOfClientCalls % 100 == 0) {
        // Deletes the (no longer used) temporary cache files from disk.
        cleanResources();//from www . ja v  a  2 s. com
    }
    CloseableHttpResponse resp = client.execute(host, request, ctx);
    HttpEntity entity = resp.getEntity();
    if (entity != null) {
        resp.setEntity(new CloseableEntity(entity, new Closeable() {
            public void close() throws IOException {
                // this also keeps this object from being finalized
                // until all its response entities are consumed
                String uri = request.getRequestLine().getUri();
                logger.debug("Remote {}{} closed", host, uri);
            }
        }));
    }
    return resp;
}

From source file:org.esigate.HttpErrorPage.java

/**
 * Create an HTTP error page exception from an Http response.
 * /*from  ww  w.j  a v a 2 s.c  o m*/
 * @param httpResponse
 *            backend response.
 */
public HttpErrorPage(CloseableHttpResponse httpResponse) {
    super(httpResponse.getStatusLine().getStatusCode() + " " + httpResponse.getStatusLine().getReasonPhrase());
    this.httpResponse = httpResponse;
    // Consume the entity and replace it with an in memory Entity
    httpResponse.setEntity(toMemoryEntity(httpResponse.getEntity()));
}

From source file:org.finra.dm.tools.common.databridge.DataBridgeWebClientTest.java

@Test
public void testGetBusinessObjectData200ValidResponse() throws Exception {
    CloseableHttpResponse httpResponse = new MockCloseableHttpResponse(
            new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "testReasonPhrase"));
    httpResponse.setEntity(new StringEntity(xmlHelper.objectToXml(new BusinessObjectData())));
    String actionDescription = "testActionDescription";
    BusinessObjectData businessObjectData = dataBridgeWebClient.getBusinessObjectData(httpResponse,
            actionDescription);//from w w w. j  a  v a2s . c  om
    Assert.assertNotNull("businessObjectData", businessObjectData);
}

From source file:org.finra.dm.tools.common.databridge.DataBridgeWebClientTest.java

@Test
public void testGetBusinessObjectData200BadContentReturnsNull() throws Exception {
    CloseableHttpResponse httpResponse = new MockCloseableHttpResponse(
            new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "testReasonPhrase"));
    httpResponse.setEntity(new StringEntity("invalid xml"));
    String actionDescription = "testActionDescription";
    BusinessObjectData businessObjectData = dataBridgeWebClient.getBusinessObjectData(httpResponse,
            actionDescription);//w  ww. j  a  v a2 s .co  m
    Assert.assertNull("businessObjectData", businessObjectData);
}

From source file:org.finra.dm.tools.common.databridge.DataBridgeWebClientTest.java

@Test
public void testGetBusinessObjectData400BadContentThrows() throws Exception {
    int expectedStatusCode = 400;
    String expectedReasonPhrase = "testReasonPhrase";
    String expectedErrorMessage = "invalid xml";

    CloseableHttpResponse httpResponse = new MockCloseableHttpResponse(
            new BasicStatusLine(HttpVersion.HTTP_1_1, expectedStatusCode, expectedReasonPhrase));
    httpResponse.setEntity(new StringEntity(expectedErrorMessage));
    String actionDescription = "testActionDescription";
    try {/*from   www  .  ja v a2  s. c o  m*/
        dataBridgeWebClient.getBusinessObjectData(httpResponse, actionDescription);
        Assert.fail("expected HttpErrorResponseException, but no exception was thrown");
    } catch (Exception e) {
        Assert.assertEquals("thrown exception type", HttpErrorResponseException.class, e.getClass());

        HttpErrorResponseException httpErrorResponseException = (HttpErrorResponseException) e;
        Assert.assertEquals("httpErrorResponseException responseMessage", expectedErrorMessage,
                httpErrorResponseException.getResponseMessage());
        Assert.assertEquals("httpErrorResponseException statusCode", expectedStatusCode,
                httpErrorResponseException.getStatusCode());
        Assert.assertEquals("httpErrorResponseException statusDescription", expectedReasonPhrase,
                httpErrorResponseException.getStatusDescription());
        Assert.assertEquals("httpErrorResponseException message", "Failed to " + actionDescription,
                httpErrorResponseException.getMessage());
    }
}

From source file:org.finra.dm.tools.common.databridge.DataBridgeWebClientTest.java

@Test
public void testGetBusinessObjectData400Throws() throws Exception {
    int expectedStatusCode = 400;
    String expectedReasonPhrase = "testReasonPhrase";
    String expectedErrorMessage = "testErrorMessage";

    ErrorInformation errorInformation = new ErrorInformation();
    errorInformation.setStatusCode(expectedStatusCode);
    errorInformation.setMessage(expectedErrorMessage);
    errorInformation.setStatusDescription(expectedReasonPhrase);

    String requestContent = xmlHelper.objectToXml(errorInformation);

    CloseableHttpResponse httpResponse = new MockCloseableHttpResponse(
            new BasicStatusLine(HttpVersion.HTTP_1_1, expectedStatusCode, expectedReasonPhrase));
    httpResponse.setEntity(new StringEntity(requestContent));
    String actionDescription = "testActionDescription";
    try {//  w w  w .  j  av a2 s. co m
        dataBridgeWebClient.getBusinessObjectData(httpResponse, actionDescription);
        Assert.fail("expected HttpErrorResponseException, but no exception was thrown");
    } catch (Exception e) {
        Assert.assertEquals("thrown exception type", HttpErrorResponseException.class, e.getClass());

        HttpErrorResponseException httpErrorResponseException = (HttpErrorResponseException) e;
        Assert.assertEquals("httpErrorResponseException responseMessage", expectedErrorMessage,
                httpErrorResponseException.getResponseMessage());
        Assert.assertEquals("httpErrorResponseException statusCode", expectedStatusCode,
                httpErrorResponseException.getStatusCode());
        Assert.assertEquals("httpErrorResponseException statusDescription", expectedReasonPhrase,
                httpErrorResponseException.getStatusDescription());
        Assert.assertEquals("httpErrorResponseException message", "Failed to " + actionDescription,
                httpErrorResponseException.getMessage());
    }
}