public class FormsHelper
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
REQ_ATTR_ACTION_SUFFIX |
static java.lang.String |
REQ_ATTR_CLIENT_VALIDATION |
static java.lang.String |
REQ_ATTR_EDIT_RESOURCES |
static java.lang.String |
REQ_ATTR_EXPRESSIONS_ENABLED |
static java.lang.String |
REQ_ATTR_FORM_STRUCTURE_HELPER |
static java.lang.String |
REQ_ATTR_FORMID |
static java.lang.String |
REQ_ATTR_FORWARD_OPTIONS |
static java.lang.String |
REQ_ATTR_FORWARD_PATH |
static java.lang.String |
REQ_ATTR_GLOBAL_LOAD_MAP |
static java.lang.String |
REQ_ATTR_GLOBAL_LOAD_RESOURCE |
static java.lang.String |
REQ_ATTR_IS_INIT |
static java.lang.String |
REQ_ATTR_PROP_WHITELIST |
static java.lang.String |
REQ_ATTR_READ_ONLY |
static java.lang.String |
REQ_ATTR_REDIRECT |
static java.lang.String |
REQ_ATTR_REDIRECT_TO_REFERRER |
static java.lang.String |
REQ_ATTR_WRITTEN_JAVASCRIPT |
Modifier and Type | Method and Description |
---|---|
static boolean |
allowExpressions(SlingHttpServletRequest req)
Indicates whether posted form values should be passed through an expression evaluator.
|
static Resource |
checkFormStructure(Resource rsrc)
Deprecated.
use
FormStructureHelper.updateFormStructure(Resource) instead. |
static boolean |
checkRule(Resource resource,
SlingHttpServletRequest req,
javax.servlet.jsp.PageContext pageContext,
java.lang.String propName)
Checks the rule specified by the given form property.
|
static java.lang.String |
decodeValue(java.lang.String value)
Url decode the value.
|
static boolean |
doClientValidation(SlingHttpServletRequest req)
Are we generating client validation?
|
static java.lang.String |
encodeValue(java.lang.String value)
Url encode the value.
|
static void |
endForm(SlingHttpServletRequest req)
Signal the end of the form.
|
static java.lang.String |
getActionSuffix(SlingHttpServletRequest req)
Get the request suffix currently set for the form action's URI.
|
static java.util.Iterator<java.lang.String> |
getContentRequestParameterNames(SlingHttpServletRequest req)
Return a list of content fields.
|
static java.lang.String |
getCss(ValueMap props,
java.lang.String defaultCss)
Return the css classes for the field.
|
static java.lang.String |
getDescription(Resource formElement,
java.lang.String defaultDescription)
Return the description for the field.
|
static java.lang.String |
getFieldId(SlingHttpServletRequest req,
Resource rsrc)
Return the id for the field
|
static java.util.List<Resource> |
getFormEditResources(SlingHttpServletRequest req)
Get the list of resources to be handled by the "edit" resources form action.
|
static java.util.Iterator<Resource> |
getFormElements(Resource formResource)
Deprecated.
use
FormStructureHelper.getFormElements(Resource) instead. |
static java.lang.String |
getFormId(SlingHttpServletRequest req)
Return the formid
|
static Resource |
getFormLoadResource(SlingHttpServletRequest req)
Get the load resource for the form.
|
static java.lang.String |
getFormsPreCheckMethodName(SlingHttpServletRequest req)
Return the name of the check method.
|
static RequestDispatcherOptions |
getForwardOptions(ServletRequest req)
Gets the Sling
RequestDispatcherOptions to be used when
forwarding to getForwardPath(SlingHttpServletRequest) . |
static java.lang.String |
getForwardPath(SlingHttpServletRequest req)
Get the forward path for processing the form.
|
static java.lang.String |
getForwardRedirect(ServletRequest request)
Returns the redirect to inject as ":redirect" parameter for the Sling
POST servlet, when a form
forward is done. |
static ValueMap |
getGlobalFormValues(SlingHttpServletRequest req)
Return the form values to load.
|
static java.util.Locale |
getLocale(SlingHttpServletRequest request)
Returns the locale.
|
static java.lang.String |
getLocalizedMessage(java.lang.String msg,
SlingHttpServletRequest request)
Returns the localized message.
|
static java.util.Map<java.lang.String,java.lang.String> |
getOptions(SlingHttpServletRequest request,
Resource elementResource)
Return the options for a form element
|
static java.lang.String |
getParameterName(Resource rsrc)
Return the parameter name for the field
|
static java.lang.String |
getReferrer(HttpServletRequest request)
Returns the HTTP "referrer" header from the request, and also looks out
for the common "referer" misspelling.
|
static Resource |
getResource(SlingHttpServletRequest request,
Resource elementResource,
java.lang.String relPath)
Returns a resource identified by a path relative to the
elementResource . |
static java.util.Map<java.lang.String,java.lang.String> |
getShowHideExpressions(Resource resource)
Return a flattened map of all showHideExpressions that are descendants of a resource.
|
static java.lang.String |
getTitle(Resource formElement,
java.lang.String defaultTitle)
Return the title for the field.
|
static java.lang.String |
getValue(SlingHttpServletRequest request,
Resource elementResource)
Return the value for the element.
|
static java.lang.String |
getValue(SlingHttpServletRequest request,
Resource elementResource,
java.lang.String nameParam)
Return the value for the element.
|
static java.lang.String |
getValue(SlingHttpServletRequest request,
java.lang.String name,
java.lang.String defaultValue)
Returns the value for the given name (property).
|
static java.lang.String[] |
getValues(SlingHttpServletRequest request,
Resource elementResource)
Return the values for the element.
|
static java.lang.String[] |
getValues(SlingHttpServletRequest request,
Resource elementResource,
java.lang.String nameParam)
Return the values for the element.
|
static java.lang.String[] |
getValues(SlingHttpServletRequest request,
java.lang.String name,
java.lang.String[] defaultValues)
Returns the values for the given name (property).
|
static java.util.List<java.lang.String> |
getValuesAsList(SlingHttpServletRequest request,
Resource elementResource)
Return the values for the element as a list
This method
|
static java.lang.String[] |
getWhitelistPatterns(SlingHttpServletRequest req)
Get the list of white listed data name patterns.
|
static boolean |
hasMultiSelection(Resource formElement)
Is this a field with multi selection?
|
static void |
includeResource(SlingHttpServletRequest request,
SlingHttpServletResponse response,
Resource resource,
java.lang.String selectorString)
Include the resource with the given selector with method GET
|
static void |
inlineValuesAsJson(SlingHttpServletRequest request,
java.io.Writer out,
java.lang.String path)
Writes the given form load resource as JSON into the given writer.
|
static void |
inlineValuesAsJson(SlingHttpServletRequest request,
java.io.Writer out,
java.lang.String path,
int nodeDepth)
Writes the given form load resource as JSON into the given writer.
|
static boolean |
isReadOnly(Resource formElement)
Deprecated.
To always support the global read-only flag, set for example by the
"view" selector of the
FormChooserServlet , use
isReadOnly(SlingHttpServletRequest, Resource) instead. |
static boolean |
isReadOnly(SlingHttpServletRequest request)
Returns true if the entire form has to be displayed in a read-only way.
|
static boolean |
isReadOnly(SlingHttpServletRequest request,
Resource formElement)
Returns true if either the passed form field is configured as read-only
or if the entire form is to be displayed in a read-only way.
|
static boolean |
isRedirectToReferrer(ServletRequest request)
Returns whether there should be a redirect to the HTTP referrer after the
forward of a form POST request.
|
static boolean |
isRequired(Resource formElement)
Is this field required?
|
static void |
popFormReadOnly(SlingHttpServletRequest request,
java.lang.Object previousState)
Pop a temporary read-only state.
|
static java.lang.Object |
pushFormReadOnly(SlingHttpServletRequest request)
Push a temporary read-only state.
|
static void |
redirectToReferrer(SlingHttpServletRequest request,
SlingHttpServletResponse res)
Redirect to the referrer.
|
static void |
redirectToReferrer(SlingHttpServletRequest req,
SlingHttpServletResponse res,
java.util.Map<java.lang.String,java.lang.String[]> params)
Redirect to the referrer.
|
static void |
runAction(java.lang.String actionType,
java.lang.String scriptName,
Resource formResource,
SlingHttpServletRequest request,
SlingHttpServletResponse response)
Runs/includes a script of the form action.
|
static void |
setActionSuffix(SlingHttpServletRequest req,
java.lang.String suffix)
Set a request suffix to be added to the form action's URI.
|
static void |
setFormEditResources(SlingHttpServletRequest req,
java.util.List<Resource> resources)
Sets the list of resources to be handled by the "edit" resources form action.
|
static void |
setFormLoadResource(SlingHttpServletRequest req,
Resource rsrc)
Set the load resource for the form.
|
static void |
setFormReadOnly(SlingHttpServletRequest request)
Marks the current form rendering to produce an read-only form.
|
static void |
setForwardOptions(ServletRequest req,
RequestDispatcherOptions options)
Sets the Sling
RequestDispatcherOptions to be used when
forwarding to getForwardPath(SlingHttpServletRequest) . |
static void |
setForwardPath(SlingHttpServletRequest req,
java.lang.String path)
Set the forward path for processing the form.
|
static void |
setForwardPath(SlingHttpServletRequest req,
java.lang.String path,
boolean clearFormSelector)
Set the forward path for processing the form and makes sure the "form"
selector from the form request is cleared upon forwarding.
|
static void |
setForwardRedirect(ServletRequest request,
java.lang.String redirect)
Sets the Sling POST serlvet's ":redirect" parameter dynamically during
the form POST execution, when
setForwardPath(SlingHttpServletRequest, String) is used. |
static void |
setRedirectToReferrer(ServletRequest request,
boolean redirectToReferrer)
Sets a flag to redirect to the HTTP referrer after the forward of a form
POST request.
|
static void |
startForm(SlingHttpServletRequest request,
SlingHttpServletResponse response)
Signal the start of the form.
|
static void |
startForm(SlingHttpServletRequest request,
SlingHttpServletResponse response,
javax.servlet.jsp.JspWriter out)
Deprecated.
|
public static final java.lang.String REQ_ATTR_GLOBAL_LOAD_MAP
public static final java.lang.String REQ_ATTR_GLOBAL_LOAD_RESOURCE
public static final java.lang.String REQ_ATTR_EDIT_RESOURCES
public static final java.lang.String REQ_ATTR_CLIENT_VALIDATION
public static final java.lang.String REQ_ATTR_FORMID
public static final java.lang.String REQ_ATTR_WRITTEN_JAVASCRIPT
public static final java.lang.String REQ_ATTR_ACTION_SUFFIX
public static final java.lang.String REQ_ATTR_FORWARD_PATH
public static final java.lang.String REQ_ATTR_FORWARD_OPTIONS
public static final java.lang.String REQ_ATTR_IS_INIT
public static final java.lang.String REQ_ATTR_READ_ONLY
public static final java.lang.String REQ_ATTR_REDIRECT
public static final java.lang.String REQ_ATTR_REDIRECT_TO_REFERRER
public static final java.lang.String REQ_ATTR_PROP_WHITELIST
public static final java.lang.String REQ_ATTR_EXPRESSIONS_ENABLED
public static final java.lang.String REQ_ATTR_FORM_STRUCTURE_HELPER
@Deprecated public static void startForm(SlingHttpServletRequest request, SlingHttpServletResponse response, javax.servlet.jsp.JspWriter out) throws java.io.IOException, ServletException
startForm(SlingHttpServletRequest, SlingHttpServletResponse)
FormsConstants.REQUEST_PROPERTY_FORMID
with the value of the form id.
FormsConstants.REQUEST_PROPERTY_FORM_START
with the relative path to the form start par
and _charset_
with the value UTF-8
request
- The current request.response
- The current response.out
- The jsp writer.java.io.IOException
- if form generation caused an errorServletException
- if form generation caused an errorpublic static void startForm(SlingHttpServletRequest request, SlingHttpServletResponse response) throws java.io.IOException, ServletException
FormsConstants.REQUEST_PROPERTY_FORMID
with the value of the form id.
FormsConstants.REQUEST_PROPERTY_FORM_START
with the relative path to the form start par
and _charset_
with the value UTF-8
request
- The current request.response
- The current response.java.io.IOException
- if form generation caused an errorServletException
- if form generation caused an errorpublic static void runAction(java.lang.String actionType, java.lang.String scriptName, Resource formResource, SlingHttpServletRequest request, SlingHttpServletResponse response) throws java.io.IOException, ServletException
actionType
- action typescriptName
- script nameformResource
- resourcerequest
- requestresponse
- responsejava.io.IOException
- if inclusion caused an errorServletException
- if inclusion caused an errorpublic static void endForm(SlingHttpServletRequest req)
req
- Requestpublic static void setForwardPath(SlingHttpServletRequest req, java.lang.String path)
req
- The current request.path
- The forward path.public static void setForwardPath(SlingHttpServletRequest req, java.lang.String path, boolean clearFormSelector)
This is not required if the forward path already contains an extension and possible selectors, in which case these overwrite the form request selector anyway.
* This uses
setForwardOptions
with
RequestDispatcherOptions.setReplaceSelectors("")
.
req
- The current request.path
- The forward path.clearFormSelector
- if the "form" selector should be clearedpublic static java.lang.String getForwardPath(SlingHttpServletRequest req)
req
- The current request.public static void setForwardOptions(ServletRequest req, RequestDispatcherOptions options)
RequestDispatcherOptions
to be used when
forwarding to getForwardPath(SlingHttpServletRequest)
.req
- The current requestoptions
- options to use with forward pathpublic static RequestDispatcherOptions getForwardOptions(ServletRequest req)
RequestDispatcherOptions
to be used when
forwarding to getForwardPath(SlingHttpServletRequest)
.req
- The current requestnull
public static void setForwardRedirect(ServletRequest request, java.lang.String redirect)
setForwardPath(SlingHttpServletRequest, String)
is used.request
- the current requestredirect
- a redirect path/urlpublic static java.lang.String getForwardRedirect(ServletRequest request)
forward
is done.request
- the current requestpublic static void setActionSuffix(SlingHttpServletRequest req, java.lang.String suffix)
req
- The current request.suffix
- The suffix or null.public static java.lang.String getActionSuffix(SlingHttpServletRequest req)
req
- The current Request.public static void setFormLoadResource(SlingHttpServletRequest req, Resource rsrc)
req
- The current request.rsrc
- The load resourcepublic static Resource getFormLoadResource(SlingHttpServletRequest req)
req
- current requestnull
if not setpublic static ValueMap getGlobalFormValues(SlingHttpServletRequest req)
req
- The requestpublic static void setFormEditResources(SlingHttpServletRequest req, java.util.List<Resource> resources)
req
- current requestresources
- the list of resourcespublic static java.util.List<Resource> getFormEditResources(SlingHttpServletRequest req)
req
- current requestnull
if not set)public static java.lang.String getFormsPreCheckMethodName(SlingHttpServletRequest req)
req
- The current request.public static void includeResource(SlingHttpServletRequest request, SlingHttpServletResponse response, Resource resource, java.lang.String selectorString) throws java.io.IOException, ServletException
request
- The current request.response
- The current response.resource
- The resource to include.selectorString
- The selector string to use for inclusion.java.io.IOException
- if requested resource is not accessibleServletException
- if requested resource is not accessiblepublic static boolean doClientValidation(SlingHttpServletRequest req)
req
- Requestpublic static java.lang.String getFormId(SlingHttpServletRequest req)
req
- Requestpublic static java.lang.String getParameterName(Resource rsrc)
rsrc
- The resourcepublic static java.lang.String getFieldId(SlingHttpServletRequest req, Resource rsrc)
req
- The current request.rsrc
- The resource.@Deprecated public static java.util.Iterator<Resource> getFormElements(Resource formResource)
FormStructureHelper.getFormElements(Resource)
instead.formResource
- The form resource-public static java.util.Iterator<java.lang.String> getContentRequestParameterNames(SlingHttpServletRequest req)
public static Resource getResource(SlingHttpServletRequest request, Resource elementResource, java.lang.String relPath)
elementResource
. This method also considers form edit
resources.request
- the current request.elementResource
- the element resource.relPath
- a path relative to elementResource
.null
if none exists at the given
relPath
.public static java.lang.String getValue(SlingHttpServletRequest request, Resource elementResource)
getValues(SlingHttpServletRequest, Resource)
and returns
if available the first value from the array.request
- The current request.elementResource
- The element resource.public static java.lang.String getValue(SlingHttpServletRequest request, Resource elementResource, java.lang.String nameParam)
getValues(SlingHttpServletRequest, Resource)
and returns
if available the first value from the array.request
- The current request.elementResource
- The element resource.nameParam
- The name of the name parameter (defaults to "name")public static java.lang.String[] getValues(SlingHttpServletRequest request, Resource elementResource)
request
- The current request.elementResource
- The element resource.public static java.lang.String[] getValues(SlingHttpServletRequest request, Resource elementResource, java.lang.String nameParam)
request
- The current request.elementResource
- The element resource.nameParam
- The name of the name parameter (defaults to "name")public static java.util.List<java.lang.String> getValuesAsList(SlingHttpServletRequest request, Resource elementResource)
request
- The current request.elementResource
- The element resource.public static java.lang.String getValue(SlingHttpServletRequest request, java.lang.String name, java.lang.String defaultValue)
request
- The current requestname
- name of the property (or property path)defaultValue
- default value to return if property is not presentpublic static java.lang.String[] getValues(SlingHttpServletRequest request, java.lang.String name, java.lang.String[] defaultValues)
request
- The current requestname
- name of the property (or property path)defaultValues
- default values to return if property is not presentpublic static void inlineValuesAsJson(SlingHttpServletRequest request, java.io.Writer out, java.lang.String path) throws java.io.IOException, RepositoryException, JSONException
inlineValuesAsJson(SlingHttpServletRequest, Writer, String, int)
for controlling the node depth.
Can be used in JSPs to inline JSON for javascript code, for example:
var data = <% FormsHelper.inlineValuesAsJson(slingRequest, out, "."); %>;which might result in:
var data = { "jcr:primaryType": "nt:unstructured", "property" : "value" };
If the path cannot be found, an empty object "{}
" will be
written. Any exception will be passed to the caller.
The underlying form load resource must be based on a JCR Node. The path
is relative and allows to specify subnodes. It cannot point to JCR
properties, please use
getValue(SlingHttpServletRequest, String, String)
or
getValues(SlingHttpServletRequest, String, String[])
for them.
request
- the current requestout
- a writer, such as a JspWriter
, to write the JSON into.
Will automatically be flushed before and after.path
- an absolute node path or a node path relativ to the current
form load resource. Use "." for the resource node itself.RepositoryException
- if some jcr error happenedJSONException
- if writing the json failedjava.io.IOException
- if there was a problem with the writerpublic static void inlineValuesAsJson(SlingHttpServletRequest request, java.io.Writer out, java.lang.String path, int nodeDepth) throws java.io.IOException, RepositoryException, JSONException
var data = <% FormsHelper.inlineValuesAsJson(slingRequest, out, "."); %>;which might result in:
var data = { "jcr:primaryType": "nt:unstructured", "property" : "value" };
If the path cannot be found, an empty object "{}
" will be
written. Any exception will be passed to the caller.
The underlying form load resource must be based on a JCR Node. The path
is relative and allows to specify subnodes. It cannot point to JCR
properties, please use
getValue(SlingHttpServletRequest, String, String)
or
getValues(SlingHttpServletRequest, String, String[])
for them.
request
- the current requestout
- a writer, such as a JspWriter
, to write the JSON into.
Will automatically be flushed before and after.path
- an absolute node path or a node path relativ to the current
form load resource. Use "." for the resource node itself.nodeDepth
- until which depth the tree should be written; 0 means the
current node and its properties only; -1 means the whole tree.RepositoryException
- if some jcr error happenedJSONException
- if writing the json failedjava.io.IOException
- if there was a problem with the writerpublic static java.util.Map<java.lang.String,java.lang.String> getShowHideExpressions(Resource resource) throws RepositoryException
resource
- the node to check to see if there's any showHideExpressions.RepositoryException
- if repository access caused an errorpublic static java.util.Map<java.lang.String,java.lang.String> getOptions(SlingHttpServletRequest request, Resource elementResource)
request
- requestelementResource
- elementpublic static boolean isRequired(Resource formElement)
formElement
- The form element.public static void setFormReadOnly(SlingHttpServletRequest request)
isReadOnly(SlingHttpServletRequest, Resource)
to decide between
edit or read-only representations.request
- the current requestpublic static java.lang.Object pushFormReadOnly(SlingHttpServletRequest request)
setFormReadOnly(SlingHttpServletRequest)
for further details of read-only states.)request
- the current requestpopFormReadOnly(SlingHttpServletRequest, Object)
public static void popFormReadOnly(SlingHttpServletRequest request, java.lang.Object previousState)
request
- the current requestpreviousState
- The token returned by pushFormReadOnly(SlingHttpServletRequest)
public static boolean isReadOnly(SlingHttpServletRequest request, Resource formElement)
setFormReadOnly(SlingHttpServletRequest)
was
called, for example if the "view" selector of the
FormChooserServlet
is used.request
- the current requestformElement
- the form field resourcetrue
if this field is to be rendered as read-only,
false
otherwise.public static boolean isReadOnly(SlingHttpServletRequest request)
request
- the current requesttrue
if a form element has to be rendered as read-only,
false
otherwise.@Deprecated public static boolean isReadOnly(Resource formElement)
FormChooserServlet
, use
isReadOnly(SlingHttpServletRequest, Resource)
instead.formElement
- the form field resourcetrue
if this field is read-only, false
otherwise.public static boolean checkRule(Resource resource, SlingHttpServletRequest req, javax.servlet.jsp.PageContext pageContext, java.lang.String propName)
Checks the rule specified by the given form property.
Rules may be used to enable/disable form elements according to some serverside conditions.
Currently, only rules depending on access rights are available.
resource
- resourcereq
- requestpropName
- property namepublic static java.lang.String getTitle(Resource formElement, java.lang.String defaultTitle)
formElement
- The form element.defaultTitle
- The default title.public static java.lang.String getDescription(Resource formElement, java.lang.String defaultDescription)
formElement
- The form element.defaultDescription
- The default description.public static boolean hasMultiSelection(Resource formElement)
formElement
- The form elementpublic static void redirectToReferrer(SlingHttpServletRequest req, SlingHttpServletResponse res, java.util.Map<java.lang.String,java.lang.String[]> params) throws java.io.IOException
req
- The current requestres
- The current responsejava.io.IOException
- if redirection caused an errorpublic static void redirectToReferrer(SlingHttpServletRequest request, SlingHttpServletResponse res) throws java.io.IOException
request
- The current requestres
- The current responsejava.io.IOException
- if redirection caused an error@Deprecated public static Resource checkFormStructure(Resource rsrc)
FormStructureHelper.updateFormStructure(Resource)
instead.public static java.lang.String encodeValue(java.lang.String value)
value
- The valuepublic static java.lang.String decodeValue(java.lang.String value)
value
- The valuepublic static java.lang.String getCss(ValueMap props, java.lang.String defaultCss)
FormsConstants.ELEMENT_PROPERTY_CSS
the value of this property is appended to the default css. If not, only the
default css is returned.props
- The field properties.defaultCss
- The default css for this field.public static java.lang.String getReferrer(HttpServletRequest request)
request
- current requestnull
if not presentpublic static void setRedirectToReferrer(ServletRequest request, boolean redirectToReferrer)
request
- current requestredirectToReferrer
- true
to enable the redirect to the referrerpublic static boolean isRedirectToReferrer(ServletRequest request)
request
- current requesttrue
if there should be a redirect to the referrerpublic static java.util.Locale getLocale(SlingHttpServletRequest request)
LanguageManager
, otherwise it returns
null
request
- The current requestnull
if not determinablepublic static java.lang.String getLocalizedMessage(java.lang.String msg, SlingHttpServletRequest request)
ResourceBundle
of the request and the
locale defined by the LanguageManager
.
Otherwise it returns the original message.msg
- The message to be localizedrequest
- The current requestpublic static java.lang.String[] getWhitelistPatterns(SlingHttpServletRequest req)
req
- The current request.public static boolean allowExpressions(SlingHttpServletRequest req)
req
- The current request.true
if expressions should be evaluated on posted valuesCopyright © 2010 - 2020 Adobe. All Rights Reserved