
com.adobe.aemds.guide.utils.GuideSubmitUtils Maven / Gradle / Ivy
/*************************************************************************
*
* ADOBE CONFIDENTIAL
* __________________
*
* Copyright Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and may be covered by U.S. and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
**************************************************************************/
package com.adobe.aemds.guide.utils;
import com.adobe.aemds.guide.model.FormSubmitInfo;
import com.adobe.aemds.guide.service.external.PortalRecordInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestDispatcherOptions;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import javax.annotation.Nonnull;
import javax.jcr.Session;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* Provides Utility API to build custom submit action
*/
public class GuideSubmitUtils {
private static Logger logger = LoggerFactory.getLogger(GuideSubmitUtils.class);
public static final String REQUEST_ATTR_PDF_NAME = "pdfName";
public static final String REQUEST_ATTR_REDIRECT_PARAMETER_MAP = "redirectParameters";
public static final String REQ_ATTR_FORWARD_PATH = "forward.path";
public static final String REQ_ATTR_FORWARD_OPTIONS = "forward.options";
public static final String REQ_ATTR_GUIDE_SUBMIT_OPTIONS = "submit.options";
/**
*
* This API should be used to set the path to forward the current request
*
* @param req Sling http servlet request
* @param path Path to forward the current request
* @param selector Sling selector to use while forwarding the request to the path. Null if no selector to be used
* @param suffix Suffix to use while forwarding the request to the path. Null if no selector to be used
*
* @see SlingHttpServletRequest
*/
public static void setForwardPath(final SlingHttpServletRequest req, final String path, String selector, String suffix) {
req.setAttribute(REQ_ATTR_FORWARD_PATH, path);
if (selector != null || suffix != null) {
RequestDispatcherOptions options = new RequestDispatcherOptions();
if (selector != null) {
options.setReplaceSelectors(selector);
}
if (suffix != null) {
options.setReplaceSuffix(suffix);
}
req.setAttribute(REQ_ATTR_FORWARD_OPTIONS, options);
}
}
/**
*
* This API should be used to set the redirect url
* @param req Sling http servlet request
* @param redirectUrl redirect url to set in the current request object
*
* @see SlingHttpServletRequest
*/
public static void setRedirectUrl(final SlingHttpServletRequest req, final String redirectUrl) {
req.setAttribute(GuideConstants.REDIRECT, redirectUrl);
}
/**
* Returns the current user ID from the request
* @param request {@link SlingHttpServletRequest} sling http servlet request
* @return string representing the user id
*/
public static String getUserID(SlingHttpServletRequest request) {
String userId = request.getResourceResolver().adaptTo(Session.class).getUserID();
int index = userId.lastIndexOf(':');
if( index > 0 && ( index + 1 < userId.length() ) ) {
userId = userId.substring(index+1);
}
return userId;
}
/**
* Returns the closest parent with given jcrPrimaryType, if jcrPrimaryType is null or empty,
* then returns immediate parent
* @param resource
* @param jcrPrimaryType
* @return parentResource
* @pad.exclude
*/
public static Resource getParentResource(Resource resource, String jcrPrimaryType) {
Resource parentResource = resource;
String parentResourceType = null;
if (resource == null) {
return null;
}
if (jcrPrimaryType.isEmpty()) {
return resource.getParent();
}
do {
parentResource = parentResource.getParent();
if (parentResource != null) {
ValueMap properties = parentResource.adaptTo(ValueMap.class);
parentResourceType = properties.get(JcrResourceConstants.JCR_PRIMARY_TYPE, "");
}
} while (!jcrPrimaryType.equals(parentResourceType));
return parentResource;
}
/**
*
* This API returns redirect parameters set in the current request. To set the
* redirect parameters, check {@link #setRedirectParameters(org.apache.sling.api.SlingHttpServletRequest, java.util.Map)}
* @param request Sling http servlet request
* @return Map containing parameter names with their corresponding values
*
* @see SlingHttpServletRequest
*/
public static Map getRedirectParameters(SlingHttpServletRequest request) {
Map redirectParameters = (Map) request.getAttribute(REQUEST_ATTR_REDIRECT_PARAMETER_MAP);
if(redirectParameters==null) {
redirectParameters = new HashMap();
}
if (!redirectParameters.containsKey(GuideConstants.SUMMARY_OWNER)) {
redirectParameters.put(GuideConstants.SUMMARY_OWNER, GuideSubmitUtils.getUserID(request));
}
if (!redirectParameters.containsKey(GuideConstants.STATUS)) {
redirectParameters.put(GuideConstants.STATUS, GuideConstants.SUBMITTED);
}
return redirectParameters;
}
private static void populateMap(JSONObject data, Map map) throws JSONException {
Iterator keys = data.keys();
while(keys.hasNext()) {
String key = (String)keys.next();
if (data.get(key) instanceof JSONObject ) {
populateMap((JSONObject) data.get(key), map);
} else {
map.put(key, data.getString(key));
}
}
}
/**
* Checks if the given resource is a child of composite field. If it is a child of composite
* field, it would return the name of the composite field resource
*
* @param resource reference to the child of the composite field resource
* @return name of the composite field resource, null if not a child
* @pad.exclude
*/
public static String getNameOfCompositeFieldFromChildResource(@Nonnull Resource resource) {
String name = null;
// get parent of child resource
Resource parent = resource.getParent();
if (parent != null && StringUtils.equals(parent.getName(), GuideConstants.ITEMS_NODENAME)) {
// we get items node here
parent = parent.getParent();
if (parent != null) {
// check if the current resource is of composite field
Iterator iter = GuideConstants.GUIDE_COMPOSITE_FIELD_RESOURCE_TYPES.iterator();
while (iter.hasNext()) {
String compositeResourceType = iter.next();
// check if this is a composite field resource type
if (parent.isResourceType(compositeResourceType)) {
name = parent.getValueMap().get(GuideConstants.NAME, "");
break;
}
}
}
}
return name;
}
/**
*
* This API returns guideValueMap, that is, Map of all the fields and their values, and portal submit link and submit id
* @param formSubmitInfo {@link FormSubmitInfo} object containing the basic information required during form submission
* @return Map of all the fields and their values, and portal submit link and submit id
*
* @see FormSubmitInfo
*/
public static Map getGuideValueMap(FormSubmitInfo formSubmitInfo) {
Map guideValueMap = null;
String data = formSubmitInfo.getData();
if (GuideConstants.CONTENT_TYPE_APPLICATION_JSON.equals(formSubmitInfo.getContentType())) {
guideValueMap = new HashMap();
try {
JSONObject dataJson = new JSONObject(data);
JSONObject afDataJson = dataJson.getJSONObject(GuideConstants.WRAPPED_SUBMIT_JSON_ROOT);
JSONObject unboundDataJson = afDataJson.getJSONObject(GuideConstants.WRAPPED_SUBMIT_UNBOUND_ROOT);
JSONObject boundDataJson = afDataJson.getJSONObject(GuideConstants.WRAPPED_SUBMIT_BOUND_ROOT);
if (unboundDataJson != null) {
populateMap(unboundDataJson, guideValueMap);
}
if (boundDataJson != null) {
populateMap(boundDataJson, guideValueMap);
}
} catch (JSONException e) {
logger.error("Failed to retrieve data from submit data JSON.", e);
}
} else {
Document document = XMLUtils.strToDoc(data);
guideValueMap = XMLUtils.getDataMap(document);
}
if (guideValueMap == null) {
guideValueMap = new HashMap();
}
PortalRecordInfo portalRecordInfo = formSubmitInfo.getPortalRecordInfo();
if (portalRecordInfo != null) {
if (StringUtils.isNotEmpty(portalRecordInfo.getSubmitID())) {
guideValueMap.put(GuideConstants.FORMS_PORTAL_SUBMIT_ID, portalRecordInfo.getSubmitID());
}
if (StringUtils.isNotEmpty(portalRecordInfo.getSubmitLink())) {
guideValueMap.put(GuideConstants.FORMS_PORTAL_SUBMIT_LINK, portalRecordInfo.getSubmitLink());
}
}
return guideValueMap;
}
/**
* Provides map of field name and parameter name pairs.
* In case of absence of parameter name, field name is used as parameter name.
*
* @param fieldParameterMapping array of field mappings in the form of <=Parameter Name>.
* @return map of field name and parameter name pairs.
*/
public static Map getMappedFieldParameters(String[] fieldParameterMapping) {
Map mappedFieldParameters = new HashMap();
if (fieldParameterMapping != null) {
for (String param : fieldParameterMapping) {
String[] map = StringUtils.split(param, "=");
String fieldName = map[0].trim();
String mappedParam = fieldName;
if (map.length == 2) {
mappedParam = map[1].trim();
}
mappedFieldParameters.put(fieldName, mappedParam);
}
}
return mappedFieldParameters;
}
/**
* Sets redirect parameters to the current request
* @param request Sling http servlet request
* @param value Map represent the redirect paramters to be set
*
* @see SlingHttpServletRequest
*/
public static void setRedirectParameters(SlingHttpServletRequest request, Map value) {
request.setAttribute(REQUEST_ATTR_REDIRECT_PARAMETER_MAP, value);
}
/**
* Returns the name of the PDF set in the request
* @param request Sling http servlet request
* @return name of the PDF from the request
*/
public static String getReqAttrPdfName(SlingHttpServletRequest request) {
return (String) request.getAttribute(REQUEST_ATTR_PDF_NAME);
}
/**
* This method will prepend context path to the URL if it starts with a /,
* provided url and context path is not blank.
*
* If URL starts with http/https, context path won't be added and the URL will be
* returned without any modification.
*
* @param url Thank You page URL
* @param contextPath Context path for the application.
* @return context path added URL
*/
public static String addContextPath(String url, String contextPath) {
if (StringUtils.isNotBlank(url)) {
if (url.matches(GuideConstants.URL_PROTOCOL_REGEX)) {
//url starts with http/https etc. so no need to prepend context path.
return url;
}else if (StringUtils.isNotBlank(contextPath) && !StringUtils.startsWith(url, contextPath) && StringUtils.startsWith(url, "/")) {
//url is an aem page. so prepending context path.
url = contextPath + url;
}
}
return url;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy