CheckHttpHeaderFlag.java
3.59 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.servlet.http.HttpServletRequest
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.ConfigurationPolicy
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.sling.commons.osgi.PropertiesUtil
* org.apache.sling.featureflags.ExecutionContext
* org.apache.sling.featureflags.Feature
*/
package com.adobe.granite.frags.impl;
import com.adobe.granite.frags.impl.AbstractFeature;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.ConfigurationPolicy;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.featureflags.ExecutionContext;
import org.apache.sling.featureflags.Feature;
@Service(value={Feature.class})
@Properties(value={@Property(name="service.ranking", intValue={200}), @Property(name="feature.name", label="Name", value={"com.adobe.granite.frags.CheckHttpHeaderFlag"}), @Property(name="feature.description", label="Description", value={"Displays the feature only for clients sending a special HTTP Header"})})
@Component(label="Adobe Granite HTTP Header Feature Flag", description="Displays the feature only for clients sending a special HTTP Header", configurationFactory=1, policy=ConfigurationPolicy.REQUIRE, metatype=1)
public final class CheckHttpHeaderFlag
extends AbstractFeature {
@Property(label="HTTP Header Name", description="The HTTP Header name has to be sent from client", value={"X-Sling-Flag"})
public static String HTTP_HEADER_NAME = "http.header.name";
@Property(label="HTTP Header value pattern", description="The regexp pattern of HTTP Header value sent from client - see Java regular expression (http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)", value={".*"})
public static String HTTP_HEADER_VALUE_PATTERN = "http.header.valuepattern";
static final String FEATURE_NAME = "com.adobe.granite.frags.CheckHttpHeaderFlag";
static final String LABEL = "Adobe Granite HTTP Header Feature Flag";
static final String DESCRIPTION = "Displays the feature only for clients sending a special HTTP Header";
private static final String DEFAULT_HTTP_HEADER_NAME = "X-Sling-Flag";
private static final String DEFAULT_HTTP_HEADER_VALUE_PATTERN = ".*";
private String httpHeader;
private Pattern httpHeaderPattern;
@Override
protected void configure(Map<String, Object> config) {
this.httpHeader = PropertiesUtil.toString((Object)config.get(HTTP_HEADER_NAME), (String)"X-Sling-Flag");
String httpHeaderPatternString = PropertiesUtil.toString((Object)config.get(HTTP_HEADER_VALUE_PATTERN), (String)".*");
this.httpHeaderPattern = Pattern.compile(httpHeaderPatternString);
}
public boolean isEnabled(ExecutionContext context) {
HttpServletRequest request = context.getRequest();
if (request != null) {
String header = request.getHeader(this.httpHeader);
return header != null && this.httpHeaderPattern.matcher(header).matches();
}
return false;
}
public String toString() {
return Object.super.toString() + ", HTTP Header name=" + this.httpHeader + ", HTTP Header pattern=" + this.httpHeaderPattern;
}
}