| Method from org.apache.commons.httpclient.methods.EntityEnclosingMethod Detail: |
protected void addContentLengthRequestHeader(HttpState state,
HttpConnection conn) throws IOException, HttpException {
LOG.trace("enter EntityEnclosingMethod.addContentLengthRequestHeader("
+ "HttpState, HttpConnection)");
if ((getRequestHeader("content-length") == null)
&& (getRequestHeader("Transfer-Encoding") == null)) {
long len = getRequestContentLength();
if (len < 0) {
if (getEffectiveVersion().greaterEquals(HttpVersion.HTTP_1_1)) {
addRequestHeader("Transfer-Encoding", "chunked");
} else {
throw new ProtocolException(getEffectiveVersion() +
" does not support chunk encoding");
}
} else {
addRequestHeader("Content-Length", String.valueOf(len));
}
}
}
Generates Content-Length or Transfer-Encoding: Chunked
request header, as long as no Content-Length request header
already exists. |
protected void addRequestHeaders(HttpState state,
HttpConnection conn) throws IOException, HttpException {
LOG.trace("enter EntityEnclosingMethod.addRequestHeaders(HttpState, "
+ "HttpConnection)");
super.addRequestHeaders(state, conn);
addContentLengthRequestHeader(state, conn);
// only use the content type of the request entity if it has not already been
// set manually
if (getRequestHeader("Content-Type") == null) {
RequestEntity requestEntity = getRequestEntity();
if (requestEntity != null && requestEntity.getContentType() != null) {
setRequestHeader("Content-Type", requestEntity.getContentType());
}
}
}
Populates the request headers map to with additional
headers to be submitted to
the given HttpConnection .
This implementation adds tt>Content-Length or Transfer-Encoding
headers.
Subclasses may want to override this method to to add additional
headers, and may choose to invoke this implementation (via
super) to add the "standard" headers.
|
protected void clearRequestBody() {
LOG.trace("enter EntityEnclosingMethod.clearRequestBody()");
this.requestStream = null;
this.requestString = null;
this.requestEntity = null;
}
Clears the request body.
This method must be overridden by sub-classes that implement
alternative request content input methods. |
protected byte[] generateRequestBody() {
LOG.trace("enter EntityEnclosingMethod.renerateRequestBody()");
return null;
}
Generates the request body.
This method must be overridden by sub-classes that implement
alternative request content input methods. |
protected RequestEntity generateRequestEntity() {
byte[] requestBody = generateRequestBody();
if (requestBody != null) {
// use the request body, if it exists.
// this is just for backwards compatability
this.requestEntity = new ByteArrayRequestEntity(requestBody);
} else if (this.requestStream != null) {
this.requestEntity = new InputStreamRequestEntity(
requestStream,
requestContentLength);
this.requestStream = null;
} else if (this.requestString != null) {
String charset = getRequestCharSet();
try {
this.requestEntity = new StringRequestEntity(
requestString, null, charset);
} catch (UnsupportedEncodingException e) {
if (LOG.isWarnEnabled()) {
LOG.warn(charset + " not supported");
}
try {
this.requestEntity = new StringRequestEntity(
requestString, null, null);
} catch (UnsupportedEncodingException ignore) {
}
}
}
return this.requestEntity;
}
|
public boolean getFollowRedirects() {
return false;
}
Entity enclosing requests cannot be redirected without user intervention
according to RFC 2616. |
public String getRequestCharSet() {
if (getRequestHeader("Content-Type") == null) {
// check the content type from request entity
// We can't call getRequestEntity() since it will probably call
// this method.
if (this.requestEntity != null) {
return getContentCharSet(
new Header("Content-Type", requestEntity.getContentType()));
} else {
return super.getRequestCharSet();
}
} else {
return super.getRequestCharSet();
}
}
Returns the request's charset. The charset is parsed from the request entity's
content type, unless the content type header has been set manually. |
protected long getRequestContentLength() {
LOG.trace("enter EntityEnclosingMethod.getRequestContentLength()");
if (!hasRequestContent()) {
return 0;
}
if (this.chunked) {
return -1;
}
if (this.requestEntity == null) {
this.requestEntity = generateRequestEntity();
}
return (this.requestEntity == null) ? 0 : this.requestEntity.getContentLength();
}
Returns the length of the request body. |
public RequestEntity getRequestEntity() {
return generateRequestEntity();
}
|
protected boolean hasRequestContent() {
LOG.trace("enter EntityEnclosingMethod.hasRequestContent()");
return (this.requestEntity != null)
|| (this.requestStream != null)
|| (this.requestString != null);
}
Returns true if there is a request body to be sent.
This method must be overridden by sub-classes that implement
alternative request content input methods
|
public void recycle() {
LOG.trace("enter EntityEnclosingMethod.recycle()");
clearRequestBody();
this.requestContentLength = InputStreamRequestEntity.CONTENT_LENGTH_AUTO;
this.repeatCount = 0;
this.chunked = false;
super.recycle();
} Deprecated! no - longer supported and will be removed in the future
version of HttpClient
Recycles the HTTP method so that it can be used again.
Note that all of the instance variables will be reset
once this method has been called. This method will also
release the connection being used by this HTTP method. |
public void setContentChunked(boolean chunked) {
this.chunked = chunked;
}
Sets whether or not the content should be chunked. |
public void setFollowRedirects(boolean followRedirects) {
if (followRedirects == true) {
throw new IllegalArgumentException("Entity enclosing requests cannot be redirected without user intervention");
}
super.setFollowRedirects(false);
}
Entity enclosing requests cannot be redirected without user intervention
according to RFC 2616. |
public void setRequestBody(InputStream body) {
LOG.trace("enter EntityEnclosingMethod.setRequestBody(InputStream)");
clearRequestBody();
this.requestStream = body;
} Deprecated! use - #setRequestEntity(RequestEntity)
Sets the request body to be the specified inputstream. |
public void setRequestBody(String body) {
LOG.trace("enter EntityEnclosingMethod.setRequestBody(String)");
clearRequestBody();
this.requestString = body;
} Deprecated! use - #setRequestEntity(RequestEntity)
Sets the request body to be the specified string.
The string will be submitted, using the encoding
specified in the Content-Type request header.
Example: setRequestHeader("Content-type", "text/xml; charset=UTF-8");
Would use the UTF-8 encoding.
If no charset is specified, the
default
content encoding is used (ISO-8859-1). |
public void setRequestContentLength(int length) {
LOG.trace("enter EntityEnclosingMethod.setRequestContentLength(int)");
this.requestContentLength = length;
} Deprecated! Use - #setContentChunked(boolean) or
#setRequestEntity(RequestEntity)
Sets length information about the request body.
Note: If you specify a content length the request is unbuffered. This
prevents redirection and automatic retry if a request fails the first
time. This means that the HttpClient can not perform authorization
automatically but will throw an Exception. You will have to set the
necessary 'Authorization' or 'Proxy-Authorization' headers manually.
|
public void setRequestContentLength(long length) {
LOG.trace("enter EntityEnclosingMethod.setRequestContentLength(int)");
this.requestContentLength = length;
} Deprecated! Use - #setContentChunked(boolean) or
#setRequestEntity(RequestEntity)
Sets length information about the request body.
Note: If you specify a content length the request is unbuffered. This
prevents redirection and automatic retry if a request fails the first
time. This means that the HttpClient can not perform authorization
automatically but will throw an Exception. You will have to set the
necessary 'Authorization' or 'Proxy-Authorization' headers manually.
|
public void setRequestEntity(RequestEntity requestEntity) {
clearRequestBody();
this.requestEntity = requestEntity;
}
|
protected boolean writeRequestBody(HttpState state,
HttpConnection conn) throws IOException, HttpException {
LOG.trace(
"enter EntityEnclosingMethod.writeRequestBody(HttpState, HttpConnection)");
if (!hasRequestContent()) {
LOG.debug("Request body has not been specified");
return true;
}
if (this.requestEntity == null) {
this.requestEntity = generateRequestEntity();
}
if (requestEntity == null) {
LOG.debug("Request body is empty");
return true;
}
long contentLength = getRequestContentLength();
if ((this.repeatCount > 0) && !requestEntity.isRepeatable()) {
throw new ProtocolException(
"Unbuffered entity enclosing request can not be repeated.");
}
this.repeatCount++;
OutputStream outstream = conn.getRequestOutputStream();
if (contentLength < 0) {
outstream = new ChunkedOutputStream(outstream);
}
requestEntity.writeRequest(outstream);
// This is hardly the most elegant solution to closing chunked stream
if (outstream instanceof ChunkedOutputStream) {
((ChunkedOutputStream) outstream).finish();
}
outstream.flush();
LOG.debug("Request body sent");
return true;
}
|