Skip to content

Commit 77517bb

Browse files
committed
documented GWT extension, moved servlet generic conditions to servlet-impl
1 parent 55901d9 commit 77517bb

8 files changed

Lines changed: 259 additions & 53 deletions

File tree

‎impl-servlet/pom.xml‎

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,38 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3-
<modelVersion>4.0.0</modelVersion>
4-
<parent>
5-
<groupId>com.ocpsoft.rewrite</groupId>
6-
<artifactId>rewrite-parent</artifactId>
7-
<version>1.0.0-SNAPSHOT</version>
8-
<relativePath>../</relativePath>
9-
</parent>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.ocpsoft.rewrite</groupId>
7+
<artifactId>rewrite-parent</artifactId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
<relativePath>../</relativePath>
10+
</parent>
1011

11-
<artifactId>rewrite-impl-servlet</artifactId>
12+
<artifactId>rewrite-impl-servlet</artifactId>
1213

13-
<dependencies>
14-
<dependency>
15-
<groupId>com.ocpsoft.rewrite</groupId>
16-
<artifactId>rewrite-api</artifactId>
17-
</dependency>
18-
<dependency>
19-
<groupId>com.ocpsoft.rewrite</groupId>
20-
<artifactId>rewrite-impl-config</artifactId>
21-
</dependency>
22-
<dependency>
23-
<groupId>com.ocpsoft.rewrite</groupId>
24-
<artifactId>rewrite-api-servlet</artifactId>
25-
</dependency>
26-
<dependency>
27-
<groupId>org.jboss.spec.javax.servlet</groupId>
28-
<artifactId>jboss-servlet-api_3.0_spec</artifactId>
29-
<scope>provided</scope>
30-
</dependency>
31-
<dependency>
32-
<groupId>com.ocpsoft.rewrite</groupId>
33-
<artifactId>rewrite-test-harness</artifactId>
34-
</dependency>
35-
</dependencies>
14+
<dependencies>
15+
<dependency>
16+
<groupId>com.ocpsoft.rewrite</groupId>
17+
<artifactId>rewrite-api</artifactId>
18+
</dependency>
19+
<dependency>
20+
<groupId>com.ocpsoft.rewrite</groupId>
21+
<artifactId>rewrite-impl-config</artifactId>
22+
</dependency>
23+
<dependency>
24+
<groupId>com.ocpsoft.rewrite</groupId>
25+
<artifactId>rewrite-api-servlet</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.jboss.spec.javax.servlet</groupId>
29+
<artifactId>jboss-servlet-api_3.0_spec</artifactId>
30+
<scope>provided</scope>
31+
</dependency>
32+
<dependency>
33+
<groupId>com.ocpsoft.rewrite</groupId>
34+
<artifactId>rewrite-test-harness</artifactId>
35+
</dependency>
36+
</dependencies>
3637

3738
</project>

integration-gwt/src/main/java/com/ocpsoft/rewrite/gwt/server/history/Resource.java renamed to impl-servlet/src/main/java/com/ocpsoft/rewrite/servlet/config/Resource.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,21 @@
3131
* Optionally, Customers may choose a Commercial License. For additional
3232
* details, contact an OCPsoft representative (sales@ocpsoft.com)
3333
*/
34-
package com.ocpsoft.rewrite.gwt.server.history;
34+
package com.ocpsoft.rewrite.servlet.config;
3535

3636
import java.net.MalformedURLException;
3737

3838
import com.ocpsoft.logging.Logger;
3939
import com.ocpsoft.rewrite.bind.Evaluation;
4040
import com.ocpsoft.rewrite.bind.ParameterizedPattern;
41+
import com.ocpsoft.rewrite.config.Condition;
4142
import com.ocpsoft.rewrite.context.EvaluationContext;
4243
import com.ocpsoft.rewrite.param.Parameter;
43-
import com.ocpsoft.rewrite.servlet.config.HttpCondition;
4444
import com.ocpsoft.rewrite.servlet.http.event.HttpServletRewrite;
4545

4646
/**
47-
* TODO move to rewrite proper
47+
* A {@link Condition} responsible for determining existence of resources within the web root of the servlet container.
48+
*
4849
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
4950
*/
5051
public class Resource extends HttpCondition
@@ -78,6 +79,10 @@ public boolean evaluateHttp(final HttpServletRewrite event, final EvaluationCont
7879
return false;
7980
}
8081

