|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.apache.commons.httpclient.HttpMethodBase
An abstract base implementation of HttpMethod.
At minimum, subclasses will need to override:
getName()
to return the approriate name for this method
When a method's request may contain a body, subclasses will typically want to override:
getRequestContentLength()
to indicate the length (in bytes)
of that bodywriteRequestBody(HttpState,HttpConnection)
to write the bodyWhen a method requires additional request headers, subclasses will typically want to override:
addRequestHeaders(HttpState,HttpConnection)
to write those headers
When a method expects specific response headers, subclasses may want to override:
processResponseHeaders(HttpState,HttpConnection)
to handle those headers
Field Summary | |
protected static Header |
USER_AGENT
The User-Agent header sent on every request. |
Constructor Summary | |
HttpMethodBase()
No-arg constructor. |
|
HttpMethodBase(String uri)
Constructor specifying a URI. |
Method Summary | |
protected void |
addAuthorizationRequestHeader(HttpState state,
HttpConnection conn)
Adds an Authorization request if needed, as long as no Authorization request header already exists. |
protected void |
addContentLengthRequestHeader(HttpState state,
HttpConnection conn)
Adds a Content-Length or Transfer-Encoding: Chunked request header, as long as no Content-Length request header already exists. |
protected void |
addCookieRequestHeader(HttpState state,
HttpConnection conn)
Adds a Cookie request containing the matching Cookie s. |
protected void |
addHostRequestHeader(HttpState state,
HttpConnection conn)
Adds a Host request header, as long as no Host request header already exists. |
protected void |
addProxyAuthorizationRequestHeader(HttpState state,
HttpConnection conn)
Adds a Proxy-Authorization request if needed, as long as no Proxy-Authorization request header already exists. |
protected void |
addProxyConnectionHeader(HttpState state,
HttpConnection conn)
Adds a Proxy-Connection: Keep-Alive request when communicating via a proxy server. |
void |
addRequestHeader(Header header)
Add the specified request header. |
void |
addRequestHeader(String headerName,
String headerValue)
Add the specified request header, NOT overwriting any previous value. |
protected void |
addRequestHeaders(HttpState state,
HttpConnection conn)
Populates the request headers map to with additional headers to be submitted to the given HttpConnection . |
void |
addResponseFooter(Header footer)
adds a response footer to the internal list |
protected void |
addUserAgentRequestHeader(HttpState state,
HttpConnection conn)
Adds a default User-Agent request header, as long as no User-Agent request header already exists. |
protected void |
checkNotUsed()
Throws an IllegalStateException if used but not recycled. |
protected void |
checkUsed()
Throws an IllegalStateException if not used since last recycle. |
int |
execute(HttpState state,
HttpConnection conn)
Execute this method. |
protected void |
fakeResponse(StatusLine statusline,
HeaderGroup responseheaders,
InputStream responseStream)
This method is a dirty hack intended to work around current (2.0) design flaw that prevents the user from obtaining correct status code, headers and response body from the preceding HTTP CONNECT method. |
protected static String |
generateRequestLine(HttpConnection connection,
String name,
String requestPath,
String query,
String version)
Generate an HTTP/S request line according to the specified attributes. |
String |
getAuthenticationRealm()
Returns authentication realm, if it has been used during authentication process. |
protected static String |
getContentCharSet(Header contentheader)
Return the character set from the header. |
boolean |
getDoAuthentication()
Whether or not I should automatically process responses where authentication is required (status code 401, etc.) |
boolean |
getFollowRedirects()
Whether or not I should automatically follow HTTP redirects (status code 302, etc.) |
HostConfiguration |
getHostConfiguration()
Returns the hostConfiguration. |
MethodRetryHandler |
getMethodRetryHandler()
|
abstract String |
getName()
Obtain the name of this method, suitable for use in the "request line", for example GET or POST. |
String |
getPath()
Get the path part of my request. |
String |
getProxyAuthenticationRealm()
Returns proxy authentication realm, if it has been used during authentication process. |
String |
getQueryString()
Get my query string. |
int |
getRecoverableExceptionCount()
Returns the number of "recoverable" exceptions thrown and handled, to allow for monitoring the quality of the connection. |
String |
getRequestCharSet()
Return the character set for the request. |
protected int |
getRequestContentLength()
Return the length (in bytes) of my request body, suitable for use in a Content-Length header. |
Header |
getRequestHeader(String headerName)
Get the request header associated with the given name. |
protected HeaderGroup |
getRequestHeaderGroup()
Gets the HeaderGroup storing the request headers. |
Header[] |
getRequestHeaders()
Provides access to the request headers. |
byte[] |
getResponseBody()
Return my response body, if any, as a byte array. |
InputStream |
getResponseBodyAsStream()
Return my response body, if any, as an InputStream . |
String |
getResponseBodyAsString()
Gets the response body as a string. |
String |
getResponseCharSet()
Return the character set for the response. |
protected int |
getResponseContentLength()
Return the length (in bytes) of my response body, as specified in a Content-Length header. |
Header |
getResponseFooter(String footerName)
Get the response footer associated with the given name. |
Header[] |
getResponseFooters()
Gets the response footers in the order in which they were read. |
Header |
getResponseHeader(String headerName)
Get the response header associated with the given name. |
protected HeaderGroup |
getResponseHeaderGroup()
Gets the HeaderGroup storing the response headers. |
Header[] |
getResponseHeaders()
Gets the response headers in the order in which they were read. |
protected InputStream |
getResponseStream()
|
protected HeaderGroup |
getResponseTrailerHeaderGroup()
Gets the HeaderGroup storing the response trailer headers as per RFC 2616 section 3.6.1. |
int |
getStatusCode()
Convenience method top provide access to the status code. |
StatusLine |
getStatusLine()
Provide access to the status line. |
String |
getStatusText()
Provide access to the status text |
URI |
getURI()
Return the URI |
boolean |
hasBeenUsed()
Whether the object has been used and not recycled. |
protected boolean |
isConnectionCloseForced()
Tests if the connection should be force-closed when no longer needed. |
boolean |
isHttp11()
Access to flag to determine if client should use HTTP/1.1 protocol. |
boolean |
isStrictMode()
Returns the value of strictMode. |
protected void |
processResponseBody(HttpState state,
HttpConnection conn)
When this method is invoked, readResponseBody(HttpState,HttpConnection) will have been invoked. |
protected void |
processResponseHeaders(HttpState state,
HttpConnection conn)
When this method is invoked, the response headers map will have been populated with the response headers (in other words, readResponseHeaders(HttpState,HttpConnection)
will have been invoked). |
protected void |
processStatusLine(HttpState state,
HttpConnection conn)
When this method is invoked, the status code and
status text values will have been set (in other
words, readStatusLine(HttpState,HttpConnection
will have been invoked). |
protected void |
readResponse(HttpState state,
HttpConnection conn)
Reads the response from the given HttpConnection . |
protected void |
readResponseBody(HttpState state,
HttpConnection conn)
Read the response body from the given HttpConnection . |
protected void |
readResponseHeaders(HttpState state,
HttpConnection conn)
Read response headers from the given HttpConnection , populating
the response headers map. |
protected void |
readStatusLine(HttpState state,
HttpConnection conn)
Read the status line from the given HttpConnection , setting my
status code and status
text . |
void |
recycle()
Recycle this method so that it can be used again. |
void |
releaseConnection()
Releases the connection being used by this method. |
void |
removeRequestHeader(String headerName)
Remove the request header associated with the given name. |
protected void |
responseBodyConsumed()
A response has been consumed. |
protected void |
setConnectionCloseForced(boolean b)
Sets whether or not the connection should be force-closed when no longer needed. |
void |
setDoAuthentication(boolean doAuthentication)
Set whether or not I should automatically process responses where authentication is required (status code 401, etc.) |
void |
setFollowRedirects(boolean followRedirects)
Set whether or not I should automatically follow HTTP redirects (status code 302, etc.) |
void |
setHostConfiguration(HostConfiguration hostConfiguration)
Sets the hostConfiguration. |
void |
setHttp11(boolean http11)
Set whether or not I should use the HTTP/1.1 protocol. |
void |
setMethodRetryHandler(MethodRetryHandler handler)
|
void |
setPath(String path)
Set the path part of my request. |
void |
setQueryString(NameValuePair[] params)
Sets the query string. |
void |
setQueryString(String queryString)
Sets the query string. |
void |
setRequestHeader(Header header)
Set the specified request header, overwriting any previous value. |
void |
setRequestHeader(String headerName,
String headerValue)
Set the specified request header, overwriting any previous value. |
protected void |
setResponseStream(InputStream responseStream)
Set the response stream. |
void |
setStrictMode(boolean strictMode)
Defines how strictly HttpClient follows the HTTP protocol specification (RFC 2616 and other relevant RFCs). |
protected boolean |
shouldCloseConnection(HttpConnection conn)
Return true if we should close the connection now. |
boolean |
validate()
Confirm that I am ready to execute. |
protected void |
writeRequest(HttpState state,
HttpConnection conn)
Writes my request to the given HttpConnection . |
protected boolean |
writeRequestBody(HttpState state,
HttpConnection conn)
Write the request body to the given HttpConnection . |
protected void |
writeRequestHeaders(HttpState state,
HttpConnection conn)
Writes the request headers to the given HttpConnection . |
protected void |
writeRequestLine(HttpState state,
HttpConnection conn)
Writes the "request line" to the given HttpConnection . |
Methods inherited from class java.lang.Object |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait |
Field Detail |
protected static final Header USER_AGENT
Constructor Detail |
public HttpMethodBase()
public HttpMethodBase(String uri) throws IllegalArgumentException, IllegalStateException
uri
- either an absolute or relative URI. The URI is expected
to be URL-encodedMethod Detail |
public abstract String getName()
public URI getURI() throws URIException
HttpMethod.getURI()
public void setFollowRedirects(boolean followRedirects)
followRedirects
- true to follow redirects, false otherwisepublic boolean getFollowRedirects()
public void setHttp11(boolean http11)
http11
- true to use HTTP/1.1, false to use 1.0public boolean getDoAuthentication()
public void setDoAuthentication(boolean doAuthentication)
doAuthentication
- true to process authenticationspublic boolean isHttp11()
public void setPath(String path)
path
- the path to request. The path is expected
to be URL-encodedpublic void addRequestHeader(Header header)
null
will be ignored. Note that header-name matching is case
insensitive.header
- the header to add to the requestpublic void addResponseFooter(Header footer)
footer
- The new footer to add.public String getPath()
public void setQueryString(String queryString)
queryString
- the query stringEncodingUtil.formUrlEncode(NameValuePair[], String)
public void setQueryString(NameValuePair[] params)
params
- an array of NameValuePair
s to add as query string
parameters. The name/value pairs will be automcatically
URL encodedEncodingUtil.formUrlEncode(NameValuePair[], String)
,
setQueryString(String)
public String getQueryString()
public void setRequestHeader(String headerName, String headerValue)
headerName
- the header's nameheaderValue
- the header's valuepublic void setRequestHeader(Header header)
header
- the headerpublic Header getRequestHeader(String headerName)
headerName
- the header name to matchpublic Header[] getRequestHeaders()
protected HeaderGroup getRequestHeaderGroup()
protected HeaderGroup getResponseTrailerHeaderGroup()
protected HeaderGroup getResponseHeaderGroup()
public int getStatusCode()
public StatusLine getStatusLine()
public Header[] getResponseHeaders()
public Header getResponseHeader(String headerName)
headerName
- the header name to matchprotected int getResponseContentLength()
Return -1 when the content-length is unknown.
public byte[] getResponseBody()
public InputStream getResponseBodyAsStream() throws IOException
InputStream
. Otherwise
return null.InputStream
public String getResponseBodyAsString()
getResponseBody()
to get
the body as an array of bytes and then do your own character encoding.public Header[] getResponseFooters()
public Header getResponseFooter(String footerName)
footerName
- the footer name to matchprotected void setResponseStream(InputStream responseStream)
responseStream
- The new response stream.protected InputStream getResponseStream()
public String getStatusText()
public void setStrictMode(boolean strictMode)
strictMode
- true for strict mode, false otherwisepublic boolean isStrictMode()
public void addRequestHeader(String headerName, String headerValue)
headerName
- the header's nameheaderValue
- the header's valueprotected boolean isConnectionCloseForced()
true
if the connection must be closedprotected void setConnectionCloseForced(boolean b)
true
in abnormal
circumstances.b
- true
if the connection must be closed, false
otherwise.protected boolean shouldCloseConnection(HttpConnection conn)
conn
- the connection in questionpublic int execute(HttpState state, HttpConnection conn) throws HttpException, HttpRecoverableException, IOException, NullPointerException
state
- HttpState
information to associate with this
request. Must be non-null.conn
- theHttpConnection
to write to/read from. Must be
non-null. Note that we cannot currently support redirects that
change the HttpConnection parameters (host, port, protocol)
because we don't yet have a good way to get the new connection.
For the time being, we just return the 302 response, and allow
the user agent to resubmit if desired.public boolean hasBeenUsed()
executed
but not
recycled.public void recycle()
public void releaseConnection()
HttpMethod.releaseConnection()
public void removeRequestHeader(String headerName)
headerName
- the header namepublic boolean validate()
This implementation always returns true.
protected int getRequestContentLength()
Return -1 when the content-length is unknown.
This implementation returns 0, indicating that the request has no body.
protected void addAuthorizationRequestHeader(HttpState state, HttpConnection conn) throws IOException, HttpException
state
- current state of http requestsconn
- the connection to use for I/Oprotected void addContentLengthRequestHeader(HttpState state, HttpConnection conn) throws IOException, HttpException
state
- current state of http requestsconn
- the connection to use for I/Oprotected void addCookieRequestHeader(HttpState state, HttpConnection conn) throws IOException, HttpException
Cookie
s.state
- current state of http requestsconn
- the connection to use for I/Oprotected void addHostRequestHeader(HttpState state, HttpConnection conn) throws IOException, HttpException
state
- current state of http requestsconn
- the connection to use for I/Oprotected void addProxyAuthorizationRequestHeader(HttpState state, HttpConnection conn) throws IOException, HttpException
state
- current state of http requestsconn
- the connection to use for I/Oprotected void addProxyConnectionHeader(HttpState state, HttpConnection conn) throws IOException, HttpException
state
- current state of http requestsconn
- the connection to use for I/Oprotected void addRequestHeaders(HttpState state, HttpConnection conn) throws IOException, HttpException
headers
to be submitted to the given HttpConnection
.
This implementation adds User-Agent, Host, Cookie, Content-Length, Transfer-Encoding, and Authorization headers, when appropriate.
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.
state
- the client stateconn
- the HttpConnection
the headers will eventually be
written towriteRequestHeaders(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection)
protected void addUserAgentRequestHeader(HttpState state, HttpConnection conn) throws IOException, HttpException
state
- the client stateconn
- the HttpConnection
the headers will eventually be
written toprotected void checkNotUsed() throws IllegalStateException
IllegalStateException
if used but not recycled.protected void checkUsed() throws IllegalStateException
IllegalStateException
if not used since last recycle.protected static String generateRequestLine(HttpConnection connection, String name, String requestPath, String query, String version)
connection
- the connection the request will be sent toname
- the method name generate a request forrequestPath
- the path string for the requestquery
- the query string for the requestversion
- the protocol version to use (e.g. HTTP/1.0)protected void processResponseBody(HttpState state, HttpConnection conn)
readResponseBody(HttpState,HttpConnection)
will have been invoked.
This implementation does nothing.
Subclasses may want to override this method.
state
- the client stateconn
- the HttpConnection
to read the response fromreadResponse(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection)
,
readResponseBody(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection)
protected void processResponseHeaders(HttpState state, HttpConnection conn)
readResponseHeaders(HttpState,HttpConnection)
will have been invoked).
This implementation will handle the Set-Cookie and
Set-Cookie2 headers, if any, adding the relevant cookies to
the given HttpState
.
Subclasses may want to override this method to specially process additional headers, and/or invoke this method (via super) to process the Set-Cookie and Set-Cookie2 headers.
state
- the client stateconn
- the HttpConnection
to read the response fromreadResponse(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection)
,
readResponseHeaders(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection)
protected void processStatusLine(HttpState state, HttpConnection conn)
status code
and
status text
values will have been set (in other
words, readStatusLine(HttpState,HttpConnection
will have been invoked).
Subclasses may want to override this method to respond to these value. This implementation does nothing.
state
- the client stateconn
- the HttpConnection
to read the response fromreadResponse(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection)
,
readStatusLine(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection)
protected void readResponse(HttpState state, HttpConnection conn) throws HttpException
HttpConnection
.
The response is written according to the following logic:
readStatusLine(HttpState,HttpConnection)
is
invoked to read the request line.
processStatusLine(HttpState,HttpConnection)
is invoked, allowing the method to respond to the status line if
desired.
readResponseHeaders(HttpState,HttpConnection
is invoked to read the
associated headers.
processResponseHeaders(HttpState,HttpConnection
is invoked, allowing
the method to respond to the headers if desired.
readResponseBody(HttpState,HttpConnection)
is
invoked to read the associated body (if any).
processResponseBody(HttpState,HttpConnection
is invoked, allowing the
method to respond to the body if desired.
state
- the client stateconn
- the HttpConnection
to read the response fromprotected void readResponseBody(HttpState state, HttpConnection conn) throws IOException, HttpException
HttpConnection
.
The current implementation wraps the socket level stream with an appropriate stream for the type of response (chunked, content-length, or auto-close). If there is no response body, the connection associated with the request will be returned to the connection manager.
Subclasses may want to override this method to to customize the processing.
state
- the client stateconn
- the HttpConnection
to read the response fromreadResponse(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection)
,
processResponseBody(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection)
protected void readResponseHeaders(HttpState state, HttpConnection conn) throws IOException, HttpException
HttpConnection
, populating
the response headers map.
Subclasses may want to override this method to to customize the processing.
"It must be possible to combine the multiple header fields into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field-value to the first, each separated by a comma." - HTTP/1.0 (4.3)
state
- the client stateconn
- the HttpConnection
to read the response fromreadResponse(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection)
,
processResponseHeaders(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection)
protected void readStatusLine(HttpState state, HttpConnection conn) throws IOException, HttpRecoverableException, HttpException
HttpConnection
, setting my
status code
and status
text
.
Subclasses may want to override this method to to customize the processing.
state
- the client stateconn
- the HttpConnection
to read the response fromStatusLine
protected void writeRequest(HttpState state, HttpConnection conn) throws IOException, HttpException
Writes my request to the given HttpConnection
.
The request is written according to the following logic:
writeRequestLine(HttpState, HttpConnection)
is
invoked to write the request line.
writeRequestHeaders(HttpState,
HttpConnection)
is invoked to write the associated headers.
writeRequestBody(HttpState, HttpConnection)
is
invoked to write the body part of the request.
Subclasses may want to override one or more of the above methods to to customize the processing. (Or they may choose to override this method if dramatically different processing is required.)
state
- the client stateconn
- the HttpConnection
to write the request toprotected boolean writeRequestBody(HttpState state, HttpConnection conn) throws IOException, HttpException
HttpConnection
.
If an expectation is required, this method should ensure that it has
been sent by checking the status code
.
This method should return true if the request body was actually sent (or is empty), or false if it could not be sent for some reason (for example, expectation required but not present).
This implementation writes nothing and returns true.
state
- the client stateconn
- the connection to write toprotected void writeRequestHeaders(HttpState state, HttpConnection conn) throws IOException, HttpException
HttpConnection
.
This implementation invokes addRequestHeaders(HttpState,HttpConnection)
, and then writes each
header to the request stream.
Subclasses may want to override this method to to customize the processing.
state
- the client stateconn
- the HttpConnection
to write toaddRequestHeaders(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection)
,
getRequestHeaders()
protected void writeRequestLine(HttpState state, HttpConnection conn) throws IOException, HttpException
HttpConnection
.
Subclasses may want to override this method to to customize the processing.
state
- the client stateconn
- the HttpConnection
to write togenerateRequestLine(org.apache.commons.httpclient.HttpConnection, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
public String getProxyAuthenticationRealm()
public String getAuthenticationRealm()
protected static String getContentCharSet(Header contentheader)
contentheader
- The content header.public String getRequestCharSet()
public String getResponseCharSet()
public int getRecoverableExceptionCount()
protected void responseBodyConsumed()
The default behavior for this class is to check to see if the connection should be closed, and close if need be, and to ensure that the connection is returned to the connection manager - if and only if we are not still inside the execute call.
public HostConfiguration getHostConfiguration()
public void setHostConfiguration(HostConfiguration hostConfiguration)
hostConfiguration
- The hostConfiguration to setpublic MethodRetryHandler getMethodRetryHandler()
public void setMethodRetryHandler(MethodRetryHandler handler)
handler
- the methodRetryHandler to use when this method executedprotected void fakeResponse(StatusLine statusline, HeaderGroup responseheaders, InputStream responseStream)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |