Search in sources :

Example 6 with AbstractHttpEntity

use of org.apache.http.entity.AbstractHttpEntity in project baker-android by bakerframework.

the class AndroidHttpClient method getCompressedEntity.

/**
 * Compress data to send to server.
 * Creates a Http Entity holding the gzipped data.
 * The data will not be compressed if it is too short.
 * @param data The bytes to compress
 * @return Entity holding the data
 */
public static AbstractHttpEntity getCompressedEntity(byte[] data, ContentResolver resolver) throws IOException {
    AbstractHttpEntity entity;
    if (data.length < getMinGzipSize(resolver)) {
        entity = new ByteArrayEntity(data);
    } else {
        ByteArrayOutputStream arr = new ByteArrayOutputStream();
        OutputStream zipper = new GZIPOutputStream(arr);
        zipper.write(data);
        zipper.close();
        entity = new ByteArrayEntity(arr.toByteArray());
        entity.setContentEncoding("gzip");
    }
    return entity;
}
Also used : ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity)

Example 7 with AbstractHttpEntity

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

the class ApacheConnector method getHttpEntity.

private HttpEntity getHttpEntity(final ClientRequest clientRequest, final boolean bufferingEnabled) {
    final Object entity = clientRequest.getEntity();
    if (entity == null) {
        return null;
    }
    final AbstractHttpEntity httpEntity = new AbstractHttpEntity() {

        @Override
        public boolean isRepeatable() {
            return false;
        }

        @Override
        public long getContentLength() {
            return -1;
        }

        @Override
        public InputStream getContent() throws IOException, IllegalStateException {
            if (bufferingEnabled) {
                final ByteArrayOutputStream buffer = new ByteArrayOutputStream(512);
                writeTo(buffer);
                return new ByteArrayInputStream(buffer.toByteArray());
            } else {
                return null;
            }
        }

        @Override
        public void writeTo(final OutputStream outputStream) throws IOException {
            clientRequest.setStreamProvider(new OutboundMessageContext.StreamProvider() {

                @Override
                public OutputStream getOutputStream(final int contentLength) throws IOException {
                    return outputStream;
                }
            });
            clientRequest.writeEntity();
        }

        @Override
        public boolean isStreaming() {
            return false;
        }
    };
    if (bufferingEnabled) {
        try {
            return new BufferedHttpEntity(httpEntity);
        } catch (final IOException e) {
            throw new ProcessingException(LocalizationMessages.ERROR_BUFFERING_ENTITY(), e);
        }
    } else {
        return httpEntity;
    }
}
Also used : BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ChunkedOutputStream(org.apache.http.impl.io.ChunkedOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) OutboundMessageContext(org.glassfish.jersey.message.internal.OutboundMessageContext) ProcessingException(javax.ws.rs.ProcessingException)

Example 8 with AbstractHttpEntity

use of org.apache.http.entity.AbstractHttpEntity in project android_frameworks_base by ParanoidAndroid.

the class AndroidHttpClient method getCompressedEntity.

/**
     * Compress data to send to server.
     * Creates a Http Entity holding the gzipped data.
     * The data will not be compressed if it is too short.
     * @param data The bytes to compress
     * @return Entity holding the data
     */
public static AbstractHttpEntity getCompressedEntity(byte[] data, ContentResolver resolver) throws IOException {
    AbstractHttpEntity entity;
    if (data.length < getMinGzipSize(resolver)) {
        entity = new ByteArrayEntity(data);
    } else {
        ByteArrayOutputStream arr = new ByteArrayOutputStream();
        OutputStream zipper = new GZIPOutputStream(arr);
        zipper.write(data);
        zipper.close();
        entity = new ByteArrayEntity(arr.toByteArray());
        entity.setContentEncoding("gzip");
    }
    return entity;
}
Also used : ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity)

Example 9 with AbstractHttpEntity

use of org.apache.http.entity.AbstractHttpEntity in project XobotOS by xamarin.

the class AndroidHttpClient method getCompressedEntity.

/**
     * Compress data to send to server.
     * Creates a Http Entity holding the gzipped data.
     * The data will not be compressed if it is too short.
     * @param data The bytes to compress
     * @return Entity holding the data
     */
public static AbstractHttpEntity getCompressedEntity(byte[] data, ContentResolver resolver) throws IOException {
    AbstractHttpEntity entity;
    if (data.length < getMinGzipSize(resolver)) {
        entity = new ByteArrayEntity(data);
    } else {
        ByteArrayOutputStream arr = new ByteArrayOutputStream();
        OutputStream zipper = new GZIPOutputStream(arr);
        zipper.write(data);
        zipper.close();
        entity = new ByteArrayEntity(arr.toByteArray());
        entity.setContentEncoding("gzip");
    }
    return entity;
}
Also used : ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity)

Example 10 with AbstractHttpEntity

use of org.apache.http.entity.AbstractHttpEntity in project wikidata-query-rdf by wikimedia.

the class EventHttpSender method httpEntity.

private HttpEntity httpEntity(Collection<Event> events) {
    AbstractHttpEntity entity = new AbstractHttpEntity() {

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

        @Override
        public long getContentLength() {
            return -1L;
        }

        @Override
        public InputStream getContent() {
            throw new UnsupportedOperationException();
        }

        @Override
        public void writeTo(OutputStream outputStream) throws IOException {
            objectWriter.writeValue(outputStream, events);
        }

        @Override
        public boolean isStreaming() {
            return false;
        }
    };
    entity.setContentType(ContentType.APPLICATION_JSON.toString());
    return entity;
}
Also used : OutputStream(java.io.OutputStream) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity)

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