Search in sources :

Example 1 with AbstractHttpEntity

use of org.apache.http.entity.AbstractHttpEntity in project camel by apache.

the class HttpEntityConverter method asHttpEntity.

private static HttpEntity asHttpEntity(byte[] data, Exchange exchange) throws Exception {
    AbstractHttpEntity entity;
    if (exchange != null && !exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) {
        String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
        InputStream stream = GZIPHelper.compressGzip(contentEncoding, data);
        entity = new InputStreamEntity(stream, stream instanceof ByteArrayInputStream ? stream.available() != 0 ? stream.available() : -1 : -1);
    } else {
        // create the Repeatable HttpEntity
        entity = new ByteArrayEntity(data);
    }
    if (exchange != null) {
        String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
        String contentType = ExchangeHelper.getContentType(exchange);
        entity.setContentEncoding(contentEncoding);
        entity.setContentType(contentType);
    }
    return entity;
}
Also used : ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) InputStreamEntity(org.apache.http.entity.InputStreamEntity)

Example 2 with AbstractHttpEntity

use of org.apache.http.entity.AbstractHttpEntity in project scout.rt by eclipse.

the class ApacheHttpRequest method execute.

@Override
public LowLevelHttpResponse execute() throws IOException {
    final StreamingContent streamingContent = getStreamingContent();
    if (streamingContent != null) {
        if (!(m_request instanceof HttpEntityEnclosingRequest)) {
            throw new ProcessingException("This request {} does not support content.", m_request);
        }
        AbstractHttpEntity entity = new AbstractHttpEntity() {

            @Override
            public void writeTo(OutputStream outstream) throws IOException {
                streamingContent.writeTo(outstream);
            }

            @Override
            public boolean isStreaming() {
                return true;
            }

            @Override
            public boolean isRepeatable() {
                if (streamingContent instanceof HttpContent) {
                    return ((HttpContent) streamingContent).retrySupported();
                }
                return false;
            }

            @Override
            public long getContentLength() {
                return ApacheHttpRequest.this.getContentLength();
            }

            @Override
            public InputStream getContent() throws IOException {
                throw new UnsupportedOperationException("Streaming entity cannot be represented as an input stream.");
            }
        };
        ((HttpEntityEnclosingRequest) m_request).setEntity(entity);
        entity.setContentEncoding(getContentEncoding());
        entity.setContentType(getContentType());
    }
    return createResponseInternal();
}
Also used : HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) OutputStream(java.io.OutputStream) StreamingContent(com.google.api.client.util.StreamingContent) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) HttpContent(com.google.api.client.http.HttpContent) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException)

Example 3 with AbstractHttpEntity

use of org.apache.http.entity.AbstractHttpEntity in project coastal-hazards by USGS-CIDA.

the class GeoserverUtil method postToWPS.

private static String postToWPS(String url, String username, String password, File wpsRequestFile) throws IOException {
    HttpPost post;
    HttpClient httpClient = HttpClientBuilder.create().build();
    post = new HttpPost(url);
    FileInputStream wpsRequestInputStream = null;
    try {
        log.info("About to perform wps post request at URL: " + url);
        wpsRequestInputStream = new FileInputStream(wpsRequestFile);
        AbstractHttpEntity entity = new InputStreamEntity(wpsRequestInputStream, wpsRequestFile.length());
        post.setEntity(entity);
        String userPass = username + ":" + password;
        post.addHeader(new BasicHeader("Authorization", "Basic " + Base64.getEncoder().encodeToString(userPass.getBytes())));
        HttpResponse response = httpClient.execute(post);
        String responseString = EntityUtils.toString(response.getEntity());
        log.info("WPS Response Recieved: " + responseString);
        return responseString;
    } finally {
        IOUtils.closeQuietly(wpsRequestInputStream);
        FileUtils.deleteQuietly(wpsRequestFile);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) FileInputStream(java.io.FileInputStream) BasicHeader(org.apache.http.message.BasicHeader) InputStreamEntity(org.apache.http.entity.InputStreamEntity)

Example 4 with AbstractHttpEntity

use of org.apache.http.entity.AbstractHttpEntity in project rdf4j by eclipse.

the class RDF4JProtocolSession method upload.

protected void upload(final Reader contents, String baseURI, final RDFFormat dataFormat, boolean overwrite, boolean preserveNodeIds, Action action, Resource... contexts) throws IOException, RDFParseException, RepositoryException, UnauthorizedException {
    final Charset charset = dataFormat.hasCharset() ? dataFormat.getCharset() : Charset.forName("UTF-8");
    HttpEntity entity = new AbstractHttpEntity() {

        private InputStream content;

        public long getContentLength() {
            // don't know
            return -1;
        }

        public Header getContentType() {
            return new BasicHeader("Content-Type", dataFormat.getDefaultMIMEType() + "; charset=" + charset.name());
        }

        public boolean isRepeatable() {
            return false;
        }

        public boolean isStreaming() {
            return true;
        }

        public synchronized InputStream getContent() throws IOException, IllegalStateException {
            if (content == null) {
                ByteArrayOutputStream buf = new ByteArrayOutputStream();
                writeTo(buf);
                content = new ByteArrayInputStream(buf.toByteArray());
            }
            return content;
        }

        public void writeTo(OutputStream out) throws IOException {
            try {
                OutputStreamWriter writer = new OutputStreamWriter(out, charset);
                IOUtil.transfer(contents, writer);
                writer.flush();
            } finally {
                contents.close();
            }
        }
    };
    upload(entity, baseURI, overwrite, preserveNodeIds, action, contexts);
}
Also used : HttpEntity(org.apache.http.HttpEntity) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) Charset(java.nio.charset.Charset) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) BasicHeader(org.apache.http.message.BasicHeader)

