diff options
Diffstat (limited to 'mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity')
9 files changed, 1207 insertions, 0 deletions
diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity/DecompressingEntity.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity/DecompressingEntity.java new file mode 100644 index 000000000..9adec5edb --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity/DecompressingEntity.java @@ -0,0 +1,105 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ +package ch.boye.httpclientandroidlib.client.entity; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import ch.boye.httpclientandroidlib.HttpEntity; +import ch.boye.httpclientandroidlib.entity.HttpEntityWrapper; +import ch.boye.httpclientandroidlib.util.Args; + +/** + * Common base class for decompressing {@link HttpEntity} implementations. + * + * @since 4.1 + */ +abstract class DecompressingEntity extends HttpEntityWrapper { + + /** + * Default buffer size. + */ + private static final int BUFFER_SIZE = 1024 * 2; + + /** + * {@link #getContent()} method must return the same {@link InputStream} + * instance when DecompressingEntity is wrapping a streaming entity. + */ + private InputStream content; + + /** + * Creates a new {@link DecompressingEntity}. + * + * @param wrapped + * the non-null {@link HttpEntity} to be wrapped + */ + public DecompressingEntity(final HttpEntity wrapped) { + super(wrapped); + } + + abstract InputStream decorate(final InputStream wrapped) throws IOException; + + private InputStream getDecompressingStream() throws IOException { + final InputStream in = wrappedEntity.getContent(); + return new LazyDecompressingInputStream(in, this); + } + + /** + * {@inheritDoc} + */ + @Override + public InputStream getContent() throws IOException { + if (wrappedEntity.isStreaming()) { + if (content == null) { + content = getDecompressingStream(); + } + return content; + } else { + return getDecompressingStream(); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void writeTo(final OutputStream outstream) throws IOException { + Args.notNull(outstream, "Output stream"); + final InputStream instream = getContent(); + try { + final byte[] buffer = new byte[BUFFER_SIZE]; + int l; + while ((l = instream.read(buffer)) != -1) { + outstream.write(buffer, 0, l); + } + } finally { + instream.close(); + } + } + +} diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity/DeflateDecompressingEntity.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity/DeflateDecompressingEntity.java new file mode 100644 index 000000000..4bce8b5b5 --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity/DeflateDecompressingEntity.java @@ -0,0 +1,96 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ +package ch.boye.httpclientandroidlib.client.entity; + +import java.io.IOException; +import java.io.InputStream; + +import ch.boye.httpclientandroidlib.Header; +import ch.boye.httpclientandroidlib.HttpEntity; + +/** + * {@link ch.boye.httpclientandroidlib.entity.HttpEntityWrapper} responsible for handling + * deflate Content Coded responses. In RFC2616 terms, <code>deflate</code> + * means a <code>zlib</code> stream as defined in RFC1950. Some server + * implementations have misinterpreted RFC2616 to mean that a + * <code>deflate</code> stream as defined in RFC1951 should be used + * (or maybe they did that since that's how IE behaves?). It's confusing + * that <code>deflate</code> in HTTP 1.1 means <code>zlib</code> streams + * rather than <code>deflate</code> streams. We handle both types in here, + * since that's what is seen on the internet. Moral - prefer + * <code>gzip</code>! + * + * @see GzipDecompressingEntity + * + * @since 4.1 + */ +public class DeflateDecompressingEntity extends DecompressingEntity { + + /** + * Creates a new {@link DeflateDecompressingEntity} which will wrap the specified + * {@link HttpEntity}. + * + * @param entity + * a non-null {@link HttpEntity} to be wrapped + */ + public DeflateDecompressingEntity(final HttpEntity entity) { + super(entity); + } + + /** + * Returns the non-null InputStream that should be returned to by all requests to + * {@link #getContent()}. + * + * @return a non-null InputStream + * @throws IOException if there was a problem + */ + @Override + InputStream decorate(final InputStream wrapped) throws IOException { + return new DeflateInputStream(wrapped); + } + + /** + * {@inheritDoc} + */ + @Override + public Header getContentEncoding() { + + /* This HttpEntityWrapper has dealt with the Content-Encoding. */ + return null; + } + + /** + * {@inheritDoc} + */ + @Override + public long getContentLength() { + + /* Length of inflated content is unknown. */ + return -1; + } + +} diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity/DeflateInputStream.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity/DeflateInputStream.java new file mode 100644 index 000000000..392a28a74 --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity/DeflateInputStream.java @@ -0,0 +1,228 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ +package ch.boye.httpclientandroidlib.client.entity; + +import java.io.IOException; +import java.io.InputStream; +import java.io.PushbackInputStream; +import java.util.zip.DataFormatException; +import java.util.zip.Inflater; +import java.util.zip.InflaterInputStream; + +/** Deflate input stream. This class includes logic needed for various Rfc's in order +* to reasonably implement the "deflate" compression style. +*/ +public class DeflateInputStream extends InputStream +{ + private InputStream sourceStream; + + public DeflateInputStream(final InputStream wrapped) + throws IOException + { + /* + * A zlib stream will have a header. + * + * CMF | FLG [| DICTID ] | ...compressed data | ADLER32 | + * + * * CMF is one byte. + * + * * FLG is one byte. + * + * * DICTID is four bytes, and only present if FLG.FDICT is set. + * + * Sniff the content. Does it look like a zlib stream, with a CMF, etc? c.f. RFC1950, + * section 2.2. http://tools.ietf.org/html/rfc1950#page-4 + * + * We need to see if it looks like a proper zlib stream, or whether it is just a deflate + * stream. RFC2616 calls zlib streams deflate. Confusing, isn't it? That's why some servers + * implement deflate Content-Encoding using deflate streams, rather than zlib streams. + * + * We could start looking at the bytes, but to be honest, someone else has already read + * the RFCs and implemented that for us. So we'll just use the JDK libraries and exception + * handling to do this. If that proves slow, then we could potentially change this to check + * the first byte - does it look like a CMF? What about the second byte - does it look like + * a FLG, etc. + */ + + /* We read a small buffer to sniff the content. */ + final byte[] peeked = new byte[6]; + + final PushbackInputStream pushback = new PushbackInputStream(wrapped, peeked.length); + + final int headerLength = pushback.read(peeked); + + if (headerLength == -1) { + throw new IOException("Unable to read the response"); + } + + /* We try to read the first uncompressed byte. */ + final byte[] dummy = new byte[1]; + + final Inflater inf = new Inflater(); + + try { + int n; + while ((n = inf.inflate(dummy)) == 0) { + if (inf.finished()) { + + /* Not expecting this, so fail loudly. */ + throw new IOException("Unable to read the response"); + } + + if (inf.needsDictionary()) { + + /* Need dictionary - then it must be zlib stream with DICTID part? */ + break; + } + + if (inf.needsInput()) { + inf.setInput(peeked); + } + } + + if (n == -1) { + throw new IOException("Unable to read the response"); + } + + /* + * We read something without a problem, so it's a valid zlib stream. Just need to reset + * and return an unused InputStream now. + */ + pushback.unread(peeked, 0, headerLength); + sourceStream = new DeflateStream(pushback, new Inflater()); + } catch (final DataFormatException e) { + + /* Presume that it's an RFC1951 deflate stream rather than RFC1950 zlib stream and try + * again. */ + pushback.unread(peeked, 0, headerLength); + sourceStream = new DeflateStream(pushback, new Inflater(true)); + } finally { + inf.end(); + } + + } + + /** Read a byte. + */ + @Override + public int read() + throws IOException + { + return sourceStream.read(); + } + + /** Read lots of bytes. + */ + @Override + public int read(final byte[] b) + throws IOException + { + return sourceStream.read(b); + } + + /** Read lots of specific bytes. + */ + @Override + public int read(final byte[] b, final int off, final int len) + throws IOException + { + return sourceStream.read(b,off,len); + } + + /** Skip + */ + @Override + public long skip(final long n) + throws IOException + { + return sourceStream.skip(n); + } + + /** Get available. + */ + @Override + public int available() + throws IOException + { + return sourceStream.available(); + } + + /** Mark. + */ + @Override + public void mark(final int readLimit) + { + sourceStream.mark(readLimit); + } + + /** Reset. + */ + @Override + public void reset() + throws IOException + { + sourceStream.reset(); + } + + /** Check if mark is supported. + */ + @Override + public boolean markSupported() + { + return sourceStream.markSupported(); + } + + /** Close. + */ + @Override + public void close() + throws IOException + { + sourceStream.close(); + } + + static class DeflateStream extends InflaterInputStream { + + private boolean closed = false; + + public DeflateStream(final InputStream in, final Inflater inflater) { + super(in, inflater); + } + + @Override + public void close() throws IOException { + if (closed) { + return; + } + closed = true; + inf.end(); + super.close(); + } + + } + +} diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity/EntityBuilder.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity/EntityBuilder.java new file mode 100644 index 000000000..ebec48a13 --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity/EntityBuilder.java @@ -0,0 +1,342 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ + +package ch.boye.httpclientandroidlib.client.entity; + +import java.io.File; +import java.io.InputStream; +import java.io.Serializable; +import java.util.Arrays; +import java.util.List; + +import ch.boye.httpclientandroidlib.HttpEntity; +import ch.boye.httpclientandroidlib.NameValuePair; +import ch.boye.httpclientandroidlib.annotation.NotThreadSafe; +import ch.boye.httpclientandroidlib.entity.AbstractHttpEntity; +import ch.boye.httpclientandroidlib.entity.BasicHttpEntity; +import ch.boye.httpclientandroidlib.entity.ByteArrayEntity; +import ch.boye.httpclientandroidlib.entity.ContentType; +import ch.boye.httpclientandroidlib.entity.FileEntity; +import ch.boye.httpclientandroidlib.entity.InputStreamEntity; +import ch.boye.httpclientandroidlib.entity.SerializableEntity; +import ch.boye.httpclientandroidlib.entity.StringEntity; + +/** + * Builder for {@link HttpEntity} instances. + * <p/> + * Several setter methods of this builder are mutually exclusive. In case of multiple invocations + * of the following methods only the last one will have effect: + * <ul> + * <li>{@link #setText(String)}</li> + * <li>{@link #setBinary(byte[])}</li> + * <li>{@link #setStream(java.io.InputStream)}</li> + * <li>{@link #setSerializable(java.io.Serializable)}</li> + * <li>{@link #setParameters(java.util.List)}</li> + * <li>{@link #setParameters(ch.boye.httpclientandroidlib.NameValuePair...)}</li> + * <li>{@link #setFile(java.io.File)}</li> + * </ul> + * + * @since 4.3 + */ +@NotThreadSafe +public class EntityBuilder { + + private String text; + private byte[] binary; + private InputStream stream; + private List<NameValuePair> parameters; + private Serializable serializable; + private File file; + private ContentType contentType; + private String contentEncoding; + private boolean chunked; + private boolean gzipCompress; + + EntityBuilder() { + super(); + } + + public static EntityBuilder create() { + return new EntityBuilder(); + } + + private void clearContent() { + this.text = null; + this.binary = null; + this.stream = null; + this.parameters = null; + this.serializable = null; + this.file = null; + } + + /** + * Returns entity content as a string if set using {@link #setText(String)} method. + */ + public String getText() { + return text; + } + + /** + * Sets entity content as a string. This method is mutually exclusive with + * {@link #setBinary(byte[])}, + * {@link #setStream(java.io.InputStream)} , + * {@link #setSerializable(java.io.Serializable)} , + * {@link #setParameters(java.util.List)}, + * {@link #setParameters(ch.boye.httpclientandroidlib.NameValuePair...)} + * {@link #setFile(java.io.File)} methods. + */ + public EntityBuilder setText(final String text) { + clearContent(); + this.text = text; + return this; + } + + /** + * Returns entity content as a byte array if set using + * {@link #setBinary(byte[])} method. + */ + public byte[] getBinary() { + return binary; + } + + /** + * Sets entity content as a byte array. This method is mutually exclusive with + * {@link #setText(String)}, + * {@link #setStream(java.io.InputStream)} , + * {@link #setSerializable(java.io.Serializable)} , + * {@link #setParameters(java.util.List)}, + * {@link #setParameters(ch.boye.httpclientandroidlib.NameValuePair...)} + * {@link #setFile(java.io.File)} methods. + */ + public EntityBuilder setBinary(final byte[] binary) { + clearContent(); + this.binary = binary; + return this; + } + + /** + * Returns entity content as a {@link InputStream} if set using + * {@link #setStream(java.io.InputStream)} method. + */ + public InputStream getStream() { + return stream; + } + + /** + * Sets entity content as a {@link InputStream}. This method is mutually exclusive with + * {@link #setText(String)}, + * {@link #setBinary(byte[])}, + * {@link #setSerializable(java.io.Serializable)} , + * {@link #setParameters(java.util.List)}, + * {@link #setParameters(ch.boye.httpclientandroidlib.NameValuePair...)} + * {@link #setFile(java.io.File)} methods. + */ + public EntityBuilder setStream(final InputStream stream) { + clearContent(); + this.stream = stream; + return this; + } + + /** + * Returns entity content as a parameter list if set using + * {@link #setParameters(java.util.List)} or + * {@link #setParameters(ch.boye.httpclientandroidlib.NameValuePair...)} methods. + */ + public List<NameValuePair> getParameters() { + return parameters; + } + + /** + * Sets entity content as a parameter list. This method is mutually exclusive with + * {@link #setText(String)}, + * {@link #setBinary(byte[])}, + * {@link #setStream(java.io.InputStream)} , + * {@link #setSerializable(java.io.Serializable)} , + * {@link #setFile(java.io.File)} methods. + */ + public EntityBuilder setParameters(final List<NameValuePair> parameters) { + clearContent(); + this.parameters = parameters; + return this; + } + + /** + * Sets entity content as a parameter list. This method is mutually exclusive with + * {@link #setText(String)}, + * {@link #setBinary(byte[])}, + * {@link #setStream(java.io.InputStream)} , + * {@link #setSerializable(java.io.Serializable)} , + * {@link #setFile(java.io.File)} methods. + */ + public EntityBuilder setParameters(final NameValuePair... parameters) { + return setParameters(Arrays.asList(parameters)); + } + + /** + * Returns entity content as a {@link Serializable} if set using + * {@link #setSerializable(java.io.Serializable)} method. + */ + public Serializable getSerializable() { + return serializable; + } + + /** + * Sets entity content as a {@link Serializable}. This method is mutually exclusive with + * {@link #setText(String)}, + * {@link #setBinary(byte[])}, + * {@link #setStream(java.io.InputStream)} , + * {@link #setParameters(java.util.List)}, + * {@link #setParameters(ch.boye.httpclientandroidlib.NameValuePair...)} + * {@link #setFile(java.io.File)} methods. + */ + public EntityBuilder setSerializable(final Serializable serializable) { + clearContent(); + this.serializable = serializable; + return this; + } + + /** + * Returns entity content as a {@link File} if set using + * {@link #setFile(java.io.File)} method. + */ + public File getFile() { + return file; + } + + /** + * Sets entity content as a {@link File}. This method is mutually exclusive with + * {@link #setText(String)}, + * {@link #setBinary(byte[])}, + * {@link #setStream(java.io.InputStream)} , + * {@link #setParameters(java.util.List)}, + * {@link #setParameters(ch.boye.httpclientandroidlib.NameValuePair...)} + * {@link #setSerializable(java.io.Serializable)} methods. + */ + public EntityBuilder setFile(final File file) { + clearContent(); + this.file = file; + return this; + } + + /** + * Returns {@link ContentType} of the entity, if set. + */ + public ContentType getContentType() { + return contentType; + } + + /** + * Sets {@link ContentType} of the entity. + */ + public EntityBuilder setContentType(final ContentType contentType) { + this.contentType = contentType; + return this; + } + + /** + * Returns content encoding of the entity, if set. + */ + public String getContentEncoding() { + return contentEncoding; + } + + /** + * Sets content encoding of the entity. + */ + public EntityBuilder setContentEncoding(final String contentEncoding) { + this.contentEncoding = contentEncoding; + return this; + } + + /** + * Returns <code>true</code> if entity is to be chunk coded, <code>false</code> otherwise. + */ + public boolean isChunked() { + return chunked; + } + + /** + * Makes entity chunk coded. + */ + public EntityBuilder chunked() { + this.chunked = true; + return this; + } + + /** + * Returns <code>true</code> if entity is to be GZIP compressed, <code>false</code> otherwise. + */ + public boolean isGzipCompress() { + return gzipCompress; + } + + /** + * Makes entity GZIP compressed. + */ + public EntityBuilder gzipCompress() { + this.gzipCompress = true; + return this; + } + + private ContentType getContentOrDefault(final ContentType def) { + return this.contentType != null ? this.contentType : def; + } + + /** + * Creates new instance of {@link HttpEntity} based on the current state. + */ + public HttpEntity build() { + final AbstractHttpEntity e; + if (this.text != null) { + e = new StringEntity(this.text, getContentOrDefault(ContentType.DEFAULT_TEXT)); + } else if (this.binary != null) { + e = new ByteArrayEntity(this.binary, getContentOrDefault(ContentType.DEFAULT_BINARY)); + } else if (this.stream != null) { + e = new InputStreamEntity(this.stream, 1, getContentOrDefault(ContentType.DEFAULT_BINARY)); + } else if (this.parameters != null) { + e = new UrlEncodedFormEntity(this.parameters, + this.contentType != null ? this.contentType.getCharset() : null); + } else if (this.serializable != null) { + e = new SerializableEntity(this.serializable); + e.setContentType(ContentType.DEFAULT_BINARY.toString()); + } else if (this.file != null) { + e = new FileEntity(this.file, getContentOrDefault(ContentType.DEFAULT_BINARY)); + } else { + e = new BasicHttpEntity(); + } + if (e.getContentType() != null && this.contentType != null) { + e.setContentType(this.contentType.toString()); + } + e.setContentEncoding(this.contentEncoding); + e.setChunked(this.chunked); + if (this.gzipCompress) { + return new GzipCompressingEntity(e); + } + return e; + } + +} diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity/GzipCompressingEntity.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity/GzipCompressingEntity.java new file mode 100644 index 000000000..89925a2b9 --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity/GzipCompressingEntity.java @@ -0,0 +1,113 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ +package ch.boye.httpclientandroidlib.client.entity; + +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.zip.GZIPOutputStream; + +import ch.boye.httpclientandroidlib.Header; +import ch.boye.httpclientandroidlib.HttpEntity; +import ch.boye.httpclientandroidlib.entity.HttpEntityWrapper; +import ch.boye.httpclientandroidlib.message.BasicHeader; +import ch.boye.httpclientandroidlib.protocol.HTTP; +import ch.boye.httpclientandroidlib.util.Args; + +/** + * Wrapping entity that compresses content when {@link #writeTo writing}. + * + * + * @since 4.0 + */ +public class GzipCompressingEntity extends HttpEntityWrapper { + + private static final String GZIP_CODEC = "gzip"; + + public GzipCompressingEntity(final HttpEntity entity) { + super(entity); + } + + @Override + public Header getContentEncoding() { + return new BasicHeader(HTTP.CONTENT_ENCODING, GZIP_CODEC); + } + + @Override + public long getContentLength() { + return -1; + } + + @Override + public boolean isChunked() { + // force content chunking + return true; + } + + @Override + public InputStream getContent() throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public void writeTo(final OutputStream outstream) throws IOException { + Args.notNull(outstream, "Output stream"); + final GZIPOutputStream gzip = new GZIPOutputStream(outstream); + wrappedEntity.writeTo(gzip); + // Only close output stream if the wrapped entity has been + // successfully written out + gzip.close(); + } + +} diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity/GzipDecompressingEntity.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity/GzipDecompressingEntity.java new file mode 100644 index 000000000..a3dd5b259 --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity/GzipDecompressingEntity.java @@ -0,0 +1,80 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ +package ch.boye.httpclientandroidlib.client.entity; + +import java.io.IOException; +import java.io.InputStream; +import java.util.zip.GZIPInputStream; + +import ch.boye.httpclientandroidlib.Header; +import ch.boye.httpclientandroidlib.HttpEntity; + +/** + * {@link ch.boye.httpclientandroidlib.entity.HttpEntityWrapper} for handling gzip + * Content Coded responses. + * + * @since 4.1 + */ +public class GzipDecompressingEntity extends DecompressingEntity { + + /** + * Creates a new {@link GzipDecompressingEntity} which will wrap the specified + * {@link HttpEntity}. + * + * @param entity + * the non-null {@link HttpEntity} to be wrapped + */ + public GzipDecompressingEntity(final HttpEntity entity) { + super(entity); + } + + @Override + InputStream decorate(final InputStream wrapped) throws IOException { + return new GZIPInputStream(wrapped); + } + + /** + * {@inheritDoc} + */ + @Override + public Header getContentEncoding() { + + /* This HttpEntityWrapper has dealt with the Content-Encoding. */ + return null; + } + + /** + * {@inheritDoc} + */ + @Override + public long getContentLength() { + + /* length of ungzipped content is not known */ + return -1; + } + +} diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity/LazyDecompressingInputStream.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity/LazyDecompressingInputStream.java new file mode 100644 index 000000000..60215ff15 --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity/LazyDecompressingInputStream.java @@ -0,0 +1,105 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ +package ch.boye.httpclientandroidlib.client.entity; + +import ch.boye.httpclientandroidlib.annotation.NotThreadSafe; + +import java.io.IOException; +import java.io.InputStream; + +/** + * Lazy init InputStream wrapper. + */ +@NotThreadSafe +class LazyDecompressingInputStream extends InputStream { + + private final InputStream wrappedStream; + + private final DecompressingEntity decompressingEntity; + + private InputStream wrapperStream; + + public LazyDecompressingInputStream( + final InputStream wrappedStream, + final DecompressingEntity decompressingEntity) { + this.wrappedStream = wrappedStream; + this.decompressingEntity = decompressingEntity; + } + + private void initWrapper() throws IOException { + if (wrapperStream == null) { + wrapperStream = decompressingEntity.decorate(wrappedStream); + } + } + + @Override + public int read() throws IOException { + initWrapper(); + return wrapperStream.read(); + } + + @Override + public int read(final byte[] b) throws IOException { + initWrapper(); + return wrapperStream.read(b); + } + + @Override + public int read(final byte[] b, final int off, final int len) throws IOException { + initWrapper(); + return wrapperStream.read(b, off, len); + } + + @Override + public long skip(final long n) throws IOException { + initWrapper(); + return wrapperStream.skip(n); + } + + @Override + public boolean markSupported() { + return false; + } + + @Override + public int available() throws IOException { + initWrapper(); + return wrapperStream.available(); + } + + @Override + public void close() throws IOException { + try { + if (wrapperStream != null) { + wrapperStream.close(); + } + } finally { + wrappedStream.close(); + } + } + +} diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity/UrlEncodedFormEntity.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity/UrlEncodedFormEntity.java new file mode 100644 index 000000000..855155927 --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity/UrlEncodedFormEntity.java @@ -0,0 +1,107 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ +package ch.boye.httpclientandroidlib.client.entity; + +import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; +import java.util.List; + +import ch.boye.httpclientandroidlib.NameValuePair; +import ch.boye.httpclientandroidlib.annotation.NotThreadSafe; +import ch.boye.httpclientandroidlib.client.utils.URLEncodedUtils; +import ch.boye.httpclientandroidlib.entity.ContentType; +import ch.boye.httpclientandroidlib.entity.StringEntity; +import ch.boye.httpclientandroidlib.protocol.HTTP; + +/** + * An entity composed of a list of url-encoded pairs. + * This is typically useful while sending an HTTP POST request. + * + * @since 4.0 + */ +@NotThreadSafe // AbstractHttpEntity is not thread-safe +public class UrlEncodedFormEntity extends StringEntity { + + /** + * Constructs a new {@link UrlEncodedFormEntity} with the list + * of parameters in the specified encoding. + * + * @param parameters list of name/value pairs + * @param charset encoding the name/value pairs be encoded with + * @throws UnsupportedEncodingException if the encoding isn't supported + */ + public UrlEncodedFormEntity ( + final List <? extends NameValuePair> parameters, + final String charset) throws UnsupportedEncodingException { + super(URLEncodedUtils.format(parameters, + charset != null ? charset : HTTP.DEF_CONTENT_CHARSET.name()), + ContentType.create(URLEncodedUtils.CONTENT_TYPE, charset)); + } + + /** + * Constructs a new {@link UrlEncodedFormEntity} with the list + * of parameters in the specified encoding. + * + * @param parameters iterable collection of name/value pairs + * @param charset encoding the name/value pairs be encoded with + * + * @since 4.2 + */ + public UrlEncodedFormEntity ( + final Iterable <? extends NameValuePair> parameters, + final Charset charset) { + super(URLEncodedUtils.format(parameters, + charset != null ? charset : HTTP.DEF_CONTENT_CHARSET), + ContentType.create(URLEncodedUtils.CONTENT_TYPE, charset)); + } + + /** + * Constructs a new {@link UrlEncodedFormEntity} with the list + * of parameters with the default encoding of {@link HTTP#DEFAULT_CONTENT_CHARSET} + * + * @param parameters list of name/value pairs + * @throws UnsupportedEncodingException if the default encoding isn't supported + */ + public UrlEncodedFormEntity ( + final List <? extends NameValuePair> parameters) throws UnsupportedEncodingException { + this(parameters, (Charset) null); + } + + /** + * Constructs a new {@link UrlEncodedFormEntity} with the list + * of parameters with the default encoding of {@link HTTP#DEFAULT_CONTENT_CHARSET} + * + * @param parameters iterable collection of name/value pairs + * + * @since 4.2 + */ + public UrlEncodedFormEntity ( + final Iterable <? extends NameValuePair> parameters) { + this(parameters, null); + } + +} diff --git a/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity/package-info.java b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity/package-info.java new file mode 100644 index 000000000..703f80d58 --- /dev/null +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/client/entity/package-info.java @@ -0,0 +1,31 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ + +/** + * Client specific HTTP entity implementations. + */ +package ch.boye.httpclientandroidlib.client.entity; |