public class FileDownloader extends AbstractExtension
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.
ClientConnector.AttachEvent, ClientConnector.AttachListener, ClientConnector.ConnectorErrorEvent, ClientConnector.DetachEvent, ClientConnector.DetachListener
Constructor and Description |
---|
FileDownloader(Resource resource)
Creates a new file downloader for the given resource.
|
Modifier and Type | Method and Description |
---|---|
void |
extend(AbstractComponent target) |
Resource |
getFileDownloadResource()
Gets the resource set for download.
|
protected FileDownloaderState |
getState()
Returns the shared state for this connector.
|
protected FileDownloaderState |
getState(boolean markAsDirty)
Returns the shared state for this connector.
|
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 to
application/octet-stream to reduce the risk of a browser
plugin choosing to display the resource instead of downloading it. |
extend, getParent, getSupportedParentType, remove, setParent
addAttachListener, addDetachListener, addExtension, 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, requestRepaint, requestRepaintAll, retrievePendingRpcCalls, setErrorHandler, setResource, updateDiffstate
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
addAttachListener, addDetachListener, attach, beforeClientResponse, detach, encodeState, getErrorHandler, getExtensions, getRpcManager, getStateType, getUI, isAttached, isConnectorEnabled, markAsDirty, markAsDirtyRecursive, removeAttachListener, removeDetachListener, removeExtension, requestRepaint, requestRepaintAll, retrievePendingRpcCalls, setErrorHandler
getConnectorId
public FileDownloader(Resource resource)
AbstractExtension.extend(AbstractClientConnector)
the
component.resource
- the resource to download when the user clicks the extended
component.public void extend(AbstractComponent target)
public Resource getFileDownloadResource()
public void setFileDownloadResource(Resource resource)
resource
- the resource to downloadpublic void setOverrideContentType(boolean overrideContentType)
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
).
overrideContentType
- true
to override the content type if possible;
false
to use the original content type.public boolean isOverrideContentType()
true
if the content type will be overridden when
possible; false
if the original content type will be
used.setOverrideContentType(boolean)
public boolean handleConnectorRequest(VaadinRequest request, VaadinResponse response, String path) throws IOException
ClientConnector
ConnectorResource
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.
handleConnectorRequest
in interface ClientConnector
handleConnectorRequest
in class AbstractClientConnector
request
- the request that should be handledresponse
- the response object to which the response should be writtenpath
- the requested relative pathtrue
if the request has been handled,
false
if no response has been written.IOException
- if there is a problem generating a response.protected FileDownloaderState getState()
AbstractClientConnector
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.
getState
in class AbstractClientConnector
protected FileDownloaderState getState(boolean markAsDirty)
AbstractClientConnector
getState
in class AbstractClientConnector
markAsDirty
- true if the connector should automatically be marked dirty,
false otherwiseAbstractClientConnector.getState()
Copyright © 2017 Vaadin Ltd. All rights reserved.