com.xpn.xwiki.web
Class Utils

java.lang.Object
  extended by com.xpn.xwiki.web.Utils

public class Utils
extends Object


Field Summary
protected static org.slf4j.Logger LOGGER
           
 
Constructor Summary
Utils()
           
 
Method Summary
static boolean arePlaceholdersEnabled(XWikiContext context)
          Check if placeholders are enabled in the current context.
static String createPlaceholder(String value, XWikiContext context)
          Create a placeholder key for a string that should be protected from further processing.
static String decode(String text, XWikiContext context)
          Deprecated. replaced by Util.decodeURI(String, XWikiContext) since 1.3M2
static void disablePlaceholders(XWikiContext context)
          Disable placeholder support in the current request context.
static void enablePlaceholders(XWikiContext context)
          Enable placeholder support in the current request context.
static String encode(String text, XWikiContext context)
          Deprecated. replaced by Util.encodeURI(String, XWikiContext) since 1.3M2
static String formEncode(String value)
          Deprecated. starting with 2.7 use $services.xml.escape(content)
static
<T> T
getComponent(Class<T> role)
          Deprecated. since 4.0M1 use getComponent(Type) instead
static
<T> T
getComponent(Class<T> role, String hint)
          Deprecated. since 4.0M1 use getComponent(Type, String) instead
static
<T> T
getComponent(Type roleType)
          Deprecated. starting with 4.1M2 use the Component Script Service instead
static
<T> T
getComponent(Type roleType, String roleHint)
          Deprecated. starting with 4.1M2 use the Component Script Service instead
static
<T> List<T>
getComponentList(Class<T> role)
          Deprecated. since 4.0M1 use getComponentManager() instead
static org.xwiki.component.manager.ComponentManager getComponentManager()
          Deprecated. starting with 4.1M2 use the Component Script Service instead
static byte[] getContent(List<org.apache.commons.fileupload.FileItem> filelist, String name)
          Get the content of an uploaded file, corresponding to the specified form field.
static XWikiContext getContext()
          Helper method for obtaining a valid xcontext from the execution context.
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.
static String getPage(XWikiRequest request, String defaultpage)
          Retrieve the name of the velocity template which should be used to generate the response.
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.
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.
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.
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.
static FileUploadPlugin handleMultipart(javax.servlet.http.HttpServletRequest request, XWikiContext context)
          Process a multi-part request, extracting all the uploaded files.
static Boolean isAjaxRequest(XWikiContext context)
          Verify if the current request is an AJAX request.
static Map<String,String[]> parseParameters(byte[] data, String encoding)
          Parse the request parameters from the specified byte array using the specified encoding.
static Map<String,String[]> parseParameters(String data, String encoding)
          Parse the request parameters from the specified String using the specified encoding.
static void parseTemplate(String template, boolean write, XWikiContext context)
          Generate the response by parsing a velocity template and (optionally) printing the result to the Response.
static void parseTemplate(String template, XWikiContext context)
          Generate the response by parsing a velocity template and printing the result to the Response.
static XWikiContext prepareContext(String action, XWikiRequest request, XWikiResponse response, XWikiEngineContext engine_context)
           
static String replacePlaceholders(String content, XWikiContext context)
          Insert back the replaced strings.
static void setComponentManager(org.xwiki.component.manager.ComponentManager componentManager)
           
static String SQLFilter(String text)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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

prepareContext

public static XWikiContext prepareContext(String action,
                                          XWikiRequest request,
                                          XWikiResponse response,
                                          XWikiEngineContext engine_context)
                                   throws XWikiException
Throws:
XWikiException

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)

encode

@Deprecated
public static String encode(String text,
                                       XWikiContext context)
Deprecated. replaced by Util.encodeURI(String, XWikiContext) since 1.3M2


decode

@Deprecated
public static String decode(String text,
                                       XWikiContext context)
Deprecated. replaced by Util.decodeURI(String, XWikiContext) since 1.3M2


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 component manager used by getComponent(Class) and getComponent(Class, String)

getComponentManager

@Deprecated
public static org.xwiki.component.manager.ComponentManager getComponentManager()
Deprecated. starting with 4.1M2 use the Component Script Service instead

Returns:
the component manager used by getComponent(Class) and getComponent(Class, String)

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


Copyright © 2004–2014 XWiki. All rights reserved.