Skip to content

Commit cfe2992

Browse files
committed
Added Scheme config object
1 parent 1ddd6f0 commit cfe2992

5 files changed

Lines changed: 240 additions & 3 deletions

File tree

‎impl-servlet/src/main/java/org/ocpsoft/rewrite/servlet/config/Domain.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public boolean evaluateHttp(final HttpServletRewrite event, final EvaluationCont
115115
else
116116
hostName = event.getRequest().getServerName();
117117

118-
if (expression.matches(event, context, hostName))
118+
if (hostName != null && expression.matches(event, context, hostName))
119119
{
120120
Map<RegexParameter, String[]> parameters = expression.parse(event, context, hostName);
121121
if (Bindings.enqueuePreOperationSubmissions(event, context, parameters))

‎impl-servlet/src/main/java/org/ocpsoft/rewrite/servlet/config/Path.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ public static Path matches(final String pattern)
7070
{
7171
return new Path(pattern);
7272
}
73+
74+
public static Path captureIn(final String param)
75+
{
76+
Path path = new Path("{"+param+"}");
77+
path.where(param).matches(".*");
78+
return path;
79+
}
7380

7481
/**
7582
* Bind each path parameter to the corresponding request parameter by name. By default, matching values are bound to

‎impl-servlet/src/main/java/org/ocpsoft/rewrite/servlet/config/QueryString.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public List<Binding> getBindings()
6767
/**
6868
* Return a new {@link org.ocpsoft.rewrite.config.Condition} matching against the entire {@link HttpServletRequest#getQueryString()}
6969
* <p>
70-
* This value may be bound.
70+
* This value may be bound using <code>{param}</code> statements.
7171
* <p>
7272
* See also: {@link #bindsTo(Binding)}
7373
*/
@@ -102,7 +102,7 @@ else if (event instanceof HttpOutboundServletRewrite)
102102
* {@link HttpServletRequest#getQueryString()}
103103
* <p>
104104
* The values of all matching parameters may be bound. By default, matching values are bound to the
105-
* {@link org.ocpsoft.rewrite.context.EvaluationContext}.
105+
* {@link org.ocpsoft.rewrite.context.EvaluationContext}, and are stored by parameter name.
106106
* <p>
107107
* See also: {@link #bindsTo(Binding)}
108108
*/
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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 javax.servlet.http.HttpServletRequest;
19+
20+
import org.junit.Assert;
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
import org.mockito.Mockito;
24+
import org.ocpsoft.rewrite.bind.Evaluation;
25+
import org.ocpsoft.rewrite.event.Rewrite;
26+
import org.ocpsoft.rewrite.mock.MockEvaluationContext;
27+
import org.ocpsoft.rewrite.servlet.config.bind.Request;
28+
import org.ocpsoft.rewrite.servlet.impl.HttpInboundRewriteImpl;
29+
import org.ocpsoft.rewrite.servlet.impl.HttpOutboundRewriteImpl;
30+
31+
/**
32+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
33+
*
34+
*/
35+
public class SchemeTest
36+
{
37+
private Rewrite inbound;
38+
private Rewrite outbound;
39+
private HttpServletRequest request;
40+
41+
@Before
42+
public void before()
43+
{
44+
request = Mockito.mock(HttpServletRequest.class);
45+
Mockito.when(request.getScheme())
46+
.thenReturn("https");
47+
48+
inbound = new HttpInboundRewriteImpl(request, null);
49+
outbound = new HttpOutboundRewriteImpl(request, null, "http://example.com:8080/path?query=value");
50+
}
51+
52+
@Test
53+
public void testSchemeMatchesInbound()
54+
{
55+
Assert.assertTrue(Scheme.matches("https").evaluate(inbound, new MockEvaluationContext()));
56+
}
57+
58+
@Test
59+
public void testShemeMatchesBindsInbound()
60+
{
61+
MockEvaluationContext context = new MockEvaluationContext();
62+
Assert.assertTrue(Scheme.matches("{scheme}").where("scheme").bindsTo(Request.attribute("scheme"))
63+
.evaluate(inbound, context));
64+
65+
// Invoke the binding.
66+
context.getPreOperations().get(0).perform(inbound, context);
67+
68+
Assert.assertEquals("https", Evaluation.property("scheme").retrieve(inbound, context));
69+
}
70+
71+
@Test
72+
public void testSchemeNotMatchesInbound()
73+
{
74+
Assert.assertFalse(Scheme.matches("ftp").evaluate(inbound, new MockEvaluationContext()));
75+
}
76+
77+
@Test
78+
public void testSchemeMatchesOutbound()
79+
{
80+
Assert.assertTrue(Scheme.matches("http").evaluate(outbound, new MockEvaluationContext()));
81+
}
82+
83+
@Test
84+
public void testSchemeNotMatchesOutbound()
85+
{
86+
Assert.assertFalse(Scheme.matches("scp").evaluate(outbound, new MockEvaluationContext()));
87+
}
88+
}

0 commit comments

Comments
 (0)