MobilePublishAbstractOperation.java 11.2 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.wcm.api.Page
 *  javax.jcr.RepositoryException
 *  org.apache.commons.lang.StringUtils
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.servlets.post.AbstractPostOperation
 *  org.apache.sling.servlets.post.Modification
 *  org.apache.sling.servlets.post.PostResponse
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.mobile.dps.impl.operations;

import com.adobe.cq.mobile.dps.DPSArticle;
import com.adobe.cq.mobile.dps.DPSCollection;
import com.adobe.cq.mobile.dps.DPSEntity;
import com.adobe.cq.mobile.dps.DPSException;
import com.adobe.cq.mobile.dps.DPSObject;
import com.adobe.cq.mobile.dps.DPSProject;
import com.adobe.cq.mobile.dps.impl.DPSClient;
import com.adobe.cq.mobile.dps.impl.DPSEntityImporter;
import com.adobe.cq.mobile.dps.impl.DPSPageManager;
import com.adobe.cq.mobile.dps.impl.service.DPSClientAdapterFactory;
import com.adobe.cq.mobile.dps.impl.service.DPSEntityImporterAdapterFactory;
import com.adobe.cq.mobile.dps.impl.service.DPSPageManagerAdapterFactory;
import com.adobe.cq.mobile.dps.impl.ui.PerfTimer;
import com.adobe.cq.mobile.dps.impl.utils.DPSUtil;
import com.adobe.cq.mobile.dps.impl.utils.RequestUtils;
import com.adobe.granite.ui.components.HtmlResponse;
import com.adobe.granite.xss.XSSAPI;
import com.day.cq.wcm.api.Page;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.jcr.RepositoryException;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.servlets.post.AbstractPostOperation;
import org.apache.sling.servlets.post.Modification;
import org.apache.sling.servlets.post.PostResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0, label="Experience Manager Mobile Abstract Operation")
@Service
public abstract class MobilePublishAbstractOperation
extends AbstractPostOperation {
    public static final String NS_DPS_APPS = "dpsapps:";
    private static final String PARAM_APP_INSTANCE = "appInstance";
    @Reference
    protected XSSAPI xssAPI;
    @Reference
    protected DPSPageManagerAdapterFactory dpsAdapterManager;
    @Reference
    protected DPSClientAdapterFactory dpsClientAdapterFactory;
    @Reference
    protected DPSEntityImporterAdapterFactory dpsEntityImporterAdapterFactory;
    private Logger logger = LoggerFactory.getLogger(MobilePublishAbstractOperation.class);

    public MobilePublishAbstractOperation() {
        this.logger = LoggerFactory.getLogger(this.getClass());
    }

    protected void doRun(SlingHttpServletRequest request, PostResponse response, List<Modification> changes) throws RepositoryException {
        PerfTimer perfTimer = PerfTimer.startTimer((Object)this, this.getClass().getName());
        this.perform(request, (HtmlResponse)response, changes);
        perfTimer.end();
    }

    protected abstract void perform(SlingHttpServletRequest var1, HtmlResponse var2, List<Modification> var3);

    protected void generateResponse(HtmlResponse response, int statusCode, String message, String title, String redirectLink, String linkTitle) {
        response.setStatus(statusCode, message);
        response.setTitle(title);
        response.setDescription(message);
        if (StringUtils.isNotBlank((String)redirectLink)) {
            response.addRedirectLink(redirectLink, linkTitle);
        }
    }

    protected void addLink(HtmlResponse response, String rel, String url, String title) {
        response.addLink(rel, url, title);
    }

    protected void generateResponse(HtmlResponse response, int statusCode, String message, String title) {
        this.generateResponse(response, statusCode, message, title, null, null);
    }

    protected void generateError(HtmlResponse response, String message, String title) {
        this.generateError(response, message, title, null);
    }

    protected void generateError(HtmlResponse response, String message, String title, Throwable exception) {
        if (exception == null) {
            this.getLogger().error(title + ":" + message);
        } else {
            this.getLogger().error(title + ":" + message, exception);
        }
        this.generateResponse(response, 500, message, title);
    }

    protected void generateError(HtmlResponse response, String message, String title, Throwable exception, String redirect, String redirectTitle) {
        if (exception == null) {
            this.getLogger().error(title + ":" + message);
        } else {
            this.getLogger().error(title + ":" + message, exception);
        }
        this.generateResponse(response, 500, message, title, redirect, redirectTitle);
    }

    protected String getRedirect(SlingHttpServletRequest request) {
        return request.getParameter("redirectTo");
    }

    protected Logger getLogger() {
        return this.logger;
    }

    protected DPSPageManager getDPSPageManager(SlingHttpServletRequest request) {
        return this.dpsAdapterManager.getAdapter((Object)request.getResourceResolver(), DPSPageManager.class);
    }

    protected DPSClient getDPSClient(SlingHttpServletRequest request) {
        return this.dpsClientAdapterFactory.getAdapter((Object)request.getResourceResolver(), DPSClient.class);
    }

    protected DPSEntityImporter getDPSEntityImporter(SlingHttpServletRequest request) {
        return this.dpsEntityImporterAdapterFactory.getAdapter((Object)request.getResourceResolver(), DPSEntityImporter.class);
    }

    protected DPSObject getDPSObject(SlingHttpServletRequest request) {
        Resource resource = request.getResource();
        String appInstancePath = request.getParameter("appInstance");
        if (StringUtils.isNotEmpty((String)appInstancePath)) {
            resource = request.getResourceResolver().resolve(appInstancePath);
        }
        Page page = (Page)resource.adaptTo(Page.class);
        DPSObject dpsObject = null;
        if (DPSUtil.isADPSResource(page)) {
            dpsObject = DPSUtil.getDPSObject(page);
        }
        return dpsObject;
    }

    protected DPSProject getDPSProject(SlingHttpServletRequest request) {
        DPSProject dpsProject = null;
        DPSObject dpsObject = this.getDPSObject(request);
        if (dpsObject != null && dpsObject instanceof DPSProject) {
            dpsProject = (DPSProject)dpsObject;
        }
        return dpsProject;
    }

    protected DPSArticle getDPSArticle(SlingHttpServletRequest request) {
        DPSArticle dpsArticle = null;
        DPSObject dpsObject = this.getDPSObject(request);
        if (dpsObject != null && dpsObject instanceof DPSArticle) {
            dpsArticle = (DPSArticle)dpsObject;
        }
        return dpsArticle;
    }

    protected DPSCollection getDPSCollection(SlingHttpServletRequest request) {
        DPSCollection dpsCollection = null;
        DPSObject dpsObject = this.getDPSObject(request);
        if (dpsObject != null && dpsObject instanceof DPSCollection) {
            dpsCollection = (DPSCollection)dpsObject;
        }
        return dpsCollection;
    }

    protected DPSEntity getDPSEntity(SlingHttpServletRequest request) {
        DPSEntity dpsEntity = null;
        DPSObject dpsObject = this.getDPSObject(request);
        if (dpsObject != null && dpsObject instanceof DPSEntity) {
            dpsEntity = (DPSEntity)dpsObject;
        }
        return dpsEntity;
    }

    protected boolean getParameter(SlingHttpServletRequest request, String paramKey, boolean defaultValue) {
        return RequestUtils.getParameter(request, paramKey, defaultValue);
    }

    protected String getParameter(SlingHttpServletRequest request, String paramKey, String defaultValue) {
        return RequestUtils.getParameter(request, paramKey, defaultValue);
    }

    protected String getCauseMessage(Exception ex) {
        String cause;
        String causeMsg = "";
        if (ex.getCause() != null && (cause = ex.getCause().getLocalizedMessage()) != null && org.apache.commons.lang3.StringUtils.isNotEmpty((CharSequence)cause)) {
            causeMsg = cause;
        }
        return causeMsg;
    }

    protected List<DPSEntity> getEntityList(ResourceResolver resourceResolver, String entityPathsCSV) throws DPSException {
        List<String> entitiesPathList = Arrays.asList(entityPathsCSV.split(","));
        ArrayList<DPSEntity> entitiesList = new ArrayList<DPSEntity>();
        for (String entityPath : entitiesPathList) {
            Resource entityResource = resourceResolver.getResource(entityPath);
            if (entityResource == null) {
                throw new DPSException("Invalid entity path " + entityPath);
            }
            Page entityPage = (Page)entityResource.adaptTo(Page.class);
            if (entityPage == null) {
                throw new DPSException("Invalid entity path " + entityPath);
            }
            DPSEntity dpsEntityObject = (DPSEntity)DPSUtil.getDPSObject(entityPage);
            if (dpsEntityObject != null) {
                entitiesList.add(dpsEntityObject);
                continue;
            }
            throw new DPSException("Invalid entity path " + entityPath);
        }
        return entitiesList;
    }

    protected void bindXssAPI(XSSAPI xSSAPI) {
        this.xssAPI = xSSAPI;
    }

    protected void unbindXssAPI(XSSAPI xSSAPI) {
        if (this.xssAPI == xSSAPI) {
            this.xssAPI = null;
        }
    }

    protected void bindDpsAdapterManager(DPSPageManagerAdapterFactory dPSPageManagerAdapterFactory) {
        this.dpsAdapterManager = dPSPageManagerAdapterFactory;
    }

    protected void unbindDpsAdapterManager(DPSPageManagerAdapterFactory dPSPageManagerAdapterFactory) {
        if (this.dpsAdapterManager == dPSPageManagerAdapterFactory) {
            this.dpsAdapterManager = null;
        }
    }

    protected void bindDpsClientAdapterFactory(DPSClientAdapterFactory dPSClientAdapterFactory) {
        this.dpsClientAdapterFactory = dPSClientAdapterFactory;
    }

    protected void unbindDpsClientAdapterFactory(DPSClientAdapterFactory dPSClientAdapterFactory) {
        if (this.dpsClientAdapterFactory == dPSClientAdapterFactory) {
            this.dpsClientAdapterFactory = null;
        }
    }

    protected void bindDpsEntityImporterAdapterFactory(DPSEntityImporterAdapterFactory dPSEntityImporterAdapterFactory) {
        this.dpsEntityImporterAdapterFactory = dPSEntityImporterAdapterFactory;
    }

    protected void unbindDpsEntityImporterAdapterFactory(DPSEntityImporterAdapterFactory dPSEntityImporterAdapterFactory) {
        if (this.dpsEntityImporterAdapterFactory == dPSEntityImporterAdapterFactory) {
            this.dpsEntityImporterAdapterFactory = null;
        }
    }
}