AudienceManagerTestServiceServlet.java 4.11 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.servlet.ServletException
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.sling.SlingServlet
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.servlets.SlingAllMethodsServlet
 */
package com.adobe.cq.aam;

import com.adobe.cq.aam.client.spi.AudienceManagerAccessDenied;
import com.adobe.cq.aam.client.spi.AudienceManagerClient;
import com.adobe.cq.aam.client.spi.OAuthResponse;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.HashMap;

@Component(metatype=0, immediate=1)
@SlingServlet(extensions={"json"}, methods={"POST"}, resourceTypes={"cq/cloudserviceconfigs/components/servicepage"}, selectors={"aamtest"}, generateComponent=0)
public class AudienceManagerTestServiceServlet
extends SlingAllMethodsServlet {
    private static final long serialVersionUID = -1138218097512352501L;
    @Reference
    private AudienceManagerClient audienceManagerClient;

    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        try {
            Resource resource = request.getResource();
            Node n = (Node)resource.adaptTo(Node.class);
            if (!n.hasProperty("componentReference") || !"cq/personalization/audiencemanager/components/cloudservices/audiencemanager".equals(n.getProperty("componentReference").getString())) {
                response.sendError(403, "Audience manager test may only be invoked on a resource with componentReference == cq/personalization/audiencemanager/components/cloudservices/audiencemanager");
            } else {
                OAuthResponse oAuthResponse;
                String username = request.getParameter("username");
                String password = request.getParameter("password");
                String partnerID = request.getParameter("partnerID");
                String containerNSID = request.getParameter("containerNSID");
                HashMap<String, Object> properties = new HashMap<String, Object>();
                if (partnerID != null) {
                    properties.put("partner", partnerID);
                    if (containerNSID != null) {
                        properties.put("container", containerNSID);
                    } else {
                        properties.put("container", 0);
                    }
                }
                if ((oAuthResponse = this.audienceManagerClient.authenticate(username, password, properties)) != null) {
                    response.setContentType("application/json; charset=utf-8");
                    response.getWriter().write(oAuthResponse.toJson());
                } else {
                    response.sendError(400, "Endpoint configuration invalid");
                }
            }
        }
        catch (RepositoryException e) {
            throw new ServletException(e.getMessage(), (Throwable)e);
        }
        catch (AudienceManagerAccessDenied e) {
            response.sendError(403, "Access Forbidden, please check cloud configuration");
        }
    }

    protected void bindAudienceManagerClient(AudienceManagerClient audienceManagerClient) {
        this.audienceManagerClient = audienceManagerClient;
    }

    protected void unbindAudienceManagerClient(AudienceManagerClient audienceManagerClient) {
        if (this.audienceManagerClient == audienceManagerClient) {
            this.audienceManagerClient = null;
        }
    }
}