AudienceManagerTestServiceServlet.java
4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* 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;
}
}
}