com.vaadin.server
Class FileDownloader

java.lang.Object
  extended by com.vaadin.server.AbstractClientConnector
      extended by com.vaadin.server.AbstractExtension
          extended by com.vaadin.server.FileDownloader
All Implemented Interfaces:
MethodEventSource, ClientConnector, Extension, com.vaadin.shared.Connector, java.io.Serializable

public class FileDownloader
extends AbstractExtension

Extension that starts a download when the extended component is clicked. This is used to overcome two challenges:

Please note that the download will be started in an iframe, which means that care should be taken to avoid serving content types that might make the browser attempt to show the content using a plugin instead of downloading it. Connector resources (e.g. FileResource and ClassResource) will automatically be served using a Content-Type: application/octet-stream header unless setOverrideContentType(boolean) has been set to false while files served in other ways, (e.g. ExternalResource or ThemeResource) will not automatically get this treatment.

Since:
7.0.0
Author:
Vaadin Ltd
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface com.vaadin.server.ClientConnector
ClientConnector.AttachEvent, ClientConnector.AttachListener, ClientConnector.ConnectorErrorEvent, ClientConnector.DetachEvent, ClientConnector.DetachListener
 
Constructor Summary
FileDownloader(Resource resource)
          Creates a new file downloader for the given resource.
 
Method Summary
 void extend(AbstractComponent target)
           
 Resource getFileDownloadResource()
          Gets the resource set for download.
 boolean handleConnectorRequest(VaadinRequest request, VaadinResponse response, java.lang.String path)
          Handle a request directed to this connector.
 boolean isOverrideContentType()
          Checks whether the content type should be overridden.
 void setFileDownloadResource(Resource resource)
          Sets the resource that is downloaded when the extended component is clicked.
 void setOverrideContentType(boolean overrideContentType)
          Sets whether the content type of served resources should be overriden to application/octet-stream to reduce the risk of a browser plugin choosing to display the resource instead of downloading it.
 
Methods inherited from class com.vaadin.server.AbstractExtension
extend, getParent, getSupportedParentType, remove, setParent
 
Methods inherited from class com.vaadin.server.AbstractClientConnector
addAttachListener, addDetachListener, addExtension, addListener, addListener, addListener, addMethodInvocationToQueue, attach, beforeClientResponse, createState, detach, encodeState, fireEvent, getAllChildrenIterable, getConnectorId, getErrorHandler, getExtensions, getListeners, getResource, getRpcManager, getRpcProxy, getSession, getState, getState, getStateType, getUI, hasListeners, isAttached, isConnectorEnabled, markAsDirty, markAsDirtyRecursive, registerRpc, registerRpc, removeAttachListener, removeDetachListener, removeExtension, removeListener, removeListener, removeListener, removeListener, requestRepaint, requestRepaintAll, retrievePendingRpcCalls, setErrorHandler, setResource
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.vaadin.server.ClientConnector
addAttachListener, addDetachListener, attach, beforeClientResponse, detach, encodeState, getErrorHandler, getExtensions, getRpcManager, getStateType, getUI, isAttached, isConnectorEnabled, markAsDirty, markAsDirtyRecursive, removeAttachListener, removeDetachListener, removeExtension, requestRepaint, requestRepaintAll, retrievePendingRpcCalls, setErrorHandler
 
Methods inherited from interface com.vaadin.shared.Connector
getConnectorId
 

Constructor Detail

FileDownloader

public FileDownloader(Resource resource)
Creates a new file downloader for the given resource. To use the downloader, you should also AbstractExtension.extend(AbstractClientConnector) the component.

Parameters:
resource - the resource to download when the user clicks the extended component.
Method Detail

extend

public void extend(AbstractComponent target)

getFileDownloadResource

public Resource getFileDownloadResource()
Gets the resource set for download.

Returns:
the resource that will be downloaded if clicking the extended component

setFileDownloadResource

public void setFileDownloadResource(Resource resource)
Sets the resource that is downloaded when the extended component is clicked.

Parameters:
resource - the resource to download

setOverrideContentType

public void setOverrideContentType(boolean overrideContentType)
Sets whether the content type of served resources should be overriden to application/octet-stream to reduce the risk of a browser plugin choosing to display the resource instead of downloading it. This is by default set to true.

Please note that this only affects Connector resources (e.g. FileResource and ClassResource) but not other resource types (e.g. ExternalResource or ThemeResource).

Parameters:
overrideContentType - true to override the content type if possible; false to use the original content type.

isOverrideContentType

public boolean isOverrideContentType()
Checks whether the content type should be overridden.

Returns:
true if the content type will be overridden when possible; false if the original content type will be used.
See Also:
setOverrideContentType(boolean)

handleConnectorRequest

public boolean handleConnectorRequest(VaadinRequest request,
                                      VaadinResponse response,
                                      java.lang.String path)
                               throws java.io.IOException
Description copied from interface: ClientConnector
Handle a request directed to this connector. This can be used by connectors to dynamically generate a response and it is also used internally when serving ConnectorResources.

Requests to /APP/connector/[ui id]/[connector id]/ are routed to this method with the remaining part of the requested path available in the path parameter.

NOTE that the session is not locked when this method is called. It is the responsibility of the connector to ensure that the session is locked while handling state or other session related data. For best performance the session should be unlocked before writing a large response to the client.

Specified by:
handleConnectorRequest in interface ClientConnector
Overrides:
handleConnectorRequest in class AbstractClientConnector
Parameters:
request - the request that should be handled
response - the response object to which the response should be written
path - the requested relative path
Returns:
true if the request has been handled, false if no response has been written.
Throws:
java.io.IOException - if there is a problem generating a response.


Copyright © 2000-2014 Vaadin Ltd. All Rights Reserved.