Groovy Documentation

org.jdesktop.html.form
[Java] Class FormRequest

java.lang.Object
  org.jdesktop.beans.AbstractBean
      org.jdesktop.http.Request
          org.jdesktop.html.form.FormRequest

public class FormRequest
extends Request

A FormRequest is a Request specifically designed for emulating HTML forms. In particular, a FormRequest does not specifically allow the setting of the body of the Request. Instead, one of several "Form Parameters" are set, which then become the body. Since FormRequest extends request, you can also specify "normal" or "GET" parameters using the setParameter() methods.

When you create a new FormRequest, by default the HTTP method is POST instead of GET (which is the default for Request). In all other ways FormRequest has the same defaults as its parent class.

FormRequest allows you to specify the encoding used for the form. Valid entries are of type Encoding. The default value is Encoding.UrlEncoding. When uploading files to the server, you would typically want to use Encoding.MultipartFormData.

Here is a typical usage example:


      FormRequest request = new FormRequest("http://www.example.com/doFileUpload");
      request.setEncoding(Encoding.MultipartFormData);
      request.setFormParameter("username", "richard");
      request.setFormParameter("compression", "none");
      request.setFormParameter("file", new File("/usr/local/desktop/photo.png"));
 
      Session s = new Session();
      Response response = s.execute(request);
 

In this example our fictitious doFileUpload resource is expecting a multipart/form-data upload with username, compression, and file parameters in the body of the form. Notice the use of the setFormParameter that takes a File. Invoking setFormParameter in this way constructs a FileParameter and sets it on the FormRequest. See FileParameter for more details.

Since the body of a FormRequest is comprised exclusively of the content of the Form parameters, the various setBody() methods declared in Request are overridden in this subclass to be no-ops. It is therefore only possible to specify the body by using Form parameters.

Authors:
Richard


Nested Class Summary
private static class FormRequest.MultipartInputStream

This InputStream allows me to construct and return an InputStream which will be efficient in terms of memory usage with large numbers of files, or with large files.

 
Field Summary
private String boundary

private Encoding encoding

private List formParams

 
Fields inherited from class Request
followRedirects, headers, method, params, password, requestBody, stringBody, url, username
 
Constructor Summary
FormRequest()

Creates a new FormRequest, which defaults its HTTP method to POST.

FormRequest(String url)

Creates a new FormRequest for the specified URL.

FormRequest(FormRequest source)

Creates a new FormRequest based on the given FormRequest.

 
Method Summary
protected InputStream getBody()

Encoding getEncoding()

Gets the encoding to use with this FormRequest.

Parameter getFormParameter(String name)

Returns the Form Parameter with the given name, or null if there is no such Form Parameter.

Parameter[] getFormParameters()

Gets an array of all the Form Parameters for this FormRequest.

void setBody(String body)

void setBody(byte[] body)

void setBody(org.w3c.dom.Document body)

void setBody(InputStream body)

void setEncoding(Encoding enc)

Specifies the encoding to use with this FormRequest.

void setFormParameter(String key, String value)

Creates a Form Parameter using the given key and value and then adds it to the set of form parameters.

void setFormParameter(String key, File file)

Creates a Form Parameter using the given key and File and then adds it to the set of form parameters.

void setFormParameter(Parameter param)

Adds the given parameter to the set of Form parameters.

void setFormParameters(Parameter... params)

Sets the Form parameters to use with this FormRequest.

String toString()

 
Methods inherited from class Request
base64Decode, base64Encode, getBody, getFollowRedirects, getHeader, getHeaders, getMethod, getParameter, getParameters, getPassword, getUrl, getUsername, removeHeader, removeHeader, resetAuthenticationHeader, setBody, setBody, setBody, setBody, setFollowRedirects, setHeader, setHeader, setHeader, setHeaders, setMethod, setParameter, setParameter, setParameters, setPassword, setUrl, setUrlImpl, setUsername, toString
 
