OptOutServiceImpl.java 8 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.servlet.ServletException
 *  javax.servlet.http.Cookie
 *  javax.servlet.http.HttpServletRequest
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.PropertyUnbounded
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.SlingHttpServletResponse
 *  org.apache.sling.api.servlets.SlingAllMethodsServlet
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.io.JSONWriter
 *  org.apache.sling.commons.osgi.PropertiesUtil
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.optout.impl;

import com.adobe.granite.optout.api.OptOutService;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Activate;
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.PropertyUnbounded;
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.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
@Component(metatype=1)
@Service
@Properties(value={@Property(name="sling.servlet.paths", value={"/libs/granite/security/optout/configuration"}, propertyPrivate=1), @Property(name="sling.servlet.extensions", value={"json"}, propertyPrivate=1), @Property(name="sling.servlet.methods", value={"GET"}, propertyPrivate=1), @Property(name="optout.cookies", unbounded=PropertyUnbounded.ARRAY, value={"cq-opt-out", "omniture_optout"}), @Property(name="optout.headers", unbounded=PropertyUnbounded.ARRAY, value={}), @Property(name="optout.whitelist.cookies", unbounded=PropertyUnbounded.ARRAY, value={"cq-show-clientcontext", "cq-scrollpos", "cq-sk-collapsed", "login-token", "ys-cq-siteadmin-tree", "ys-cq-damadmin-tree", "ys-cq-collabadmin-tree", "ys-cq-miscadmin-tree", "ys-cq-tagadmin", "ys-cq-cf-clipboard", "ys-cq-cf-tabpanel", "ys-cq-sk-tabpanel", "SessionPersistence", "wcmmode", "cq-authoring-mode"})})
public class OptOutServiceImpl
extends SlingAllMethodsServlet
implements OptOutService {
    private static final Logger log = LoggerFactory.getLogger(OptOutServiceImpl.class);
    private static final String[] DEFAULT_OPTOUT_COOKIES = new String[]{"cq-opt-out", "omniture_optout"};
    private static final String[] DEFAULT_OPTOUT_HEADERS = new String[0];
    private static final String[] DEFAULT_WHITELIST_COOKIES = new String[]{"cq-show-clientcontext", "cq-scrollpos", "cq-sk-collapsed", "login-token", "ys-cq-siteadmin-tree", "ys-cq-damadmin-tree", "ys-cq-collabadmin-tree", "ys-cq-miscadmin-tree", "ys-cq-tagadmin", "ys-cq-cf-clipboard", "ys-cq-cf-tabpanel", "ys-cq-sk-tabpanel", "SessionPersistence", "wcmmode", "cq-authoring-mode"};
    protected static final String NAME_OPTOUT_COOKIES = "optout.cookies";
    protected static final String NAME_OPTOUT_HEADERS = "optout.headers";
    protected static final String NAME_WHITELIST_COOKIES = "optout.whitelist.cookies";
    private Collection<String> optOutCookies;
    private Map<String, String> optOutHeaders;
    private Collection<String> whiteListCookies;

    @Activate
    protected void configure(ComponentContext context) {
        Dictionary properties = context.getProperties();
        this.optOutCookies = new HashSet<String>();
        Collections.addAll(this.optOutCookies, PropertiesUtil.toStringArray(properties.get("optout.cookies"), (String[])DEFAULT_OPTOUT_COOKIES));
        List<String> headerNameValuePairs = Arrays.asList(PropertiesUtil.toStringArray(properties.get("optout.headers"), (String[])DEFAULT_OPTOUT_HEADERS));
        this.optOutHeaders = new HashMap<String, String>();
        for (String headerNameValuePair : headerNameValuePairs) {
            String[] fragments = StringUtils.split((String)headerNameValuePair, (String)";");
            if (fragments.length == 2) {
                this.optOutHeaders.put(fragments[0], fragments[1]);
                log.debug("configure: added header name-value pair: [{}]", (Object)headerNameValuePair);
                continue;
            }
            log.warn("configure: invalid header name-value pair specified [{}], ignoring.", (Object)headerNameValuePair);
        }
        this.whiteListCookies = new HashSet<String>();
        Collections.addAll(this.whiteListCookies, PropertiesUtil.toStringArray(properties.get("optout.whitelist.cookies"), (String[])DEFAULT_WHITELIST_COOKIES));
        log.info("configured for opt-out cookies [{}], white-list: [{}]", this.optOutCookies, this.whiteListCookies);
    }

    @Override
    public Collection<String> getCookieNames() {
        return this.optOutCookies;
    }

    @Override
    public Map<String, String> getHeaders() {
        return this.optOutHeaders;
    }

    @Override
    public Collection<String> getWhitelistCookieNames() {
        return this.whiteListCookies;
    }

    @Override
    public boolean isOptedOut(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (null != cookies && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if (!this.optOutCookies.contains(cookie.getName())) continue;
                log.debug("isOptedOut: found opt-out cookie [{}]", (Object)cookie.getName());
                return true;
            }
        }
        for (String headerName : this.optOutHeaders.keySet()) {
            String header = request.getHeader(headerName);
            if (null == header || !header.equals(this.optOutHeaders.get(headerName))) continue;
            log.debug("isOptedOut: found opt-out header [{}] with value [{}]", (Object)headerName, (Object)header);
            return true;
        }
        log.debug("isOptedOut: none of the opt-out cookies or headers found in request.");
        return false;
    }

    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        JSONWriter writer = new JSONWriter((Writer)response.getWriter());
        try {
            writer.object().key("cookienames").array();
            for (String cookieName2 : this.getCookieNames()) {
                writer.value((Object)cookieName2);
            }
            writer.endArray();
            writer.key("headers").object();
            for (Map.Entry entry : this.optOutHeaders.entrySet()) {
                writer.key((String)entry.getKey()).value(entry.getValue());
            }
            writer.endObject();
            writer.key("whitelistcookienames").array();
            for (String cookieName : this.getWhitelistCookieNames()) {
                writer.value((Object)cookieName);
            }
            writer.endArray();
            writer.endObject();
        }
        catch (JSONException e) {
            log.error("error writing JSON: ", (Throwable)e);
        }
    }
}