PagePayloadInfoBuilder.java 3.45 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.workflow.exec.InboxItem
 *  com.adobe.granite.workflow.payload.PayloadInfo
 *  com.adobe.granite.workflow.payload.PayloadInfoBuilder
 *  com.adobe.granite.workflow.payload.PayloadInfoBuilderContext
 *  com.day.cq.wcm.api.Page
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.wcm.workflow.impl;

import com.adobe.granite.workflow.exec.InboxItem;
import com.adobe.granite.workflow.payload.PayloadInfo;
import com.adobe.granite.workflow.payload.PayloadInfoBuilder;
import com.adobe.granite.workflow.payload.PayloadInfoBuilderContext;
import com.day.cq.wcm.api.Page;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@Service(value={PayloadInfoBuilder.class})
@Property(name="service.ranking", intValue={-1000}, propertyPrivate=1)
public class PagePayloadInfoBuilder
implements PayloadInfoBuilder {
    private static final Logger log = LoggerFactory.getLogger(PagePayloadInfoBuilder.class);
    public static final String TYPE_JCR_PATH = "JCR_PATH";
    public static final String CONTENT_FINDER_SHORTCUT = "/bin/wcmcommand?cmd=open&path=";
    public static final String CHARSET = "_charset_=utf-8";
    public static final String DIRECTORY_ADMIN_SHORTCUT = "/sites.html";

    public PayloadInfo getPayloadInfo(PayloadInfoBuilderContext context) {
        String payloadPath = null;
        payloadPath = context.getInboxItem() != null ? context.getInboxItem().getContentPath() : context.getPayloadPath();
        String browserPath = null;
        Resource resource = context.getResourceResolver().getResource(payloadPath);
        if (resource != null && resource.adaptTo(Node.class) != null && resource.getPath().startsWith("/content") && !resource.getPath().startsWith("/content/dam")) {
            try {
                if (((Node)resource.adaptTo(Node.class)).isNodeType("nt:folder")) {
                    browserPath = "/sites.html" + resource.getPath();
                }
            }
            catch (RepositoryException e) {
                log.warn("exception in buildpath", (Throwable)e);
            }
        }
        if (resource != null && resource.adaptTo(Page.class) != null) {
            browserPath = "/bin/wcmcommand?cmd=open&path=" + this.encodeURIComponent(resource.getPath()) + "&" + "_charset_=utf-8";
        }
        if (browserPath != null) {
            PayloadInfo payloadInfo = context.createPayloadInfo();
            payloadInfo.setPath(payloadPath).setBrowserPath(browserPath);
            return payloadInfo;
        }
        return null;
    }

    private String encodeURIComponent(String s) {
        try {
            return URLEncoder.encode(s, "UTF-8");
        }
        catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}