OptOutServiceImpl.java
8 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* 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);
}
}
}