Example 5 with AbstractHttpEntity

use of org.apache.http.entity.AbstractHttpEntity in project rdf4j by eclipse.

the class RDF4JProtocolSession method sendTransaction.

/**
 * Sends a transaction list as serialized XML to the server.
 *
 * @deprecated since 2.8.0
 * @param txn
 * @throws IOException
 * @throws RepositoryException
 * @throws UnauthorizedException
 */
@Deprecated
public void sendTransaction(final Iterable<? extends TransactionOperation> txn) throws IOException, RepositoryException, UnauthorizedException {
    checkRepositoryURL();
    HttpPost method = new HttpPost(Protocol.getStatementsLocation(getQueryURL()));
    try {
        // Create a RequestEntity for the transaction data
        method.setEntity(new AbstractHttpEntity() {

            public long getContentLength() {
                // don't know
                return -1;
            }

            public Header getContentType() {
                return new BasicHeader("Content-Type", Protocol.TXN_MIME_TYPE);
            }

            public boolean isRepeatable() {
                return true;
            }

            public boolean isStreaming() {
                return true;
            }

            public InputStream getContent() throws IOException, IllegalStateException {
                ByteArrayOutputStream buf = new ByteArrayOutputStream();
                writeTo(buf);
                return new ByteArrayInputStream(buf.toByteArray());
            }

            public void writeTo(OutputStream out) throws IOException {
                TransactionWriter txnWriter = new TransactionWriter();
                txnWriter.serialize(txn, out);
            }
        });
        try {
            executeNoContent(method);
        } catch (RepositoryException e) {
            throw e;
        } catch (RDF4JException e) {
            throw new RepositoryException(e);
        }
    } finally {
        method.reset();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) TransactionWriter(org.eclipse.rdf4j.http.protocol.transaction.TransactionWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) RepositoryException(org.eclipse.rdf4j.repository.RepositoryException) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) ByteArrayInputStream(java.io.ByteArrayInputStream) RDF4JException(org.eclipse.rdf4j.RDF4JException) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) BasicHeader(org.apache.http.message.BasicHeader)

Aggregations

AbstractHttpEntity (org.apache.http.entity.AbstractHttpEntity)16 OutputStream (java.io.OutputStream)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 IOException (java.io.IOException)7 InputStream (java.io.InputStream)7 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)5 GZIPOutputStream (java.util.zip.GZIPOutputStream)4 HttpPost (org.apache.http.client.methods.HttpPost)4 BasicHeader (org.apache.http.message.BasicHeader)3 TestHttpClient (io.undertow.testutils.TestHttpClient)2 ProcessingException (javax.ws.rs.ProcessingException)2 BufferedHttpEntity (org.apache.http.entity.BufferedHttpEntity)2 InputStreamEntity (org.apache.http.entity.InputStreamEntity)2 ChunkedOutputStream (org.apache.http.impl.io.ChunkedOutputStream)2 OutboundMessageContext (org.glassfish.jersey.message.internal.OutboundMessageContext)2 Test (org.junit.Test)2 ReadTimeoutException (org.xnio.channels.ReadTimeoutException)2 HttpContent (com.google.api.client.http.HttpContent)1 StreamingContent (com.google.api.client.util.StreamingContent)1