Skip to content

Commit a3e085e

Browse files
committed
Added CDN support
1 parent ee3acf0 commit a3e085e

5 files changed

Lines changed: 505 additions & 6 deletions

File tree

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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.rule;
17+
18+
import org.ocpsoft.rewrite.bind.Binding;
19+
import org.ocpsoft.rewrite.bind.ParameterizedPattern;
20+
import org.ocpsoft.rewrite.config.Condition;
21+
import org.ocpsoft.rewrite.config.ConditionBuilder;
22+
import org.ocpsoft.rewrite.config.DefaultConditionBuilder;
23+
import org.ocpsoft.rewrite.config.Direction;
24+
import org.ocpsoft.rewrite.config.Operation;
25+
import org.ocpsoft.rewrite.context.EvaluationContext;
26+
import org.ocpsoft.rewrite.event.Rewrite;
27+
import org.ocpsoft.rewrite.servlet.config.IPath;
28+
import org.ocpsoft.rewrite.servlet.config.Path;
29+
import org.ocpsoft.rewrite.servlet.config.Substitute;
30+
31+
/**
32+
* {@link org.ocpsoft.rewrite.config.Rule} that creates a bi-directional rewrite rule between an externally facing URL
33+
* and an internal server resource URL
34+
*
35+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
36+
*/
37+
public class CDN implements ICDN
38+
{
39+
private String id;
40+
41+
private Substitute location;
42+
private final IPath resource;
43+
44+
private Operation operation;
45+
private Condition condition;
46+
47+
protected CDN(final String pattern)
48+
{
49+
this.resource = Path.matches(pattern);
50+
}
51+
52+
/**
53+
* The outward facing URL path to which this {@link CDN} will apply.
54+
*/
55+
public static ICDN relocate(final String pattern)
56+
{
57+
return new CDN(pattern);
58+
}
59+
60+
@Override
61+
public ICDN to(final String location)
62+
{
63+
this.location = Substitute.with(location);
64+
return this;
65+
}
66+
67+
@Override
68+
public boolean evaluate(final Rewrite event, final EvaluationContext context)
69+
{
70+
if (Direction.isOutbound().evaluate(event, context))
71+
return resource.evaluate(event, context);
72+
return false;
73+
}
74+
75+
@Override
76+
public void perform(final Rewrite event, final EvaluationContext context)
77+
{
78+
if (Direction.isOutbound().evaluate(event, context))
79+
location.and(operation).perform(event, context);
80+
}
81+
82+
@Override
83+
public CDNParameter where(final String parameter)
84+
{
85+
return new CDNParameter(this, resource.getPathExpression().getParameter(parameter));
86+
}
87+
88+
@Override
89+
public CDNParameter where(final String param, final Binding binding)
90+
{
91+
return where(param).bindsTo(binding);
92+
}
93+
94+
@Override
95+
public String getId()
96+
{
97+
return id;
98+
}
99+
100+
@Override
101+
public ICDN when(final Condition condition)
102+
{
103+
this.condition = condition;
104+
return this;
105+
}
106+
107+
@Override
108+
public ICDN perform(final Operation operation)
109+
{
110+
this.operation = operation;
111+
return this;
112+
}
113+
114+
@Override
115+
public ICDN withId(final String id)
116+
{
117+
this.id = id;
118+
return this;
119+
}
120+
121+
@Override
122+
public String toString()
123+
{
124+
return "Join [resource=" + resource + ", to=" + location + ", id=" + id + "]";
125+
}
126+
127+
@Override
128+
public ConditionBuilder and(Condition condition)
129+
{
130+
return DefaultConditionBuilder.wrap(this.condition).and(condition);
131+
}
132+
133+
@Override
134+
public ConditionBuilder andNot(Condition condition)
135+
{
136+
return DefaultConditionBuilder.wrap(this.condition).andNot(condition);
137+
}
138+
139+
@Override
140+
public ConditionBuilder or(Condition condition)
141+
{
142+
return DefaultConditionBuilder.wrap(this.condition).or(condition);
143+
}
144+
145+
@Override
146+
public ConditionBuilder orNot(Condition condition)
147+
{
148+
return DefaultConditionBuilder.wrap(this.condition).orNot(condition);
149+
}
150+
151+
@Override
152+
public ParameterizedPattern getLocationExpression()
153+
{
154+
return location.getTargetExpression();
155+
}
156+
157+
@Override
158+
public ParameterizedPattern getResourcExpression()
159+
{
160+
return resource.getPathExpression();
161+
}
162+
163+
}
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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.rule;
17+
18+
import java.util.Arrays;
19+
import java.util.List;
20+
21+
import org.ocpsoft.rewrite.bind.Bindable;
22+
import org.ocpsoft.rewrite.bind.Binding;
23+
import org.ocpsoft.rewrite.bind.ParameterizedPattern;
24+
import org.ocpsoft.rewrite.bind.RegexCapture;
25+
import org.ocpsoft.rewrite.config.Condition;
26+
import org.ocpsoft.rewrite.config.ConditionBuilder;
27+
import org.ocpsoft.rewrite.config.Operation;
28+
import org.ocpsoft.rewrite.config.Rule;
29+
import org.ocpsoft.rewrite.context.EvaluationContext;
30+
import org.ocpsoft.rewrite.event.Rewrite;
31+
import org.ocpsoft.rewrite.param.Parameter;
32+
import org.ocpsoft.rewrite.param.ParameterBuilder;
33+
import org.ocpsoft.rewrite.param.Parameterized;
34+
import org.ocpsoft.rewrite.servlet.config.rule.ICDN.CDNParameter;
35+
36+
/**
37+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
38+
*/
39+
public interface ICDN extends Parameterized<ICDN, CDNParameter, String>, Rule, ConditionBuilder
40+
{
41+
/**
42+
* The new resource (real or virtual) to be served, either from a local context or remote CDN.
43+
*/
44+
public ICDN to(String location);
45+
46+
public ParameterizedPattern getLocationExpression();
47+
48+
public ParameterizedPattern getResourcExpression();
49+
50+
/**
51+
* Define additional {@link Operation} instances to be performed when this rule matches successfully.
52+
*/
53+
public ICDN perform(Operation operation);
54+
55+
/**
56+
* Define additional {@link Condition} instances that must be satisfied in order for this rule to match successfully.
57+
*/
58+
public ICDN when(Condition condition);
59+
60+
/**
61+
* Set the ID of this {@link Join}.
62+
*/
63+
public ICDN withId(String id);
64+
65+
public interface ICDNParameter extends ICDN, Bindable<CDNParameter>, Parameter<CDNParameter, String>
66+
{
67+
/**
68+
* Specify the pattern to which this Parameter must match.
69+
*/
70+
ICDNParameter matches(String string);
71+
}
72+
73+
public class CDNParameter extends ParameterBuilder<CDNParameter, String> implements ICDNParameter
74+
{
75+
private final ICDN parent;
76+
private final List<RegexCapture> captures;
77+
78+
public CDNParameter(ICDN join, RegexCapture... captures)
79+
{
80+
super((Object[]) captures);
81+
this.parent = join;
82+
this.captures = Arrays.asList(captures);
83+
}
84+
85+
@Override
86+
public ICDNParameter matches(String string)
87+
{
88+
for (RegexCapture capture : captures) {
89+
if (capture != null)
90+
capture.matches(string);
91+
}
92+
return this;
93+
}
94+
95+
@Override
96+
public String getName()
97+
{
98+
for (RegexCapture capture : captures) {
99+
if (capture != null)
100+
return capture.getName();
101+
}
102+
return null;
103+
}
104+
105+
@Override
106+
public CDNParameter where(String param)
107+
{
108+
return parent.where(param);
109+
}
110+
111+
@Override
112+
public CDNParameter where(String param, Binding binding)
113+
{
114+
return parent.where(param, binding);
115+
}
116+
117+
@Override
118+
public boolean evaluate(Rewrite event, EvaluationContext context)
119+
{
120+
return parent.evaluate(event, context);
121+
}
122+
123+
@Override
124+
public ParameterizedPattern getLocationExpression()
125+
{
126+
return parent.getLocationExpression();
127+
}
128+
129+
@Override
130+
public ParameterizedPattern getResourcExpression()
131+
{
132+
return parent.getResourcExpression();
133+
}
134+
135+
@Override
136+
public ConditionBuilder and(Condition condition)
137+
{
138+
return parent.and(condition);
139+
}
140+
141+
@Override
142+
public ConditionBuilder andNot(Condition condition)
143+
{
144+
return parent.andNot(condition);
145+
}
146+
147+
@Override
148+
public ConditionBuilder or(Condition condition)
149+
{
150+
return parent.or(condition);
151+
}
152+
153+
@Override
154+
public ConditionBuilder orNot(Condition condition)
155+
{
156+
return parent.orNot(condition);
157+
}
158+
159+
@Override
160+
public String getId()
161+
{
162+
return parent.getId();
163+
}
164+
165+
@Override
166+
public void perform(Rewrite event, EvaluationContext context)
167+
{
168+
parent.perform(event, context);
169+
}
170+
171+
@Override
172+
public ICDN to(String location)
173+
{
174+
return parent.to(location);
175+
}
176+
177+
@Override
178+
public ICDN perform(Operation operation)
179+
{
180+
return parent.perform(operation);
181+
}
182+
183+
@Override
184+
public ICDN when(Condition condition)
185+
{
186+
return parent.when(condition);
187+
}
188+
189+
@Override
190+
public ICDN withId(String id)
191+
{
192+
return parent.withId(id);
193+
}
194+
}
195+
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
22
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
7+
*
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -36,9 +36,9 @@
3636
import org.ocpsoft.rewrite.servlet.config.rule.IJoin.JoinParameter;
3737

3838
/**
39-
*
39+
*
4040
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
41-
*
41+
*
4242
*/
4343
public interface IJoin extends Parameterized<IJoin, JoinParameter, String>, Rule, ConditionBuilder
4444
{
@@ -57,7 +57,7 @@ public interface IJoin extends Parameterized<IJoin, JoinParameter, String>, Rule
5757
public IJoin perform(Operation operation);
5858

5959
/**
60-
* Define additional {@link Condition} instances that must be satisfied in order for this rule to matche
60+
* Define additional {@link Condition} instances that must be satisfied in order for this rule to match
6161
* successfully.
6262
*/
6363
public IJoin when(Condition condition);

0 commit comments

Comments
 (0)