Skip to content

Commit 4215bee

Browse files
committed
Added some new configuration types. Need tests.
1 parent 9fbdab7 commit 4215bee

3 files changed

Lines changed: 202 additions & 2 deletions

File tree

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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.config;
17+
18+
import java.io.File;
19+
import java.io.FileFilter;
20+
import java.util.Set;
21+
22+
import org.ocpsoft.rewrite.config.Condition;
23+
import org.ocpsoft.rewrite.config.ConfigurationRuleParameterBuilder;
24+
import org.ocpsoft.rewrite.context.EvaluationContext;
25+
import org.ocpsoft.rewrite.event.Rewrite;
26+
import org.ocpsoft.rewrite.param.ParameterStore;
27+
import org.ocpsoft.rewrite.param.Parameterized;
28+
import org.ocpsoft.rewrite.param.ParameterizedPattern;
29+
import org.ocpsoft.rewrite.param.ParameterizedPatternParser;
30+
import org.ocpsoft.rewrite.param.RegexParameterizedPatternParser;
31+
import org.ocpsoft.rewrite.util.Transforms;
32+
33+
/**
34+
* A {@link Condition} responsible for determining existence of {@link File} paths on the host file-system.
35+
*
36+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
37+
*/
38+
public class Filesystem implements Condition, Parameterized
39+
{
40+
private final ParameterizedPatternParser resource;
41+
private final FileFilter filter;
42+
43+
private Filesystem(final File resource, FileFilter filter)
44+
{
45+
this.resource = new RegexParameterizedPatternParser(resource.getAbsolutePath());
46+
this.filter = filter;
47+
}
48+
49+
@Override
50+
public boolean evaluate(final Rewrite event, final EvaluationContext context)
51+
{
52+
if (resource != null)
53+
{
54+
String filePath = resource.getBuilder().build(event, context, Transforms.encodePath());
55+
File file = new File(filePath);
56+
return filter.accept(file);
57+
}
58+
return false;
59+
}
60+
61+
/**
62+
* Create a {@link Condition} that returns <code>true</code> if the given {@link File} exists in the file-system of
63+
* the host environment, and is a normal file.
64+
*
65+
* <p>
66+
* The given file path may be parameterized:
67+
* <p>
68+
* <code>
69+
* /tmp/file.txt <br>
70+
* c:\tmp\{param}.txt <br>
71+
* ...
72+
* </code>
73+
* <p>
74+
*
75+
* @param location {@link ParameterizedPattern} specifying the location of the {@link File}.
76+
*
77+
* @see {@link ConfigurationRuleParameterBuilder#where(String)}
78+
*/
79+
public static Filesystem fileExists(final File resource)
80+
{
81+
return new Filesystem(resource, new FileFilter() {
82+
@Override
83+
public boolean accept(File file)
84+
{
85+
return file.exists() && file.isFile();
86+
}
87+
});
88+
}
89+
90+
/**
91+
* Create a {@link Condition} that returns <code>true</code> if the given {@link File} exists in the file-system of
92+
* the host environment, and is a directory.
93+
*
94+
* <p>
95+
* The given file path may be parameterized:
96+
* <p>
97+
* <code>
98+
* /tmp/file.txt <br>
99+
* c:\tmp\{param}.txt <br>
100+
* ...
101+
* </code>
102+
* <p>
103+
*
104+
* @param location {@link ParameterizedPattern} specifying the location of the directory.
105+
*
106+
* @see {@link ConfigurationRuleParameterBuilder#where(String)}
107+
*/
108+
public static Filesystem directoryExists(final File resource)
109+
{
110+
return new Filesystem(resource, new FileFilter() {
111+
@Override
112+
public boolean accept(File file)
113+
{
114+
return file.exists() && file.isDirectory();
115+
}
116+
});
117+
}
118+
119+
/**
120+
* Get the {@link ParameterizedPattern} of this {@link Filesystem}.
121+
*/
122+
public ParameterizedPatternParser getExpression()
123+
{
124+
return resource;
125+
}
126+
127+
@Override
128+
public Set<String> getRequiredParameterNames()
129+
{
130+
return resource.getRequiredParameterNames();
131+
}
132+
133+
@Override
134+
public void setParameterStore(ParameterStore store)
135+
{
136+
resource.setParameterStore(store);
137+
}
138+
139+
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818
import java.net.MalformedURLException;
1919
import java.util.Set;
2020

21-
import javax.servlet.RequestDispatcher;
2221
import javax.servlet.ServletContext;
23-
import javax.servlet.http.HttpServletRequest;
2422

2523
import org.ocpsoft.logging.Logger;
24+
import org.ocpsoft.rewrite.config.Condition;
2625
import org.ocpsoft.rewrite.config.ConfigurationRuleParameterBuilder;
2726
import org.ocpsoft.rewrite.context.EvaluationContext;
2827
import org.ocpsoft.rewrite.exception.ParameterizationException;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.HttpServletResponse;
19+
20+
import org.ocpsoft.rewrite.config.Operation;
21+
import org.ocpsoft.rewrite.context.EvaluationContext;
22+
import org.ocpsoft.rewrite.servlet.http.event.HttpInboundServletRewrite;
23+
import org.ocpsoft.rewrite.servlet.http.event.HttpServletRewrite;
24+
25+
/**
26+
* An {@link Operation} responsible for sending status codes via {@link HttpServletResponse#setStatus(int)}. This does
27+
* not call {@link HttpServletResponse#flushBuffer()}
28+
*
29+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
30+
*/
31+
public class SetStatus extends HttpOperation
32+
{
33+
// TODO Test me!
34+
35+
private final int code;
36+
37+
private SetStatus(final int code)
38+
{
39+
this.code = code;
40+
}
41+
42+
@Override
43+
public void performHttp(final HttpServletRewrite event, final EvaluationContext context)
44+
{
45+
if (event instanceof HttpInboundServletRewrite)
46+
event.getResponse().setStatus(code);
47+
}
48+
49+
protected int getCode()
50+
{
51+
return code;
52+
}
53+
54+
/**
55+
* Create an {@link Operation} that will set an HTTP status code via {@link HttpServletResponse#setStatus(int)}.
56+
*/
57+
public static SetStatus code(final int code)
58+
{
59+
return new SetStatus(code);
60+
}
61+
62+
}

0 commit comments

Comments
 (0)