Skip to content

Commit c27a656

Browse files
committed
Tuckey integration is working
1 parent 3326d5b commit c27a656

12 files changed

Lines changed: 414 additions & 4 deletions

File tree

‎config-tuckey/pom.xml‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<artifactId>rewrite-parent</artifactId>
9+
<groupId>com.ocpsoft.rewrite</groupId>
10+
<version>1.0.0-SNAPSHOT</version>
11+
<relativePath>../</relativePath>
12+
</parent>
13+
14+
<artifactId>rewrite-config-tuckey</artifactId>
15+
16+
<properties>
17+
<version.tuckey>3.1.0</version.tuckey>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>com.ocpsoft.rewrite</groupId>
23+
<artifactId>rewrite-impl-servlet</artifactId>
24+
</dependency>
25+
<dependency>
26+
<groupId>com.ocpsoft.rewrite</groupId>
27+
<artifactId>rewrite-test-harness</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.jboss.spec.javax.servlet</groupId>
31+
<artifactId>jboss-servlet-api_3.0_spec</artifactId>
32+
<scope>provided</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.tuckey</groupId>
36+
<artifactId>urlrewritefilter</artifactId>
37+
<version>${version.tuckey}</version>
38+
</dependency>
39+
</dependencies>
40+
41+
<build>
42+
<finalName>config-tuckey</finalName>
43+
<plugins>
44+
<plugin>
45+
<artifactId>maven-compiler-plugin</artifactId>
46+
<version>2.3.2</version>
47+
<configuration>
48+
<source>1.6</source>
49+
<target>1.6</target>
50+
</configuration>
51+
</plugin>
52+
</plugins>
53+
</build>
54+
</project>
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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 com.ocpsoft.rewrite.config.tuckey;
17+
18+
import java.io.InputStream;
19+
import java.net.MalformedURLException;
20+
import java.net.URL;
21+
22+
import javax.servlet.ServletContext;
23+
24+
import org.jboss.logging.Logger;
25+
import org.tuckey.web.filters.urlrewrite.Conf;
26+
import org.tuckey.web.filters.urlrewrite.UrlRewriter;
27+
28+
import com.ocpsoft.rewrite.config.Configuration;
29+
import com.ocpsoft.rewrite.config.ConfigurationBuilder;
30+
import com.ocpsoft.rewrite.servlet.config.HttpConfigurationProvider;
31+
32+
/**
33+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
34+
*
35+
*/
36+
public class TuckeyConfigurationProvider extends HttpConfigurationProvider
37+
{
38+
Logger log = Logger.getLogger(TuckeyConfigurationProvider.class);
39+
private UrlRewriter urlRewriter;
40+
41+
@Override
42+
public Configuration getConfiguration(final ServletContext context)
43+
{
44+
loadUrlRewriteConfig(context);
45+
ConfigurationBuilder config = ConfigurationBuilder.begin();
46+
if (urlRewriter != null)
47+
{
48+
config.addRule(new TuckeyRewriteRuleAdaptor(urlRewriter));
49+
}
50+
return config;
51+
}
52+
53+
private void loadUrlRewriteConfig(final ServletContext context)
54+
{
55+
String confPath = "/WEB-INF/urlrewrite.xml";
56+
InputStream inputStream = context.getResourceAsStream(confPath);
57+
// attempt to retrieve from location other than local WEB-INF
58+
if (inputStream == null) {
59+
inputStream = ClassLoader.getSystemResourceAsStream(confPath);
60+
}
61+
URL confUrl = null;
62+
try {
63+
confUrl = context.getResource(confPath);
64+
}
65+
catch (MalformedURLException e) {
66+
log.debug(e);
67+
}
68+
String confUrlStr = null;
69+
if (confUrl != null) {
70+
confUrlStr = confUrl.toString();
71+
}
72+
if (inputStream == null) {
73+
log.error("unable to find urlrewrite conf file at " + confPath);
74+
// set the writer back to null
75+
if (urlRewriter != null) {
76+
log.error("unloading existing conf");
77+
urlRewriter = null;
78+
}
79+
80+
}
81+
else {
82+
Conf conf = new Conf(context, inputStream, confPath, confUrlStr, false);
83+
checkConfLocal(conf);
84+
}
85+
}
86+
87+
private void checkConfLocal(final Conf conf)
88+
{
89+
if (log.isDebugEnabled()) {
90+
if (conf.getRules() != null) {
91+
log.debug("inited with " + conf.getRules().size() + " rules");
92+
}
93+
log.debug("conf is " + (conf.isOk() ? "ok" : "NOT ok"));
94+
}
95+
if (conf.isOk() && conf.isEngineEnabled()) {
96+
urlRewriter = new UrlRewriter(conf);
97+
log.info("loaded (conf ok)");
98+
99+
}
100+
else {
101+
if (!conf.isOk()) {
102+
log.error("Conf failed to load");
103+
}
104+
if (!conf.isEngineEnabled()) {
105+
log.error("Engine explicitly disabled in conf"); // not really an error but we want ot to show in logs
106+
}
107+
if (urlRewriter != null) {
108+
log.error("unloading existing conf");
109+
urlRewriter = null;
110+
}
111+
}
112+
}
113+
114+
@Override
115+
public int priority()
116+
{
117+
return 100;
118+
}
119+
120+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 com.ocpsoft.rewrite.config.tuckey;
17+
18+
import java.io.IOException;
19+
20+
import javax.servlet.FilterChain;
21+
import javax.servlet.ServletException;
22+
import javax.servlet.ServletRequest;
23+
import javax.servlet.ServletResponse;
24+
25+
import org.tuckey.web.filters.urlrewrite.UrlRewriter;
26+
27+
import com.ocpsoft.rewrite.config.Condition;
28+
import com.ocpsoft.rewrite.config.Direction;
29+
import com.ocpsoft.rewrite.config.Operation;
30+
import com.ocpsoft.rewrite.config.Rule;
31+
import com.ocpsoft.rewrite.exception.RewriteException;
32+
import com.ocpsoft.rewrite.servlet.config.HttpInboundOperation;
33+
import com.ocpsoft.rewrite.servlet.http.event.HttpInboundServletRewrite;
34+
35+
/**
36+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
37+
*
38+
*/
39+
public class TuckeyRewriteRuleAdaptor implements Rule
40+
{
41+
private final UrlRewriter rewriter;
42+
43+
public TuckeyRewriteRuleAdaptor(final UrlRewriter urlRewriter)
44+
{
45+
this.rewriter = urlRewriter;
46+
}
47+
48+
@Override
49+
public Condition getCondition()
50+
{
51+
return Direction.isInbound();
52+
}
53+
54+
@Override
55+
public Operation getOperation()
56+
{
57+
return new HttpInboundOperation() {
58+
59+
boolean forwarded = false;
60+
61+
@Override
62+
public void performInbound(final HttpInboundServletRewrite event)
63+
{
64+
try {
65+
FilterChain filterChainAdaptor = new FilterChain() {
66+
@Override
67+
public void doFilter(final ServletRequest request, final ServletResponse response)
68+
throws IOException, ServletException
69+
{
70+
forwarded = true;
71+
}
72+
};
73+
74+
if (rewriter.processRequest(event.getRequest(), event.getResponse(), filterChainAdaptor))
75+
{
76+
if (forwarded)
77+
event.handled();
78+
else
79+
event.abort();
80+
}
81+
}
82+
catch (Exception e) {
83+
throw new RewriteException("Error processing [" + this.getClass().getName() + "]", e);
84+
}
85+
}
86+
};
87+
}
88+
89+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<forge/>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.ocpsoft.rewrite.config.tuckey.TuckeyConfigurationProvider
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 com.ocpsoft.rewrite.config.tuckey;
17+
18+
import junit.framework.Assert;
19+
20+
import org.apache.http.client.methods.HttpGet;
21+
import org.jboss.arquillian.container.test.api.Deployment;
22+
import org.jboss.arquillian.junit.Arquillian;
23+
import org.jboss.shrinkwrap.api.spec.WebArchive;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
27+
import com.ocpsoft.rewrite.test.HttpAction;
28+
import com.ocpsoft.rewrite.test.RewriteTestBase;
29+
30+
/**
31+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
32+
*
33+
*/
34+
@RunWith(Arquillian.class)
35+
public class TuckeyConfigurationProviderTest extends RewriteTestBase
36+
{
37+
@Deployment(testable = true)
38+
public static WebArchive getDeployment()
39+
{
40+
WebArchive deployment = RewriteTestBase.getDeployment()
41+
.addAsLibraries(resolveDependencies("org.tuckey:urlrewritefilter:3.1.0"))
42+
.addAsWebInfResource("urlrewrite.xml")
43+
.addPackages(true, TuckeyRoot.class.getPackage());
44+
45+
return deployment;
46+
}
47+
48+
@Test
49+
public void testConfigurationIntegratesWithRedirectFlow()
50+
{
51+
HttpAction<HttpGet> action = get("/some/olddir/value");
52+
Assert.assertEquals("/very/newdir/value", action.getRelativeURL());
53+
}
54+
55+
@Test
56+
public void testConfigurationIntegratesWithForwardFlow()
57+
{
58+
HttpAction<HttpGet> action = get("/some/fordir/value");
59+
Assert.assertEquals("/very/newdir/value", action.getRelativeURL());
60+
}
61+
62+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 com.ocpsoft.rewrite.config.tuckey;
17+
18+
/**
19+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
20+
*
21+
*/
22+
public interface TuckeyRoot
23+
{
24+
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<!DOCTYPE urlrewrite
4+
PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN"
5+
"http://tuckey.org/res/dtds/urlrewrite3.0.dtd">
6+
7+
<urlrewrite>
8+
9+
<rule>
10+
<from>^/some/olddir/(.*)$</from>
11+
<to type="redirect">/very/newdir/$1</to>
12+
</rule>
13+
14+
<rule>
15+
<from>^/some/fordir/(.*)$</from>
16+
<to type="forward">/some/olddir/$1</to>
17+
</rule>
18+
19+
<rule match-type="wildcard">
20+
<from>/blog/archive/**</from>
21+
<to type="redirect">/roller/history/$1</to>
22+
</rule>
23+
24+
</urlrewrite>

0 commit comments

Comments
 (0)