SitecatalystServlet.java
7.96 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.webservicesupport.Configuration
* com.day.cq.wcm.webservicesupport.ConfigurationManager
* com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory
* javax.jcr.Node
* javax.jcr.RepositoryException
* javax.jcr.Session
* 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.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.analytics.sitecatalyst.impl.servlets;
import com.day.cq.analytics.sitecatalyst.SitecatalystException;
import com.day.cq.analytics.sitecatalyst.SitecatalystWebservice;
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 javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
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.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.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service
@Properties(value={@Property(name="sling.servlet.extensions", value={"json"}), @Property(name="sling.servlet.paths", value={"/libs/cq/analytics/sitecatalyst/service"}), @Property(name="sling.servlet.methods", value={"POST"})})
public class SitecatalystServlet
extends SlingAllMethodsServlet {
private final Logger log;
private static final String PARAMETER_METHOD = "method";
private static final String PARAMETER_CONFIG_PATH = "configPath";
private static final String PARAMETER_COMPANY = "company";
private static final String PARAMETER_USERNAME = "username";
private static final String PARAMETER_PASSWORD = "password";
private static final String PARAMETER_SERVER = "server";
private static final String PARAMETER_RSID = "rsid";
@Reference
private ConfigurationManagerFactory configManagerFactory;
@Reference
protected SitecatalystWebservice catalystService;
public SitecatalystServlet() {
this.log = LoggerFactory.getLogger(this.getClass());
}
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
block8 : {
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String method = request.getParameter("method");
try {
if ("Connect".equals(method)) {
this.handleConnect(request, out);
break block8;
}
this.handleAPICall(request, out, method);
}
catch (SitecatalystException e) {
this.log.error("Call to SiteCatalyst method '" + request.getParameter("method") + "' failed", (Throwable)e);
out.write("{error: '" + e.getMessage() + "'}");
}
catch (Exception e) {
this.log.error("Exception occured while calling Sitecatalyst service", (Throwable)e);
throw new ServletException("Exception occured while calling Sitecatalyst service", (Throwable)e);
}
finally {
out.flush();
}
}
}
private void handleConnect(SlingHttpServletRequest request, Writer out) throws Exception {
String jsonResponse = "{}";
String company = request.getParameter("company");
String username = request.getParameter("username");
String password = request.getParameter("password");
String server = request.getParameter("server");
try {
String secret = this.catalystService.getLoginKey(server, company, username, password);
JSONObject json = new JSONObject();
if (password != null) {
json.put("secret", (Object)secret);
}
jsonResponse = json.toString();
}
catch (JSONException e) {
this.log.error(e.getMessage(), (Throwable)e);
}
out.write(jsonResponse);
}
private void handleAPICall(SlingHttpServletRequest request, Writer out, String method) throws Exception {
String jsonResponse = "{}";
String rsid = request.getParameter("rsid");
String configPath = request.getParameter("configPath");
Configuration configuration = this.configManagerFactory.getConfigurationManager(request.getResourceResolver()).getConfiguration(configPath);
if (configuration == null) {
throw new SitecatalystException("Configuration has not been found");
}
if (rsid == null) {
rsid = (String)configuration.getInherited("reportsuite", (Object)"");
}
if (method.equals("Company.GetReportSuites")) {
jsonResponse = this.catalystService.getReportSuites(configuration);
} else if (method.equals("Company.GetTrackingServer")) {
String[] rsidArray = rsid.split(",");
if (rsidArray.length > 0) {
jsonResponse = this.catalystService.getTrackingServer(configuration, rsidArray[0]);
}
} else if (method.equals("ReportSuite.GetEVars")) {
jsonResponse = this.catalystService.getEvars(configuration, rsid);
} else if (method.equals("ReportSuite.GetTrafficVars")) {
jsonResponse = this.catalystService.getTrafficVars(configuration, rsid);
} else if (method.equals("ReportSuite.GetSuccessEvents")) {
jsonResponse = this.catalystService.getSuccessEvents(configuration, rsid);
}
out.write(jsonResponse);
}
private void saveConfiguration(Configuration configuration) {
try {
Session session;
Resource resource = configuration.getResource();
if (resource != null && (session = ((Node)resource.adaptTo(Node.class)).getSession()).hasPendingChanges()) {
session.save();
}
}
catch (RepositoryException e) {
this.log.error("Could not save updated configuration", (Throwable)e);
}
}
protected void bindConfigManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
this.configManagerFactory = configurationManagerFactory;
}
protected void unbindConfigManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
if (this.configManagerFactory == configurationManagerFactory) {
this.configManagerFactory = null;
}
}
protected void bindCatalystService(SitecatalystWebservice sitecatalystWebservice) {
this.catalystService = sitecatalystWebservice;
}
protected void unbindCatalystService(SitecatalystWebservice sitecatalystWebservice) {
if (this.catalystService == sitecatalystWebservice) {
this.catalystService = null;
}
}
}