Package com.xpn.xwiki.web
Class Utils
- java.lang.Object
-
- com.xpn.xwiki.web.Utils
-
public class Utils extends Object
-
-
Field Summary
Fields Modifier and Type Field Description protected static org.slf4j.Logger
LOGGER
-
Constructor Summary
Constructors Constructor Description Utils()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description 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 byUtil.decodeURI(String, XWikiContext)
since 1.3M2static 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 byUtil.encodeURI(String, XWikiContext)
since 1.3M2static 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 usegetComponent(Type)
insteadstatic <T> T
getComponent(Class<T> role, String hint)
Deprecated.since 4.0M1 usegetComponent(Type, String)
insteadstatic <T> T
getComponent(Type roleType)
Deprecated.starting with 4.1M2 use the Component Script Service insteadstatic <T> T
getComponent(Type roleType, String roleHint)
Deprecated.starting with 4.1M2 use the Component Script Service insteadstatic <T> List<T>
getComponentList(Class<T> role)
Deprecated.since 4.0M1 usegetComponentManager()
insteadstatic org.xwiki.component.manager.ComponentManager
getComponentManager()
Deprecated.since 6.1M1, usegetContextComponentManager()
insteadstatic 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 org.xwiki.component.manager.ComponentManager
getContextComponentManager()
Deprecated.last resort way of accessing theComponentManager
, make sure you cannot do it any other way possible since it add a strong dependency to a static to your codestatic 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(XWikiRequest request, String defaultRedirect)
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(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, String queryString, String... redirectParameters)
Retrieve the URL to which the client should be redirected after the successful completion of the requested action.static org.xwiki.component.manager.ComponentManager
getRootComponentManager()
Deprecated.last resort way of accessing theComponentManager
, make sure you cannot do it any other way possible since it add a strong dependency to a static to your codestatic 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 theResponse
.static void
parseTemplate(String template, XWikiContext context)
Generate the response by parsing a velocity template and printing the result to theResponse
.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)
-
-
-
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 theResponse
. 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 theResponse
.- 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. Iffalse
, 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 thexredirect
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 requestdefaultRedirect
- the default value to use if noxredirect
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 thexredirect
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 URLcontext
- 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 specifiedredirectParameters
(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 URLredirectParameters
- 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 thexredirect
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 thexpage
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 requestdefaultpage
- the default value to use if noxpage
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 pluginname
- 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 pluginname
- 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 parametersencoding
- the encoding to use for transforming bytes into characters- Throws:
IllegalArgumentException
- if the data is malformedUnsupportedEncodingException
-
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 parametersencoding
- 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 aString
using numerical XML entities.- Parameters:
value
- the text to escape, may be null- Returns:
- a new escaped
String
,null
if null input
-
encode
@Deprecated public static String encode(String text, XWikiContext context)
Deprecated.replaced byUtil.encodeURI(String, XWikiContext)
since 1.3M2
-
decode
@Deprecated public static String decode(String text, XWikiContext context)
Deprecated.replaced byUtil.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 processcontext
- 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 bygetComponent(Class)
andgetComponent(Class, String)
-
getRootComponentManager
@Deprecated public static org.xwiki.component.manager.ComponentManager getRootComponentManager()
Deprecated.last resort way of accessing theComponentManager
, 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
-
getComponentManager
@Deprecated public static org.xwiki.component.manager.ComponentManager getComponentManager()
Deprecated.since 6.1M1, usegetContextComponentManager()
instead- Returns:
- the contextual component manager used by
getComponent(Class)
andgetComponent(Class, String)
-
getContextComponentManager
@Deprecated public static org.xwiki.component.manager.ComponentManager getContextComponentManager()
Deprecated.last resort way of accessing theComponentManager
, 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)
andgetComponent(Class, String)
- Since:
- 6.0RC1
-
getComponent
@Deprecated public static <T> T getComponent(Class<T> role, String hint)
Deprecated.since 4.0M1 usegetComponent(Type, String)
insteadLookup a XWiki component by role and hint.- Parameters:
role
- the class (aka role) that the component implementshint
- 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 usegetComponent(Type)
insteadLookup 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 insteadLookup a XWiki component by role and hint.- Parameters:
roleType
- the class (aka role) that the component implementsroleHint
- 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 insteadLookup 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 usegetComponentManager()
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
-
-