Class WFileWidget

  • All Implemented Interfaces:
    AjaxTarget, BeanAware, BeanBound, BeanProviderBound, DataBound, Diagnosable, Disableable, Input, Labelable, Mandatable, SubordinateTarget, WComponent, WebComponent, Serializable

    @Deprecated
    public class WFileWidget
    extends AbstractInput
    implements AjaxTarget, SubordinateTarget
    Deprecated.
    Use WMultiFileWidget instead.

    The WFileWidget represents a "File Chooser" form widget. The "bytes" property is updated with the binary data from the uploaded file. If the user submits a form with no file chosen, the bytes array will be set to null.

    The current implementation creates a FileItem which will be written temporarily to disk if the size of the file reaches a threshold. A reaper thread is started to clean up those temp files no longer being used. When using this component, developers should include the WFileWidgetCleanup context listener to their application to kill this thread when the application is stopped. i.e the web.xml should include:

        <listener>
        <listener-class>com.github.bordertech.wcomponents.portlet.context.WFileWidgetCleanup</listener-class>
        </listener>
     

    The WFileWidget allows developers to limit the maximum file size and types of files which can be uploaded.

    If one or more file types is set setFileTypes(java.util.List), then each uploaded file will be validated against the accepted list. If accepted list contains any extension(s) then uploaded files will be first checked against them. Developers can choose to have custom validation of file by retrieving it's MIME type, see getMimeType().

    Since:
    1.0.0
    Author:
    James Gifford, Martin Shevchenko, Jonathan Austin, Aswin Kandula
    See Also:
    Serialized Form
    • Constructor Detail

      • WFileWidget

        public WFileWidget()
        Deprecated.
    • Method Detail

      • getFileTypes

        public List<String> getFileTypes()
        Deprecated.
        Returns a list of strings that determine the allowable file mime types accepted by the file input. If no types have been added an empty list is returned. An empty list indicates that all file types are accepted.
        Returns:
        The mime types accepted by this file input e.g. "text/plain", "text/html", "application/pdf".
      • setFileTypes

        public void setFileTypes​(String[] types)
        Deprecated.
        Set each file type to be accepted by the WFileWidget.
        Parameters:
        types - The file types that will be accepted by the file input.
        See Also:
        for the file types
      • setFileTypes

        public void setFileTypes​(List<String> types)
        Deprecated.
        Determines the file types accepted by this widget. Note that duplicates are not allowed and these are not case sensitive.
        The file type(s) can be either:
        • MIME type
        • Extension
        MIME type: it is type/subtype, where type is text, image, application etc, and subtype is plain, jpeg, * etc. Some example MIME types are:
        • text/* - indicates that all text files MIME types are accepted, text/html, text/plain etc.
        • image/jpeg - indicates that only jpeg image files are accepted.
        Setting mime type is more reliable, as the contents of the file is validated against accepted list.
        Extension: A string whose first character is a "." (U+002E) character (Indicates that files with the specified file extension are accepted). Some example extensions are:
        • .txt - indicates any files with extension txt are accepted.
        • .jpg - indicates any files with extension jpg are accepted.
        Setting extension is less reliable, as only the extension of uploaded file (if available) is validated against accepted list.
        Parameters:
        types - The file types that will be accepted by the file input. Note that this is not additive, it will overwrite any previously set fileTypes. Pass null or and empty collection to clear all file types.
      • setMaxFileSize

        public void setMaxFileSize​(long bytes)
        Deprecated.
        Set the maximum file size (in bytes) that will be accepted by the file input. If the user selects a file larger than this value the client script will tell the user it cannot be uploaded.
        Parameters:
        bytes - The maximum size (in bytes) that can be uploaded by this input.
      • getMaxFileSize

        public long getMaxFileSize()
        Deprecated.
        Return the maximum file size (in bytes) that can be accepted by this file input.
        Returns:
        The maximum size (in bytes) that can be uploaded by this component.
      • hasMaxFileSize

        public boolean hasMaxFileSize()
        Deprecated.
        Returns:
        true if max file size is supplied.
        See Also:
        setMaxFileSize(long)
      • doHandleRequest

        protected boolean doHandleRequest​(Request request)
        Deprecated.
        Specific handle request processing for an input component is provided here.

        Input components are required to determine if the component has changed in the request, set the component data to the new value (if changed) and return the changed flag.

        Specified by:
        doHandleRequest in class AbstractInput
        Parameters:
        request - the request being responded to.
        Returns:
        true if the input component has changed, otherwise return false
      • isFileTypeValid

        public boolean isFileTypeValid()
        Deprecated.
        Indicates whether the uploaded file is valid.
        If getFileTypes() is set then it is validated, otherwise getFile() is considered valid.
        Returns:
        true if file type valid, false file type invalid.
      • isFileSizeValid

        public boolean isFileSizeValid()
        Deprecated.
        Indicates whether the uploaded file is valid. If getMaxFileSize() is set then it is validated, otherwise getFile() is considered valid.
        Returns:
        true if file size valid, false file size invalid, otherwise null.
      • validateComponent

        protected void validateComponent​(List<Diagnostic> diags)
        Deprecated.
        Override WComponent's validatorComponent in order to use the validators which have been added to this input field. Subclasses may still override this method, but it is suggested to call super.validateComponent to ensure that the validators are still used.
        Overrides:
        validateComponent in class AbstractInput
        Parameters:
        diags - the list into which any validation diagnostics are added.
      • getActionCommand

        public String getActionCommand()
        Deprecated.
        Same as {#getValueAsString()}. This method exists simply to clarify the relationship between the input component, its Action, and the ActionEvent sent to the execute() method of the Action.
        Specified by:
        getActionCommand in interface Input
        Overrides:
        getActionCommand in class AbstractInput
        Returns:
        the current selection, as a string.
      • getRequestValue

        public FileItemWrap getRequestValue​(Request request)
        Deprecated.
        Provide the value of the component on the Request.

        If the component is not on the request, the components current value will be provided.

        Specified by:
        getRequestValue in interface Input
        Parameters:
        request - the request being responded to.
        Returns:
        the value of this component on the Request, or its current state if it is not on the request.
      • isPresent

        protected boolean isPresent​(Request request)
        Deprecated.
        * Determine if this component is on the Request.
        Overrides:
        isPresent in class AbstractInput
        Parameters:
        request - the request being responded to.
        Returns:
        true if this component is on the Request, otherwise return false.
      • getBytes

        public byte[] getBytes()
        Deprecated.
        Retrieves the contents of the uploaded file.
        Returns:
        the file contents, or null if there was no file uploaded.
      • getInputStream

        public InputStream getInputStream()
                                   throws IOException
        Deprecated.
        Retrieves an input stream of the uploaded file's contents.
        Returns:
        an input stream of the file's contents, or null if there was no file uploaded
        Throws:
        IOException - if there is an error obtaining the input stream from the uploaded file.
      • getSize

        public long getSize()
        Deprecated.
        Returns:
        the size of the uploaded file, or zero if there was no file uploaded
      • getFileName

        public String getFileName()
        Deprecated.
        Returns:
        the file name of the uploaded file, or null if there was no file uploaded
      • getFile

        public FileItemWrap getFile()
        Deprecated.
        Retrieves the File item that has been uploaded.
        Returns:
        the File item that has been uploaded by the client.
      • getValue

        public FileItemWrap getValue()
        Deprecated.
        Provide the value of the component returned by DataBound.getData() in the correct format.

        If required, this method can convert the data into the correct type and also do any validation before the value is used.

        Specified by:
        getValue in interface Input
        Overrides:
        getValue in class AbstractInput
        Returns:
        the value of the component returned by DataBound.getData() in the correct format.
      • getMimeType

        public String getMimeType()
        Deprecated.
        Retrieves an mime type of the uploaded file's contents. This is not the content type passed by the browser.
        Returns:
        an file's mime type, or null if no file has been uploaded
      • getComponentModel

        protected WFileWidget.FileWidgetModel getComponentModel()
        Deprecated.
        Returns the effective component model for this component. Subclass may override this method to narrow the return type to their specific model type.
        Overrides:
        getComponentModel in class AbstractInput
        Returns:
        the effective component model
      • getOrCreateComponentModel

        protected WFileWidget.FileWidgetModel getOrCreateComponentModel()
        Deprecated.
        Retrieves the model for this component so that it can be modified. If this method is called during request processing, and a session specific model does not yet exist, then a new model is created. Subclasses may override this method to narrow the return type to their specific model type.
        Overrides:
        getOrCreateComponentModel in class AbstractInput
        Returns:
        the model for this component