Skip to content

Commit 968ddcb

Browse files
committed
Initial UserAgent support
1 parent c531ad6 commit 968ddcb

5 files changed

Lines changed: 1213 additions & 25 deletions

File tree

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.servlet.config;
17+
18+
import org.ocpsoft.rewrite.bind.Bindable;
19+
import org.ocpsoft.rewrite.bind.Binding;
20+
import org.ocpsoft.rewrite.bind.RegexCapture;
21+
import org.ocpsoft.rewrite.config.Condition;
22+
import org.ocpsoft.rewrite.config.ConditionBuilder;
23+
import org.ocpsoft.rewrite.context.EvaluationContext;
24+
import org.ocpsoft.rewrite.event.Rewrite;
25+
import org.ocpsoft.rewrite.param.Constraint;
26+
import org.ocpsoft.rewrite.param.Parameter;
27+
import org.ocpsoft.rewrite.param.ParameterBuilder;
28+
import org.ocpsoft.rewrite.param.Parameterized;
29+
import org.ocpsoft.rewrite.param.Transform;
30+
import org.ocpsoft.rewrite.servlet.config.IUserAgent.UserAgentParameter;
31+
32+
/**
33+
*
34+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
35+
*
36+
*/
37+
public interface IUserAgent extends Parameterized<IUserAgent, UserAgentParameter, String>, ConditionBuilder
38+
{
39+
public interface IUserAgentParameter extends IUserAgent, Bindable<UserAgentParameter>, Parameter<UserAgentParameter, String>
40+
{
41+
IUserAgentParameter matches(String string);
42+
}
43+
44+
public class UserAgentParameter extends ParameterBuilder<UserAgentParameter, String> implements IUserAgentParameter
45+
{
46+
private final IUserAgent parent;
47+
private final RegexCapture parameter;
48+
49+
public UserAgentParameter(IUserAgent path, RegexCapture parameter)
50+
{
51+
this.parent = path;
52+
this.parameter = parameter;
53+
}
54+
55+
@Override
56+
public UserAgentParameter constrainedBy(Constraint<String> constraint)
57+
{
58+
parameter.constrainedBy(constraint);
59+
return this;
60+
}
61+
62+
@Override
63+
public UserAgentParameter transformedBy(Transform<String> transform)
64+
{
65+
parameter.transformedBy(transform);
66+
return this;
67+
}
68+
69+
@Override
70+
public UserAgentParameter bindsTo(Binding binding)
71+
{
72+
parameter.bindsTo(binding);
73+
return this;
74+
}
75+
76+
@Override
77+
public UserAgentParameter where(String param)
78+
{
79+
return parent.where(param);
80+
}
81+
82+
@Override
83+
public UserAgentParameter where(String param, Binding binding)
84+
{
85+
return parent.where(param, binding);
86+
}
87+
88+
@Override
89+
public IUserAgentParameter matches(String string)
90+
{
91+
parameter.matches(string);
92+
return this;
93+
}
94+
95+
@Override
96+
public String getName()
97+
{
98+
return parameter.getName();
99+
}
100+
101+
@Override
102+
public boolean evaluate(Rewrite event, EvaluationContext context)
103+
{
104+
return parent.evaluate(event, context);
105+
}
106+
107+
@Override
108+
public ConditionBuilder and(Condition condition)
109+
{
110+
return parent.and(condition);
111+
}
112+
113+
@Override
114+
public ConditionBuilder andNot(Condition condition)
115+
{
116+
return parent.andNot(condition);
117+
}
118+
119+
@Override
120+
public ConditionBuilder or(Condition condition)
121+
{
122+
return parent.or(condition);
123+
}
124+
125+
@Override
126+
public ConditionBuilder orNot(Condition condition)
127+
{
128+
return parent.orNot(condition);
129+
}
130+
}
131+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ocpsoft.rewrite.servlet.config;
17+
18+
import java.util.Map;
19+
20+
import javax.servlet.http.HttpServletRequest;
21+
22+
import org.ocpsoft.common.util.Assert;
23+
import org.ocpsoft.rewrite.bind.Binding;
24+
import org.ocpsoft.rewrite.bind.Bindings;
25+
import org.ocpsoft.rewrite.bind.Evaluation;
26+
import org.ocpsoft.rewrite.bind.ParameterizedPattern;
27+
import org.ocpsoft.rewrite.bind.RegexCapture;
28+
import org.ocpsoft.rewrite.context.EvaluationContext;
29+
import org.ocpsoft.rewrite.servlet.http.event.HttpServletRewrite;
30+
31+
/**
32+
* A {@link org.ocpsoft.rewrite.config.Condition} that inspects the value of {@link HttpServletRequest#getScheme()}
33+
*
34+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
35+
*/
36+
public abstract class UserAgent extends HttpCondition
37+
{
38+
39+
/**
40+
* Condition asserting that the user-agent matches the given pattern. (May be parameterized using {param}
41+
* expressions.)
42+
*/
43+
public static UserAgent matches(final String pattern)
44+
{
45+
return new PatternUserAgent(pattern);
46+
}
47+
48+
/**
49+
* Condition asserting that the user-agent is a mobile device.
50+
*/
51+
public static ClassificationUserAgent isMobile()
52+
{
53+
return new ClassificationUserAgent(Classification.MOBILE);
54+
}
55+
56+
/**
57+
* Condition asserting that the user-agent is a tablet device.
58+
*/
59+
public static ClassificationUserAgent isTablet()
60+
{
61+
return new ClassificationUserAgent(Classification.TABLET);
62+
}
63+
64+
/**
65+
* Condition asserting that the user-agent is a non-mobile device.
66+
*/
67+
public static ClassificationUserAgent isDesktop()
68+
{
69+
return new ClassificationUserAgent(Classification.DESKTOP);
70+
}
71+
72+
private static enum Classification
73+
{
74+
MOBILE, TABLET, DESKTOP
75+
}
76+
77+
private static class ClassificationUserAgent extends UserAgent
78+
{
79+
private final Classification type;
80+
81+
private ClassificationUserAgent(final Classification type)
82+
{
83+
Assert.notNull(type, "Classification must not be null.");
84+
this.type = type;
85+
}
86+
87+
@Override
88+
public boolean evaluateHttp(HttpServletRewrite event, EvaluationContext context)
89+
{
90+
String userAgentStr = event.getRequest().getHeader("user-agent");
91+
String httpAccept = event.getRequest().getHeader("Accept");
92+
UserAgentUtil util = new UserAgentUtil(userAgentStr, httpAccept);
93+
94+
switch (type)
95+
{
96+
case DESKTOP:
97+
return !util.isMobile();
98+
case MOBILE:
99+
return util.isMobile();
100+
case TABLET:
101+
return util.isTablet();
102+
}
103+
104+
return false;
105+
}
106+
}
107+
108+
private static class PatternUserAgent extends UserAgent implements IUserAgent
109+
{
110+
private final ParameterizedPattern expression;
111+
112+
private PatternUserAgent(final String pattern)
113+
{
114+
Assert.notNull(pattern, "Scheme must not be null.");
115+
this.expression = new ParameterizedPattern(pattern);
116+
117+
for (RegexCapture parameter : this.expression.getParameters().values()) {
118+
parameter.bindsTo(Evaluation.property(parameter.getName()));
119+
}
120+
}
121+
122+
@Override
123+
public boolean evaluateHttp(final HttpServletRewrite event, final EvaluationContext context)
124+
{
125+
String agent = event.getRequest().getHeader("user-agent");;
126+
127+
if (agent != null && expression.matches(event, context, agent))
128+
{
129+
Map<RegexCapture, String[]> parameters = expression.parse(event, context, agent);
130+
if (Bindings.enqueuePreOperationSubmissions(event, context, parameters))
131+
return true;
132+
}
133+
return false;
134+
}
135+
136+
@Override
137+
public UserAgentParameter where(final String param)
138+
{
139+
return new UserAgentParameter(this, expression.getParameter(param));
140+
}
141+
142+
@Override
143+
public UserAgentParameter where(final String param, final Binding binding)
144+
{
145+
return where(param).bindsTo(binding);
146+
}
147+
148+
@Override
149+
public String toString()
150+
{
151+
return expression.toString();
152+
}
153+
}
154+
}

0 commit comments

Comments
 (0)