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;
}
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();
}
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);
}
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();
}
}
}
}
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
}
}
Aggregations