com.vaadin.server.

Class VaadinServlet

  • java.lang.Object
    • javax.servlet.GenericServlet
      • javax.servlet.http.HttpServlet
        • com.vaadin.server.VaadinServlet
    • Constructor Detail

      • VaadinServlet

        public VaadinServlet()
    • Method Detail

      • init

        public void init​(javax.servlet.ServletConfig servletConfig)
                  throws javax.servlet.ServletException

        Called by the servlet container to indicate to a servlet that the servlet is being placed into service.

        Specified by:

        init in interface javax.servlet.Servlet

        Overrides:

        init in class javax.servlet.GenericServlet

        Parameters:

        servletConfig - the object containing the servlet's configuration and initialization parameters

        Throws:

        javax.servlet.ServletException - if an exception has occurred that interferes with the servlet's normal operation.

      • servletInitialized

        protected void servletInitialized()
                                   throws javax.servlet.ServletException

        Throws:

        javax.servlet.ServletException

      • getCurrent

        public static VaadinServlet getCurrent()

        Gets the currently used Vaadin servlet. The current servlet is automatically defined when initializing the servlet and when processing requests to the server (see ThreadLocal) and in VaadinSession.access(Runnable) and UI.access(Runnable). In other cases, (e.g. from background threads), the current servlet is not automatically defined.

        The current servlet is derived from the current service using VaadinService.getCurrent()

        Returns:

        the current Vaadin servlet instance if available, otherwise null

        Since:

        7.0

      • createDeploymentConfiguration

        protected DeploymentConfiguration createDeploymentConfiguration()
                                                                 throws javax.servlet.ServletException

        Creates a deployment configuration to be used for the creation of a VaadinService. Intended to be used by dependency injection frameworks.

        Returns:

        the created deployment configuration

        Throws:

        javax.servlet.ServletException - if construction of the Properties for createDeploymentConfiguration(Properties) fails

        Since:

        8.2

      • createDeploymentConfiguration

        protected DeploymentConfiguration createDeploymentConfiguration​(Properties initParameters)

        Creates a deployment configuration to be used for the creation of a VaadinService. Override this if you want to override certain properties.

        Parameters:

        initParameters - the context-param and init-param values as properties

        Returns:

        the created deployment configuration

        Since:

        7.0.0

      • createServletService

        protected VaadinServletService createServletService​(DeploymentConfiguration deploymentConfiguration)
                                                     throws ServiceException

        Creates a vaadin servlet service.

        Parameters:

        deploymentConfiguration - the deployment configuration to be used

        Returns:

        the created vaadin servlet service

        Throws:

        ServiceException - if creating the vaadin servlet service fails

        Since:

        7.0.0

      • service

        protected void service​(javax.servlet.http.HttpServletRequest request,
                               javax.servlet.http.HttpServletResponse response)
                        throws javax.servlet.ServletException,
                               IOException

        Receives standard HTTP requests from the public service method and dispatches them.

        Overrides:

        service in class javax.servlet.http.HttpServlet

        Parameters:

        request - the object that contains the request the client made of the servlet.

        response - the object that contains the response the servlet returns to the client.

        Throws:

        javax.servlet.ServletException - if an input or output error occurs while the servlet is handling the TRACE request.

        IOException - if the request for the TRACE cannot be handled.

      • handleContextRootWithoutSlash

        protected boolean handleContextRootWithoutSlash​(javax.servlet.http.HttpServletRequest request,
                                                        javax.servlet.http.HttpServletResponse response)
                                                 throws IOException

        Invoked for every request to this servlet to potentially send a redirect to avoid problems with requests to the context root with no trailing slash.

        Parameters:

        request - the processed request

        response - the processed response

        Returns:

        true if a redirect has been sent and the request should not be processed further; false if the request should be processed as usual

        Throws:

        IOException - If an input or output exception occurs

      • getLastPathParameter

        protected static String getLastPathParameter​(String uri)

        Finds any path parameter added to the last part of the uri. A path parameter is any string separated by ";" from the path and ends in / or at the end of the string.

        For example the uri http://myhost.com/foo;a=1/bar;b=1 contains two path parameters, a=1 related to /foo and b=1 related to /bar.

        For http://myhost.com/foo;a=1/bar;b=1 this method will return ;b=1

        Parameters:

        uri - a URI

        Returns:

        the last path parameter of the uri including the semicolon or an empty string. Never null.

        Since:

        7.2

      • createVaadinRequest

        protected VaadinServletRequest createVaadinRequest​(javax.servlet.http.HttpServletRequest request)

        Creates a Vaadin request for a http servlet request. This method can be overridden if the Vaadin request should have special properties.

        Parameters:

        request - the original http servlet request

        Returns:

        a Vaadin request for the original request

      • getService

        protected VaadinServletService getService()

        Gets a the vaadin service for this servlet.

        Returns:

        the vaadin service

      • criticalNotification

        @Deprecated
        protected void criticalNotification​(VaadinServletRequest request,
                                            VaadinServletResponse response,
                                            String caption,
                                            String message,
                                            String details,
                                            String url)
                                     throws IOException
        Deprecated.
        As of 7.0. This method is retained only for backwards compatibility and for GAEVaadinServlet.

        Send a notification to client-side widgetset. Used to notify client of critical errors, session expiration and more. Server has no knowledge of what UI client refers to.

        Parameters:

        request - the HTTP request instance.

        response - the HTTP response to write to.

        caption - the notification caption

        message - to notification body

        details - a detail message to show in addition to the message. Currently shown directly below the message but could be hidden behind a details drop down in the future. Mainly used to give additional information not necessarily useful to the end user.

        url - url to load when the message is dismissed. Null will reload the current page.

        Throws:

        IOException - if the writing failed due to input/output error.

      • getResourcePath

        @Deprecated
        protected static String getResourcePath​(javax.servlet.ServletContext servletContext,
                                                String path)
        Deprecated.
        As of 7.0. Will likely change or be removed in a future version

        Gets resource path using different implementations. Required to supporting different servlet container implementations (application servers).

        Parameters:

        servletContext - the ServletContext in which this servlet is running

        path - the resource path.

        Returns:

        the resource path.

      • stripSpecialChars

        @Deprecated
        public static String stripSpecialChars​(String themeName)
        Deprecated.
        As of 7.0. Will likely change or be removed in a future version

        A helper method to strip away characters that might somehow be used for XSS attacks. Leaves at least alphanumeric characters intact. Also removes e.g. '(' and ')', so values should be safe in javascript too.

        Parameters:

        themeName - name of the theme

        Returns:

        name of the theme without special characters

      • getDefaultTheme

        public static String getDefaultTheme()

        Returns the default theme. Must never return null.

        Returns:

        default theme name

      • serveStaticResources

        protected boolean serveStaticResources​(javax.servlet.http.HttpServletRequest request,
                                               javax.servlet.http.HttpServletResponse response)
                                        throws IOException,
                                               javax.servlet.ServletException

        Check if this is a request for a static resource and, if it is, serve the resource to the client.

        Parameters:

        request - The request

        response - The response

        Returns:

        true if a file was served and the request has been handled; false otherwise.

        Throws:

        IOException

        javax.servlet.ServletException

        Since:

        8.5

      • serveStaticResourcesInVAADIN

        protected void serveStaticResourcesInVAADIN​(String filename,
                                                    javax.servlet.http.HttpServletRequest request,
                                                    javax.servlet.http.HttpServletResponse response)
                                             throws IOException,
                                                    javax.servlet.ServletException

        Serve resources from VAADIN directory.

        Parameters:

        filename - The filename to serve. Should always start with /VAADIN/.

        request - The request

        response - The response

        Throws:

        IOException - if an I/O exception occurs

        javax.servlet.ServletException - if a servlet exception occurs

        Since:

        8.5

      • getCacheTime

        protected int getCacheTime​(String filename)

        Calculates the cache lifetime for the given filename in seconds. By default filenames containing ".nocache." return 0, filenames containing ".cache." return one year, all other return the value defined in the web.xml using resourceCacheTime (defaults to 1 hour).

        Parameters:

        filename - the filename

        Returns:

        cache lifetime for the given filename in seconds

      • writeStaticResourceResponse

        protected void writeStaticResourceResponse​(javax.servlet.http.HttpServletRequest request,
                                                   javax.servlet.http.HttpServletResponse response,
                                                   URL resourceUrl)
                                            throws IOException

        Writes the contents of the given resourceUrl in the response. Can be overridden to add/modify response headers and similar.

        WARNING: note that this should not be used for a resourceUrl that represents a directory! For security reasons, the directory contents should not be ever written into the response, and the implementation which is used for setting the content length relies on URLConnection.getContentLength() method which returns incorrect values for directories.

        Parameters:

        request - The request for the resource

        response - The response

        resourceUrl - The url to send

        Throws:

        IOException - if an I/O exception occurs

      • allowServePrecompressedResource

        protected boolean allowServePrecompressedResource​(javax.servlet.http.HttpServletRequest request,
                                                          String url)

        Returns whether this servlet should attempt to serve a precompressed version of the given static resource. If this method returns true, the suffix .gz is appended to the URL and the corresponding resource is served if it exists. It is assumed that the compression method used is gzip. If this method returns false or a compressed version is not found, the original URL is used. The base implementation of this method returns true if and only if the request indicates that the client accepts gzip compressed responses and the filename extension of the requested resource is .js, .css, or .html.

        Parameters:

        request - the request for the resource

        url - the URL of the requested resource

        Returns:

        true if the servlet should attempt to serve a precompressed version of the resource, false otherwise

        Since:

        7.5.0

      • findResourceURL

        public URL findResourceURL​(String filename)
                            throws IOException

        Finds the given resource from the web content folder or using the class loader.

        Parameters:

        filename - The file to find, starting with a "/"

        Returns:

        The URL to the given file, or null if the file was not found

        Throws:

        IOException - if there was a problem while locating the file

        Since:

        7.7

      • isAllowedVAADINResourceUrl

        @Deprecated
        protected boolean isAllowedVAADINResourceUrl​(javax.servlet.http.HttpServletRequest request,
                                                     URL resourceUrl)
        Deprecated.
        As of 7.0. Will likely change or be removed in a future version

        Check whether a URL obtained from a classloader refers to a valid static resource in the directory VAADIN. Directories do not count as valid resources. Warning: Overriding of this method is not recommended, but is possible to support non-default classloaders or servers that may produce URLs different from the normal ones. The method prototype may change in the future. Care should be taken not to expose class files or other resources outside the VAADIN directory if the method is overridden.

        Parameters:

        request - current request

        resourceUrl - URL of the resource to validate

        Returns:

        true if the resource is a valid VAADIN resource, false otherwise

        Since:

        6.6.7

      • getRequestType

        @Deprecated
        protected VaadinServlet.RequestType getRequestType​(VaadinServletRequest request)
        Deprecated.
        As of 7.0. This is no longer used and only provided for backwards compatibility. Each RequestHandler can individually decide whether it wants to handle a request or not.

        Parameters:

        request - the request that is to be evaluated

        Returns:

        request type

      • isStaticResourceRequest

        protected boolean isStaticResourceRequest​(javax.servlet.http.HttpServletRequest request)
      • getStaticFilePath

        protected String getStaticFilePath​(javax.servlet.http.HttpServletRequest request)

        Returns the relative path at which static files are served for a request (if any).

        NOTE: This method does not check whether the requested resource is a directory and as such not a valid VAADIN resource.

        Parameters:

        request - HTTP request

        Returns:

        relative servlet path or null if the request path does not contain "/VAADIN/" or the request has no path info

        Since:

        8.0

      • getApplicationUrl

        @Deprecated
        protected URL getApplicationUrl​(javax.servlet.http.HttpServletRequest request)
                                 throws MalformedURLException
        Deprecated.
        As of 7.0. Will likely change or be removed in a future version

        Gets the current application URL from request.

        Parameters:

        request - the HTTP request.

        Returns:

        current application URL

        Throws:

        MalformedURLException - if the application is denied access to the persistent data store represented by the given URL.

      • destroy

        public void destroy()

        Specified by:

        destroy in interface javax.servlet.Servlet

        Overrides:

        destroy in class javax.servlet.GenericServlet

      • safeEscapeForHtml

        @Deprecated
        public static final String safeEscapeForHtml​(String unsafe)
        Deprecated.
        As of 7.0. Will likely change or be removed in a future version

        Escapes characters to html entities. An exception is made for some "safe characters" to keep the text somewhat readable.

        Parameters:

        unsafe - the string that needs to be made safe

        Returns:

        a safe string to be added inside an html tag