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.
FileResource
andClassResource
) will automatically be served using aContent-Type: application/octet-stream
header unlesssetOverrideContentType(boolean)
has been set tofalse
while files served in other ways, (e.g.ExternalResource
orThemeResource
) 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 void
extend(AbstractComponent target)
Resource
getFileDownloadResource()
Gets the resource set for download.boolean
handleConnectorRequest(VaadinRequest request, VaadinResponse response, 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 toapplication/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, equals, fireEvent, getAllChildrenIterable, getConnectorId, getErrorHandler, getExtensions, getListeners, getResource, getRpcManager, getRpcProxy, getSession, getState, getState, getStateType, getUI, hashCode, hasListeners, isAttached, isConnectorEnabled, isThis, markAsDirty, markAsDirtyRecursive, registerRpc, registerRpc, removeAttachListener, removeDetachListener, removeExtension, removeListener, removeListener, removeListener, removeListener, requestRepaint, requestRepaintAll, retrievePendingRpcCalls, setErrorHandler, setResource
-
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)
-
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 toapplication/octet-stream
to 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.
FileResource
andClassResource
) but not other resource types (e.g.ExternalResource
orThemeResource
).- 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, String path) throws 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 servingConnectorResource
s.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 interfaceClientConnector
- Overrides:
handleConnectorRequest
in classAbstractClientConnector
- Parameters:
request
- the request that should be handledresponse
- the response object to which the response should be writtenpath
- the requested relative path- Returns:
true
if the request has been handled,false
if no response has been written.- Throws:
IOException
- if there is a problem generating a response.
-
-