use of org.apache.http.entity.InputStreamEntity in project cxf by apache.
the class CXF4130Test method testCxf4130.
@Test
public void testCxf4130() throws Exception {
InputStream body = getClass().getResourceAsStream("cxf4130data.txt");
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(ADDRESS);
post.setEntity(new InputStreamEntity(body, ContentType.TEXT_XML));
CloseableHttpResponse response = client.execute(post);
Document doc = StaxUtils.read(response.getEntity().getContent());
Element root = doc.getDocumentElement();
Node child = root.getFirstChild();
boolean foundBody = false;
while (child != null) {
if ("Body".equals(child.getLocalName())) {
foundBody = true;
assertEquals(1, child.getChildNodes().getLength());
assertEquals("FooResponse", child.getFirstChild().getLocalName());
}
child = child.getNextSibling();
}
assertTrue("Did not find the soap:Body element", foundBody);
}
use of org.apache.http.entity.InputStreamEntity in project cxf by apache.
the class CXF4818Test method testCXF4818.
@Test
public void testCXF4818() throws Exception {
InputStream body = getClass().getResourceAsStream("cxf4818data.txt");
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(ADDRESS);
post.setEntity(new InputStreamEntity(body, ContentType.TEXT_XML));
CloseableHttpResponse response = client.execute(post);
Document doc = StaxUtils.read(response.getEntity().getContent());
// System.out.println(StaxUtils.toString(doc));
Element root = doc.getDocumentElement();
Node child = root.getFirstChild();
boolean foundBody = false;
boolean foundHeader = false;
while (child != null) {
if ("Header".equals(child.getLocalName())) {
foundHeader = true;
assertFalse("Already found body", foundBody);
} else if ("Body".equals(child.getLocalName())) {
foundBody = true;
assertTrue("Did not find header before the body", foundHeader);
}
child = child.getNextSibling();
}
assertTrue("Did not find the soap:Body element", foundBody);
assertTrue("Did not find the soap:Header element", foundHeader);
}
use of org.apache.http.entity.InputStreamEntity in project stocator by SparkTC.
the class SwiftOutputStream method OpenHttpConnection.
private synchronized void OpenHttpConnection() throws IOException {
// Let know that the connection is open
if (!openConnection.get()) {
final PipedInputStream in = new PipedInputStream();
pipOutStream.connect(in);
Callable<Integer> connectionTask = new Callable<Integer>() {
@Override
public Integer call() throws Exception {
LOG.info("Invoke HTTP Put request");
int responseCode;
String reasonPhrase;
InputStreamEntity entity = new InputStreamEntity(in, -1);
entity.setChunked(true);
entity.setContentType(contentType);
request.setEntity(entity);
LOG.info("HTTP PUT request {}", mUrl.toString());
openConnection.set(true);
try (CloseableHttpResponse response = client.execute(request)) {
responseCode = response.getStatusLine().getStatusCode();
reasonPhrase = response.getStatusLine().getReasonPhrase();
}
LOG.info("HTTP PUT response {}. Response code {}", mUrl.toString(), responseCode);
if (responseCode == HTTP_UNAUTHORIZED) {
// Unauthorized error
mAccount.authenticate();
request.removeHeaders("X-Auth-Token");
request.addHeader("X-Auth-Token", mAccount.getAuthToken());
LOG.warn("Token recreated for {}. Retry request", mUrl.toString());
try (CloseableHttpResponse response = client.execute(request)) {
responseCode = response.getStatusLine().getStatusCode();
reasonPhrase = response.getStatusLine().getReasonPhrase();
}
}
if (responseCode >= HTTP_BAD_REQUEST) {
// Code may have changed from retrying
throw new IOException("HTTP Error: " + responseCode + " Reason: " + reasonPhrase);
}
return responseCode;
}
};
futureTask = executor.submit(connectionTask);
do {
// Wait till the connection is open and the task isn't done
} while (!openConnection.get() && !futureTask.isDone());
}
}
use of org.apache.http.entity.InputStreamEntity in project cxf by apache.
the class JAXRSMultipartTest method doAddBook.
private void doAddBook(String type, String address, InputStream is, int status) throws Exception {
CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(address);
String ct = type + "; type=\"text/xml\"; " + "start=\"rootPart\"; " + "boundary=\"----=_Part_4_701508.1145579811786\"";
post.setHeader("Content-Type", ct);
post.setEntity(new InputStreamEntity(is));
try {
CloseableHttpResponse response = client.execute(post);
assertEquals(status, response.getStatusLine().getStatusCode());
if (status == 200) {
InputStream expected = getClass().getResourceAsStream("resources/expected_add_book.txt");
assertEquals(stripXmlInstructionIfNeeded(getStringFromInputStream(expected)), stripXmlInstructionIfNeeded(EntityUtils.toString(response.getEntity())));
}
} finally {
// Release current connection to the connection pool once you are done
post.releaseConnection();
}
}
use of org.apache.http.entity.InputStreamEntity in project Notes by lguipeng.
the class TEvernoteHttpClient method flush.
@Deprecated
public void flush() throws TTransportException {
long timer = System.currentTimeMillis();
HttpEntity httpEntity;
// Extract request and reset buffer
try {
// Prepare http post request
HttpPost request = new HttpPost(url.toExternalForm());
this.request = request;
request.addHeader("Content-Type", "application/x-thrift");
request.addHeader("Cache-Control", "no-transform");
if (customHeaders != null) {
for (Map.Entry<String, String> header : customHeaders.entrySet()) {
request.addHeader(header.getKey(), header.getValue());
}
}
InputStreamEntity entity = new InputStreamEntity(requestBuffer.getInputStream(), requestBuffer.getBytesWritten());
request.setEntity(entity);
request.addHeader("Accept", "application/x-thrift");
request.addHeader("User-Agent", userAgent == null ? "Java/THttpClient" : userAgent);
request.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
DefaultHttpClient dHTTP = getHTTPClient();
// noinspection ConstantConditions
HttpResponse response = dHTTP.execute(request);
httpEntity = response.getEntity();
int responseCode = response.getStatusLine().getStatusCode();
if (responseCode != 200) {
if (httpEntity != null) {
httpEntity.consumeContent();
}
throw new TTransportException("HTTP Response code: " + responseCode);
}
// Read the responses
requestBuffer.reset();
inputStream = response.getEntity().getContent();
} catch (Exception ex) {
throw new TTransportException(ex);
} finally {
try {
requestBuffer.reset();
} catch (IOException ignored) {
}
this.request = null;
}
}
Aggregations