EchoSignEndpointServlet.java 6.14 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aemds.guide.addon.signing.DocumentSigningService
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.servlet.Servlet
 *  javax.servlet.ServletOutputStream
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.request.RequestPathInfo
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.servlets.SlingAllMethodsServlet
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.aemds.guide.addon.signing.echosign.servlet;

import com.adobe.aemds.guide.addon.signing.DocumentSigningService;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.Servlet;
import javax.servlet.ServletOutputStream;
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.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=1, immediate=1, label="AdobeSignEndpointServlet", description="AdobeSignEndpointServlet")
@Service(value={Servlet.class})
@Properties(value={@Property(name="sling.servlet.resourceTypes", value={"fd/af/components/guideContainer"}), @Property(name="sling.servlet.methods", value={"GET"}), @Property(name="sling.servlet.selectors", value={"adobesign"})})
public class EchoSignEndpointServlet
extends SlingAllMethodsServlet {
    private static final long serialVersionUID = 1936534819338551669L;
    private final Logger log = LoggerFactory.getLogger(EchoSignEndpointServlet.class);
    @Reference
    private DocumentSigningService documentSigningService;

    public void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) {
        throw new UnsupportedOperationException("Post is not supported for AnalyticsReportingServlet.");
    }

    public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
        block10 : {
            ResourceResolver requestResolver = request.getResourceResolver();
            Session session = (Session)requestResolver.adaptTo(Session.class);
            RequestPathInfo requestPathInfo = request.getRequestPathInfo();
            try {
                if (this.documentSigningService != null && session != null) {
                    String documentKeyNode = "echosigndocumentkey";
                    String cloudService = "/etc/cloudservices/echosign/" + requestPathInfo.getSuffix();
                    String receivedDocumentKey = request.getParameter("documentKey");
                    HashMap<String, String> configMap = new HashMap<String, String>();
                    configMap.put("CONFIG_PATH", cloudService);
                    try {
                        Node dataNode = null;
                        if (requestPathInfo.getSelectors().length == 2) {
                            String UUID2 = requestPathInfo.getSelectors()[1];
                            Resource tempResource = request.getResourceResolver().getResource("/tmp/fd/af//" + UUID2);
                            if (tempResource != null) {
                                Node uuidNode = (Node)tempResource.adaptTo(Node.class);
                                dataNode = uuidNode.hasNode(documentKeyNode) ? uuidNode.getNode(documentKeyNode) : uuidNode.addNode(documentKeyNode, "nt:unstructured");
                            } else {
                                response.sendError(500, "Failed to obtain the location access for storage.");
                            }
                            byte[] bytesData = this.documentSigningService.getSignedDocument(receivedDocumentKey, configMap);
                            if (bytesData != null) {
                                response.setContentType("application/pdf");
                                response.setContentLength(bytesData.length);
                                response.getOutputStream().write(bytesData);
                                dataNode.setProperty("documentKey", receivedDocumentKey);
                                session.save();
                            } else {
                                response.sendError(500, "Failed to download the signed pdf.");
                            }
                            break block10;
                        }
                        this.log.error("Invalid request not having proper selectors.");
                    }
                    catch (RepositoryException repoException) {
                        this.log.error("Repository exception while saving the document key", (Throwable)repoException);
                    }
                    break block10;
                }
                response.sendError(500, "Failed to get Adobe Sign service instance or session");
            }
            catch (IOException ioException) {
                this.log.error("IO Exception while obtaining the signed PDF", (Throwable)ioException);
            }
        }
    }

    protected void bindDocumentSigningService(DocumentSigningService documentSigningService) {
        this.documentSigningService = documentSigningService;
    }

    protected void unbindDocumentSigningService(DocumentSigningService documentSigningService) {
        if (this.documentSigningService == documentSigningService) {
            this.documentSigningService = null;
        }
    }
}