Class FileDownloader
- java.lang.Object
-
- com.vaadin.server.AbstractClientConnector
-
- com.vaadin.server.AbstractExtension
-
- com.vaadin.server.FileDownloader
-
- All Implemented Interfaces:
MethodEventSource,ClientConnector,Extension,Connector,Serializable
public class FileDownloader extends AbstractExtension
Extension that starts a download when the extended component is clicked. This is used to overcome two challenges:- Resource should be bound to a component to allow it to be garbage collected when there are no longer any ways of reaching the resource.
- Download should be started directly when the user clicks e.g. a Button without going through a server-side click listener to avoid triggering security warnings in some browsers.
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.
FileResourceandClassResource) will automatically be served using aContent-Type: application/octet-streamheader unlesssetOverrideContentType(boolean)has been set tofalsewhile files served in other ways, (e.g.ExternalResourceorThemeResource) 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
Constructors Constructor Description FileDownloader(Resource resource)Creates a new file downloader for the given resource.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidextend(EventTrigger eventTrigger)Add this extension to theEventTrigger.voidextend(AbstractComponent target)Add this extension to the target component.ResourcegetFileDownloadResource()Gets the resource set for download.protected FileDownloaderStategetState()Returns the shared state for this connector.protected FileDownloaderStategetState(boolean markAsDirty)Returns the shared state for this connector.booleanhandleConnectorRequest(VaadinRequest request, VaadinResponse response, String path)Handle a request directed to this connector.booleanisOverrideContentType()Checks whether the content type should be overridden.voidsetFileDownloadResource(Resource resource)Sets the resource that is downloaded when the extended component is clicked.voidsetOverrideContentType(boolean overrideContentType)Sets whether the content type of served resources should be overridden toapplication/octet-streamto 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, addListener, addListener, addListener, addMethodInvocationToQueue, attach, beforeClientResponse, createState, detach, encodeState, equals, fireEvent, getAllChildrenIterable, getConnectorId, getErrorHandler, getExtensions, getListeners, getResource, getRpcManager, getRpcProxy, getSession, getStateType, getUI, hashCode, hasListeners, isAttached, isConnectorEnabled, isThis, markAsDirty, markAsDirtyRecursive, registerRpc, registerRpc, removeAttachListener, removeDetachListener, removeExtension, removeListener, removeListener, removeListener, removeListener, removeListener, requestRepaint, requestRepaintAll, retrievePendingRpcCalls, setErrorHandler, setResource, updateDiffstate
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, 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 alsoAbstractExtension.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)
Add this extension to the target component.- Parameters:
target- the component to attach this extension to
-
extend
public void extend(EventTrigger eventTrigger)
Add this extension to theEventTrigger.- Parameters:
eventTrigger- the trigger to attach this extension to- Since:
- 8.4
-
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 overridden toapplication/octet-streamto reduce the risk of a browser plugin choosing to display the resource instead of downloading it. This is by default set totrue.Please note that this only affects Connector resources (e.g.
FileResourceandClassResource) but not other resource types (e.g.ExternalResourceorThemeResource).- Parameters:
overrideContentType-trueto override the content type if possible;falseto use the original content type.
-
isOverrideContentType
public boolean isOverrideContentType()
Checks whether the content type should be overridden.- Returns:
trueif the content type will be overridden when possible;falseif the original content type will be used.- See Also:
setOverrideContentType(boolean)
-
handleConnectorRequest
public boolean handleConnectorRequest(VaadinRequest request, VaadinResponse response, String path) throws IOException
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 servingConnectorResources.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:
handleConnectorRequestin interfaceClientConnector- Overrides:
handleConnectorRequestin classAbstractClientConnector- Parameters:
request- the request that should be handledresponse- the response object to which the response should be writtenpath- the requested relative path- Returns:
trueif the request has been handled,falseif no response has been written.- Throws:
IOException- if something goes wrong with the download or the user cancelled the file download process.
-
getState
protected FileDownloaderState getState()
Description copied from class:AbstractClientConnectorReturns the shared state for this connector. The shared state object is shared between the server connector and the client connector. Changes are only communicated from the server to the client and not in the other direction.As a side effect, marks the connector dirty so any changes done to the state will be sent to the client. Use
getState(false)to avoid marking the connector as dirty.- Overrides:
getStatein classAbstractClientConnector- Returns:
- The shared state for this connector. Never null.
-
getState
protected FileDownloaderState getState(boolean markAsDirty)
Description copied from class:AbstractClientConnectorReturns the shared state for this connector.- Overrides:
getStatein classAbstractClientConnector- Parameters:
markAsDirty- true if the connector should automatically be marked dirty, false otherwise- Returns:
- The shared state for this connector. Never null.
- See Also:
AbstractClientConnector.getState()
-
-