TargetAudienceUpdateServlet.java
7.3 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.tsdk.components.audience.SegmentBuilder
* com.day.cq.wcm.webservicesupport.Configuration
* com.day.cq.wcm.webservicesupport.ConfigurationManager
* com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory
* com.google.gson.ExclusionStrategy
* com.google.gson.FieldAttributes
* com.google.gson.Gson
* com.google.gson.GsonBuilder
* com.google.gson.JsonObject
* com.google.gson.JsonSyntaxException
* com.google.gson.TypeAdapterFactory
* javax.annotation.Nonnull
* javax.servlet.ServletException
* org.apache.commons.lang.StringUtils
* 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.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.analytics.testandtarget.impl.servlets;
import com.adobe.tsdk.components.audience.SegmentBuilder;
import com.day.cq.analytics.testandtarget.Segment;
import com.day.cq.analytics.testandtarget.impl.ISO8601DateTypeAdapter;
import com.day.cq.analytics.testandtarget.impl.SegmentImporter;
import com.day.cq.analytics.testandtarget.impl.TestandtargetPrivateService;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.google.gson.TypeAdapterFactory;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.Principal;
import java.util.Collections;
import javax.annotation.Nonnull;
import javax.servlet.ServletException;
import org.apache.commons.lang.StringUtils;
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.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SlingServlet(name="com.day.cq.analytics.testandtarget.impl.servlets.TargetAudienceCreateServlet", resourceTypes={"cq/analytics/components/testandtargetpage"}, selectors={"audienceupdate"}, extensions={"json"}, methods={"POST"})
public class TargetAudienceUpdateServlet
extends SlingAllMethodsServlet {
private static final Logger LOG = LoggerFactory.getLogger(TargetAudienceUpdateServlet.class);
private static final String PARAM_AUDIENCE_JSON = "audienceJson";
private static final String PARAM_AUDIENCE_PATH = "audiencePath";
private static final String TARGET_AUDIENCES_ROOT_PATH = "/etc/segmentation/adobe-target";
@Reference
private TestandtargetPrivateService testandtargetService;
@Reference
private ConfigurationManagerFactory configurationManagerFactory;
protected void doPost(@Nonnull SlingHttpServletRequest request, @Nonnull SlingHttpServletResponse response) throws ServletException, IOException {
String configurationPath = request.getResource().getParent().getPath();
String audienceJson = request.getParameter("audienceJson");
String audiencePath = request.getParameter("audiencePath");
long editAudienceId = -1;
if (StringUtils.isNotEmpty((String)audiencePath)) {
editAudienceId = Long.parseLong(ResourceUtil.getName((String)audiencePath));
}
JsonObject responseJson = new JsonObject();
PrintWriter out = response.getWriter();
int responseCode = 200;
String errorMessage = "";
ResourceResolver resolver = request.getResourceResolver();
try {
ConfigurationManager configurationManager = this.configurationManagerFactory.getConfigurationManager(request.getResourceResolver());
Configuration config = configurationManager.getConfiguration(configurationPath);
String clientCode = (String)config.getInherited("clientcode", (Object)"");
Resource targetRes = ResourceUtil.getOrCreateResource((ResourceResolver)resolver, (String)("/etc/segmentation/adobe-target/" + clientCode), Collections.singletonMap("jcr:primaryType", "sling:OrderedFolder"), (String)null, (boolean)false);
String audienceJsonToSync = new SegmentBuilder().setAudience(audienceJson).build().getSegment();
String audienceUpdateResponse = "";
audienceUpdateResponse = editAudienceId > 0 ? this.testandtargetService.updateAudience(config, audienceJsonToSync, editAudienceId, true, request.getUserPrincipal().getName()) : this.testandtargetService.createAudience(config, audienceJsonToSync, true, request.getUserPrincipal().getName());
Segment segment = this.extractAudience(audienceUpdateResponse);
SegmentImporter.writeSegment(targetRes, segment, configurationPath, audienceJson);
request.getResourceResolver().commit();
}
catch (Exception e) {
responseCode = 500;
errorMessage = e.getMessage();
LOG.error("Can't create new audience! Audience Json body = " + audienceJson, (Throwable)e);
}
response.setContentType("application/json");
response.setStatus(responseCode);
if (StringUtils.isNotEmpty((String)errorMessage)) {
responseJson.addProperty("errorMessage", errorMessage);
}
out.print(responseJson.toString());
}
private Segment extractAudience(String segmentJson) throws JsonSyntaxException {
return (Segment)new GsonBuilder().registerTypeAdapterFactory(ISO8601DateTypeAdapter.FACTORY).setExclusionStrategies(new ExclusionStrategy[]{new ExclusionStrategy(){
public boolean shouldSkipField(FieldAttributes f) {
return f.getName().equals("segmentRule");
}
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
}}).create().fromJson(segmentJson, Segment.class);
}
protected void bindTestandtargetService(TestandtargetPrivateService testandtargetPrivateService) {
this.testandtargetService = testandtargetPrivateService;
}
protected void unbindTestandtargetService(TestandtargetPrivateService testandtargetPrivateService) {
if (this.testandtargetService == testandtargetPrivateService) {
this.testandtargetService = null;
}
}
protected void bindConfigurationManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
this.configurationManagerFactory = configurationManagerFactory;
}
protected void unbindConfigurationManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
if (this.configurationManagerFactory == configurationManagerFactory) {
this.configurationManagerFactory = null;
}
}
}