org.jdesktop.html.form
Class FileParameter

java.lang.Object
  extended by org.jdesktop.beans.AbstractBean
      extended by org.jdesktop.http.NameValuePair
          extended by org.jdesktop.http.Parameter
              extended by org.jdesktop.html.form.FileParameter
All Implemented Interfaces:
Cloneable

public class FileParameter
extends Parameter

A special Parameter intended to be used as a Form Parameter on a FormRequest for uploading files.

FileParameter adds the contentType and fileName properties. These properties are used to construct the multipart/form-data body of a form request. It is important that the fileName refer to an actual existing file object, and that the contentType be accurate. If either of these two constraints are not met, then the request will likely fail.

The fileName must be formatted such that creating a new File will succeed in locating the file. We use the same rules as the URLConnection class for determining the contentType automatically for the file. In the common case, it will be unnecessary to indicate the contentType manually. However, in special cases you may need to specify the content type manually. The format of the contentType is a mime type, such as text/plain.

If the fileName is correctly specified, then the InputStream returned from getValueStream() will be non-null. You are responsible for closing this stream when finished reading from it.

The "value" of this Parameter is specified to be the fileName. This is enforced by overridding setValue to be a null-op, and by setFilename() calling super.setValue().


Constructor Summary
FileParameter()
          Create a new FileParameter.
FileParameter(String name, File file)
          Create a new FileParameter using the given name as the name of this parameter (the key, if you will), and the given File as the file to be uploaded by the FormRequest.
FileParameter(String name, String fileName)
          Create a new FileParameter using the given name as the name of this parameter (the key if you will) and the given fileName as the absolute path to the file to be uploaded by the FormRequest.
 
Method Summary
 FileParameter clone()
          Creates and returns a copy of this object.
 String getContentType()
          Gets the content type of the data being uploaded.
 String getFilename()
          Gets the absolute path of the file being uploaded.
 InputStream getValueStream()
          Creates and returns a new InputStream based on the underlying file data.
 void setContentType(String contentType)
          Sets the mime-type of the content being uploaded.
 void setFilename(String name)
          Sets the absolute path to the file to be uploaded.
 void setValue(String value)
          Sets the value.
 
Methods inherited from class org.jdesktop.http.NameValuePair
getName, getValue, setName, toString
 
Methods inherited from class org.jdesktop.beans.AbstractBean
addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, addVetoableChangeListener, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, fireVetoableChange, getPropertyChangeListeners, getPropertyChangeListeners, getVetoableChangeListeners, getVetoableChangeListeners, hasPropertyChangeListeners, hasVetoableChangeListeners, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, removeVetoableChangeListener
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FileParameter

public FileParameter()
Create a new FileParameter. You will need to specify a name and fileName at some point for this parameter to be useful.


FileParameter

public FileParameter(String name,
                     File file)
Create a new FileParameter using the given name as the name of this parameter (the key, if you will), and the given File as the file to be uploaded by the FormRequest.

Parameters:
name -
file - may be null.

FileParameter

public FileParameter(String name,
                     String fileName)
Create a new FileParameter using the given name as the name of this parameter (the key if you will) and the given fileName as the absolute path to the file to be uploaded by the FormRequest.

Parameters:
name -
fileName - may be null.
Method Detail

setContentType

public void setContentType(String contentType)
Sets the mime-type of the content being uploaded. Generally this is determined automatically when the fileName is set. In rare cases where the mime-type cannot be determined, or when you want to force the mime type, you can set the content type manually with this method.

Parameters:
contentType - if set to null, then the mime type "content/unknown" is used.

getContentType

public final String getContentType()
Gets the content type of the data being uploaded. This is never null, and defaults to "content/unknown" when the mime type of the data couldn't be determined and was not set manually.

Returns:

setFilename

public void setFilename(String name)
Sets the absolute path to the file to be uploaded. If null, then the filename is cleared and the parameter becomes useless for uploading. Invoking this method causes the content type to be set automatically, unless it has been set manually.

Parameters:
name -

getFilename

public final String getFilename()
Gets the absolute path of the file being uploaded.

Returns:

setValue

public void setValue(String value)
Description copied from class: NameValuePair
Sets the value. A PropertyChangeEvent will be fired if the value is different from the current value.

Overrides:
setValue in class NameValuePair
Parameters:
value - The value to use. May be null.

getValueStream

public InputStream getValueStream()
Creates and returns a new InputStream based on the underlying file data. If it is not possible to create such an InputStream due to an incorrect or missing file, then null is returned.

Returns:

clone

public FileParameter clone()
Description copied from class: java.lang.Object
Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any object x, the expression:
 x.clone() != x
will be true, and that the expression:
 x.clone().getClass() == x.getClass()
will be true, but these are not absolute requirements. While it is typically the case that:
 x.clone().equals(x)
will be true, this is not an absolute requirement.

By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getClass() == x.getClass().

By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified.

The method clone for class Object performs a specific cloning operation. First, if the class of this object does not implement the interface Cloneable, then a CloneNotSupportedException is thrown. Note that all arrays are considered to implement the interface Cloneable. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.

The class Object does not itself implement the interface Cloneable, so calling the clone method on an object whose class is Object will result in throwing an exception at run time.

Overrides:
clone in class Parameter
Returns:
a clone of this instance.
See Also:
Cloneable