Methods inherited from class org.jdesktop.beans.AbstractBean
org.jdesktop.beans.AbstractBean#clone(), org.jdesktop.beans.AbstractBean#addPropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener), org.jdesktop.beans.AbstractBean#addPropertyChangeListener(java.beans.PropertyChangeListener), org.jdesktop.beans.AbstractBean#removePropertyChangeListener(java.beans.PropertyChangeListener), org.jdesktop.beans.AbstractBean#removePropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener), org.jdesktop.beans.AbstractBean#getPropertyChangeListeners(java.lang.String), org.jdesktop.beans.AbstractBean#getPropertyChangeListeners(), org.jdesktop.beans.AbstractBean#addVetoableChangeListener(java.lang.String, java.beans.VetoableChangeListener), org.jdesktop.beans.AbstractBean#addVetoableChangeListener(java.beans.VetoableChangeListener), org.jdesktop.beans.AbstractBean#removeVetoableChangeListener(java.lang.String, java.beans.VetoableChangeListener), org.jdesktop.beans.AbstractBean#removeVetoableChangeListener(java.beans.VetoableChangeListener), org.jdesktop.beans.AbstractBean#getVetoableChangeListeners(java.lang.String), org.jdesktop.beans.AbstractBean#getVetoableChangeListeners(), org.jdesktop.beans.AbstractBean#wait(), org.jdesktop.beans.AbstractBean#wait(long), org.jdesktop.beans.AbstractBean#wait(long, int), org.jdesktop.beans.AbstractBean#equals(java.lang.Object), org.jdesktop.beans.AbstractBean#toString(), org.jdesktop.beans.AbstractBean#hashCode(), org.jdesktop.beans.AbstractBean#getClass(), org.jdesktop.beans.AbstractBean#notify(), org.jdesktop.beans.AbstractBean#notifyAll()
 

Field Detail

boundary

private String boundary


encoding

private Encoding encoding


formParams

private List formParams


 
Constructor Detail

FormRequest

public FormRequest()
Creates a new FormRequest, which defaults its HTTP method to POST.


FormRequest

public FormRequest(String url)
Creates a new FormRequest for the specified URL. It defaults its HTTP method to POST.
Parameters:
url


FormRequest

public FormRequest(FormRequest source)
Creates a new FormRequest based on the given FormRequest. All data is copied from the src to the new instance, including all form parameters (which comprise the body of the request).
Parameters:
source - cannot be null.


 
Method Detail

getBody

protected InputStream getBody()


getEncoding

public final Encoding getEncoding()
Gets the encoding to use with this FormRequest. The Encoding will never be null. It specifies how the Form parameters should be encoded into the body of the request.
Returns:
a non-null Encoding for this FormRequest


getFormParameter

public final Parameter getFormParameter(String name)
Returns the Form Parameter with the given name, or null if there is no such Form Parameter.
Parameters:
name - the name to look for. If null, null is returned.
Returns:
the Parameter with the given name.


getFormParameters

public final Parameter[] getFormParameters()
Gets an array of all the Form Parameters for this FormRequest. This array will never be null. Ordering of items is guaranteed based on the order in which the params were added.
Returns:
the array of Parameters for this request


setBody

// the
public void setBody(String body)


setBody

final
public void setBody(byte[] body)


setBody

for (i
public void setBody(org.w3c.dom.Document body)


setBody

pa
public void setBody(InputStream body)


setEncoding

public void setEncoding(Encoding enc)
Specifies the encoding to use with this FormRequest. By default, this property is set to Encoding.UrlEncoded. It cannot ever be set to null. Calling this method with null results in Encoding.UrlEncoded.
Parameters:
enc


setFormParameter

public final void setFormParameter(String key, String value)
Creates a Form Parameter using the given key and value and then adds it to the set of form parameters.
Parameters:
key - must not be null
value


setFormParameter

public final void setFormParameter(String key, File file)
Creates a Form Parameter using the given key and File and then adds it to the set of form parameters.
Parameters:
key - must not be null
value


setFormParameter

public void setFormParameter(Parameter param)
Adds the given parameter to the set of Form parameters.
Parameters:
parem - the Parameter to add. This must not be null.


setFormParameters

public final void setFormParameters(Parameter... params)
Sets the Form parameters to use with this FormRequest. This replaces whatever Form parameters may have been previously defined. If null, this array is treated as an empty array, causing the list of Form params to be removed.
Parameters:
params - the Parameters to set for this Request. May be null.


toString

public String toString()


 

Groovy Documentation