RecommendationConfigureServlet.java
8.35 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.analytics.testandtarget.util.MboxHelper
* com.day.cq.wcm.api.PageManagerFactory
* com.day.cq.wcm.webservicesupport.Configuration
* com.day.cq.wcm.webservicesupport.ConfigurationManager
* com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory
* javax.servlet.ServletException
* 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.resource.PersistableValueMap
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.targetrecommendations.impl.servlet;
import com.adobe.cq.targetrecommendations.api.TargetRecommendationsAPIClient;
import com.adobe.cq.targetrecommendations.api.model.TargetRecommendation;
import com.adobe.cq.targetrecommendations.impl.model.TargetRecommendationImpl;
import com.adobe.cq.targetrecommendations.impl.util.TargetRecommendationsConfigUtil;
import com.day.cq.analytics.testandtarget.util.MboxHelper;
import com.day.cq.wcm.api.PageManagerFactory;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.ServletException;
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.resource.PersistableValueMap;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(label="Adobe AEM Target Recommendations Configuration Servlet", description="Handles configuration of a recommendation in Target Recommendations backend based on the settings provided by a recommendations component")
@Service
@Properties(value={@Property(name="sling.servlet.resourceTypes", value={"cq/personalization/components/recommendation/mbox/start"}), @Property(name="sling.servlet.selectors", value={"configureRecommendation"}), @Property(name="sling.servlet.extensions", value={"json"}), @Property(name="sling.servlet.methods", value={"POST"})})
public class RecommendationConfigureServlet
extends SlingAllMethodsServlet {
private static final Logger LOG = LoggerFactory.getLogger(RecommendationConfigureServlet.class);
public static final String PARAM_ALGORITHMS = "algorithmIds";
public static final String PARAM_TEMPLATES = "templateNames";
public static final String PARAM_REC_NAME = "recommendationName";
public static final String PARAM_MBOX_NAME = "mboxName";
public static final String PROP_RECOMMENDATION_ID = "recommendationId";
@Reference
private PageManagerFactory pageManagerFactory;
@Reference
private ConfigurationManagerFactory configManagerFactory;
@Reference
private TargetRecommendationsAPIClient recsApiClient;
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
Resource currentResource = request.getResource();
if (currentResource == null) {
LOG.error("No resource found on request!");
return;
}
Configuration ttCloudConfiguration = TargetRecommendationsConfigUtil.getTTConfigForResource(currentResource, this.pageManagerFactory, this.configManagerFactory.getConfigurationManager(request.getResourceResolver()));
if (ttCloudConfiguration == null) {
LOG.error("No valid TnT cloud configuration found for component " + currentResource.getPath());
return;
}
JSONWriter out = new JSONWriter((Writer)response.getWriter());
try {
PersistableValueMap componentProperties = (PersistableValueMap)currentResource.adaptTo(PersistableValueMap.class);
List<Integer> algorithmIds = this.getAlgorithmIds(request);
String templateNamesParam = request.getParameter("templateNames");
List<String> templateNames = Arrays.asList(templateNamesParam.split(","));
String recommendationName = request.getParameter("recommendationName");
String mboxName = MboxHelper.getMboxName((Resource)currentResource);
Integer recommendationId = (Integer)componentProperties.get("recommendationId", (Object)-1);
TargetRecommendation recommendation = new TargetRecommendationImpl(recommendationId, recommendationName, TargetRecommendation.RecommendationState.ACTIVE.toString().toLowerCase(), -1, mboxName, templateNames, algorithmIds, null, null, 11);
recommendation = this.recsApiClient.saveRecommendation(recommendation, ttCloudConfiguration);
if (recommendationId.intValue() != recommendation.getId()) {
componentProperties.put((Object)"recommendationId", (Object)recommendation.getId());
componentProperties.save();
}
out.object();
out.key("recommendationId").value((long)recommendation.getId());
out.key("recommendationName").value((Object)recommendation.getName());
out.key("recommendationMbox").value((Object)recommendation.getLocationDisplayMbox());
out.endObject();
}
catch (Exception e) {
LOG.error("Could not configure recommendation for component" + currentResource.getPath(), (Throwable)e);
response.setStatus(500);
try {
out.object();
out.key("errorMessage").value((Object)e.getMessage());
out.endObject();
}
catch (JSONException je) {
LOG.error(je.getMessage(), (Throwable)je);
}
}
}
private List<Integer> getAlgorithmIds(SlingHttpServletRequest request) {
ArrayList<Integer> algorithmIds = new ArrayList<Integer>();
String algorithmIdsParam = request.getParameter("algorithmIds");
for (String algIdStr : algorithmIdsParam.split(",")) {
try {
algorithmIds.add(Integer.parseInt(algIdStr));
continue;
}
catch (NumberFormatException nfe) {
LOG.debug("Not a valid algorithm id!", (Throwable)nfe);
}
}
return algorithmIds;
}
protected void bindPageManagerFactory(PageManagerFactory pageManagerFactory) {
this.pageManagerFactory = pageManagerFactory;
}
protected void unbindPageManagerFactory(PageManagerFactory pageManagerFactory) {
if (this.pageManagerFactory == pageManagerFactory) {
this.pageManagerFactory = null;
}
}
protected void bindConfigManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
this.configManagerFactory = configurationManagerFactory;
}
protected void unbindConfigManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
if (this.configManagerFactory == configurationManagerFactory) {
this.configManagerFactory = null;
}
}
protected void bindRecsApiClient(TargetRecommendationsAPIClient targetRecommendationsAPIClient) {
this.recsApiClient = targetRecommendationsAPIClient;
}
protected void unbindRecsApiClient(TargetRecommendationsAPIClient targetRecommendationsAPIClient) {
if (this.recsApiClient == targetRecommendationsAPIClient) {
this.recsApiClient = null;
}
}
}