CreateMobilePageOperation.java 6.48 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.ui.components.HtmlResponse
 *  com.adobe.granite.xss.XSSAPI
 *  com.day.cq.i18n.I18n
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.PageManager
 *  com.day.cq.wcm.api.WCMException
 *  com.day.text.Text
 *  javax.jcr.PathNotFoundException
 *  javax.servlet.http.HttpServletRequest
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.ModifiableValueMap
 *  org.apache.sling.api.resource.PersistenceException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.servlets.post.Modification
 *  org.apache.sling.servlets.post.ModificationType
 */
package com.adobe.cq.mobile.platform.impl.operations;

import com.adobe.cq.mobile.platform.impl.operations.MobileAbstractOperation;
import com.adobe.cq.mobile.platform.impl.operations.MobileOperationException;
import com.adobe.granite.ui.components.HtmlResponse;
import com.adobe.granite.xss.XSSAPI;
import com.day.cq.i18n.I18n;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.WCMException;
import com.day.text.Text;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import javax.jcr.PathNotFoundException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.servlets.post.Modification;
import org.apache.sling.servlets.post.ModificationType;

@Component(metatype=0, label="Create Mobile Page Operation")
@Service
@Properties(value={@Property(name="sling.post.operation", value={"mobileapps:createMobilePage"})})
public class CreateMobilePageOperation
extends MobileAbstractOperation {
    public static final String OPERATION_NAME = "createMobilePage";
    public static final String PARAM_TEMPLATE = "template";
    public static final String PARAM_PARENT_PATH = "parentPath";
    public static final String PARAM_PAGE_NAME = "pageName";
    public static final String PARAM_PAGE_TITLE = "pageTitle";
    public static final String PARAM_PAGE_DESCRIPTION = "description";
    public static final String CQ_APPS_ADMIN_CREATEPAGE_EDIT = "cq-apps-admin-createpage-edit";

    @Override
    protected void perform(SlingHttpServletRequest request, HtmlResponse response, List<Modification> modifications) {
        I18n i18n = new I18n((HttpServletRequest)request);
        try {
            this.createPage(request, response, modifications, i18n);
        }
        catch (MobileOperationException e) {
            String title = i18n.get("Error");
            this.generateError(response, e.getMessage(), title);
        }
        catch (Exception ex) {
            String message = i18n.get("The server has problem processing your request");
            String title = i18n.get("Error");
            if (ex.getCause() instanceof PathNotFoundException) {
                message = i18n.get("The provided path does not exist") + ":<br/>" + ex.getCause().getMessage();
            }
            this.generateError(response, message, title);
        }
    }

    protected Page createPage(SlingHttpServletRequest request, HtmlResponse response, List<Modification> modifications, I18n i18n) throws WCMException, MobileOperationException, UnsupportedEncodingException {
        String redirectTo;
        String parentPath = request.getParameter("parentPath");
        String pageLabel = request.getParameter("pageName");
        String template = request.getParameter("template");
        String pageTitle = request.getParameter("pageTitle");
        String pageDescription = request.getParameter("description");
        if (StringUtils.isBlank((CharSequence)parentPath)) {
            String message = i18n.get("Missing mandatory parameter {0}", null, new Object[]{"parentPath"});
            throw new MobileOperationException(message);
        }
        if (StringUtils.isBlank((CharSequence)pageTitle)) {
            String message = i18n.get("Missing mandatory parameter {0}", null, new Object[]{"pageTitle"});
            throw new MobileOperationException(message);
        }
        if (StringUtils.isBlank((CharSequence)template)) {
            String message = i18n.get("Missing mandatory parameter {0}", null, new Object[]{"template"});
            throw new MobileOperationException(message);
        }
        PageManager pageManager = (PageManager)request.getResourceResolver().adaptTo(PageManager.class);
        Page page = pageManager.create(parentPath, pageLabel, template, pageTitle);
        if (StringUtils.isNotBlank((CharSequence)pageDescription)) {
            Resource pageContentResource = page.getContentResource();
            ModifiableValueMap map = (ModifiableValueMap)pageContentResource.adaptTo(ModifiableValueMap.class);
            map.put((Object)"jcr:description", (Object)pageDescription);
            try {
                pageContentResource.getResourceResolver().commit();
            }
            catch (PersistenceException e) {
                // empty catch block
            }
        }
        if (StringUtils.isBlank((CharSequence)(redirectTo = this.getRedirect(request)))) {
            redirectTo = "/libs/mobileapps/admin/content.html" + Text.escapePath((String)page.getPath());
        }
        String title = i18n.get("Page created");
        String message = i18n.get("Your page has been created.");
        String openUrl = request.getContextPath() + "/bin/wcmcommand?cmd=open&_charset_=utf-8&path=" + URLEncoder.encode(page.getPath(), "utf-8");
        response.addLink("cq-apps-admin-createpage-edit", this.xssAPI.getValidHref(openUrl), i18n.get("Open page"));
        modifications.add(new Modification(ModificationType.CREATE, page.getPath(), page.getPath()));
        this.generateResponse(response, 201, message, title, redirectTo, i18n.get("Done"));
        return page;
    }
}