Search in sources :

Example 46 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project CodeUtils by boredream.

the class LeanCloudHttpUtils method entityFromConnection.

/**
 * Initializes an {@link HttpEntity} from the given
 * {@link HttpURLConnection}.
 *
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}
Also used : BasicHttpEntity(org.apache.http.entity.BasicHttpEntity)

Example 47 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project pentaho-kettle by pentaho.

the class HTTPProtocolTest method httpClientGetsClosed.

@Test
public void httpClientGetsClosed() throws IOException, AuthenticationException {
    CloseableHttpClient httpClient = Mockito.mock(CloseableHttpClient.class);
    CloseableHttpResponse response = Mockito.mock(CloseableHttpResponse.class);
    HTTPProtocol httpProtocol = new HTTPProtocol() {

        @Override
        CloseableHttpClient openHttpClient(String username, String password) {
            return httpClient;
        }
    };
    String urlAsString = "http://url/path";
    when(httpClient.execute(Matchers.argThat(matchesGet()))).thenReturn(response);
    StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("http", 2, 0), HttpStatus.SC_OK, "blah");
    BasicHttpEntity entity = new BasicHttpEntity();
    String content = "plenty of mocks for this test";
    entity.setContent(new ByteArrayInputStream(content.getBytes()));
    when(response.getEntity()).thenReturn(entity);
    when(response.getStatusLine()).thenReturn(statusLine);
    assertEquals(content, httpProtocol.get(urlAsString, "", ""));
    verify(httpClient).close();
}
Also used : BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) ByteArrayInputStream(java.io.ByteArrayInputStream) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) ProtocolVersion(org.apache.http.ProtocolVersion) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 48 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project pentaho-kettle by pentaho.

the class HTTPTest method setup.

@Before
public void setup() throws Exception {
    HttpClientManager.HttpClientBuilderFacade builder = mock(HttpClientManager.HttpClientBuilderFacade.class);
    doReturn(builder).when(manager).createBuilder();
    doReturn(client).when(builder).build();
    CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    doReturn(response).when(client).execute(any(HttpGet.class), any(HttpClientContext.class));
    BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContent(new ByteArrayInputStream(DATA.getBytes()));
    doReturn(entity).when(response).getEntity();
    mockStatic(HttpClientManager.class);
    when(HttpClientManager.getInstance()).thenReturn(manager);
    setInternalState(data, "realUrl", "http://pentaho.com");
    setInternalState(data, "argnrs", new int[0]);
    doReturn(false).when(meta).isUrlInField();
    doReturn("body").when(meta).getFieldName();
    doReturn(false).when(log).isDetailed();
    doCallRealMethod().when(http).callHttpService(any(RowMetaInterface.class), any(Object[].class));
    doReturn(HttpURLConnection.HTTP_OK).when(http).requestStatusCode(any(CloseableHttpResponse.class));
    doReturn(new Header[0]).when(http).searchForHeaders(any(CloseableHttpResponse.class));
    setInternalState(http, "log", log);
    setInternalState(http, "data", data);
    setInternalState(http, "meta", meta);
}
Also used : HttpClientManager(org.pentaho.di.core.util.HttpClientManager) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) Before(org.junit.Before)

Example 49 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project wso2-synapse by wso2.

the class TargetRequest method processChunking.

/**
 * Handles the chuking messages in Passthough context, create a temporary buffer and calculate the message
 * size before writing to the external buffer, which is required the context of handling DISABLED chunking
 * messages
 *
 * @param conn
 * @param requestMsgCtx
 * @throws IOException
 * @throws AxisFault
 */
private void processChunking(NHttpClientConnection conn, MessageContext requestMsgCtx) throws IOException, AxisFault {
    String disableChunking = (String) requestMsgCtx.getProperty(PassThroughConstants.DISABLE_CHUNKING);
    String forceHttp10 = (String) requestMsgCtx.getProperty(PassThroughConstants.FORCE_HTTP_1_0);
    if ("true".equals(disableChunking) || "true".equals(forceHttp10)) {
        if (requestMsgCtx.getEnvelope().getBody().getFirstElement() == null) {
            BasicHttpEntity entity = (BasicHttpEntity) ((BasicHttpEntityEnclosingRequest) request).getEntity();
            try {
                RelayUtils.buildMessage(requestMsgCtx);
                this.hasEntityBody = true;
                Pipe pipe = (Pipe) requestMsgCtx.getProperty(PassThroughConstants.PASS_THROUGH_PIPE);
                if (pipe != null) {
                    pipe.attachConsumer(conn);
                    this.connect(pipe);
                    if (Boolean.TRUE.equals(requestMsgCtx.getProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED))) {
                        ByteArrayOutputStream out = new ByteArrayOutputStream();
                        MessageFormatter formatter = MessageProcessorSelector.getMessageFormatter(requestMsgCtx);
                        OMOutputFormat format = PassThroughTransportUtils.getOMOutputFormat(requestMsgCtx);
                        formatter.writeTo(requestMsgCtx, format, out, false);
                        OutputStream _out = pipe.getOutputStream();
                        IOUtils.write(out.toByteArray(), _out);
                        entity.setContentLength(new Long(out.toByteArray().length));
                        entity.setChunked(false);
                    }
                }
            // pipe.setSerializationComplete(true);
            } catch (XMLStreamException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MessageFormatter(org.apache.axis2.transport.MessageFormatter) OMOutputFormat(org.apache.axiom.om.OMOutputFormat)

Example 50 with BasicHttpEntity

use of org.apache.http.entity.BasicHttpEntity in project scribejava by scribejava.

the class OauthAsyncCompletionHandlerTest method shouldReleaseLatchOnCancel.

@Test
public void shouldReleaseLatchOnCancel() throws Exception {
    handler = new OAuthAsyncCompletionHandler<>(callback, ALL_GOOD_RESPONSE_CONVERTER);
    final HttpResponse response = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("4", 1, 1), 200, "ok"));
    final BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContent(new ByteArrayInputStream(new byte[0]));
    response.setEntity(entity);
    handler.cancelled();
    assertNull(callback.getResponse());
    assertNotNull(callback.getThrowable());
    assertTrue(callback.getThrowable() instanceof CancellationException);
    // verify latch is released
    try {
        handler.getResult();
        fail();
    } catch (ExecutionException expected) {
    // expected
    }
}
Also used : BasicHttpResponse(org.apache.http.message.BasicHttpResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) CancellationException(java.util.concurrent.CancellationException) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) ProtocolVersion(org.apache.http.ProtocolVersion) ExecutionException(java.util.concurrent.ExecutionException) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Aggregations

BasicHttpEntity (org.apache.http.entity.BasicHttpEntity)139 ByteArrayInputStream (java.io.ByteArrayInputStream)104 Test (org.junit.Test)89 InputStream (java.io.InputStream)60 IOException (java.io.IOException)24 HttpResponse (org.apache.http.HttpResponse)15 StatusLine (org.apache.http.StatusLine)12 BasicStatusLine (org.apache.http.message.BasicStatusLine)11 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)9 HttpException (org.apache.http.HttpException)8 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)8 HttpContext (org.apache.http.protocol.HttpContext)8 URISyntaxException (java.net.URISyntaxException)7 Map (java.util.Map)7 URI (java.net.URI)6 Header (org.apache.http.Header)6 ProtocolVersion (org.apache.http.ProtocolVersion)6 HttpGet (org.apache.http.client.methods.HttpGet)6 ChunkedInputStream (org.apache.http.impl.io.ChunkedInputStream)6 ContentLengthInputStream (org.apache.http.impl.io.ContentLengthInputStream)6