|
|||||||||
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()
When a method's request may contain a body, subclasses will typically want to override:
getRequestContentLength()
writeRequestBody(HttpState,HttpConnection)
When a method requires additional request headers, subclasses will typically want to override:
addRequestHeaders(HttpState,HttpConnection)
When a method expects specific response headers, subclasses may want to override:
processResponseHeaders(HttpState,HttpConnection)
Field Summary | |
protected static Header |
USER_AGENT
The User-Agent header sent on every request. |
Constructor Summary | |
HttpMethodBase()
No-arg constructor. |
|
HttpMethodBase(java.lang.String path)
Path-specifying constructor. |
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 Transer-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. |
void |
addRequestHeader(Header header)
Add the specified request header. |
void |
addRequestHeader(java.lang.String headerName,
java.lang.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 static java.lang.String |
generateRequestLine(HttpConnection connection,
java.lang.String name,
java.lang.String reqPath,
java.lang.String qString,
java.lang.String protocol)
Generate an HTTP/S request line according to the specified attributes. |
boolean |
getFollowRedirects()
Whether or not I should automatically follow HTTP redirects (status code 302, etc.) |
abstract java.lang.String |
getName()
Obtain the name of this method, suitable for use in the "request line", for example GET or POST. |
java.lang.String |
getPath()
Get the path part of my request. |
java.lang.String |
getQueryString()
Get my query string. |
protected int |
getRequestContentLength()
Return the length (in bytes) of my request body, suitable for use in a Content-Length header. |
Header |
getRequestHeader(java.lang.String headerName)
Get the request header associated with the given name. |
Header[] |
getRequestHeaders()
Provides access to the request headers. |
byte[] |
getResponseBody()
Return my response body, if any, as a byte array. |
java.io.InputStream |
getResponseBodyAsStream()
Return my response body, if any, as an InputStream . |
java.lang.String |
getResponseBodyAsString()
Gets the response body as a string. |
Header |
getResponseFooter(java.lang.String footerName)
Get the response footer associated with the given name. |
Header[] |
getResponseFooters()
Return an array of response footers. |
Header |
getResponseHeader(java.lang.String headerName)
Get the response header associated with the given name. |
Header[] |
getResponseHeaders()
Provide access to the response headers |
int |
getStatusCode()
Provide access to the status code. |
java.lang.String |
getStatusText()
Provide access to the status text |
boolean |
hasBeenUsed()
Whether the object has been used and not recycled. |
boolean |
isHttp11()
Return true if I should use the 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 |
removeRequestHeader(java.lang.String headerName)
Remove the request header associated with the given name. |
void |
setFollowRedirects(boolean followRedirects)
Set whether or not I should automatically follow HTTP redirects (status code 302, etc.) |
void |
setHttp11(boolean http11)
Set whether or not I should use the HTTP/1.1 protocol. |
void |
setPath(java.lang.String path)
Set the path part of my request. |
void |
setQueryString(NameValuePair[] params)
Set my query string. |
void |
setQueryString(java.lang.String queryString)
Set my query string. |
void |
setRequestHeader(Header header)
Set the specified request header, overwriting any previous value. |
void |
setRequestHeader(java.lang.String headerName,
java.lang.String headerValue)
Set the specified request header, overwriting any previous value. |
protected void |
setResponseStream(java.io.InputStream responseStream)
|
void |
setStrictMode(boolean strictMode)
Turns strict mode on or off. |
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(java.lang.String path)
path
- my pathMethod Detail |
public abstract java.lang.String getName()
getName
in interface HttpMethod
public void setFollowRedirects(boolean followRedirects)
setFollowRedirects
in interface HttpMethod
followRedirects
- true to follow redirects, false otherwisepublic boolean getFollowRedirects()
getFollowRedirects
in interface HttpMethod
public void setHttp11(boolean http11)
http11
- true to use HTTP/1.1, false to use 1.0public boolean isHttp11()
public void setPath(java.lang.String path)
setPath
in interface HttpMethod
path
- the path to requestpublic void addRequestHeader(Header header)
null
will be ignored.
Note that header-name matching is case insensitive.addRequestHeader
in interface HttpMethod
header
- the header to add to the requestpublic void addResponseFooter(Header footer)
addResponseFooter
in interface HttpMethod
public java.lang.String getPath()
getPath
in interface HttpMethod
public void setQueryString(java.lang.String queryString)
setQueryString
in interface HttpMethod
queryString
- the query stringpublic void setQueryString(NameValuePair[] params)
setQueryString
in interface HttpMethod
params
- an array of NameValuePair
s to add as query string
parametersspublic java.lang.String getQueryString()
getQueryString
in interface HttpMethod
public void setRequestHeader(java.lang.String headerName, java.lang.String headerValue)
setRequestHeader
in interface HttpMethod
headerName
- the header's nameheaderValue
- the header's valuepublic void setRequestHeader(Header header)
setRequestHeader
in interface HttpMethod
header
- the headerpublic Header getRequestHeader(java.lang.String headerName)
getRequestHeader
in interface HttpMethod
headerName
- the header name to matchpublic Header[] getRequestHeaders()
getRequestHeaders
in interface HttpMethod
public int getStatusCode()
getStatusCode
in interface HttpMethod
public Header[] getResponseHeaders()
getResponseHeaders
in interface HttpMethod
public Header getResponseHeader(java.lang.String headerName)
getResponseHeader
in interface HttpMethod
headerName
- the header name to matchpublic byte[] getResponseBody()
getResponseBody
in interface HttpMethod
public java.io.InputStream getResponseBodyAsStream() throws java.io.IOException
InputStream
. Otherwise
return null.getResponseBodyAsStream
in interface HttpMethod
InputStream
java.io.IOException
- when there are errors obtaining the responsepublic java.lang.String getResponseBodyAsString()
getResponseBodyAsString
in interface HttpMethod
String
. Otherwise return
null.public Header[] getResponseFooters()
getResponseFooters
in interface HttpMethod
public Header getResponseFooter(java.lang.String footerName)
getResponseFooter
in interface HttpMethod
footerName
- the footer name to matchprotected void setResponseStream(java.io.InputStream responseStream)
public java.lang.String getStatusText()
getStatusText
in interface HttpMethod
public void setStrictMode(boolean strictMode)
setStrictMode
in interface HttpMethod
strictMode
- true for strict mode, false otherwisepublic boolean isStrictMode()
isStrictMode
in interface HttpMethod
public void addRequestHeader(java.lang.String headerName, java.lang.String headerValue)
addRequestHeader
in interface HttpMethod
headerName
- the header's nameheaderValue
- the header's valuepublic int execute(HttpState state, HttpConnection conn) throws HttpException, java.io.IOException, java.lang.NullPointerException
execute
in interface HttpMethod
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.HttpException
- if an protocol exception occursjava.io.IOException
- if an I/O error occursjava.lang.NullPointerException
- if the state is nullpublic boolean hasBeenUsed()
hasBeenUsed
in interface HttpMethod
executed
but not
recycled.public void recycle()
recycle
in interface HttpMethod
public void removeRequestHeader(java.lang.String headerName)
removeRequestHeader
in interface HttpMethod
headerName
- the header namepublic boolean validate()
This implementation always returns true.
validate
in interface HttpMethod
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 java.io.IOException, HttpException
state
- current state of http requestsconn
- the connection to use for I/Ojava.io.IOException
- when errors occur reading or writing to/from the
connectionHttpException
- when a recoverable error occursprotected void addContentLengthRequestHeader(HttpState state, HttpConnection conn) throws java.io.IOException, HttpException
state
- current state of http requestsconn
- the connection to use for I/Ojava.io.IOException
- when errors occur reading or writing to/from the
connectionHttpException
- when a recoverable error occursprotected void addCookieRequestHeader(HttpState state, HttpConnection conn) throws java.io.IOException, HttpException
Cookie
s.state
- current state of http requestsconn
- the connection to use for I/Ojava.io.IOException
- when errors occur reading or writing to/from the
connectionHttpException
- when a recoverable error occursprotected void addHostRequestHeader(HttpState state, HttpConnection conn) throws java.io.IOException, HttpException
state
- current state of http requestsconn
- the connection to use for I/Ojava.io.IOException
- when errors occur reading or writing to/from the
connectionHttpException
- when a recoverable error occursprotected void addProxyAuthorizationRequestHeader(HttpState state, HttpConnection conn) throws java.io.IOException, HttpException
state
- current state of http requestsconn
- the connection to use for I/Ojava.io.IOException
- when errors occur reading or writing to/from the
connectionHttpException
- when a recoverable error occursprotected void addRequestHeaders(HttpState state, HttpConnection conn) throws java.io.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 tojava.io.IOException
- when an error occurs writing the requestHttpException
- when a HTTP protocol error occurswriteRequestHeaders(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection)
protected void addUserAgentRequestHeader(HttpState state, HttpConnection conn) throws java.io.IOException, HttpException
state
- the client stateconn
- the HttpConnection
the headers will eventually be
written tojava.io.IOException
- when an error occurs writing the requestHttpException
- when a HTTP protocol error occursprotected void checkNotUsed() throws java.lang.IllegalStateException
IllegalStateException
if used but not recycled.java.lang.IllegalStateException
- if the method has been used and not
recycledprotected void checkUsed() throws java.lang.IllegalStateException
IllegalStateException
if not used since last recycle.java.lang.IllegalStateException
- if not usedprotected static java.lang.String generateRequestLine(HttpConnection connection, java.lang.String name, java.lang.String reqPath, java.lang.String qString, java.lang.String protocol)
connection
- the connection the request will be sent toname
- the method name generate a request forreqPath
- the path for the requestqString
- the query string for the requestprotocol
- the protocol 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 java.io.IOException, 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 fromjava.io.IOException
- when i/o errors occur reading the responseHttpException
- when a protocol error occurs or state is invalidprotected void readResponseBody(HttpState state, HttpConnection conn) throws java.io.IOException, HttpException
HttpConnection
.
The current implementation simply consumes the expected response body (according to the values of the Content-Length and Transfer-Encoding headers, if any).
Subclasses may want to override this method to to customize the processing.
state
- the client stateconn
- the HttpConnection
to read the response fromjava.io.IOException
- when i/o errors occur reading the responseHttpException
- when a protocol error occurs or state is invalidreadResponse(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 java.io.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 fromjava.io.IOException
- when i/o errors occur reading the responseHttpException
- when a protocol error occurs or state is invalidreadResponse(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 java.io.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 fromjava.io.IOException
- when errors occur reading the status lineHttpException
- If there is no status line, the protocol is not
recognised, if we are unable to parse the status code from the line,
or there was no status textHttpRecoverableException
- when the status line is null and the
request should be retriedreadResponse(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection)
,
processStatusLine(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection)
protected void writeRequest(HttpState state, HttpConnection conn) throws java.io.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 tojava.io.IOException
- when i/o errors occur reading the responseHttpException
- when a protocol error occurs or state is invalidprotected boolean writeRequestBody(HttpState state, HttpConnection conn) throws java.io.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 tojava.io.IOException
- when i/o errors occur reading the responseHttpException
- when a protocol error occurs or state is invalidprotected void writeRequestHeaders(HttpState state, HttpConnection conn) throws java.io.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 tojava.io.IOException
- when i/o errors occur reading the responseHttpException
- when a protocol error occurs or state is invalidaddRequestHeaders(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection)
,
getRequestHeaders()
protected void writeRequestLine(HttpState state, HttpConnection conn) throws java.io.IOException, HttpException
HttpConnection
.
Subclasses may want to override this method to to customize the processing.
state
- the client stateconn
- the HttpConnection
to write tojava.io.IOException
- when i/o errors occur reading the responseHttpException
- when a protocol error occurs or state is invalidgenerateRequestLine(org.apache.commons.httpclient.HttpConnection, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |