Class Utils


  • public class Utils
    extends Object
    • Field Detail

      • LOGGER

        protected static final org.slf4j.Logger LOGGER
    • Constructor Detail

      • Utils

        public Utils()
    • Method Detail

      • parseTemplate

        public static void parseTemplate​(String template,
                                         XWikiContext context)
                                  throws XWikiException
        Generate the response by parsing a velocity template and printing the result to the Response. This is the main entry point to the View part of the XWiki MVC architecture.
        Parameters:
        template - The name of the template to parse, without the .vm prefix. The template will be searched in the usual places: current XWikiSkins object, attachment of the current skin document, current skin folder, baseskin folder, /templates/ folder.
        context - the current context
        Throws:
        XWikiException - when the response cannot be written to the client (for example when the client canceled the request, thus closing the socket)
        See Also:
        XWiki.parseTemplate(String, XWikiContext)
      • parseTemplate

        public static void parseTemplate​(String template,
                                         boolean write,
                                         XWikiContext context)
                                  throws XWikiException
        Generate the response by parsing a velocity template and (optionally) printing the result to the Response.
        Parameters:
        template - The name of the template to parse, without the .vm prefix. The template will be searched in the usual places: current XWikiSkins object, attachment of the current skin document, current skin folder, baseskin folder, /templates/ folder.
        write - Whether the generated response should be written to the client or not. If false, only the needed headers are generated, suitable for implementing a HEAD response.
        context - the current context
        Throws:
        XWikiException - when the response cannot be written to the client (for example when the client canceled the request, thus closing the socket)
        See Also:
        XWiki.parseTemplate(String, XWikiContext)
      • getRedirect

        public static String getRedirect​(XWikiRequest request,
                                         String defaultRedirect)
        Retrieve the URL to which the client should be redirected after the successful completion of the requested action. This is taken from the xredirect parameter in the query string. If this parameter is not set, or is set to an empty value, return the default redirect specified as the second argument.
        Parameters:
        request - the current request
        defaultRedirect - the default value to use if no xredirect parameter is present
        Returns:
        the destination URL, as specified in the xredirect parameter, or the specified default URL
      • getRedirect

        public static String getRedirect​(String action,
                                         String queryString,
                                         XWikiContext context)
        Retrieve the URL to which the client should be redirected after the successful completion of the requested action. This is taken from the xredirect parameter in the query string. If this parameter is not set, or is set to an empty value, compose an URL back to the current document, using the specified action and query string, and return it.
        Parameters:
        action - the XWiki action to use for composing the default redirect URL (view, edit, etc)
        queryString - the query parameters to append to the fallback URL
        context - the current context
        Returns:
        the destination URL, as specified in the xredirect parameter, or computed using the current document and the specified action and query string
      • getRedirect

        public static String getRedirect​(String action,
                                         String queryString,
                                         String... redirectParameters)
        Retrieve the URL to which the client should be redirected after the successful completion of the requested action. If any of the specified redirectParameters (in order) is present in the query string, it is returned as the redirect destination. If none of the parameters is set, compose an URL back to the current document using the specified action and query string, and return it.
        Parameters:
        action - the XWiki action to use for composing the default redirect URL (view, edit, etc)
        queryString - the query parameters to append to the fallback URL
        redirectParameters - list of request parameters to look for as the redirect destination; each of the parameters is tried in the order they are passed, and the first one set to a non-empty value is returned, if any
        Returns:
        the destination URL, as specified in one of the redirectParameters, or computed using the current document and the specified action and query string
      • getRedirect

        public static String getRedirect​(String action,
                                         XWikiContext context)
        Retrieve the URL to which the client should be redirected after the successful completion of the requested action. This is taken from the xredirect parameter in the query string. If this parameter is not set, or is set to an empty value, compose an URL back to the current document, using the specified action, and return it.
        Parameters:
        action - the XWiki action to use for composing the default redirect URL (view, edit, etc)
        context - the current context
        Returns:
        the destination URL, as specified in the xredirect parameter, or computed using the current document and the specified action
      • getPage

        public static String getPage​(XWikiRequest request,
                                     String defaultpage)
        Retrieve the name of the velocity template which should be used to generate the response. This is taken from the xpage parameter in the query string. If this parameter is not set, or is set to an empty value, return the provided default name.
        Parameters:
        request - the current request
        defaultpage - the default value to use if no xpage parameter is set
        Returns:
        the name of the requested template, as specified in the xpage parameter, or the specified default template
      • getFileName

        public static String getFileName​(List<org.apache.commons.fileupload.FileItem> filelist,
                                         String name)
        Get the name of an uploaded file, corresponding to the specified form field.
        Parameters:
        filelist - the list of uploaded files, computed by the FileUpload plugin
        name - the name of the form field
        Returns:
        the original name of the file, if the specified field name does correspond to an uploaded file, or null otherwise
      • getContent

        public static byte[] getContent​(List<org.apache.commons.fileupload.FileItem> filelist,
                                        String name)
                                 throws XWikiException
        Get the content of an uploaded file, corresponding to the specified form field.
        Parameters:
        filelist - the list of uploaded files, computed by the FileUpload plugin
        name - the name of the form field
        Returns:
        the content of the file, if the specified field name does correspond to an uploaded file, or null otherwise
        Throws:
        XWikiException - if the file cannot be read due to an underlying I/O exception
      • parseParameters

        public static Map<String,​String[]> parseParameters​(String data,
                                                                 String encoding)
                                                          throws UnsupportedEncodingException
        Parse the request parameters from the specified String using the specified encoding. IMPLEMENTATION NOTE: URL decoding is performed individually on the parsed name and value elements, rather than on the entire query string ahead of time, to properly deal with the case where the name or value includes an encoded = or & character that would otherwise be interpreted as a delimiter.

        Code borrowed from Apache Tomcat 5.0

        Parameters:
        data - input string containing request parameters
        encoding - the encoding to use for transforming bytes into characters
        Throws:
        IllegalArgumentException - if the data is malformed
        UnsupportedEncodingException
      • parseParameters

        public static Map<String,​String[]> parseParameters​(byte[] data,
                                                                 String encoding)
                                                          throws UnsupportedEncodingException
        Parse the request parameters from the specified byte array using the specified encoding. IMPLEMENTATION NOTE: URL decoding is performed individually on the parsed name and value elements, rather than on the entire query string ahead of time, to properly deal with the case where the name or value includes an encoded = or & character that would otherwise be interpreted as a delimiter.

        NOTE: byte array data is modified by this method. Caller beware.

        Code borrowed from Apache Tomcat 5.0

        Parameters:
        data - input byte array containing request parameters
        encoding - Encoding to use for converting hex
        Throws:
        UnsupportedEncodingException - if the data is malformed
      • formEncode

        @Deprecated
        public static String formEncode​(String value)
        Deprecated.
        starting with 2.7 use $services.xml.escape(content)
        Escapes the XML special characters in a String using numerical XML entities.
        Parameters:
        value - the text to escape, may be null
        Returns:
        a new escaped String, null if null input
      • SQLFilter

        public static String SQLFilter​(String text)
      • handleMultipart

        public static FileUploadPlugin handleMultipart​(javax.servlet.http.HttpServletRequest request,
                                                       XWikiContext context)
        Process a multi-part request, extracting all the uploaded files.
        Parameters:
        request - the current request to process
        context - the current context
        Returns:
        the instance of the FileUploadPlugin used to parse the uploaded files
      • setComponentManager

        public static void setComponentManager​(org.xwiki.component.manager.ComponentManager componentManager)
        Parameters:
        componentManager - the root component manager used by getComponent(Class) and getComponent(Class, String)
      • getRootComponentManager

        @Deprecated
        public static org.xwiki.component.manager.ComponentManager getRootComponentManager()
        Deprecated.
        last resort way of accessing the ComponentManager, make sure you cannot do it any other way possible since it add a strong dependency to a static to your code
        Returns:
        the root component manager
      • getContextComponentManager

        @Deprecated
        public static org.xwiki.component.manager.ComponentManager getContextComponentManager()
        Deprecated.
        last resort way of accessing the ComponentManager, make sure you cannot do it any other way possible since it add a strong dependency to a static to your code
        Returns:
        the contextual component manager used by getComponent(Class) and getComponent(Class, String)
        Since:
        6.0RC1
      • getComponent

        @Deprecated
        public static <T> T getComponent​(Class<T> role,
                                         String hint)
        Deprecated.
        since 4.0M1 use getComponent(Type, String) instead
        Lookup a XWiki component by role and hint.
        Parameters:
        role - the class (aka role) that the component implements
        hint - a value to differentiate different component implementations for the same role
        Returns:
        the component's instance
        Throws:
        RuntimeException - if the component cannot be found/initialized, or if the component manager is not initialized
      • getComponent

        @Deprecated
        public static <T> T getComponent​(Class<T> role)
        Deprecated.
        since 4.0M1 use getComponent(Type) instead
        Lookup a XWiki component by role (uses the default hint).
        Parameters:
        role - the class (aka role) that the component implements
        Returns:
        the component's instance
        Throws:
        RuntimeException - if the component cannot be found/initialized, or if the component manager is not initialized
      • getComponent

        @Deprecated
        public static <T> T getComponent​(Type roleType,
                                         String roleHint)
        Deprecated.
        starting with 4.1M2 use the Component Script Service instead
        Lookup a XWiki component by role and hint.
        Parameters:
        roleType - the class (aka role) that the component implements
        roleHint - a value to differentiate different component implementations for the same role
        Returns:
        the component's instance
        Throws:
        RuntimeException - if the component cannot be found/initialized, or if the component manager is not initialized
      • getComponent

        @Deprecated
        public static <T> T getComponent​(Type roleType)
        Deprecated.
        starting with 4.1M2 use the Component Script Service instead
        Lookup a XWiki component by role (uses the default hint).
        Parameters:
        roleType - the class (aka role) that the component implements
        Returns:
        the component's instance
        Throws:
        RuntimeException - if the component cannot be found/initialized, or if the component manager is not initialized
      • getComponentList

        @Deprecated
        public static <T> List<T> getComponentList​(Class<T> role)
        Deprecated.
        since 4.0M1 use getComponentManager() instead
        Type Parameters:
        T - the component type
        Parameters:
        role - the role for which to return implementing components
        Returns:
        all components implementing the passed role
        Throws:
        RuntimeException - if some of the components cannot be found/initialized, or if the component manager is not initialized
        Since:
        2.0M3
      • getContext

        public static XWikiContext getContext()
        Helper method for obtaining a valid xcontext from the execution context.

        NOTE: Don't use this method to access the XWiki context in a component because setComponentManager(ComponentManager) is not called when running component unit tests. You have to take the XWiki context yourself from the injected Execution when inside a component. This method should be used only by non-component code.

        Returns:
        the current context or null if the execution context is not yet initialized
        Since:
        3.2M3
      • arePlaceholdersEnabled

        public static boolean arePlaceholdersEnabled​(XWikiContext context)
        Check if placeholders are enabled in the current context.
        Parameters:
        context - The current context.
        Returns:
        true if placeholders can be used, false otherwise.
      • enablePlaceholders

        public static void enablePlaceholders​(XWikiContext context)
        Enable placeholder support in the current request context.
        Parameters:
        context - The current context.
      • disablePlaceholders

        public static void disablePlaceholders​(XWikiContext context)
        Disable placeholder support in the current request context.
        Parameters:
        context - The current context.
      • createPlaceholder

        public static String createPlaceholder​(String value,
                                               XWikiContext context)
        Create a placeholder key for a string that should be protected from further processing. The value is stored in the context, and the returned key can be used by the calling code as many times in the rendering result. At the end of the rendering process all placeholder keys are replaced with the values they replace.
        Parameters:
        value - The string to hide.
        context - The current context.
        Returns:
        The key to be used instead of the value.
      • replacePlaceholders

        public static String replacePlaceholders​(String content,
                                                 XWikiContext context)
        Insert back the replaced strings.
        Parameters:
        content - The rendered content, with placeholders.
        context - The current context.
        Returns:
        The content with all placeholders replaced with the real values.
      • isAjaxRequest

        public static Boolean isAjaxRequest​(XWikiContext context)
        Verify if the current request is an AJAX request.
        Parameters:
        context - the current request context
        Returns:
        True if this is an AJAX request, false otherwise.
        Since:
        2.4M2