CheckCredentialsServlet.java 5.33 KB
/*
 * 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;
        }
    }
}