|
|||||||||
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
User-Agent: Jakarta HTTP Client/1.0 header. |
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,
if any, as long as no Cookie request header
already exists. |
protected void |
addHostRequestHeader(HttpState state,
HttpConnection conn)
Adds a Host request header, as long as no Host request header already exists. |
void |
addRequestHeader(Header header)
Add the specified request header, NOT overwriting any previous value. |
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 . |
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 by not recycled. |
protected void |
checkUsed()
Throws an IllegalStateException if
not used since last recycle. |
int |
execute(HttpState state,
HttpConnection connection)
Execute this method. |
boolean |
followRedirects()
Whether or not I should automatically follow HTTP redirects (status code 302, etc.) |
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. |
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()
Return an array of my 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()
Return my response body, if any, as a String . |
Header |
getResponseHeader(java.lang.String headerName)
Return the specified response headers. |
Header[] |
getResponseHeaders()
Return an array my response headers. |
int |
getStatusCode()
Return the status code associated with the latest response. |
java.lang.String |
getStatusText()
Return the status text (or "reason phrase") associated with the latest response. |
boolean |
hasBeenUsed()
Return true if I have been executed
but not recycled. |
protected boolean |
isHttp11()
Return true if I should use the HTTP/1.1 protocol. |
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.) |
protected 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. |
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 setPath(java.lang.String path)
setPath
in interface HttpMethod
path
- the path to requestpublic java.lang.String getPath()
getPath
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 void addRequestHeader(java.lang.String headerName, java.lang.String headerValue)
addRequestHeader
in interface HttpMethod
headerName
- the header's nameheaderValue
- the header's valuepublic void addRequestHeader(Header header)
addRequestHeader
in interface HttpMethod
header
- the headerpublic Header getRequestHeader(java.lang.String headerName)
getRequestHeader
in interface HttpMethod
headerName
- the header namepublic void removeRequestHeader(java.lang.String headerName)
removeRequestHeader
in interface HttpMethod
headerName
- the header namepublic boolean followRedirects()
followRedirects
in interface HttpMethod
public void setFollowRedirects(boolean followRedirects)
setFollowRedirects
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
org.apache.commons.httpclient.HttpMethod
public Header[] getRequestHeaders()
getRequestHeaders
in interface HttpMethod
public boolean validate()
This implementation always returns true.
validate
in interface HttpMethod
public int getStatusCode()
getStatusCode
in interface HttpMethod
public java.lang.String getStatusText()
getStatusText
in interface HttpMethod
public Header[] getResponseHeaders()
getResponseHeaders
in interface HttpMethod
public Header getResponseHeader(java.lang.String headerName)
getResponseHeader
in interface HttpMethod
public byte[] getResponseBody()
getResponseBody
in interface HttpMethod
public java.lang.String getResponseBodyAsString()
String
.
Otherwise return null.getResponseBodyAsString
in interface HttpMethod
public java.io.InputStream getResponseBodyAsStream() throws java.io.IOException
InputStream
.
Otherwise return null.getResponseBodyAsStream
in interface HttpMethod
public boolean hasBeenUsed()
executed
but not recycled.hasBeenUsed
in interface HttpMethod
public int execute(HttpState state, HttpConnection connection) throws HttpException, java.io.IOException
execute
in interface HttpMethod
state
- HttpState
information to associate with this requestconnection
- the HttpConnection
to write to/read fromjava.io.IOException
- if an I/O error occursHttpException
- if an protocol exception occursprotected 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 toprotected 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 togenerateRequestLine(org.apache.commons.httpclient.HttpConnection, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
protected 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 toaddRequestHeaders(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection)
,
getRequestHeaders()
protected 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 towriteRequestHeaders(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection)
protected void addUserAgentRequestHeader(HttpState state, HttpConnection conn) throws java.io.IOException, HttpException
protected void addHostRequestHeader(HttpState state, HttpConnection conn) throws java.io.IOException, HttpException
protected void addCookieRequestHeader(HttpState state, HttpConnection conn) throws java.io.IOException, HttpException
Cookie
s,
if any, as long as no Cookie request header
already exists.protected void addAuthorizationRequestHeader(HttpState state, HttpConnection conn) throws java.io.IOException, HttpException
protected void addContentLengthRequestHeader(HttpState state, HttpConnection conn) throws java.io.IOException, HttpException
protected int getRequestContentLength()
Return -1 when the content-length is unknown.
This implementation returns 0, indicating that the request has no body.
protected 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.
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 fromprotected void readStatusLine(HttpState state, HttpConnection conn) throws java.io.IOException, 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 fromreadResponse(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection)
,
processStatusLine(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 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.
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 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 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 fromreadResponse(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection)
,
processResponseBody(org.apache.commons.httpclient.HttpState, org.apache.commons.httpclient.HttpConnection)
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)
public void recycle()
recycle
in interface HttpMethod
protected boolean isHttp11()
protected void setHttp11(boolean http11)
protected void checkNotUsed()
IllegalStateException
if
used by not recycled.protected void checkUsed()
IllegalStateException
if
not used since last recycle.protected static java.lang.String generateRequestLine(HttpConnection connection, java.lang.String name, java.lang.String reqPath, java.lang.String qString, java.lang.String protocol)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |