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+
24+ import org .ocpsoft .rewrite .bind .Binding ;
25+ import org .ocpsoft .rewrite .bind .Bindings ;
26+ import org .ocpsoft .rewrite .bind .Evaluation ;
27+ import org .ocpsoft .rewrite .bind .ParameterizedPattern ;
28+ import org .ocpsoft .rewrite .bind .RegexConditionParameterBuilder ;
29+ import org .ocpsoft .rewrite .bind .RegexParameter ;
30+ import org .ocpsoft .rewrite .context .EvaluationContext ;
31+ import org .ocpsoft .rewrite .param .ConditionParameterBuilder ;
32+ import org .ocpsoft .rewrite .param .Parameter ;
33+ import org .ocpsoft .rewrite .param .ParameterizedCondition ;
34+ import org .ocpsoft .rewrite .servlet .http .event .HttpOutboundServletRewrite ;
35+ import org .ocpsoft .rewrite .servlet .http .event .HttpServletRewrite ;
36+ import org .ocpsoft .rewrite .servlet .util .URLBuilder ;
37+
38+ /**
39+ * A {@link org.ocpsoft.rewrite.config.Condition} that inspects the value of {@link HttpServletRequest#getScheme()}
40+ *
41+ * @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
42+ */
43+ public class Scheme extends HttpCondition implements
44+ ParameterizedCondition <ConditionParameterBuilder <RegexConditionParameterBuilder , String >, String >
45+ {
46+ private final ParameterizedPattern expression ;
47+
48+ private Scheme (final String pattern )
49+ {
50+ Assert .notNull (pattern , "Scheme must not be null." );
51+ this .expression = new ParameterizedPattern (pattern );
52+
53+ for (Parameter <String > parameter : this .expression .getParameters ().values ()) {
54+ parameter .bindsTo (Evaluation .property (parameter .getName ()));
55+ }
56+ }
57+
58+ /**
59+ * Inspect the current request scheme, comparing against the given pattern.
60+ * <p>
61+ * The given pattern may be parameterized using the following format:
62+ * <p>
63+ * <code>
64+ * https
65+ * {scheme}
66+ * {scheme}-custom <br>
67+ * ... and so on
68+ * </code>
69+ * <p>
70+ * By default, matching parameter values are bound to the {@link org.ocpsoft.rewrite.context.EvaluationContext}. See also {@link #where(String)}
71+ */
72+ public static Scheme matches (final String pattern )
73+ {
74+ return new Scheme (pattern );
75+ }
76+
77+ @ Override
78+ public RegexConditionParameterBuilder where (final String param )
79+ {
80+ return new RegexConditionParameterBuilder (this , expression .getParameter (param ));
81+ }
82+
83+ @ Override
84+ public RegexConditionParameterBuilder where (final String param , final String pattern )
85+ {
86+ return where (param ).matches (pattern );
87+ }
88+
89+ @ Override
90+ public RegexConditionParameterBuilder where (final String param , final String pattern ,
91+ final Binding binding )
92+ {
93+ return where (param , pattern ).bindsTo (binding );
94+ }
95+
96+ @ Override
97+ public RegexConditionParameterBuilder where (final String param , final Binding binding )
98+ {
99+ return where (param ).bindsTo (binding );
100+ }
101+
102+ @ Override
103+ public boolean evaluateHttp (final HttpServletRewrite event , final EvaluationContext context )
104+ {
105+ String scheme = null ;
106+
107+ if (event instanceof HttpOutboundServletRewrite )
108+ {
109+ String url = event .getURL ();
110+ URLBuilder builder = URLBuilder .createFrom (url );
111+ scheme = builder .toURI ().getScheme ();
112+ if (scheme == null )
113+ scheme = event .getRequest ().getScheme ();
114+ }
115+ else
116+ scheme = event .getRequest ().getScheme ();
117+
118+ if (scheme != null && expression .matches (event , context , scheme ))
119+ {
120+ Map <RegexParameter , String []> parameters = expression .parse (event , context , scheme );
121+ if (Bindings .enqueuePreOperationSubmissions (event , context , parameters ))
122+ return true ;
123+ }
124+ return false ;
125+ }
126+
127+ /**
128+ * Get the underlying {@link ParameterizedPattern} for this {@link Scheme}
129+ * <p>
130+ * See also: {@link #where(String)}
131+ */
132+ public ParameterizedPattern getExpression ()
133+ {
134+ return expression ;
135+ }
136+
137+ @ Override
138+ public String toString ()
139+ {
140+ return expression .toString ();
141+ }
142+ }
0 commit comments