82+
/**
83+
* Create a new {@link Condition} that returns true if the given resource exists relative to the web root of the
84+
* current application.
85+
*/
8186
public static Resource exists(final String resource)
8287
{
8388
return new Resource(resource);

integration-gwt/src/main/java/com/ocpsoft/rewrite/gwt/server/history/ServletMapping.java renamed to impl-servlet/src/main/java/com/ocpsoft/rewrite/servlet/config/ServletMapping.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
1-
package com.ocpsoft.rewrite.gwt.server.history;
1+
package com.ocpsoft.rewrite.servlet.config;
22

33
import java.net.MalformedURLException;
44
import java.util.Collection;
55
import java.util.Map.Entry;
66

7+
import javax.servlet.Servlet;
78
import javax.servlet.ServletRegistration;
89

910
import com.ocpsoft.logging.Logger;
1011
import com.ocpsoft.rewrite.bind.Evaluation;
1112
import com.ocpsoft.rewrite.bind.ParameterizedPattern;
13+
import com.ocpsoft.rewrite.config.Condition;
1214
import com.ocpsoft.rewrite.context.EvaluationContext;
1315
import com.ocpsoft.rewrite.param.Parameter;
14-
import com.ocpsoft.rewrite.servlet.config.HttpCondition;
1516
import com.ocpsoft.rewrite.servlet.http.event.HttpServletRewrite;
1617

18+
/**
19+
* A {@link Condition} responsible for comparing URLs to Servlet Mappings.
20+
*
21+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
22+
*/
1723
public class ServletMapping extends HttpCondition
1824
{
19-
// TODO move to rewrite proper
2025
private static final Logger log = Logger.getLogger(Resource.class);
2126

22-
private ParameterizedPattern resource;
27+
private final ParameterizedPattern resource;
2328

2429
private ServletMapping(final String resource)
2530
{
@@ -37,30 +42,31 @@ public boolean evaluateHttp(final HttpServletRewrite event, final EvaluationCont
3742
{
3843
String path = resource.build(event, context);
3944
try {
40-
41-
for(Entry<String, ? extends ServletRegistration> entry : event.getRequest().getServletContext().getServletRegistrations().entrySet())
45+
46+
for (Entry<String, ? extends ServletRegistration> entry : event.getRequest().getServletContext()
47+
.getServletRegistrations().entrySet())
4248
{
4349
ServletRegistration servlet = entry.getValue();
4450
Collection<String> mappings = servlet.getMappings();
45-
51+
4652
for (String mapping : mappings) {
47-
if(path.startsWith("/") && !mapping.startsWith("/"))
53+
if (path.startsWith("/") && !mapping.startsWith("/"))
4854
{
4955
mapping = "/" + mapping;
5056
}
51-
52-
if(mapping.contains("*"))
57+
58+
if (mapping.contains("*"))
5359
{
5460
mapping = mapping.replaceAll("\\*", ".*");
5561
}
56-
57-
if(path.matches(mapping))
62+
63+
if (path.matches(mapping))
5864
{
5965
return true;
6066
}
6167
}
6268
}
63-
69+
6470
return event.getRequest().getServletContext().getResource(path) != null;
6571
}
6672
catch (MalformedURLException e) {
@@ -70,6 +76,10 @@ public boolean evaluateHttp(final HttpServletRewrite event, final EvaluationCont
7076
return false;
7177
}
7278

79+
/**
80+
* Create a condition which returns true if the given resource is mapped by any {@link Servlet} instances registered
81+
* within the current application, and returns false if no {@link Servlet} will handle the resource.
82+
*/
7383
public static ServletMapping includes(final String resource)
7484
{
7585
return new ServletMapping(resource);

‎integration-gwt/README.md‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
Rewrite GWT Integration
2+
=================================================
3+
4+
Get Started
5+
===========
6+
7+
1. Include Rewrite GWT in your application's POM file:
8+
9+
<dependency>
10+
<groupId>com.ocpsoft.rewrite</groupId>
11+
<artifactId>rewrite-integation-gwt</artifactId>
12+
<version>${rewrite.version}</version>
13+
</dependency>
14+
15+
2. This enables the HTML5 History.pushState(...) support in your application, which will now use pretty/URLs whenever possible instead of the '#' anchor tag with 'name=value' pairs. Note that you must modify your place tokenization to process the new information.
16+
17+
3. Enable bookmarking by routing some or all URLs back to your host application file. Below is a recommended rule format (replace */index.jsp* with the path to your application):
18+
19+
public class HistoryRewriteConfiguration extends HttpConfigurationProvider
20+
{
21+
public static Logger log = Logger.getLogger(HistoryRewriteConfiguration.class);
22+
23+
@Override
24+
public Configuration getConfiguration(ServletContext context)
25+
{
26+
return ConfigurationBuilder
27+
.begin()
28+
29+
.defineRule()
30+
.when(DispatchType.isRequest()
31+
.and(Path.matches("{path}").where("path").matches(".*"))
32+
.andNot(Resource.exists("{path}"))
33+
.andNot(ServletMapping.includes("{path}")))
34+
.perform(Forward.to("/index.jsp"));
35+
}
36+
37+
@Override
38+
public int priority()
39+
{
40+
return 0;
41+
}
42+
}
43+
44+
4. Run your application!
45+
46+
FEEDBACK
47+
========
48+
This project is looking for your feedback! What would make your task easier? How can we simplify this experience?
49+
50+
TODO's
51+
======
52+
53+
* Implement GWT 'name=value' pair tokenization mapping support.
54+

‎integration-gwt/pom.xml‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@
2828
<artifactId>rewrite-impl-servlet</artifactId>
2929
<scope>provided</scope>
3030
</dependency>
31-
31+
<dependency>
32+
<groupId>com.ocpsoft.rewrite</groupId>
33+
<artifactId>rewrite-test-harness</artifactId>
34+
</dependency>
35+
3236
<dependency>
3337
<groupId>com.google.gwt</groupId>
3438
<artifactId>gwt-user</artifactId>
@@ -40,10 +44,12 @@
4044
<build>
4145
<plugins>
4246
<plugin>
47+
<!-- Attach sources for GWT compiler -->
4348
<artifactId>maven-source-plugin</artifactId>
4449
<version>2.1.2</version>
4550
<executions>
4651
<execution>
52+
<id>attach-sources</id>
4753
<goals>
4854
<goal>jar</goal>
4955
</goals>
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.servlet;
17+
18+
/**
19+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
20+
*
21+
*/
22+
public interface GWTRoot
23+
{
24+
25+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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.servlet.config;
17+
18+
import junit.framework.Assert;
19+
20+
import org.apache.http.client.methods.HttpGet;
21+
import org.apache.http.client.methods.HttpHead;
22+
import org.jboss.arquillian.container.test.api.Deployment;
23+
import org.jboss.arquillian.junit.Arquillian;
24+
import org.jboss.shrinkwrap.api.asset.StringAsset;
25+
import org.jboss.shrinkwrap.api.spec.WebArchive;
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
29+
import com.ocpsoft.rewrite.servlet.GWTRoot;
30+
import com.ocpsoft.rewrite.test.HttpAction;
31+
import com.ocpsoft.rewrite.test.RewriteTestBase;
32+
33+
/**
34+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
35+
*/
36+
@RunWith(Arquillian.class)
37+
public class HistoryRewriteConfigurationTest extends RewriteTestBase
38+
{
39+
@Deployment(testable = true)
40+
public static WebArchive getDeployment()
41+
{
42+
WebArchive deployment = RewriteTestBase
43+
.getDeployment()
44+
.addPackages(true, GWTRoot.class.getPackage())
45+
.addAsResource(
46+
new StringAsset(
47+
"com.ocpsoft.rewrite.gwt.server.history.HistoryRewriteConfiguration"),
48+
"/META-INF/services/com.ocpsoft.rewrite.config.ConfigurationProvider");
49+
return deployment;
50+
}
51+
52+
@Test
53+
public void testContextPathServedFromHeadRequest()
54+
{
55+
HttpAction<HttpHead> action = head("/?com.ocpsoft.rewrite.gwt.history.contextPath");
56+
Assert.assertEquals(200, action.getResponse().getStatusLine().getStatusCode());
57+
58+
Assert.assertEquals(action.getContextPath(),
59+
action.getResponseHeaderValues("com.ocpsoft.rewrite.gwt.history.contextPath").get(0));
60+
}
61+
62+
@Test
63+
public void testContextPathNotServedFromGetRequest()
64+
{
65+
HttpAction<HttpGet> action = get("/?com.ocpsoft.rewrite.gwt.history.contextPath");
66+
Assert.assertEquals(200, action.getResponse().getStatusLine().getStatusCode());
67+
68+
Assert.assertTrue(action.getResponseHeaderValues("com.ocpsoft.rewrite.gwt.history.contextPath").isEmpty());
69+
}
70+
71+
@Test
72+
public void testContextPathServedFromCookieOnNormalRequest()
73+
{
74+
HttpAction<HttpGet> action = get("/");
75+
Assert.assertEquals(200, action.getResponse().getStatusLine().getStatusCode());
76+
77+
String cookie = action.getResponseHeaderValues("Set-Cookie").get(0);
78+
Assert.assertTrue(cookie.contains("com.ocpsoft.rewrite.gwt.history.contextPath=" + action.getContextPath()));
79+
}
80+
81+
}

0 commit comments

Comments
 (0)