CheckCredentialsServlet.java
5.33 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
/*
* 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.NameValuePair
* org.apache.commons.httpclient.methods.GetMethod
* 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.servlets.SlingSafeMethodsServlet
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.mcm.campaign.servlets;
import com.day.cq.mcm.campaign.impl.HttpClientBuilder;
import com.day.cq.mcm.campaign.impl.IntegrationConfig;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.URISyntaxException;
import java.util.regex.Matcher;
import javax.servlet.ServletException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
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.servlets.SlingSafeMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SlingServlet(selectors={"campaign.credentials"}, resourceTypes={"mcm/campaign/components/configpage"}, extensions={"json"}, methods={"GET"})
public class CheckCredentialsServlet
extends SlingSafeMethodsServlet {
private static final String PRM_TEMPLATE = "template";
private final Logger log;
@Reference
private IntegrationConfig config;
public CheckCredentialsServlet() {
this.log = LoggerFactory.getLogger(this.getClass());
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private boolean tryPing(String apiEndpoint, String host, String user, String password) {
boolean isSuccess = false;
GetMethod get = null;
String url = host + apiEndpoint;
String token = "#user#/#password#".replaceAll("#user#", Matcher.quoteReplacement(user)).replaceAll("#password#", Matcher.quoteReplacement(password));
NameValuePair[] params = new NameValuePair[]{new NameValuePair("__sessiontoken", token)};
try {
HttpClient hc = HttpClientBuilder.createHttpClient(url, this.config);
get = new GetMethod(apiEndpoint);
get.setQueryString(params);
this.log.info("Trying to access '{}' to verify credentials ...", (Object)url);
int status = hc.executeMethod((HttpMethod)get);
this.log.info("Connection established successfully; return code is: {}", (Object)status);
isSuccess = status == 200;
}
catch (URISyntaxException use) {
this.log.info("Invalid URL: {}", (Object)url, (Object)use);
}
catch (IOException ioe) {
this.log.info("Connection to '{}' failed", (Object)host, (Object)ioe);
}
finally {
if (get != null) {
try {
get.getResponseBody();
}
catch (IOException ioe) {}
get.releaseConnection();
}
}
return isSuccess;
}
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
boolean isSuccess = false;
try {
String user = request.getParameter("user");
if (user == null) {
throw new ServletException("Missing parameter 'user'.");
}
String password = request.getParameter("pwd");
if (password == null) {
throw new ServletException("Missing parameter 'pwd'.");
}
String host = request.getParameter("host");
if (host == null) {
throw new ServletException("Missing parameter 'host'.");
}
isSuccess = this.tryPing("/jssp/nms/amcPing.jssp", host, user, password);
if (!isSuccess) {
isSuccess = this.tryPing("/nl/jsp/ping.jsp", host, user, password);
}
}
catch (ServletException se) {
this.log.error("Could not test connection; URL: " + request.getRequestURI(), (Throwable)se);
}
response.setContentType("application/json");
JSONWriter writer = new JSONWriter((Writer)response.getWriter());
try {
writer.object();
writer.key("connected").value(isSuccess);
writer.endObject();
}
catch (JSONException je) {
throw new ServletException((Throwable)je);
}
}
protected void bindConfig(IntegrationConfig integrationConfig) {
this.config = integrationConfig;
}
protected void unbindConfig(IntegrationConfig integrationConfig) {
if (this.config == integrationConfig) {
this.config = null;
}
}
}