Search in sources :

Example 46 with InputStreamEntity

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);
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Document(org.w3c.dom.Document) InputStreamEntity(org.apache.http.entity.InputStreamEntity) Test(org.junit.Test)

Example 47 with InputStreamEntity

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);
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Document(org.w3c.dom.Document) InputStreamEntity(org.apache.http.entity.InputStreamEntity) Test(org.junit.Test)

Example 48 with InputStreamEntity

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());
    }
}
Also used : CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) InputStreamEntity(org.apache.http.entity.InputStreamEntity)

Example 49 with InputStreamEntity

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();
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) PushbackInputStream(java.io.PushbackInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) InputStreamEntity(org.apache.http.entity.InputStreamEntity)

Example 50 with InputStreamEntity

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;
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) HttpResponse(org.apache.http.HttpResponse) TTransportException(com.evernote.thrift.transport.TTransportException) IOException(java.io.IOException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) IOException(java.io.IOException) TTransportException(com.evernote.thrift.transport.TTransportException) InputStreamEntity(org.apache.http.entity.InputStreamEntity) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

InputStreamEntity (org.apache.http.entity.InputStreamEntity)50 HttpPost (org.apache.http.client.methods.HttpPost)19 ByteArrayInputStream (java.io.ByteArrayInputStream)17 InputStream (java.io.InputStream)15 HttpResponse (org.apache.http.HttpResponse)12 Test (org.junit.Test)12 HttpEntity (org.apache.http.HttpEntity)10 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)9 HttpPut (org.apache.http.client.methods.HttpPut)9 IOException (java.io.IOException)8 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)6 Map (java.util.Map)5 HttpEntityEnclosingRequest (org.apache.http.HttpEntityEnclosingRequest)4 HttpClient (org.apache.http.client.HttpClient)4 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)4 URISyntaxException (java.net.URISyntaxException)3 HashMap (java.util.HashMap)3 Header (org.apache.http.Header)3 ProtocolVersion (org.apache.http.ProtocolVersion)3 HttpGet (org.apache.http.client.methods.HttpGet)3