public abstract class VaadinService extends Object implements Serializable
Modifier and Type | Field and Description |
---|---|
static String |
URL_PARAMETER_CLOSE_APPLICATION
Deprecated.
As of 7.0. Only supported for
LegacyApplication . |
static String |
URL_PARAMETER_RESTART_APPLICATION
Deprecated.
As of 7.0. Only supported for
LegacyApplication . |
Constructor and Description |
---|
VaadinService(DeploymentConfiguration deploymentConfiguration)
Creates a new vaadin service based on a deployment configuration
|
Modifier and Type | Method and Description |
---|---|
Future<Void> |
accessSession(VaadinSession session,
Runnable runnable)
Implementation for
VaadinSession.access(Runnable) . |
void |
addServiceDestroyListener(ServiceDestroyListener listener)
Adds a service destroy listener that gets notified when this service is
destroyed.
|
void |
addSessionDestroyListener(SessionDestroyListener listener)
Adds a listener that gets notified when a Vaadin service session that has
been initialized for this service is destroyed.
|
void |
addSessionInitListener(SessionInitListener listener)
Adds a listener that gets notified when a new Vaadin service session is
initialized for this service.
|
void |
closeSession(VaadinSession session)
Sets the given session to be closed and all its UI state to be discarded
at the end of the current request, or at the end of the next request if
there is no ongoing one.
|
static String |
createCriticalNotificationJSON(String caption,
String message,
String details,
String url)
Creates a JSON message which, when sent to client as-is, will cause a
critical error to be shown with the given details.
|
protected List<RequestHandler> |
createRequestHandlers()
Called during initialization to add the request handlers for the service.
|
protected VaadinSession |
createVaadinSession(VaadinRequest request)
Creates a new Vaadin session for this service and request
|
void |
criticalNotification(VaadinRequest request,
VaadinResponse response,
String caption,
String message,
String details,
String url)
Deprecated.
As of 7.0. Will likely change or be removed in a future
version
|
void |
destroy()
Called when the servlet, portlet or similar for this service is being
destroyed.
|
void |
ensureAccessQueuePurged(VaadinSession session)
Makes sure the pending access queue is purged for the provided session.
|
boolean |
ensurePushAvailable()
Enables push if push support is available and push has not yet been
enabled.
|
UI |
findUI(VaadinRequest request)
Finds the
UI that belongs to the provided request. |
VaadinSession |
findVaadinSession(VaadinRequest request)
Attempts to find a Vaadin service session associated with this request.
|
void |
fireSessionDestroy(VaadinSession vaadinSession)
Handles destruction of the given session.
|
protected URL |
getApplicationUrl(VaadinRequest request)
Deprecated.
As of 7.0. Only used to support
LegacyApplication . |
abstract File |
getBaseDirectory()
Returns the context base directory.
|
ClassLoader |
getClassLoader()
Gets the class loader to use for loading classes loaded by name, e.g.
|
abstract String |
getConfiguredTheme(VaadinRequest request)
Gets the theme that is configured for this deployment, e.g.
|
abstract String |
getConfiguredWidgetset(VaadinRequest request)
Gets the widgetset that is configured for this deployment, e.g.
|
static VaadinService |
getCurrent()
Gets the currently used Vaadin service.
|
static VaadinRequest |
getCurrentRequest()
Gets the currently processed Vaadin request.
|
static VaadinResponse |
getCurrentResponse()
Gets the currently processed Vaadin response.
|
DeploymentConfiguration |
getDeploymentConfiguration()
Gets the deployment configuration.
|
protected VaadinSession |
getExistingSession(VaadinRequest request,
boolean allowSessionCreation) |
abstract String |
getMainDivId(VaadinSession session,
VaadinRequest request,
Class<? extends UI> uiClass)
Creates and returns a unique ID for the DIV where the UI is to be
rendered.
|
abstract String |
getMimeType(String resourceName)
Returns the MIME type of the specified file, or null if the MIME type is
not known.
|
Iterable<RequestHandler> |
getRequestHandlers()
Returns the request handlers that are registered with this service.
|
abstract String |
getServiceName()
Gets a unique name for this service.
|
protected String |
getSessionAttributeName()
Returns the name used for storing the VaadinSession in the underlying
HTTP session
|
protected Lock |
getSessionLock(WrappedSession wrappedSession)
Gets the lock instance used to lock the VaadinSession associated with the
given wrapped session.
|
abstract String |
getStaticFileLocation(VaadinRequest request)
Return the URL from where static files, e.g.
|
SystemMessages |
getSystemMessages(Locale locale,
VaadinRequest request)
Gets the system message to use for a specific locale.
|
SystemMessagesProvider |
getSystemMessagesProvider()
Gets the system messages provider currently defined for this service.
|
abstract InputStream |
getThemeResourceAsStream(UI ui,
String themeName,
String resource)
Finds the given theme resource from the web content folder or using the
class loader and returns a stream for it
|
void |
handleRequest(VaadinRequest request,
VaadinResponse response)
Handles the incoming request and writes the response into the response
object.
|
protected void |
handleSessionExpired(VaadinRequest request,
VaadinResponse response)
Called when the session has expired and the request handling is therefore
aborted.
|
void |
init()
Initializes this service.
|
protected boolean |
isAtmosphereAvailable()
Checks whether Atmosphere is avilable for use
|
static boolean |
isCsrfTokenValid(VaadinSession session,
String requestToken)
Verifies that the given CSRF token (aka double submit cookie) is valid
for the given session.
|
static boolean |
isOtherSessionLocked(VaadinSession session)
Checks whether there might be some
VaadinSession other than the
provided one for which the current thread holds a lock. |
abstract boolean |
isStandalone(VaadinRequest request)
Checks whether the UI will be rendered on its own in the browser or
whether it will be included into some other context.
|
protected VaadinSession |
loadSession(WrappedSession wrappedSession)
Called when the VaadinSession should be loaded from the underlying HTTP
session
|
protected void |
lockSession(WrappedSession wrappedSession)
Locks the given session for this service instance.
|
boolean |
preserveUIOnRefresh(UIProvider provider,
UICreateEvent event)
Check if the given UI should be associated with the
window.name so that it can be re-used if the browser window
is reloaded. |
protected VaadinSession |
readFromHttpSession(WrappedSession wrappedSession)
Performs the actual read of the VaadinSession from the underlying HTTP
session after sanity checks have been performed.
|
static void |
reinitializeSession(VaadinRequest request)
Discards the current session and creates a new session with the same
contents.
|
protected void |
removeFromHttpSession(WrappedSession wrappedSession)
Performs the actual removal of the VaadinSession from the underlying HTTP
session after sanity checks have been performed
|
void |
removeServiceDestroyListener(ServiceDestroyListener listener)
Removes a service destroy listener that was previously added with
addServiceDestroyListener(ServiceDestroyListener) . |
void |
removeSession(WrappedSession wrappedSession)
Called when the VaadinSession should be removed from the underlying HTTP
session
|
void |
removeSessionDestroyListener(SessionDestroyListener listener)
Removes a Vaadin service session destroy listener from this service.
|
void |
removeSessionInitListener(SessionInitListener listener)
Removes a Vaadin service session initialization listener from this
service.
|
protected abstract boolean |
requestCanCreateSession(VaadinRequest request)
Checks whether it's valid to create a new service session as a result of
the given request.
|
void |
requestEnd(VaadinRequest request,
VaadinResponse response,
VaadinSession session)
Called after the framework has handled a request and the response has
been written.
|
void |
requestStart(VaadinRequest request,
VaadinResponse response)
Called before the framework starts handling a request
|
void |
runPendingAccessTasks(VaadinSession session)
Purges the queue of pending access invocations enqueued with
VaadinSession.access(Runnable) . |
void |
setClassLoader(ClassLoader classLoader)
Sets the class loader to use for loading classes loaded by name, e.g.
|
static void |
setCurrent(VaadinService service)
Sets the given Vaadin service as the current service.
|
void |
setCurrentInstances(VaadinRequest request,
VaadinResponse response)
Sets the this Vaadin service as the current service and also sets the
current Vaadin request and Vaadin response.
|
protected void |
setDefaultClassLoader()
Tries to acquire default class loader and sets it as a class loader for
this
VaadinService if found. |
void |
setSystemMessagesProvider(SystemMessagesProvider systemMessagesProvider)
Sets the system messages provider to use for getting system messages to
display to users of this service.
|
protected void |
storeSession(VaadinSession session,
WrappedSession wrappedSession)
Called when the VaadinSession should be stored.
|
protected void |
unlockSession(WrappedSession wrappedSession)
Releases the lock for the given session for this service instance.
|
static void |
verifyNoOtherSessionLocked(VaadinSession session)
Checks that another
VaadinSession instance is not locked. |
void |
writeStringResponse(VaadinResponse response,
String contentType,
String reponseString)
Writes the given string as a response using the given content type.
|
protected void |
writeToHttpSession(WrappedSession wrappedSession,
VaadinSession session)
Performs the actual write of the VaadinSession to the underlying HTTP
session after sanity checks have been performed.
|
@Deprecated public static final String URL_PARAMETER_RESTART_APPLICATION
LegacyApplication
.@Deprecated public static final String URL_PARAMETER_CLOSE_APPLICATION
LegacyApplication
.public VaadinService(DeploymentConfiguration deploymentConfiguration)
deploymentConfiguration
- the deployment configuration for the servicepublic void init() throws ServiceException
ServiceException
- if a problem occurs when creating the serviceprotected List<RequestHandler> createRequestHandlers() throws ServiceException
ServiceException
- if a problem occurs when creating the request handlerspublic abstract String getStaticFileLocation(VaadinRequest request)
request
- the request for which the location should be determinedpublic abstract String getConfiguredWidgetset(VaadinRequest request)
request
- the request for which a widgetset is requiredpublic abstract String getConfiguredTheme(VaadinRequest request)
request
- the request for which a theme is requiredpublic abstract boolean isStandalone(VaadinRequest request)
request
- the request for which the UI is loadedpublic ClassLoader getClassLoader()
null
setClassLoader(ClassLoader)
public void setClassLoader(ClassLoader classLoader)
classLoader
- the new class loader to set, not null
.getClassLoader()
public abstract String getMimeType(String resourceName)
resourceName
- a String specifying the name of a fileServletContext.getMimeType(String)
,
PortletContext#getMimeType(String)
public DeploymentConfiguration getDeploymentConfiguration()
public void setSystemMessagesProvider(SystemMessagesProvider systemMessagesProvider)
systemMessagesProvider
- the system messages provider; null
is not
allowed.getSystemMessagesProvider()
public SystemMessagesProvider getSystemMessagesProvider()
By default, the DefaultSystemMessagesProvider
which always
provides the built-in default SystemMessages
is used.
null
setSystemMessagesProvider(SystemMessagesProvider)
,
SystemMessagesProvider
,
SystemMessages
public SystemMessages getSystemMessages(Locale locale, VaadinRequest request)
locale
- the desired locale for the system messagesrequest
- public abstract File getBaseDirectory()
public void addSessionInitListener(SessionInitListener listener)
Because of the way different service instances share the same session, the listener is not necessarily notified immediately when the session is created but only when the first request for that session is handled by this service.
listener
- the Vaadin service session initialization listenerremoveSessionInitListener(SessionInitListener)
,
SessionInitListener
public void removeSessionInitListener(SessionInitListener listener)
listener
- the Vaadin service session initialization listener to remove.addSessionInitListener(SessionInitListener)
public void addSessionDestroyListener(SessionDestroyListener listener)
The session being destroyed is locked and its UIs have been removed when the listeners are called.
listener
- the vaadin service session destroy listeneraddSessionInitListener(SessionInitListener)
public void fireSessionDestroy(VaadinSession vaadinSession)
vaadinSession
- The session to destroypublic void removeSessionDestroyListener(SessionDestroyListener listener)
listener
- the vaadin service session destroy listeneraddSessionDestroyListener(SessionDestroyListener)
public VaadinSession findVaadinSession(VaadinRequest request) throws ServiceException, SessionExpiredException
Handles locking of the session internally to avoid creation of duplicate sessions by two threads simultaneously.
request
- the request to get a vaadin service session for.null
if no session is found and this is a request for which a new
session shouldn't be created.ServiceException
SessionExpiredException
VaadinSession
protected Lock getSessionLock(WrappedSession wrappedSession)
This method uses the wrapped session instead of VaadinSession to be able to lock even before the VaadinSession has been initialized.
wrappedSession
- The wrapped sessionprotected void lockSession(WrappedSession wrappedSession)
VaadinSession.lock()
instead of this method.wrappedSession
- The session to lockIllegalStateException
- if the session is invalidated before it can be lockedprotected void unlockSession(WrappedSession wrappedSession)
VaadinSession.unlock()
instead of this
method.wrappedSession
- The session to unlock@Deprecated protected URL getApplicationUrl(VaadinRequest request) throws MalformedURLException
LegacyApplication
.This is only used to support legacy cases.
request
- MalformedURLException
protected VaadinSession createVaadinSession(VaadinRequest request) throws ServiceException
request
- The request for which to create a VaadinSessionServiceException
protected VaadinSession getExistingSession(VaadinRequest request, boolean allowSessionCreation) throws SessionExpiredException
SessionExpiredException
protected abstract boolean requestCanCreateSession(VaadinRequest request)
request
- the requesttrue
if it's valid to create a new service session
for the request; else false
public static VaadinService getCurrent()
InheritableThreadLocal
). In other cases, (e.g. from background
threads started in some other way), the current service is not
automatically defined.null
setCurrentInstances(VaadinRequest, VaadinResponse)
public void setCurrentInstances(VaadinRequest request, VaadinResponse response)
The application developer can also use this method to define the current instances outside the normal request handling, e.g. when initiating custom background threads.
request
- the Vaadin request to set as the current request, or
null
if no request should be set.response
- the Vaadin response to set as the current response, or
null
if no response should be set.getCurrent()
,
getCurrentRequest()
,
getCurrentResponse()
public static void setCurrent(VaadinService service)
service
- public static VaadinRequest getCurrentRequest()
null
setCurrentInstances(VaadinRequest, VaadinResponse)
public static VaadinResponse getCurrentResponse()
null
setCurrentInstances(VaadinRequest, VaadinResponse)
public abstract String getServiceName()
public UI findUI(VaadinRequest request)
UI
that belongs to the provided request. This is
generally only supported for UIDL requests as other request types are not
related to any particular UI or have the UI information encoded in a
non-standard way. The returned UI is also set as the current UI (
UI.setCurrent(UI)
).request
- the request for which a UI is desiredpublic boolean preserveUIOnRefresh(UIProvider provider, UICreateEvent event)
window.name
so that it can be re-used if the browser window
is reloaded. This is typically determined by the UI provider which
typically checks the @PreserveOnRefresh
annotation but UI
providers and ultimately VaadinService implementations may choose to
override the defaults.provider
- the UI provider responsible for the UIevent
- the UI create event with details about the UItrue
if the UI should be preserved on refresh;
false
if a new UI instance should be initialized on
refreshed.public static void reinitializeSession(VaadinRequest request)
Please note that this method makes certain assumptions about how data is stored in the underlying session and may thus not be compatible with some environments.
request
- The Vaadin request for which the session should be
reinitializedpublic abstract InputStream getThemeResourceAsStream(UI ui, String themeName, String resource) throws IOException
ui
- The ui for which to find the resourcethemeName
- The name of the themeresource
- The name of the resource, e.g. "layouts/mycustomlayout.html"IOException
- if a problem occurred while finding or opening the resourcepublic abstract String getMainDivId(VaadinSession session, VaadinRequest request, Class<? extends UI> uiClass)
session
- The service session to which the bootstrapped UI will belong.request
- The request for which a div id is neededuiClass
- The class of the UI that will be bootstrappedpublic void closeSession(VaadinSession session)
After the session has been discarded, any UIs that have been left open will give a Session Expired error and a new session will be created for serving new UIs.
To avoid causing out of sync errors, you should typically redirect to
some other page using Page.setLocation(String)
to make the
browser unload the invalidated UI.
session
- the session to closeSystemMessages.getSessionExpiredCaption()
public void requestStart(VaadinRequest request, VaadinResponse response)
request
- The requestresponse
- The responsepublic void requestEnd(VaadinRequest request, VaadinResponse response, VaadinSession session)
request
- The request objectresponse
- The response objectsession
- The session which was used during the request or null if the
request did not use a sessionpublic Iterable<RequestHandler> getRequestHandlers()
createRequestHandlers()
public void handleRequest(VaadinRequest request, VaadinResponse response) throws ServiceException
getRequestHandlers()
for handling the request.
If a session expiration is detected during request handling then each
request handler
has an opportunity to handle the
expiration event if it implements SessionExpiredHandler
. If no
request handler handles session expiration a default expiration message
will be written.
request
- The incoming requestresponse
- The outgoing responseServiceException
- Any exception that occurs during response handling will be
wrapped in a ServiceExceptionpublic void writeStringResponse(VaadinResponse response, String contentType, String reponseString) throws IOException
response
- The response referencecontentType
- The content type of the responsereponseString
- The actual responseIOException
- If an error occured while writing the responseprotected void handleSessionExpired(VaadinRequest request, VaadinResponse response) throws ServiceException
request
- The requestresponse
- The responseServiceException
- Thrown if there was any problem handling the expiration of
the sessionpublic static String createCriticalNotificationJSON(String caption, String message, String details, String url)
caption
- The caption of the error or null to omitmessage
- The error message or null to omitdetails
- Additional error details or null to omiturl
- A url to redirect to. If no other details are given then the
user will be immediately redirected to this URL. Otherwise the
message will be shown and the browser will redirect to the
given URL only after the user acknowledges the message. If
null then the browser will refresh the current page.@Deprecated public void criticalNotification(VaadinRequest request, VaadinResponse response, String caption, String message, String details, String url) throws IOException
IOException
public boolean ensurePushAvailable()
true
if push can be used; false
if push
is not available.protected boolean isAtmosphereAvailable()
public static void verifyNoOtherSessionLocked(VaadinSession session)
VaadinSession
instance is not locked. This is
internally used by VaadinSession.accessSynchronously(Runnable)
and UI.accessSynchronously(Runnable)
to help avoid causing
deadlocks.session
- the session that is being lockedIllegalStateException
- if the current thread holds the lock for another sessionpublic static boolean isOtherSessionLocked(VaadinSession session)
VaadinSession
other than the
provided one for which the current thread holds a lock. This method might
not detect all cases where some other session is locked, but it should
cover the most typical situations.session
- the session that is expected to be lockedtrue
if another session is also locked by the
current thread; false
if no such session was foundpublic static boolean isCsrfTokenValid(VaadinSession session, String requestToken)
This protection is enabled by default, but it might need to be disabled
to allow a certain type of testing. For these cases, the check can be
disabled by setting the init parameter
disable-xsrf-protection
to true
.
session
- the vaadin session for which the check should be donerequestToken
- the CSRF token provided in the requesttrue
if the token is valid or if the protection is
disabled; false
if protection is enabled and the
token is invalidDeploymentConfiguration.isXsrfProtectionEnabled()
public Future<Void> accessSession(VaadinSession session, Runnable runnable)
VaadinSession.access(Runnable)
. This method is
implemented here instead of in VaadinSession
to enable overriding
the implementation without using a custom subclass of VaadinSession.session
- the vaadin session to accessrunnable
- the runnable to run with the session lockedVaadinSession.access(Runnable)
public void ensureAccessQueuePurged(VaadinSession session)
session
- the session for which the access queue should be purgedpublic void runPendingAccessTasks(VaadinSession session)
VaadinSession.access(Runnable)
.
This method is automatically run by the framework at appropriate situations and is not intended to be used by application developers.
session
- the vaadin session to purge the queue forpublic void addServiceDestroyListener(ServiceDestroyListener listener)
listener
- the service destroy listener to adddestroy()
,
removeServiceDestroyListener(ServiceDestroyListener)
,
ServiceDestroyListener
public void removeServiceDestroyListener(ServiceDestroyListener listener)
addServiceDestroyListener(ServiceDestroyListener)
.listener
- the service destroy listener to removepublic void destroy()
addServiceDestroyListener(ServiceDestroyListener)
,
Servlet.destroy()
,
Portlet#destroy()
protected void setDefaultClassLoader()
VaadinService
if found. If current security policy disallows
acquiring class loader instance it will log a message and re-throw
SecurityException
SecurityException
- If current security policy forbids acquiring class loaderprotected void storeSession(VaadinSession session, WrappedSession wrappedSession)
By default stores the VaadinSession in the underlying HTTP session.
session
- the VaadinSession to storewrappedSession
- the underlying HTTP sessionprotected void writeToHttpSession(WrappedSession wrappedSession, VaadinSession session)
wrappedSession
- the underlying HTTP sessionsession
- the VaadinSession to storeprotected VaadinSession loadSession(WrappedSession wrappedSession)
wrappedSession
- the underlying HTTP sessionprotected VaadinSession readFromHttpSession(WrappedSession wrappedSession)
Called by loadSession(WrappedSession)
.
wrappedSession
- the underlying HTTP sessionpublic void removeSession(WrappedSession wrappedSession)
wrappedSession
- the underlying HTTP sessionprotected void removeFromHttpSession(WrappedSession wrappedSession)
wrappedSession
- the underlying HTTP sessionprotected String getSessionAttributeName()
Copyright © 2019 Vaadin Ltd. All rights reserved.