AdminServerServlet.java
4.34 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.servlet.ServletException
* org.apache.commons.httpclient.HttpClient
* org.apache.commons.httpclient.HttpMethod
* org.apache.commons.httpclient.methods.GetMethod
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Modified
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.servlets.SlingSafeMethodsServlet
* org.apache.sling.commons.osgi.PropertiesUtil
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.analytics.testandtarget.impl.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Modified;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1, label="Adobe Test & Target Endpoint Configuration")
@Service
@Properties(value={@Property(name="sling.servlet.paths", value={"/libs/cq/analytics/testandtarget/adminserver"}, propertyPrivate=1), @Property(name="sling.servlet.extensions", value={"json"}, propertyPrivate=1), @Property(name="sling.servlet.methods", value={"GET"}, propertyPrivate=1)})
public class AdminServerServlet
extends SlingSafeMethodsServlet {
private final Logger log;
private static final String NO_PARAM_ERROR = "The 'account' parameter is missing";
private static final String INVALID_PARAM_ERROR = "The value of the 'account' parameter is invalid";
private static final String PARAM_ACCOUNT_NO = "account";
private static final String DEFAULT_ENDPOINT_URL = "https://admin.testandtarget.omniture.com/rest/v1/endpoint";
@Property(value={"https://admin.testandtarget.omniture.com/rest/v1/endpoint"}, label="T&T endpoint URL")
private static final String ENDPOINT_URL = "testandtarget.endpoint.url";
private static final Pattern ACCOUNT_PATTERN = Pattern.compile("^(?!(mbox[0-9]|dev|staging|qa))[a-zA-Z0-9]*$");
private String testAndTargetEndpointUrl;
public AdminServerServlet() {
this.log = LoggerFactory.getLogger(this.getClass());
}
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
String accountNo = request.getParameter("account");
if (StringUtils.isEmpty((String)accountNo)) {
response.sendError(400, "The 'account' parameter is missing");
return;
}
Matcher matcher = ACCOUNT_PATTERN.matcher(accountNo);
if (!matcher.matches()) {
response.sendError(400, "The value of the 'account' parameter is invalid");
return;
}
HttpClient client = new HttpClient();
GetMethod get = new GetMethod(this.testAndTargetEndpointUrl + "/" + accountNo);
int responseCode = client.executeMethod((HttpMethod)get);
String responseString = get.getResponseBodyAsString();
response.setContentType("application/json");
response.setStatus(responseCode);
response.getWriter().write(responseString);
}
@Activate
@Modified
private void activate(Map<String, Object> props) {
if (props != null) {
this.testAndTargetEndpointUrl = PropertiesUtil.toString((Object)props.get("testandtarget.endpoint.url"), (String)"https://admin.testandtarget.omniture.com/rest/v1/endpoint");
}
}
}