Class AsyncHttpRequest
- All Implemented Interfaces:
Constants
- Direct Known Subclasses:
XmlHttpRequest
A simple non visual bean that uses HTTP to interact with web servers. This class conforms (except for XML specific functionality) with XMLHttpRequest as defined in the W3C Working Draft Specification http://www.w3.org/TR/XMLHttpRequest/.
Several methods, such as the abort() method, cause the AsyncHttpRequest to be "reset". This means that any state associated with the response is cleared, and the readyState reset to UNINITIALIZED.
AsyncHttpRequest relies on Session for communicating
with the server. It is possible to reuse an existing Session. However, if a
Session is not specified, a new one is constructed for the request.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classstatic classIndicates the state of this AsyncHttpRequest. -
Field Summary
Fields inherited from interface org.jdesktop.http.Constants
HDAER_FROM, HEADER_ACCEPT_CHARSET, HEADER_ACCEPT_ENCODING, HEADER_AUTHORIZATION, HEADER_CONTENT_BASE, HEADER_CONTENT_LENGTH, HEADER_CONTENT_LOCATION, HEADER_CONTENT_MD5, HEADER_CONTENT_RANGE, HEADER_CONTENT_TYPE, HEADER_CONTENT_VERSION, HEADER_DATE, HEADER_DELTA_BASE, HEADER_DEPTH, HEADER_DESTINATION, HEADER_E_TAG, HEADER_EXPECT, HEADER_HOST, HEADER_IF_MODIFIED_SINCE, HEADER_IF_RANGE, HEADER_IF_UNMODIFIED_SINCE, HEADER_KEEP_ALIVE, HEADER_MAX_FORWARDS, HEADER_MIME_VERSION, HEADER_OVERWRITE, HEADER_PROXY_AUTHORIZATION, HEADER_REFERER, HEADER_SOAP_ACTION, HEADER_TE, HEADER_TIMEOUT, HEADER_TRAILER, HEADER_TRANSFER_ENCODING, HEADER_UPGRADE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidabort()Cancels any network activity and resets the object.voidprotected AsyncHttpRequest.AsyncWorkercreateAsyncWorker(String content)Gets all of the response headers as one long string.If during the processing of this request an exception occured, then this method will return that Exception.booleanReturns true if this request should automatically follow redirects, false otherwise.Returns the ready-state change listener.getParameter(String name)Returns the Parameter with the given name, or null if there is no such Parameter.Gets an array of all the Parameters for this Request.Sets the passsword to use for Basic Authentication.Gets the ready state of this AsyncHttpRequest.getResponseHeader(String headerLabel)Gets a single response header as a string.If the readyState property has a value other than RECEIVING or LOADED, reponseText MUST be the empty string.Returns theSessionused with this Request.intIf the status property is not available it MUST raise an exception.If the statusText property is not available, it MUST raise an exception.Sets the username to use for Basic Authentication.protected voidhandleResponse(String responseText)Method that provides a hook for subclasses to create concrete types (such as DOM, JSONObject, etc) when the response has been fully read.voidInitializes the AsyncHttpRequest prior to sending a request.voidInitializes the AsyncHttpRequest prior to sending a request.voidInitializes the AsyncHttpRequest prior to sending a request.voidprotected voidreset()Clears any response state and resets the readyState to UNINITIALIZED.voidsend()Sends the request to the server.voidvoidvoidsetFollowsRedirects(boolean flag)Specifies whether the request should automatically follow redirects.voidsetOnReadyStateChange(PropertyChangeListener listener)Sets the listener to use for ready-state change event notification.voidsetParameter(String name, String value)Adds the given parameter to the set of parameters.voidsetParameter(Parameter param)Adds the given parameter to the set of parameters.voidsetParameters(Parameter... params)Sets the parameters to use with this Request.voidsetPassword(String password)Sets the passsword to use for Basic Authentication.voidsetRequestHeader(String header, String value)Specifies a request header for the HTTP request.voidsetSession(Session s)Sets theSessionto use with this request.voidsetUsername(String username)Sets the username to use for Basic Authentication.Methods inherited from class org.jdesktop.beans.AbstractBean
addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, addVetoableChangeListener, clone, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, fireVetoableChange, getPropertyChangeListeners, getPropertyChangeListeners, getVetoableChangeListeners, getVetoableChangeListeners, hasPropertyChangeListeners, hasVetoableChangeListeners, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, removeVetoableChangeListener
-
Constructor Details
-
AsyncHttpRequest
public AsyncHttpRequest()Creates a new instance of AsyncHttpRequest.
-
-
Method Details
-
setSession
Sets theSessionto use with this request.- Parameters:
s- the Session to use. This may be null. If null, then a default Session will be created as necessary. This default Session will NOT be available via the getSession() method, but will be internal to this implementation.
-
getSession
Returns theSessionused with this Request.- Returns:
- the Session. May be null.
-
setOnReadyStateChange
Sets the listener to use for ready-state change event notification. This is functionally equivilent to adding the listener to the list of listeners via #addReadyStateChangeListener. This method exists soley for compliance with the spec.- Parameters:
listener- the listener
-
getOnReadyStateChange
Returns the ready-state change listener. This method returns whatever is set via the #setOnReadyStateChange method. If listeners were added via #addReadyStateChangeListener, they are not reflected in this method.- Returns:
- the onReadyStateChange listener.
-
getReadyState
Gets the ready state of this AsyncHttpRequest.
Ready state will be one of the following values:
- UNINITIALIZED: The initial value.
- OPEN: The open() method has been successfully called.
- SENT: The UA successfully completed the request, but no data has yet been received.
- RECEIVING: Immediately before receiving the message body (if any). All HTTP headers have been received.
- LOADED: The data transfer has been completed.
- Returns:
- the readyState property. This will never be null.
-
open
Initializes the AsyncHttpRequest prior to sending a request. -
open
Initializes the AsyncHttpRequest prior to sending a request. -
open
Initializes the AsyncHttpRequest prior to sending a request. Calling this method initializes AsyncHttpRequest by storing the method, uri, asyncFlag, userName, and password values. This also sets the readyState to OPEN, resets the response state to their initial values, and resets the request headers. If open() is called when readyState is LOADED, then the entire object is reset. (huh?) NOTE: Private because authentication is not yet implemented (and indeed, I don't know yet how to implement it.)- Parameters:
method-uri-asyncFlag- Defaults to "true"userName- Defaults to nullpassword- Defaults to null
-
setUsername
Sets the username to use for Basic Authentication.- Parameters:
username- the user name to use
-
setPassword
Sets the passsword to use for Basic Authentication.- Parameters:
password-
-
getUsername
Sets the username to use for Basic Authentication.- Parameters:
username- the user name to use
-
getPassword
Sets the passsword to use for Basic Authentication.- Parameters:
password-
-
setRequestHeader
Specifies a request header for the HTTP request.- Parameters:
header-value-
-
send
public void send()Sends the request to the server. If the readyState property has a value other than OPEN, then an IllegalStateException will be thrown. At the beginning of this method the readyState will be set to SENT. If the async flag is set to false, then the method will not return until the request has completed (ie: the method will block). Otherwise, a background task is used and this method will return immediately. Note: Authors should specify the Content-Type header via setRequestHeader before invoking send() with an argument. Redirects must be followed. Proxies should be supported Authentication should be supported. State management (cookies)? caching(?) et etc Immediately before receiving the message body (if any), the readyState attribute will be changed to RECEIVING. When the request has completed loading, the readyState attribute will be set to LOADED. In case of a HEAD request, readyState will be set to LOADED immediately after having been set to RECEIVING. -
send
- Parameters:
content-
-
send
- Parameters:
dom-
-
abort
public void abort()Cancels any network activity and resets the object. -
getAllResponseHeaders
Gets all of the response headers as one long string.
If the readyState property has a value other than RECEIVING or LOADED, this method will return null. Otherwise, it will return all the HTTP headers as a single string, with each header line separated by a CR/LF pair. The status line will not be included.
- Returns:
- the response headers as a single string
-
getResponseHeader
Gets a single response header as a string.
If the readyState property has a value other than RECEIVING or LOADED, this method will return null. Otherwise, it will represent the value of the given HTTP header in the data received so far from the last request sent, as a single string. If more than one header of the given name was received, then the values will be concatenated, separated from each other by a comma followed by a single space. If no headers of that name were received, then it will return the empty String.
- Parameters:
headerLabel- the label of the response header to retreive.- Returns:
- the response header corrosponding to the provided label
-
getResponseText
If the readyState property has a value other than RECEIVING or LOADED, reponseText MUST be the empty string. Otherwise, it MUST be the fragment of the entity body received so far (when readyState is RECEIVING) or the complete entity body (when readyState is LOADED), interpreted as a stream of characters. -
getStatus
public int getStatus()If the status property is not available it MUST raise an exception. It MUST be available when readyState is RECEIVING or LOADED. When available, it MUST represent the HTTP status code. -
getStatusText
If the statusText property is not available, it MUST raise an exception. It MUST be available when readyState is RECEIVING or LOADED. When available, it MUST represent the HTTP status text sent by the server -
setFollowsRedirects
public void setFollowsRedirects(boolean flag)Specifies whether the request should automatically follow redirects. By default, this is set to false.- Parameters:
flag- indicates whether to follow redirects automatically
-
getFollowRedirects
public final boolean getFollowRedirects()Returns true if this request should automatically follow redirects, false otherwise.- Returns:
- whether to follow redirect requests
-
getException
If during the processing of this request an exception occured, then this method will return that Exception. Otherwise, it returns null. It MUST return null if in any state other than LOADED -
getParameter
Returns the Parameter with the given name, or null if there is no such Parameter. These are reset whenever this AsyncHttpRequest is reset.- Parameters:
name- the name to look for. This must not be null.- Returns:
- the Parameter with the given name.
-
setParameter
Adds the given parameter to the set of parameters. These are reset whenever this AsyncHttpRequest is reset.- Parameters:
param- the Parameter to add. This must not be null.
-
setParameter
Adds the given parameter to the set of parameters. These are reset whenever this AsyncHttpRequest is reset. This is a convenience method.- Parameters:
name- the name of the parametervalue- the value of the parameter
-
getParameters
Gets an array of all the Parameters for this Request. This array will never be null. Ordering of items is not guaranteed. These are reset whenever this AsyncHttpRequest is reset.- Returns:
- the array of Parameters for this request
-
setParameters
Sets the parameters to use with this Request. This replaces whatever parameters may have been previously defined. If null, this array is treated as an empty array. These are reset whenever this AsyncHttpRequest is reset.- Parameters:
params- the Parameters to set for this Request. May be null.
-
addReadyStateChangeListener
-
removeReadyStateChangeListener
-
getReadyStateChangeListeners
-
createAsyncWorker
-
reset
protected void reset()Clears any response state and resets the readyState to UNINITIALIZED. Any overriding implementations MUST call super.reset() at the end of the implementation. -
handleResponse
Method that provides a hook for subclasses to create concrete types (such as DOM, JSONObject, etc) when the response has been fully read. There is no need to call super.handleResponse(txt).- Throws:
Exception
-