DTMCompaniesServlet.java 4.34 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.crypto.CryptoSupport
 *  com.day.cq.i18n.I18n
 *  javax.servlet.ServletException
 *  javax.servlet.http.HttpServletRequest
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.sling.SlingServlet
 *  org.apache.http.osgi.services.HttpClientBuilderFactory
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.servlets.SlingAllMethodsServlet
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 */
package com.adobe.cq.dtm.impl.servlets;

import com.adobe.cq.dtm.impl.util.DTMConfigurationUtil;
import com.adobe.granite.crypto.CryptoSupport;
import com.day.cq.i18n.I18n;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.http.osgi.services.HttpClientBuilderFactory;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
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;

@SlingServlet(methods={"POST"}, resourceTypes={"cq/dtm/components/dynamictagmanagementpage"}, selectors={"companies"}, extensions={"json"})
public class DTMCompaniesServlet
extends SlingAllMethodsServlet {
    @Reference
    private CryptoSupport cryptoSupport;
    @Reference
    private HttpClientBuilderFactory clientBuilderFactory = null;

    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        block10 : {
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            JSONObject jsonResponse = new JSONObject();
            I18n i18n = new I18n((HttpServletRequest)request);
            try {
                ResourceResolver resourceResolver = request.getResourceResolver();
                String dtmApiUrl = DTMConfigurationUtil.getDTMApiUrl(resourceResolver);
                if (dtmApiUrl != null) {
                    String token = DTMConfigurationUtil.getValueIfEncrypted(this.cryptoSupport, request.getParameter("token"));
                    if (token != null) {
                        JSONObject jsonObject = DTMConfigurationUtil.callDTMApi(this.clientBuilderFactory, dtmApiUrl = dtmApiUrl + "/companies", token);
                        if (jsonObject != null) {
                            jsonResponse = jsonObject;
                        } else {
                            DTMConfigurationUtil.setErrorResponse(response, jsonResponse, 500, i18n, "Failed to obtain a response from the DTM API Server.", null);
                        }
                    } else {
                        DTMConfigurationUtil.setErrorResponse(response, jsonResponse, 400, i18n, "Empty token parameter.", null);
                    }
                    break block10;
                }
                DTMConfigurationUtil.setErrorResponse(response, jsonResponse, 500, i18n, "DTM API server URL not set.", null);
            }
            catch (JSONException e) {
                throw new ServletException("Error creating JSON response.", (Throwable)e);
            }
            finally {
                response.getWriter().write(jsonResponse.toString());
            }
        }
    }

    protected void bindCryptoSupport(CryptoSupport cryptoSupport) {
        this.cryptoSupport = cryptoSupport;
    }

    protected void unbindCryptoSupport(CryptoSupport cryptoSupport) {
        if (this.cryptoSupport == cryptoSupport) {
            this.cryptoSupport = null;
        }
    }

    protected void bindClientBuilderFactory(HttpClientBuilderFactory httpClientBuilderFactory) {
        this.clientBuilderFactory = httpClientBuilderFactory;
    }

    protected void unbindClientBuilderFactory(HttpClientBuilderFactory httpClientBuilderFactory) {
        if (this.clientBuilderFactory == httpClientBuilderFactory) {
            this.clientBuilderFactory = null;
        }
    }
}