Class Utilities


  • public final class Utilities
    extends Object
    Various static utility methods.
    • Method Detail

      • close

        public static void close​(Connection connection,
                                 Statement stmt,
                                 ResultSet rs)
                          throws SQLException
        Convenience method to close resources in a safe way.
        Parameters:
        connection - a connection, or null
        stmt - a statement, or null
        rs - a resultset, or null
        Throws:
        SQLException
      • closeQuietly

        public static void closeQuietly​(Connection connection,
                                        Statement stmt,
                                        ResultSet rs,
                                        Logger log)
        Convenience method to close resources in a safe way. If a logger is provided any exception will be logged at level fine.
        Parameters:
        connection - a connection, or null
        stmt - a statement, or null
        rs - a resultset, or null
        log - a logger, or null
      • getDataSource

        public static DataSource getDataSource​(String databaseName)
                                        throws NamingException
        Returns the data source with the specified name.
        Parameters:
        databaseName - The name of the database.
        Returns:
        The data source for creating connections to the database.
        Throws:
        NamingException - If the datasource for the database could not be returned.
      • getMediaRepositoryDataSourceName

        public static String getMediaRepositoryDataSourceName()
        Returns the data source name for the Media repository as configured through the configuration management service.
        Returns:
        the data source name for the Media repository as configured through the configuration management service
      • getMediaRepositoryDataSource

        public static DataSource getMediaRepositoryDataSource()
        Returns the data source for the Media repository as configured through the configuration management service.
        Returns:
        the data source for the Media repository as configured through the configuration management service
      • getCurrentDateFromMediaRepository

        public static Date getCurrentDateFromMediaRepository()
        Retrieves the current date from the database. Date comparisons on publication/expiration date are done in the database server. Therefore, to avoid problems with slightly different clocks on the application server and the database server the current datetime must be retrieved from the database.
        This is mostly problematic in integration tests.
        Returns:
        current date according to the database server.
      • getConfigurationService

        public static ConfigurationManagement getConfigurationService()
        Returns the configuration management service which can be used to retrieve configuration parameters
        Returns:
        Configuration management service
      • getAuthorizationService

        public static AuthorizationService getAuthorizationService()
        Returns the authorization management service which can be used to check permissions
        Returns:
        Configuration management service
      • getProfilerService

        public static ProfilerService getProfilerService()
        Returns the profiler service which can be used to profile stuff. If an instance of the framework is available, this method is guaranteed to return an instance of the profiler service. This, however, may be a dummy if for some reason the service is not available.
        Returns:
        Profiler service
      • getJcrService

        public static JcrRepository getJcrService()
        Returns the JCR service
        Returns:
        JCR service
      • getInternationalizationService

        public static nl.gx.webmanager.services.internationalization.InternationalizationService getInternationalizationService()
        Returns the InternationalizationService.
        Returns:
        InternationalizationService
      • getSessionService

        public static SessionManager getSessionService()
        Returns the WebManager session service.
        Returns:
        The WebManager session service
      • getQueryManager

        public static JcrIndexQueryManager getQueryManager()
        Returns the Query manager service.
        Returns:
        The Query Manager service
      • getFileDistributionService

        public static nl.gx.webmanager.services.filedistribution.FileDistributionService getFileDistributionService()
        Returns the FileDistributionService.
        Returns:
        The FileDistributionService.
      • getFileTypeValidationService

        public static nl.gx.webmanager.services.filetypevalidation.FileTypeValidationService getFileTypeValidationService()
        Returns the FileTypeValidationService.
        Returns:
        The FileTypeValidationService.
      • getImageSize

        public static Dimension getImageSize​(InputStream in,
                                             String filename)
        The dimension of an image.
        Parameters:
        in - image input stream.
        Returns:
        the dimension.
      • getImageSize

        public static Dimension getImageSize​(File file)
        The dimension of an image.
        Parameters:
        file - image file.
        Returns:
        the dimension or null in case of IOExceptions
      • getCause

        public static Throwable getCause​(Throwable original)
        Returns the cause of a throwable. This method is useful since some Exceptions classes do not implement the getCause() method of Throwable but use their own getRootCause method.
        Parameters:
        original - The throwable of which the cause should be returned.
        Returns:
        The cause of the argument or null the cause is not available.
      • getRootCause

        public static Throwable getRootCause​(Throwable original)
        Returns the root cause of a throwable by getting recursively getting the causes of the original exception.
        Parameters:
        original - The throwable of which the root cause should be returned.
        Returns:
        The root cause of the argument, possibly original.
      • getCurrentWebUserGroups

        public static int[] getCurrentWebUserGroups​(javax.servlet.http.HttpServletRequest request)
        Get the list of webgroups that the current user belongs to.
        Parameters:
        request -
        Returns:
        an array of webgroup IDs.
      • isWebuserLoggedIn

        public static boolean isWebuserLoggedIn​(javax.servlet.http.HttpServletRequest request)
        Check if the request is from a logged in webuser.
        Parameters:
        request -
        Returns:
        True if it is a logged in webuser.
      • unescapeWmHostnameTag

        public static String unescapeWmHostnameTag​(String htmlEscapedUrl)
        Unescape the wm-hostname tag in a html escaped url.
        Parameters:
        htmlEscapedUrl - html escaped url in which the wm-hostname tag will be unescaped.
        Returns:
        html escaped url with html unescaped wm-hostname tag.
      • checkPersonalizationItems

        public static boolean checkPersonalizationItems​(javax.servlet.http.HttpServletRequest request,
                                                        javax.servlet.http.HttpServletResponse response,
                                                        PersonalizationItem[] personalizations,
                                                        String xslStylesheetBase)
        Returns true if all the personalizations allow access to enclosed content.
        Parameters:
        request - The current HttpServletRequest
        personalizations - All personalizations which should be checked against
        xslStylesheetBase - Base for xsl stylesheet that should be used to transform the session xml with
        Returns:
        true if all the personalizations allow access to enclosed content
      • getPrefixXml

        public static String getPrefixXml​(String xslHeader,
                                          javax.servlet.http.HttpServletRequest request,
                                          javax.servlet.http.HttpServletResponse response)
                                   throws IOException
        Collects the extra xml for personalization.
        Parameters:
        xslHeader - The comma delimited string of session/request keys that should be added to the resulting xml.
        Returns:
        The prefix xml for personalization
        Throws:
        IOException - If the xml could not be retrieved
      • getInContextTime

        protected static Date getInContextTime​(javax.servlet.http.HttpServletRequest httpServletRequest)
        Returns the in-context time in the server's timezone if the given httpServletRequest contains one in its parameter, parsed against the #IN_CONTEXT_TIME_DATE_FORMAT, returns null otherwise.
        Parameters:
        httpServletRequest - A HttpServletRequest object
        Returns:
        the in-context time in the server's timezone if the given httpServletRequest contains one in its parameter, parsed against the #IN_CONTEXT_TIME_DATE_FORMAT, returns null otherwise
      • getFileResourceOwner

        public static FileResourceSecurityCheckPoint getFileResourceOwner​(javax.servlet.http.HttpServletRequest request,
                                                                          Session session)
        Returns the owner object based on the current HttpServletRequest and the current Session, or null if it can't be determined.
        Parameters:
        request - The current HttpServletRequest.
        session - The current Session.
        Returns:
        The owner object based on the current HttpServletRequest and the current Session, or null if it can't be determined.
      • checkedOnMayView

        public static boolean checkedOnMayView​(javax.servlet.http.HttpServletRequest request,
                                               Session session,
                                               FileResourceSecurityPolicy objectToCheck)
        Returns true if for the given FileResourceSecurityPolicy object the mayView method has been called during the current HttpServletRequest and the current Session.
        Parameters:
        request - The current HttpServletRequest.
        session - The current Session.
        objectToCheck - Object which should be checked.
        Returns:
        True if for the given FileResourceSecurityPolicy object the mayView method has been called during the current HttpServletRequest and the current Session.
      • getSiteworksLinkType

        public static nl.gx.siteworks.core.ObjectType getSiteworksLinkType​(Class<? extends RelatedLink> relatedLinkClass)
        Get the sitework object type for the RelatedLink This method also exists in ElementHolderImpl for all other ElementHolder childs. It would be nice if this class could also extend ElementHolderImpl
        Parameters:
        relatedLinkClass - the API link class
        Returns:
        the sitework objecttype representing the API link class
      • getNextLinkCode

        public static String getNextLinkCode​(RelatedLink[] relatedLinks)
        Returns the next link code that can be used for a new link.
        Parameters:
        relatedLinks - Links with codes that are in use.
        Returns:
        the next link code
      • getOEmbedService

        public static nl.gx.webmanager.services.oembed.consumer.OEmbedConsumerService getOEmbedService()
        Returns the oEmbed service which can be used to access oEmbed endpoints.
        Returns:
        oEmbed service
      • isSecureRequest

        public static boolean isSecureRequest​(javax.servlet.http.HttpServletRequest request)
        Returns true when the request is secure (determined by the container or the forwarded protocol).
        Parameters:
        request - the request
        Returns:
        true when the request is secure
      • replaceLayoutUniqueIdsInElements

        public static void replaceLayoutUniqueIdsInElements​(ElementHolder version)
        Search for layout divs in richtextelement. The unique ids are replaced by new id values. Called when copying pages, articles and pagesections.
        Parameters:
        version - Elementholder with elements.
      • setUniqueIdInLayout

        public static void setUniqueIdInLayout​(org.jsoup.nodes.Document doc)
        Sets unique layout identifiers for layouts within the given JSoup document.
        Parameters:
        doc - Jsoup document with html.
      • normalize

        public static String normalize​(String text)
        Normalizes a text, so it can be used in a metatags contents attribute.
        Parameters:
        text - The text that should be normalized
        Returns:
        a normalized string without line breaks and L codes
        Since:
        10.32.0 - originally this was located in PageVersionImpl
      • getFirstText

        public static String getFirstText​(Element[] elements)
        Finds the first text element.
        Parameters:
        elements - the array of candidates
        Returns:
        The text of the first non-empty text element.
        Since:
        10.32.0 - originally this was located in PageVersionImpl