diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2008-10-21 07:00:00 -0700 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2008-10-21 07:00:00 -0700 |
commit | 417f3b92ba4549b2f22340e3107d869d2b9c5bb8 (patch) | |
tree | 2e08a2a91d6d14995df54490e3667f7943fbc6d6 /src/org/apache/http/entity | |
download | external_apache-http-417f3b92ba4549b2f22340e3107d869d2b9c5bb8.zip external_apache-http-417f3b92ba4549b2f22340e3107d869d2b9c5bb8.tar.gz external_apache-http-417f3b92ba4549b2f22340e3107d869d2b9c5bb8.tar.bz2 |
Initial Contribution
Diffstat (limited to 'src/org/apache/http/entity')
-rw-r--r-- | src/org/apache/http/entity/AbstractHttpEntity.java | 213 | ||||
-rw-r--r-- | src/org/apache/http/entity/BasicHttpEntity.java | 146 | ||||
-rw-r--r-- | src/org/apache/http/entity/BufferedHttpEntity.java | 120 | ||||
-rw-r--r-- | src/org/apache/http/entity/ByteArrayEntity.java | 94 | ||||
-rw-r--r-- | src/org/apache/http/entity/ContentLengthStrategy.java | 54 | ||||
-rw-r--r-- | src/org/apache/http/entity/ContentProducer.java | 53 | ||||
-rw-r--r-- | src/org/apache/http/entity/EntityTemplate.java | 86 | ||||
-rw-r--r-- | src/org/apache/http/entity/FileEntity.java | 106 | ||||
-rw-r--r-- | src/org/apache/http/entity/HttpEntityWrapper.java | 113 | ||||
-rw-r--r-- | src/org/apache/http/entity/InputStreamEntity.java | 116 | ||||
-rw-r--r-- | src/org/apache/http/entity/SerializableEntity.java | 107 | ||||
-rw-r--r-- | src/org/apache/http/entity/StringEntity.java | 106 | ||||
-rw-r--r-- | src/org/apache/http/entity/package.html | 58 |
13 files changed, 1372 insertions, 0 deletions
diff --git a/src/org/apache/http/entity/AbstractHttpEntity.java b/src/org/apache/http/entity/AbstractHttpEntity.java new file mode 100644 index 0000000..0fce6eb --- /dev/null +++ b/src/org/apache/http/entity/AbstractHttpEntity.java @@ -0,0 +1,213 @@ +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/AbstractHttpEntity.java $ + * $Revision: 496070 $ + * $Date: 2007-01-14 04:18:34 -0800 (Sun, 14 Jan 2007) $ + * + * ==================================================================== + * 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 org.apache.http.entity; + +import java.io.IOException; + +import org.apache.http.Header; +import org.apache.http.HttpEntity; +import org.apache.http.message.BasicHeader; +import org.apache.http.protocol.HTTP; + +/** + * Abstract base class for entities. + * Provides the commonly used attributes for streamed and self-contained + * implementations of {@link HttpEntity HttpEntity}. + * + * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> + * + * @version $Revision: 496070 $ + * + * @since 4.0 + */ +public abstract class AbstractHttpEntity implements HttpEntity { + + /** + * The Content-Type header. + * Returned by {@link #getContentType getContentType}, + * unless that method is overridden. + */ + protected Header contentType; + + /** + * The Content-Encoding header. + * Returned by {@link #getContentEncoding getContentEncoding}, + * unless that method is overridden. + */ + protected Header contentEncoding; + + /** + * The 'chunked' flag. + * Returned by {@link #isChunked isChunked}, + * unless that method is overridden. + */ + protected boolean chunked; + + + /** + * Protected default constructor. + * The attributes of the created object remain + * <code>null</code> and <code>false</code>, respectively. + */ + protected AbstractHttpEntity() { + super(); + } + + + /** + * Obtains the Content-Type header. + * The default implementation returns the value of the + * {@link #contentType contentType} attribute. + * + * @return the Content-Type header, or <code>null</code> + */ + public Header getContentType() { + return this.contentType; + } + + + /** + * Obtains the Content-Encoding header. + * The default implementation returns the value of the + * {@link #contentEncoding contentEncoding} attribute. + * + * @return the Content-Encoding header, or <code>null</code> + */ + public Header getContentEncoding() { + return this.contentEncoding; + } + + /** + * Obtains the 'chunked' flag. + * The default implementation returns the value of the + * {@link #chunked chunked} attribute. + * + * @return the 'chunked' flag + */ + public boolean isChunked() { + return this.chunked; + } + + + /** + * Specifies the Content-Type header. + * The default implementation sets the value of the + * {@link #contentType contentType} attribute. + * + * @param contentType the new Content-Encoding header, or + * <code>null</code> to unset + */ + public void setContentType(final Header contentType) { + this.contentType = contentType; + } + + /** + * Specifies the Content-Type header, as a string. + * The default implementation calls + * {@link #setContentType(Header) setContentType(Header)}. + * + * @param ctString the new Content-Type header, or + * <code>null</code> to unset + */ + public void setContentType(final String ctString) { + Header h = null; + if (ctString != null) { + h = new BasicHeader(HTTP.CONTENT_TYPE, ctString); + } + setContentType(h); + } + + + /** + * Specifies the Content-Encoding header. + * The default implementation sets the value of the + * {@link #contentEncoding contentEncoding} attribute. + * + * @param contentEncoding the new Content-Encoding header, or + * <code>null</code> to unset + */ + public void setContentEncoding(final Header contentEncoding) { + this.contentEncoding = contentEncoding; + } + + /** + * Specifies the Content-Encoding header, as a string. + * The default implementation calls + * {@link #setContentEncoding(Header) setContentEncoding(Header)}. + * + * @param ceString the new Content-Encoding header, or + * <code>null</code> to unset + */ + public void setContentEncoding(final String ceString) { + Header h = null; + if (ceString != null) { + h = new BasicHeader(HTTP.CONTENT_ENCODING, ceString); + } + setContentEncoding(h); + } + + + /** + * Specifies the 'chunked' flag. + * The default implementation sets the value of the + * {@link #chunked chunked} attribute. + * + * @param b the new 'chunked' flag + */ + public void setChunked(boolean b) { + this.chunked = b; + } + + + /** + * Does not consume anything. + * The default implementation does nothing if + * {@link HttpEntity#isStreaming isStreaming} + * returns <code>false</code>, and throws an exception + * if it returns <code>true</code>. + * This removes the burden of implementing + * an empty method for non-streaming entities. + * + * @throws IOException in case of an I/O problem + * @throws UnsupportedOperationException + * if a streaming subclass does not override this method + */ + public void consumeContent() + throws IOException, UnsupportedOperationException{ + if (isStreaming()) { + throw new UnsupportedOperationException + ("streaming entity does not implement consumeContent()"); + } + } // consumeContent + + +} // class AbstractHttpEntity diff --git a/src/org/apache/http/entity/BasicHttpEntity.java b/src/org/apache/http/entity/BasicHttpEntity.java new file mode 100644 index 0000000..df3c07c --- /dev/null +++ b/src/org/apache/http/entity/BasicHttpEntity.java @@ -0,0 +1,146 @@ +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/BasicHttpEntity.java $ + * $Revision: 496070 $ + * $Date: 2007-01-14 04:18:34 -0800 (Sun, 14 Jan 2007) $ + * + * ==================================================================== + * 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 org.apache.http.entity; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * A generic streamed entity being received on a connection. + * + * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> + * + * @version $Revision: 496070 $ + * + * @since 4.0 + */ +public class BasicHttpEntity extends AbstractHttpEntity { + + private InputStream content; + private boolean contentObtained; + private long length; + + /** + * Creates a new basic entity. + * The content is initially missing, the content length + * is set to a negative number. + */ + public BasicHttpEntity() { + super(); + this.length = -1; + } + + // non-javadoc, see interface HttpEntity + public long getContentLength() { + return this.length; + } + + /** + * Obtains the content, once only. + * + * @return the content, if this is the first call to this method + * since {@link #setContent setContent} has been called + * + * @throws IllegalStateException + * if the content has been obtained before, or + * has not yet been provided + */ + public InputStream getContent() + throws IllegalStateException { + if (this.content == null) { + throw new IllegalStateException("Content has not been provided"); + } + if (this.contentObtained) { + throw new IllegalStateException("Content has been consumed"); + } + this.contentObtained = true; + return this.content; + + } // getContent + + /** + * Tells that this entity is not repeatable. + * + * @return <code>false</code> + */ + public boolean isRepeatable() { + return false; + } + + /** + * Specifies the length of the content. + * + * @param len the number of bytes in the content, or + * a negative number to indicate an unknown length + */ + public void setContentLength(long len) { + this.length = len; + } + + /** + * Specifies the content. + * + * @param instream the stream to return with the next call to + * {@link #getContent getContent} + */ + public void setContent(final InputStream instream) { + this.content = instream; + this.contentObtained = false; + } + + // non-javadoc, see interface HttpEntity + public void writeTo(final OutputStream outstream) throws IOException { + if (outstream == null) { + throw new IllegalArgumentException("Output stream may not be null"); + } + InputStream instream = getContent(); + int l; + byte[] tmp = new byte[2048]; + while ((l = instream.read(tmp)) != -1) { + outstream.write(tmp, 0, l); + } + } + + // non-javadoc, see interface HttpEntity + public boolean isStreaming() { + return !this.contentObtained && this.content != null; + } + + // non-javadoc, see interface HttpEntity + public void consumeContent() throws IOException { + if (content != null) { + content.close(); // reads to the end of the entity + } + } + +} // class BasicHttpEntity diff --git a/src/org/apache/http/entity/BufferedHttpEntity.java b/src/org/apache/http/entity/BufferedHttpEntity.java new file mode 100644 index 0000000..9888797 --- /dev/null +++ b/src/org/apache/http/entity/BufferedHttpEntity.java @@ -0,0 +1,120 @@ +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/BufferedHttpEntity.java $ + * $Revision: 496070 $ + * $Date: 2007-01-14 04:18:34 -0800 (Sun, 14 Jan 2007) $ + * + * ==================================================================== + * 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 org.apache.http.entity; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.apache.http.HttpEntity; +import org.apache.http.util.EntityUtils; + +/** + * A wrapping entity that buffers it content if necessary. + * The buffered entity is always repeatable. + * If the wrapped entity is repeatable itself, calls are passed through. + * If the wrapped entity is not repeatable, the content is read into a + * buffer once and provided from there as often as required. + * + * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> + * + * @version $Revision: 496070 $ + * + * @since 4.0 + */ +public class BufferedHttpEntity extends HttpEntityWrapper { + + private final byte[] buffer; + + public BufferedHttpEntity(final HttpEntity entity) throws IOException { + super(entity); + if (!entity.isRepeatable() || entity.getContentLength() < 0) { + this.buffer = EntityUtils.toByteArray(entity); + } else { + this.buffer = null; + } + } + + public long getContentLength() { + if (this.buffer != null) { + return this.buffer.length; + } else { + return wrappedEntity.getContentLength(); + } + } + + public InputStream getContent() throws IOException { + if (this.buffer != null) { + return new ByteArrayInputStream(this.buffer); + } else { + return wrappedEntity.getContent(); + } + } + + /** + * Tells that this entity does not have to be chunked. + * + * @return <code>false</code> + */ + public boolean isChunked() { + return (buffer == null) && wrappedEntity.isChunked(); + } + + /** + * Tells that this entity is repeatable. + * + * @return <code>true</code> + */ + public boolean isRepeatable() { + return true; + } + + + public void writeTo(final OutputStream outstream) throws IOException { + if (outstream == null) { + throw new IllegalArgumentException("Output stream may not be null"); + } + if (this.buffer != null) { + outstream.write(this.buffer); + } else { + wrappedEntity.writeTo(outstream); + } + } + + + // non-javadoc, see interface HttpEntity + public boolean isStreaming() { + return (buffer == null) && wrappedEntity.isStreaming(); + } + +} // class BufferedHttpEntity diff --git a/src/org/apache/http/entity/ByteArrayEntity.java b/src/org/apache/http/entity/ByteArrayEntity.java new file mode 100644 index 0000000..c7257f7 --- /dev/null +++ b/src/org/apache/http/entity/ByteArrayEntity.java @@ -0,0 +1,94 @@ +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/ByteArrayEntity.java $ + * $Revision: 604625 $ + * $Date: 2007-12-16 06:11:11 -0800 (Sun, 16 Dec 2007) $ + * + * ==================================================================== + * 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 org.apache.http.entity; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * An entity whose content is retrieved from a byte array. + * + * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> + * + * @version $Revision: 604625 $ + * + * @since 4.0 + */ +public class ByteArrayEntity extends AbstractHttpEntity implements Cloneable { + + protected final byte[] content; + + public ByteArrayEntity(final byte[] b) { + super(); + if (b == null) { + throw new IllegalArgumentException("Source byte array may not be null"); + } + this.content = b; + } + + public boolean isRepeatable() { + return true; + } + + public long getContentLength() { + return this.content.length; + } + + public InputStream getContent() { + return new ByteArrayInputStream(this.content); + } + + public void writeTo(final OutputStream outstream) throws IOException { + if (outstream == null) { + throw new IllegalArgumentException("Output stream may not be null"); + } + outstream.write(this.content); + outstream.flush(); + } + + + /** + * Tells that this entity is not streaming. + * + * @return <code>false</code> + */ + public boolean isStreaming() { + return false; + } + + public Object clone() throws CloneNotSupportedException { + return super.clone(); + } + +} // class ByteArrayEntity diff --git a/src/org/apache/http/entity/ContentLengthStrategy.java b/src/org/apache/http/entity/ContentLengthStrategy.java new file mode 100644 index 0000000..cc4ab7d --- /dev/null +++ b/src/org/apache/http/entity/ContentLengthStrategy.java @@ -0,0 +1,54 @@ +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/ContentLengthStrategy.java $ + * $Revision: 613298 $ + * $Date: 2008-01-18 14:09:22 -0800 (Fri, 18 Jan 2008) $ + * + * ==================================================================== + * 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 org.apache.http.entity; + +import org.apache.http.HttpException; +import org.apache.http.HttpMessage; + +/** + * Represents a strategy to determine the content length based on the properties + * of an HTTP message. + * + * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> + * + * @version $Revision: 613298 $ + * + * @since 4.0 + */ +public interface ContentLengthStrategy { + + public static final int IDENTITY = -1; + public static final int CHUNKED = -2; + + long determineLength(HttpMessage message) throws HttpException; + +} diff --git a/src/org/apache/http/entity/ContentProducer.java b/src/org/apache/http/entity/ContentProducer.java new file mode 100644 index 0000000..456eae3 --- /dev/null +++ b/src/org/apache/http/entity/ContentProducer.java @@ -0,0 +1,53 @@ +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/ContentProducer.java $ + * $Revision: 496070 $ + * $Date: 2007-01-14 04:18:34 -0800 (Sun, 14 Jan 2007) $ + * + * ==================================================================== + * 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 org.apache.http.entity; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * An abstract entity content producer. + * + *<p>Content producers are expected to be able to produce their + * content multiple times</p> + * + * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> + * + * @version $Revision: 496070 $ + * + * @since 4.0 + */ +public interface ContentProducer { + + void writeTo(OutputStream outstream) throws IOException; + +} diff --git a/src/org/apache/http/entity/EntityTemplate.java b/src/org/apache/http/entity/EntityTemplate.java new file mode 100644 index 0000000..0c6002e --- /dev/null +++ b/src/org/apache/http/entity/EntityTemplate.java @@ -0,0 +1,86 @@ +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/EntityTemplate.java $ + * $Revision: 496070 $ + * $Date: 2007-01-14 04:18:34 -0800 (Sun, 14 Jan 2007) $ + * + * ==================================================================== + * 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 org.apache.http.entity; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * Entity that delegates the process of content generation to an abstract + * content producer. + * + * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> + * + * @version $Revision: 496070 $ + * + * @since 4.0 + */ +public class EntityTemplate extends AbstractHttpEntity { + + private final ContentProducer contentproducer; + + public EntityTemplate(final ContentProducer contentproducer) { + super(); + if (contentproducer == null) { + throw new IllegalArgumentException("Content producer may not be null"); + } + this.contentproducer = contentproducer; + } + + public long getContentLength() { + return -1; + } + + public InputStream getContent() { + throw new UnsupportedOperationException("Entity template does not implement getContent()"); + } + + public boolean isRepeatable() { + return true; + } + + public void writeTo(final OutputStream outstream) throws IOException { + if (outstream == null) { + throw new IllegalArgumentException("Output stream may not be null"); + } + this.contentproducer.writeTo(outstream); + } + + public boolean isStreaming() { + return true; + } + + public void consumeContent() throws IOException { + } + +} diff --git a/src/org/apache/http/entity/FileEntity.java b/src/org/apache/http/entity/FileEntity.java new file mode 100644 index 0000000..a991058 --- /dev/null +++ b/src/org/apache/http/entity/FileEntity.java @@ -0,0 +1,106 @@ +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/FileEntity.java $ + * $Revision: 604625 $ + * $Date: 2007-12-16 06:11:11 -0800 (Sun, 16 Dec 2007) $ + * + * ==================================================================== + * 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 org.apache.http.entity; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * An entity whose content is retrieved from a file. + * + * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> + * + * @version $Revision: 604625 $ + * + * @since 4.0 + */ +public class FileEntity extends AbstractHttpEntity implements Cloneable { + + protected final File file; + + public FileEntity(final File file, final String contentType) { + super(); + if (file == null) { + throw new IllegalArgumentException("File may not be null"); + } + this.file = file; + setContentType(contentType); + } + + public boolean isRepeatable() { + return true; + } + + public long getContentLength() { + return this.file.length(); + } + + public InputStream getContent() throws IOException { + return new FileInputStream(this.file); + } + + public void writeTo(final OutputStream outstream) throws IOException { + if (outstream == null) { + throw new IllegalArgumentException("Output stream may not be null"); + } + InputStream instream = new FileInputStream(this.file); + try { + byte[] tmp = new byte[4096]; + int l; + while ((l = instream.read(tmp)) != -1) { + outstream.write(tmp, 0, l); + } + outstream.flush(); + } finally { + instream.close(); + } + } + + /** + * Tells that this entity is not streaming. + * + * @return <code>false</code> + */ + public boolean isStreaming() { + return false; + } + + public Object clone() throws CloneNotSupportedException { + // File instance is considered immutable + // No need to make a copy of it + return super.clone(); + } + +} // class FileEntity diff --git a/src/org/apache/http/entity/HttpEntityWrapper.java b/src/org/apache/http/entity/HttpEntityWrapper.java new file mode 100644 index 0000000..17a4149 --- /dev/null +++ b/src/org/apache/http/entity/HttpEntityWrapper.java @@ -0,0 +1,113 @@ +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/HttpEntityWrapper.java $ + * $Revision: 496070 $ + * $Date: 2007-01-14 04:18:34 -0800 (Sun, 14 Jan 2007) $ + * + * ==================================================================== + * 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 org.apache.http.entity; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.apache.http.Header; +import org.apache.http.HttpEntity; + +/** + * Base class for wrapping entities. + * Keeps a {@link #wrappedEntity wrappedEntity} and delegates all + * calls to it. Implementations of wrapping entities can derive + * from this class and need to override only those methods that + * should not be delegated to the wrapped entity. + * + * @version $Revision: 496070 $ + * + * @since 4.0 + */ +public class HttpEntityWrapper implements HttpEntity { + + /** The wrapped entity. */ + protected HttpEntity wrappedEntity; + + /** + * Creates a new entity wrapper. + * + * @param wrapped the entity to wrap + */ + public HttpEntityWrapper(HttpEntity wrapped) { + super(); + + if (wrapped == null) { + throw new IllegalArgumentException + ("wrapped entity must not be null"); + } + wrappedEntity = wrapped; + + } // constructor + + + public boolean isRepeatable() { + return wrappedEntity.isRepeatable(); + } + + public boolean isChunked() { + return wrappedEntity.isChunked(); + } + + public long getContentLength() { + return wrappedEntity.getContentLength(); + } + + public Header getContentType() { + return wrappedEntity.getContentType(); + } + + public Header getContentEncoding() { + return wrappedEntity.getContentEncoding(); + } + + public InputStream getContent() + throws IOException { + return wrappedEntity.getContent(); + } + + public void writeTo(OutputStream outstream) + throws IOException { + wrappedEntity.writeTo(outstream); + } + + public boolean isStreaming() { + return wrappedEntity.isStreaming(); + } + + public void consumeContent() + throws IOException { + wrappedEntity.consumeContent(); + } + +} // class HttpEntityWrapper diff --git a/src/org/apache/http/entity/InputStreamEntity.java b/src/org/apache/http/entity/InputStreamEntity.java new file mode 100644 index 0000000..6d33fe4 --- /dev/null +++ b/src/org/apache/http/entity/InputStreamEntity.java @@ -0,0 +1,116 @@ +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/InputStreamEntity.java $ + * $Revision: 617591 $ + * $Date: 2008-02-01 10:21:17 -0800 (Fri, 01 Feb 2008) $ + * + * ==================================================================== + * 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 org.apache.http.entity; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * A streamed entity obtaining content from an {@link InputStream InputStream}. + * + * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> + * + * @version $Revision: 617591 $ + * + * @since 4.0 + */ +public class InputStreamEntity extends AbstractHttpEntity { + + private final static int BUFFER_SIZE = 2048; + + private final InputStream content; + private final long length; + private boolean consumed = false; + + public InputStreamEntity(final InputStream instream, long length) { + super(); + if (instream == null) { + throw new IllegalArgumentException("Source input stream may not be null"); + } + this.content = instream; + this.length = length; + } + + public boolean isRepeatable() { + return false; + } + + public long getContentLength() { + return this.length; + } + + public InputStream getContent() throws IOException { + return this.content; + } + + public void writeTo(final OutputStream outstream) throws IOException { + if (outstream == null) { + throw new IllegalArgumentException("Output stream may not be null"); + } + InputStream instream = this.content; + byte[] buffer = new byte[BUFFER_SIZE]; + int l; + if (this.length < 0) { + // consume until EOF + while ((l = instream.read(buffer)) != -1) { + outstream.write(buffer, 0, l); + } + } else { + // consume no more than length + long remaining = this.length; + while (remaining > 0) { + l = instream.read(buffer, 0, (int)Math.min(BUFFER_SIZE, remaining)); + if (l == -1) { + break; + } + outstream.write(buffer, 0, l); + remaining -= l; + } + } + this.consumed = true; + } + + // non-javadoc, see interface HttpEntity + public boolean isStreaming() { + return !this.consumed; + } + + // non-javadoc, see interface HttpEntity + public void consumeContent() throws IOException { + this.consumed = true; + // If the input stream is from a connection, closing it will read to + // the end of the content. Otherwise, we don't care what it does. + this.content.close(); + } + +} // class InputStreamEntity diff --git a/src/org/apache/http/entity/SerializableEntity.java b/src/org/apache/http/entity/SerializableEntity.java new file mode 100644 index 0000000..171977b --- /dev/null +++ b/src/org/apache/http/entity/SerializableEntity.java @@ -0,0 +1,107 @@ +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/SerializableEntity.java $ + * $Revision: 647816 $ + * $Date: 2008-04-14 07:37:13 -0700 (Mon, 14 Apr 2008) $ + * + * ==================================================================== + * 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 org.apache.http.entity; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectOutputStream; +import java.io.OutputStream; +import java.io.Serializable; + +public class SerializableEntity extends AbstractHttpEntity { + + private byte[] objSer; + + private Serializable objRef; + + public SerializableEntity(Serializable ser, boolean bufferize) throws IOException { + super(); + if (ser == null) { + throw new IllegalArgumentException("Source object may not be null"); + } + + if (bufferize) { + createBytes(ser); + } else { + this.objRef = ser; + } + } + + private void createBytes(Serializable ser) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ObjectOutputStream out = new ObjectOutputStream(baos); + out.writeObject(ser); + out.flush(); + this.objSer = baos.toByteArray(); + } + + public InputStream getContent() throws IOException, IllegalStateException { + if (this.objSer == null) { + createBytes(this.objRef); + } + return new ByteArrayInputStream(this.objSer); + } + + public long getContentLength() { + if (this.objSer == null) { + return -1; + } else { + return this.objSer.length; + } + } + + public boolean isRepeatable() { + return true; + } + + public boolean isStreaming() { + return this.objSer == null; + } + + public void writeTo(OutputStream outstream) throws IOException { + if (outstream == null) { + throw new IllegalArgumentException("Output stream may not be null"); + } + + if (this.objSer == null) { + ObjectOutputStream out = new ObjectOutputStream(outstream); + out.writeObject(this.objRef); + out.flush(); + } else { + outstream.write(this.objSer); + outstream.flush(); + } + } + +} diff --git a/src/org/apache/http/entity/StringEntity.java b/src/org/apache/http/entity/StringEntity.java new file mode 100644 index 0000000..cbc382b --- /dev/null +++ b/src/org/apache/http/entity/StringEntity.java @@ -0,0 +1,106 @@ +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/StringEntity.java $ + * $Revision: 618367 $ + * $Date: 2008-02-04 10:26:06 -0800 (Mon, 04 Feb 2008) $ + * + * ==================================================================== + * 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 org.apache.http.entity; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; + +import org.apache.http.protocol.HTTP; + +/** + * An entity whose content is retrieved from a string. + * + * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a> + * + * @version $Revision: 618367 $ + * + * @since 4.0 + */ +public class StringEntity extends AbstractHttpEntity implements Cloneable { + + protected final byte[] content; + + public StringEntity(final String s, String charset) + throws UnsupportedEncodingException { + super(); + if (s == null) { + throw new IllegalArgumentException("Source string may not be null"); + } + if (charset == null) { + charset = HTTP.DEFAULT_CONTENT_CHARSET; + } + this.content = s.getBytes(charset); + setContentType(HTTP.PLAIN_TEXT_TYPE + HTTP.CHARSET_PARAM + charset); + } + + public StringEntity(final String s) + throws UnsupportedEncodingException { + this(s, null); + } + + public boolean isRepeatable() { + return true; + } + + public long getContentLength() { + return this.content.length; + } + + public InputStream getContent() throws IOException { + return new ByteArrayInputStream(this.content); + } + + public void writeTo(final OutputStream outstream) throws IOException { + if (outstream == null) { + throw new IllegalArgumentException("Output stream may not be null"); + } + outstream.write(this.content); + outstream.flush(); + } + + /** + * Tells that this entity is not streaming. + * + * @return <code>false</code> + */ + public boolean isStreaming() { + return false; + } + + public Object clone() throws CloneNotSupportedException { + return super.clone(); + } + +} // class StringEntity diff --git a/src/org/apache/http/entity/package.html b/src/org/apache/http/entity/package.html new file mode 100644 index 0000000..11d491e --- /dev/null +++ b/src/org/apache/http/entity/package.html @@ -0,0 +1,58 @@ +<html> +<head> +<!-- +/* + * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/entity/package.html $ + * $Revision: 496070 $ + * $Date: 2007-01-14 04:18:34 -0800 (Sun, 14 Jan 2007) $ + * + * ==================================================================== + * 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/>. + * + */ +--> +</head> +<body> +Representations for HTTP message entities. + +An {@link org.apache.http.HttpEntity entity} is the optional content of a +{@link org.apache.http.HttpMessage message}. +You'll find a basic selection of entity implementations here. +If you need to send an entity, you can provide it for example as a +{@link org.apache.http.entity.ByteArrayEntity byte array}, +{@link org.apache.http.entity.StringEntity string}, +{@link org.apache.http.entity.FileEntity file}, or through an arbitrary +{@link org.apache.http.entity.InputStreamEntity input stream}. +If you receive a message with an entity, you typically get that as a +{@link org.apache.http.entity.BasicHttpEntity basic} entity. +Entity implementations can be +{@link org.apache.http.entity.HttpEntityWrapper wrapped}, +for example to +{@link org.apache.http.entity.BufferedHttpEntity buffer} +the content in memory. + + + +</body> +</html> |