Class Request
- All Implemented Interfaces:
Constants
Request is constructed and then
passed to a Session for execution. The Session then returns
a Response after execution finishes.
It is not possible to reuse Requests with content bodies because
those bodies are specified as InputStreams. This is done for
efficient handling of large files that may be used as content bodies.
To help simplify reuse of Requests, a copy constructor is provided
which will copy everything _except_ the content body from the source
Request
A Request is composed of a URL and HTTP method and optionally Headers, Parameters, and a body.
The URL for convenience is specified as a String, not a java.net.URL. The
evaluation of the URL is done when the Request is executed, as opposed to
when it is first set. The HTTP Method must be non-null.
HTTP headers are represented by the Header API. All HTTP headers
that will be sent as part of this request are represented with a Header
in this class. By default, all Request objects are created with an
Accept-Encoding header set to "gzip", and have a Content-Type header
set to 'text/plain; charset="UTF-8"'. If you send other data be sure to
replace the value of the content type header.
According to the HTTP specification, HTTP headers are not case sensivite. Therefore, this class will allow headers to be lookedup in a case insensitive manner, unlike parameters which are case sensitive.
For convenience, this class supports automatic header generation for basic
authentication when the username property is set. Whenever
username or password is set it will reset the
"Authentication" header. Be aware that manual modifications of this header
will be lost whenever the username/password is changed.
Request also supports setting query parameters. A URL is composed of the protocol part, path part, and optionally the query parameter part.
http://www.example.com/foo.html?a=b;c=d
|-----|------------------------|-------|
proto path portion of URI params
Request supports the setting of query parameters either in the URL or separately from it. In the next code snippet, the query parameters are set as part of the URL. As you can see from the code snippet, the query parameters, even though specified as part of the URL, are extracted from the URL and can be read and/or modified via the parameter API:
Request req = new Request("http://www.example.com/foo.html?a=b;c=d");
System.out.println(req.getUrl()); // prints out http://www.example.com/foo.html
System.out.println(req.getParameter("a")); // prints out a=b
System.out.println(req.getParameter("c")); // prints out c=d
You may also specify the query parameters completely separately from the URL:
Request req = new Request("http://www.example.com/foo.html");
req.setParameter("a", "b");
req.setParameter("c", "d");
HTTP parameters must be URL encoded prior to transmission. This task is not handled by the Request, but by the Session. All parameter names and values are not URL encoded.
Some HTTP oriented APIs distinguish between "GET" parameters and "POST" parameters. This one does not. All parameters in this Request class are "GET" parameters, meaning that regardless of the HTTP method being used the parameters are set on the query string in the URL, not the body of the request. A subclass, FormRequest, handles "POST" parameters in a more complete way by also supporting different encoding schemes for POST requests.
-
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
ConstructorsConstructorDescriptionRequest()Creates a new instance of Request.Creaets a new instance of Request with the specified URL.Creates a new instance of Request with the specified HTTP method and url.Creates a new instance of Request, usingsourceas the basis for all of the initial property values (except for requestBody, which is always null). -
Method Summary
Modifier and TypeMethodDescriptionprotected InputStreamgetBody()Protected method which returns the request body.booleanGets whether to automatically follow redirct requests.Returns the Header with the given name, or null if there is no such header.Header[]Gets an array of all the Headers for this Request.Gets the http Method used.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.Gets the password.getUrl()Returns the URL to request content from.Gets the username used for Basic Authentication.voidremoveHeader(String header)Removes the given named header from this Request.voidremoveHeader(Header header)Removes the given header from this Request.voidsetBody(byte[] body)Sets the request body to be the specified array of bytes.voidsetBody(InputStream body)Sets the request body to be the specifiedInputStream.voidSets the request body to be the specified String.voidSets the request body to be the specifiedDocument.voidsetFollowRedirects(boolean b)Specifies whether to automatically follow redirects.voidCreates a new Header with the given name and value, and no elements and adds it to the set of headers.voidsetHeader(String name, String value, Header.Element... elements)Creates a new Header with the given name, value, and elements and adds it to the set of headers.voidAdds the given header to the set of headers.voidsetHeaders(Header... headers)Sets the headers to use with this Request.voidSets the httpMethodto use for this Request.voidsetParameter(String name, String value)Creates a Parameter using the given name and value and then adds it 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.voidThe URL to request content from.voidsetUsername(String username)Sets the username to use for Basic Authentication.toString()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
-
Request
public Request()Creates a new instance of Request. The following default values are used:
- headers: Accept-Encoding = gzip
- parameters: empty set
- followRedirects: true
- method: GET
- url: null
- requestBody: null
-
Request
Creaets a new instance of Request with the specified URL. Other default values are the same as for the default constructor.- Parameters:
url-
-
Request
Creates a new instance of Request with the specified HTTP method and url. All other default values are the same as for the default consturctor.- Parameters:
method- The HTTP method. If null, Method.GET is used.url- The url. If non null, any query parameters are extracted and set as params for this request.
-
Request
Creates a new instance of Request, using
sourceas the basis for all of the initial property values (except for requestBody, which is always null). This is a copy constructor.- Parameters:
source- The source Request to copy
-
-
Method Details
-
getHeader
Returns the Header with the given name, or null if there is no such header. Header names are checked in a case insensitive manner.- Parameters:
name- the name to look for. If null then a null value will be returned- Returns:
- the Header with the given name.
-
setHeader
Creates a new Header with the given name and value, and no elements and adds it to the set of headers.- Parameters:
name- The name. Must not be null.value- The value. May be null.
-
setHeader
Creates a new Header with the given name, value, and elements and adds it to the set of headers.- Parameters:
name- The name. Must not be null.value- The value. May be null.elements- The elements. May be null.
-
setHeader
Adds the given header to the set of headers.- Parameters:
header- the Header to add. This must not be null.
-
removeHeader
Removes the given header from this Request.- Parameters:
header- the Header to remove. If null, nothing happens. If the header is not specified in this Request, nothing happens.
-
removeHeader
Removes the given named header from this Request. The header is case-insensitive.- Parameters:
header- the name of the Header to remove. If null, nothing happens. If the header is not specified in this Request, nothing happens. Matches in a case-insensitive manner.
-
getHeaders
Gets an array of all the Headers for this Request. This array will never be null. Ordering of items is not guaranteed.- Returns:
- the array of Headers for this request
-
setHeaders
Sets the headers to use with this Request. This replaces whatever headers may have been previously defined. If null, this array is treated as an empty array.- Parameters:
headers- the Headers to set for this Request. May be null.
-
getParameter
Returns the Parameter with the given name, or null if there is no such Parameter.- Parameters:
name- the name to look for. If null, null is returned.- Returns:
- the Parameter with the given name.
-
setParameter
Creates a Parameter using the given name and value and then adds it to the set of parameters.- Parameters:
name- must not be nullvalue-
-
setParameter
Adds the given parameter to the set of parameters.- Parameters:
param- the Parameter to add. This must not be null.
-
getParameters
Gets an array of all the Parameters for this Request. This array will never be null. Ordering of items is not guaranteed.- 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.- Parameters:
params- the Parameters to set for this Request. May be null.
-
setFollowRedirects
public void setFollowRedirects(boolean b)Specifies whether to automatically follow redirects. An HTTP response may indicate that the system should be redirected to a new page. In that case, if followRedirects is true, this will happen automatically and the Response will be the new page (as long as the new page does not also cause a redirect). It is possible to encounter infinite redirects. boolean b whether to automatically follow redirects -
getFollowRedirects
public final boolean getFollowRedirects()Gets whether to automatically follow redirct requests. @see #setFollowRedirects(boolean).- Returns:
- whether to automatically follow redirects.
-
setMethod
Sets the httpMethodto use for this Request. If null, a GET method will be used.- Parameters:
method- theMethodto use. If null,Method.GETis used.
-
getMethod
Gets the http Method used.- Returns:
- the
Methodfor this Request.
-
setUrl
The URL to request content from. This must be an absolute URL. An IllegalArgumentException will be thrown if this url is malformed. This value may be null, but must be specified prior to executing this Request, otherwise an IllegalStateException will occur at execution time.
This URL may contain parameters (ie: in the query string). These parameters will be left in place. Any parameters added via #setParameters(Parameter[]) will be appened to this query string if this is not a POST request, otherwise, they will be included in the body of the post.
- Parameters:
url- The url to request content from. May be null- Throws:
IllegalArgumentException- if the url is malformed.
-
getUrl
Returns the URL to request content from.- Returns:
- the url
-
setUsername
Sets the username to use for Basic Authentication. If a username is specified, then the Session will attempt to use Basic Authentication with the web server. Setting username to null essentially turns off Basic Authentication. Setting this property causes a header to be added.- Parameters:
username- the user name to use
-
getUsername
Gets the username used for Basic Authentication.- Returns:
- may be null.
-
setPassword
Sets the passsword to use for Basic Authentication. This property is only used if a username is also specified. For security reasons, the password cannot be retrieved after being set, and no property change event is fired for this property.- Parameters:
password-
-
getPassword
Gets the password. May be null.- Returns:
- may be null.
-
setBody
Sets the request body to be the specified String.- Parameters:
body- the String to use for the body. May be null.
-
setBody
public void setBody(byte[] body)Sets the request body to be the specified array of bytes.- Parameters:
body- the byte array to use for the body. May be null.
-
setBody
Sets the request body to be the specifiedDocument.- Parameters:
body- the DOM document to use for the body. May be null.
-
setBody
Sets the request body to be the specifiedInputStream.- Parameters:
body- the InputStream to use for the body. May be null.
-
getBody
Protected method which returns the request body. This is only called by the Session. This method should never be called by client code, and should only be overridden in subclasses where the body is constructed in a manner unique to the request (for example: FormRequest).- Throws:
Exception
-
toString
-