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