Class XWikiAction
- java.lang.Object
-
- com.xpn.xwiki.web.XWikiAction
-
- All Implemented Interfaces:
com.xpn.xwiki.internal.web.LegacyAction
- Direct Known Subclasses:
AbstractPropChangeAction
,AdminAction
,AttachAction
,CancelAction
,CommentAddAction
,CreateAction
,DeleteAction
,DeleteAttachmentAction
,DeleteVersionsAction
,DownloadAction
,EditAction
,ExportAction
,GetAction
,ImportAction
,LockAction
,LoginAction
,LoginErrorAction
,LoginSubmitAction
,LogoutAction
,ObjectAddAction
,ObjectRemoveAction
,ObjectsSynchronizeAction
,PropAddAction
,PropUpdateAction
,RedirectAction
,RegisterAction
,ResetVersionsAction
,RollbackAction
,SaveAndContinueAction
,SkinAction
,SVGAction
,UndeleteAction
,UnknownAction
,UploadAction
,ViewAction
,ViewAttachRevAction
public abstract class XWikiAction extends Object implements com.xpn.xwiki.internal.web.LegacyAction
Root class for most XWiki actions. It provides a common framework that allows actions to execute just the specific action code, handling the extra activities, such as preparing the context and retrieving the document corresponding to the URL.
It defines two methods,
action(XWikiContext)
andrender(XWikiContext)
, that should be overridden by specific actions.action(XWikiContext)
should contain the processing part of the action.render(XWikiContext)
should return the name of a template that should be rendered, or manually write to theresponse
stream.Serving a request goes through the following phases:
- Wrapping the request and response object in XWiki specific wrappers
- Prepare the request
XWiki-specific context
- Initialize/retrieve the XWiki object corresponding to the requested wiki
- Handle file uploads
- Prepare the velocity context
- Prepare the document objects corresponding to the requested URL
- Send action pre-notifications to listeners
- Run the overridden
action(XWikiContext)
- If
action(XWikiContext)
returns true, run the overriddenrender(XWikiContext)
- If
render(XWikiContext)
returned a string (template name), render the template with that name - Send action post-notifications to listeners
During this process, also handle specific errors, like when a document does not exist, or the user does not have the right to perform the current action.
-
-
Field Summary
Fields Modifier and Type Field Description static String
ACTION_PROGRESS
protected org.xwiki.component.descriptor.ComponentDescriptor<com.xpn.xwiki.internal.web.LegacyAction>
componentDescriptor
protected org.xwiki.container.Container
container
protected org.xwiki.context.Execution
execution
protected org.xwiki.observation.ObservationManager
observation
protected boolean
waitForXWikiInitialization
Indicate if the action allow asynchronous display (among which the XWiki initialization).
-
Constructor Summary
Constructors Constructor Description XWikiAction()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
action(XWikiContext context)
protected void
answerJSON(XWikiContext context, int status, Map<String,String> answer)
Answer to a request with a JSON content.protected void
cleanupComponents()
protected boolean
csrfTokenCheck(XWikiContext context)
Perform CSRF check and redirect to the resubmission page if needed.protected boolean
csrfTokenCheck(XWikiContext context, boolean jsonAnswer)
Perform CSRF check and redirect to the resubmission page if needed.void
execute(XWikiContext context)
void
execute(javax.servlet.http.HttpServletRequest servletRequest, javax.servlet.http.HttpServletResponse servletResponse)
protected ContextualAuthorizationManager
getContextualAuthorizationManager()
protected DocumentReferenceResolver<String>
getCurrentMixedDocumentReferenceResolver()
protected ScriptContext
getCurrentScriptContext()
protected EntityNameValidationConfiguration
getEntityNameValidationConfiguration()
protected EntityNameValidationManager
getEntityNameValidationManager()
protected Class<? extends XWikiForm>
getFormClass()
protected ContextualLocalizationManager
getLocalization()
protected EntityReferenceSerializer<String>
getLocalSerializer()
protected String
getName()
protected org.xwiki.job.event.status.JobProgressManager
getProgress()
String
getRealPath(String path)
protected XWikiDocument
getTranslatedDocument(XWikiDocument doc, String language, XWikiContext context)
Gets the translated version of a document, in the specified language.protected void
handleRevision(XWikiContext context)
protected void
initializeContainerComponent(XWikiContext context)
protected XWikiContext
initializeXWikiContext(javax.servlet.http.HttpServletRequest servletRequest, javax.servlet.http.HttpServletResponse servletResponse)
protected XWikiContext
initializeXWikiContext(javax.servlet.http.HttpServletRequest servletRequest, javax.servlet.http.HttpServletResponse servletResponse, XWikiForm form)
protected boolean
isEntityReferenceNameValid(EntityReference entityReference)
Ensure that the given entity reference is valid according to the configured name strategy.protected String
localizePlainOrKey(String key, Object... parameters)
protected boolean
readFromTemplate(XWikiDocument document, String template, XWikiContext context)
Helper used by various actions to initialize a document by copying a template to it.String
render(XWikiContext context)
protected DocumentReference
resolveTemplate(String template)
Helper used resolve the template passed to the action if the current user have access to it.protected boolean
sendGlobalRedirect(XWikiResponse response, String url, XWikiContext context)
Send redirection based on a regexp pattern (if any) set at the main wiki level.protected void
sendRedirect(XWikiResponse response, String url)
Perform a redirect to the given URL.protected void
setContentLength(XWikiResponse response, long length)
Make sure to set the right length (or nothing) in the response.protected boolean
supportRedirections()
Indicate if the action support redirection.protected void
writeAjaxErrorResponse(int httpStatusCode, String message, XWikiContext context)
Write an error response to an ajax request.
-
-
-
Field Detail
-
ACTION_PROGRESS
public static final String ACTION_PROGRESS
- See Also:
- Constant Field Values
-
componentDescriptor
@Inject protected org.xwiki.component.descriptor.ComponentDescriptor<com.xpn.xwiki.internal.web.LegacyAction> componentDescriptor
-
container
@Inject protected org.xwiki.container.Container container
-
execution
@Inject protected org.xwiki.context.Execution execution
-
observation
@Inject protected org.xwiki.observation.ObservationManager observation
-
waitForXWikiInitialization
protected boolean waitForXWikiInitialization
Indicate if the action allow asynchronous display (among which the XWiki initialization).
-
-
Method Detail
-
getFormClass
protected Class<? extends XWikiForm> getFormClass()
- Returns:
- the class of the XWikiForm in charge of parsing the request
- Since:
- 13.0
-
getLocalization
protected ContextualLocalizationManager getLocalization()
-
getCurrentMixedDocumentReferenceResolver
protected DocumentReferenceResolver<String> getCurrentMixedDocumentReferenceResolver()
- Since:
- 12.10.6, 13.2RC1
-
getContextualAuthorizationManager
protected ContextualAuthorizationManager getContextualAuthorizationManager()
- Since:
- 12.10.6, 13.2RC1
-
getProgress
protected org.xwiki.job.event.status.JobProgressManager getProgress()
-
getEntityNameValidationManager
protected EntityNameValidationManager getEntityNameValidationManager()
-
getEntityNameValidationConfiguration
protected EntityNameValidationConfiguration getEntityNameValidationConfiguration()
-
getLocalSerializer
protected EntityReferenceSerializer<String> getLocalSerializer()
-
getCurrentScriptContext
protected ScriptContext getCurrentScriptContext()
- Returns:
- the current unmodified
ScriptContext
instance - Since:
- 8.3M1
-
execute
public void execute(javax.servlet.http.HttpServletRequest servletRequest, javax.servlet.http.HttpServletResponse servletResponse) throws Exception
- Specified by:
execute
in interfacecom.xpn.xwiki.internal.web.LegacyAction
- Parameters:
servletRequest
- the request passed to the servletservletResponse
- the response passed to the servlet- Throws:
Exception
- when the action produces an unexptected error
-
isEntityReferenceNameValid
protected boolean isEntityReferenceNameValid(EntityReference entityReference)
Ensure that the given entity reference is valid according to the configured name strategy. Always returns true if the name strategy is not found.- Parameters:
entityReference
- the entity reference name to validate- Returns:
true
if the entity reference name is valid according to the name strategy.- Since:
- 12.0RC1
-
writeAjaxErrorResponse
protected void writeAjaxErrorResponse(int httpStatusCode, String message, XWikiContext context)
Write an error response to an ajax request.- Parameters:
httpStatusCode
- The status code to set on the response.message
- The message that should be displayed.context
- the context.
-
execute
public void execute(XWikiContext context) throws Exception
- Throws:
Exception
-
initializeXWikiContext
protected XWikiContext initializeXWikiContext(javax.servlet.http.HttpServletRequest servletRequest, javax.servlet.http.HttpServletResponse servletResponse) throws XWikiException, javax.servlet.ServletException, InstantiationException, IllegalAccessException
- Throws:
XWikiException
javax.servlet.ServletException
InstantiationException
IllegalAccessException
-
getName
protected String getName()
- Returns:
- the name to put in the
XWikiContext
, by default the component role hint is used - Since:
- 13.0
-
initializeXWikiContext
protected XWikiContext initializeXWikiContext(javax.servlet.http.HttpServletRequest servletRequest, javax.servlet.http.HttpServletResponse servletResponse, XWikiForm form) throws XWikiException, javax.servlet.ServletException
- Throws:
XWikiException
javax.servlet.ServletException
-
initializeContainerComponent
protected void initializeContainerComponent(XWikiContext context) throws javax.servlet.ServletException
- Throws:
javax.servlet.ServletException
-
cleanupComponents
protected void cleanupComponents()
-
action
public boolean action(XWikiContext context) throws XWikiException
- Throws:
XWikiException
-
render
public String render(XWikiContext context) throws XWikiException
- Throws:
XWikiException
-
supportRedirections
@Unstable protected boolean supportRedirections()
Indicate if the action support redirection. The default value isfalse
.- Returns:
true
if the action supports redirections,false
otherwise- Since:
- 14.0RC1
-
handleRevision
protected void handleRevision(XWikiContext context) throws XWikiException
- Throws:
XWikiException
-
sendGlobalRedirect
protected boolean sendGlobalRedirect(XWikiResponse response, String url, XWikiContext context) throws Exception
Send redirection based on a regexp pattern (if any) set at the main wiki level. To enable this feature you must add xwiki.preferences.redirect=1 to your xwiki.cfg.- Parameters:
response
- the servlet responseurl
- url of the requestcontext
- the XWiki context- Returns:
- true if a redirection has been sent
- Throws:
Exception
-
sendRedirect
protected void sendRedirect(XWikiResponse response, String url) throws XWikiException
Perform a redirect to the given URL.- Parameters:
response
- the response to use to perform the redirecturl
- the location of the redirect- Throws:
XWikiException
- in case of IOException when performing the redirect.
-
getTranslatedDocument
protected XWikiDocument getTranslatedDocument(XWikiDocument doc, String language, XWikiContext context) throws XWikiException
Gets the translated version of a document, in the specified language. If the translation does not exist, a new document translation is created. If the requested language does not correspond to a translation (is not defined or is the same as the main document), then the main document is returned.- Parameters:
doc
- the main (default, untranslated) document to translatelanguage
- the requested document languagecontext
- the current request context- Returns:
- the translated document, or the original untranslated document if the requested language is not a translation
- Throws:
XWikiException
- if the translation cannot be retrieved from the database
-
csrfTokenCheck
protected boolean csrfTokenCheck(XWikiContext context) throws XWikiException
Perform CSRF check and redirect to the resubmission page if needed. Throws an exception if the access should be denied, returns false if the check failed and the user will be redirected to a resubmission page.- Parameters:
context
- current xwiki context containing the request- Returns:
- true if the check succeeded, false if resubmission is needed
- Throws:
XWikiException
- if the check fails
-
csrfTokenCheck
protected boolean csrfTokenCheck(XWikiContext context, boolean jsonAnswer) throws XWikiException
Perform CSRF check and redirect to the resubmission page if needed. Throws an exception if the access should be denied, returns false if the check failed and the user will be redirected to a resubmission page.- Parameters:
context
- current xwiki context containing the requestjsonAnswer
- if true, returns a JSON answer in case of AJAX request: allow to process it properly on client.- Returns:
- true if the check succeeded, false if resubmission is needed
- Throws:
XWikiException
- if the check fails- Since:
- 11.3RC1
-
answerJSON
protected void answerJSON(XWikiContext context, int status, Map<String,String> answer) throws XWikiException
Answer to a request with a JSON content.- Parameters:
context
- the current context of the request.status
- the status code to send back.answer
- the content of the JSON answer.- Throws:
XWikiException
- in case of error during the serialization of the JSON.
-
setContentLength
protected void setContentLength(XWikiResponse response, long length)
Make sure to set the right length (or nothing) in the response.- Parameters:
response
- the responselength
- the length to set in the response- Since:
- 11.10, 10.11.10, 11.3.6
-
resolveTemplate
protected DocumentReference resolveTemplate(String template)
Helper used resolve the template passed to the action if the current user have access to it.- Parameters:
template
- the template to copy- Returns:
- the reference of the template if not empty and the current user have access to it
- Since:
- 12.10.6, 13.2RC1
-
readFromTemplate
protected boolean readFromTemplate(XWikiDocument document, String template, XWikiContext context) throws XWikiException
Helper used by various actions to initialize a document by copying a template to it.- Parameters:
document
- the document to updatetemplate
- the template to copycontext
- the XWiki context- Returns:
- true if the document was updated, false otherwise (for example when the current user does not have view right on the template document)
- Throws:
XWikiException
- when failing to copy the template- Since:
- 12.10.6, 13.2RC1
-
-