org.jets3t.service.impl.rest.httpclient
Class RepeatableRequestEntity

java.lang.Object
  extended by org.jets3t.service.impl.rest.httpclient.RepeatableRequestEntity
All Implemented Interfaces:
org.apache.http.HttpEntity

public class RepeatableRequestEntity
extends java.lang.Object
implements org.apache.http.HttpEntity

An HttpClient request entity whose underlying data can be re-read (that is, repeated) if necessary to retry failed transmissions. This class also provides basic byte-rate throttling by throttling the reading of request bodies, the throttling value is set with the JetS3t property httpclient.read-throttle. If Logging is enabled for this class the MD5 hash values (Base64 and Hex) are logged after all data has been written to the output stream.

This class works by taking advantage of the reset capability of the original data input stream, or by wrapping the input stream in a reset-able class if it is not so capable.

When data is repeated, any attached ProgressMonitoredInputStream is notified that a repeat transmission is occurring.


Field Summary
static int DEFAULT_BUFFER_SIZE
           
protected static long MAX_BYTES_PER_SECOND
           
protected  boolean mChunked
           
protected  org.apache.http.Header mContentEncoding
           
 
Constructor Summary
RepeatableRequestEntity(java.lang.String name, java.io.InputStream is, java.lang.String contentType, long contentLength, Jets3tProperties jets3tProperties, boolean enableLiveMD5Hashing)
          Creates a repeatable request entity for the input stream provided.
 
Method Summary
 void consumeContent()
           
 java.io.InputStream getContent()
           
 org.apache.http.Header getContentEncoding()
           
 long getContentLength()
           
 org.apache.http.Header getContentType()
           
 byte[] getMD5DigestOfData()
           
 boolean isChunked()
           
 boolean isRepeatable()
           
 boolean isStreaming()
           
protected static void throttle(int bytesToWrite)
          Throttles the speed at which data is written by this request entity to the maximum rate in KB/s specified by MAX_BYTES_PER_SECOND.
 void writeTo(java.io.OutputStream out)
          Writes the request to the output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_BUFFER_SIZE

public static final int DEFAULT_BUFFER_SIZE
See Also:
Constant Field Values

MAX_BYTES_PER_SECOND

protected static long MAX_BYTES_PER_SECOND

mContentEncoding

protected org.apache.http.Header mContentEncoding

mChunked

protected boolean mChunked
Constructor Detail

RepeatableRequestEntity

public RepeatableRequestEntity(java.lang.String name,
                               java.io.InputStream is,
                               java.lang.String contentType,
                               long contentLength,
                               Jets3tProperties jets3tProperties,
                               boolean enableLiveMD5Hashing)
Creates a repeatable request entity for the input stream provided.

If the input stream provided, or any underlying wrapped input streams, supports the InputStream.reset() method then it will be capable of repeating data transmission. If the input stream provided does not supports this method, it will automatically be wrapped in a RepeatableInputStream -- in this case, the data read from the wrapped input stream will be buffered up to the limit set by the JetS3t property uploads.stream-retry-buffer-size (default: 131072 bytes).

This constructor also detects when an underlying ProgressMonitoredInputStream is present, and will notify this monitor if a repeat occurs.

If the JetS3t properties option httpclient.read-throttle is set to a non-zero value, all simultaneous uploads performed by this class will be throttled to the specified speed.

Parameters:
name -
is - the input stream that supplies the data to be made repeatable.
contentType -
contentLength -
enableLiveMD5Hashing - if true, data that passes through the object will be hashed to an MD5 digest and this digest will be available from getMD5DigestOfData(). If false, the digest will not be calculated.
Method Detail

getContentEncoding

public org.apache.http.Header getContentEncoding()
Specified by:
getContentEncoding in interface org.apache.http.HttpEntity

isChunked

public boolean isChunked()
Specified by:
isChunked in interface org.apache.http.HttpEntity

getContent

public java.io.InputStream getContent()
Specified by:
getContent in interface org.apache.http.HttpEntity

consumeContent

public void consumeContent()
Specified by:
consumeContent in interface org.apache.http.HttpEntity

isStreaming

public boolean isStreaming()
Specified by:
isStreaming in interface org.apache.http.HttpEntity
Returns:
Return true if entity content has not yet been consumed.

getContentLength

public long getContentLength()
Specified by:
getContentLength in interface org.apache.http.HttpEntity

getContentType

public org.apache.http.Header getContentType()
Specified by:
getContentType in interface org.apache.http.HttpEntity

isRepeatable

public boolean isRepeatable()
Specified by:
isRepeatable in interface org.apache.http.HttpEntity
Returns:
always returns true. If the input stream is not actually repeatable, an IOException will be thrown later by the writeTo(OutputStream) method when the repeat is attempted.

writeTo

public void writeTo(java.io.OutputStream out)
             throws java.io.IOException
Writes the request to the output stream. If the request is being repeated, the underlying repeatable input stream will be reset with a call to InputStream.reset().

If a ProgressMonitoredInputStream is attached, this monitor will be notified that data is being repeated by being reset with ProgressMonitoredInputStream.resetProgressMonitor().

Specified by:
writeTo in interface org.apache.http.HttpEntity
Parameters:
out -
Throws:
java.io.IOException

getMD5DigestOfData

public byte[] getMD5DigestOfData()
Returns:
The MD5 digest of the data transmitted by this RequestEntity.

throttle

protected static void throttle(int bytesToWrite)
                        throws java.io.IOException
Throttles the speed at which data is written by this request entity to the maximum rate in KB/s specified by MAX_BYTES_PER_SECOND. The method works by repeatedly delaying its completion until writing the requested number of bytes will not exceed the imposed limit for the current second. The delay imposed each time the completion is deferred is a random value between 0-250ms.

This method is static and is shared by all instances of this class, so the byte rate limit applies for all currently active RepeatableRequestEntity instances.

Parameters:
bytesToWrite - the count of bytes that will be written once this method returns.
Throws:
java.io.IOException - an exception is thrown if the sleep delay is interrupted.