Skip to content

Commit abc676b

Browse files
committed
Fixed GWT issue when context path is '/'
1 parent d5f102a commit abc676b

7 files changed

Lines changed: 135 additions & 41 deletions

File tree

‎integration-gwt/src/main/java/org/ocpsoft/rewrite/gwt/client/history/HistoryStateImpl.java‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,15 @@ public boolean init()
3434

3535
static void setContextPath(String contextPath)
3636
{
37+
if (contextPath != null && !contextPath.endsWith("/"))
38+
{
39+
contextPath += "/";
40+
}
41+
3742
HistoryStateImpl.contextPath = contextPath;
43+
3844
System.out.println("Set contextPath: " + contextPath);
45+
3946
for (ContextPathListener listner : listeners) {
4047
listner.onContextPathSet(contextPath);
4148
}
@@ -55,10 +62,6 @@ public void removeListener()
5562

5663
public static String getContextPath()
5764
{
58-
if (contextPath != null && !contextPath.endsWith("/"))
59-
{
60-
contextPath += "/";
61-
}
6265
return contextPath;
6366
}
6467

‎integration-gwt/src/main/java/org/ocpsoft/rewrite/gwt/server/history/HistoryRewriteConfiguration.java‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,22 @@ public Configuration getConfiguration(ServletContext context)
2929

3030
if (config == null)
3131
{
32+
String contextPath = context.getContextPath();
33+
if (contextPath == null || contextPath.isEmpty())
34+
contextPath = "/";
35+
3236
config = ConfigurationBuilder
3337
.begin()
3438
.defineRule()
35-
.perform(Response.addCookie(new Cookie("org.ocpsoft.rewrite.gwt.history.contextPath", context
36-
.getContextPath())))
39+
.perform(Response
40+
.addCookie(new Cookie("org.ocpsoft.rewrite.gwt.history.contextPath", contextPath)))
3741

3842
.defineRule()
3943
.when(Method.isHead().and(
4044
QueryString.parameterExists("org.ocpsoft.rewrite.gwt.history.contextPath")))
4145
.perform(Response.setCode(200).and(
4246
Response.addHeader("org.ocpsoft.rewrite.gwt.history.contextPath",
43-
context.getContextPath())));
47+
contextPath)));
4448
}
4549
}
4650
return config;

integration-gwt/src/test/java/org/ocpsoft/rewrite/servlet/GWTRoot.java renamed to integration-gwt/src/test/java/org/ocpsoft/rewrite/gwt/GWTRoot.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.ocpsoft.rewrite.servlet;
16+
package org.ocpsoft.rewrite.gwt;
1717

1818
/**
1919
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>

integration-gwt/src/test/java/org/ocpsoft/rewrite/servlet/config/HistoryRewriteConfigurationTest.java renamed to integration-gwt/src/test/java/org/ocpsoft/rewrite/gwt/config/HistoryRewriteConfigurationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.ocpsoft.rewrite.servlet.config;
16+
package org.ocpsoft.rewrite.gwt.config;
1717

1818
import junit.framework.Assert;
1919

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.gwt.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+
import org.ocpsoft.rewrite.config.ConfigurationProvider;
29+
import org.ocpsoft.rewrite.gwt.server.history.HistoryRewriteConfiguration;
30+
import org.ocpsoft.rewrite.test.HttpAction;
31+
import org.ocpsoft.rewrite.test.RewriteTest;
32+
33+
/**
34+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
35+
*/
36+
@RunWith(Arquillian.class)
37+
public class HistoryRewriteRootContextConfigurationTest extends RewriteTest
38+
{
39+
@Deployment(testable = false)
40+
public static WebArchive getDeployment()
41+
{
42+
WebArchive deployment = RewriteTest
43+
.getDeployment("ROOT.war")
44+
.addAsWebResource(new StringAsset(""), "index.html")
45+
.addAsServiceProvider(ConfigurationProvider.class, HistoryRewriteConfiguration.class);
46+
return deployment;
47+
}
48+
49+
@Test
50+
public void testContextPathServedFromHeadRequest()
51+
{
52+
HttpAction<HttpHead> action = head("/?org.ocpsoft.rewrite.gwt.history.contextPath");
53+
Assert.assertEquals(200, action.getResponse().getStatusLine().getStatusCode());
54+
55+
Assert.assertEquals(action.getContextPath(),
56+
action.getResponseHeaderValues("org.ocpsoft.rewrite.gwt.history.contextPath").get(0));
57+
}
58+
59+
@Test
60+
public void testContextPathNotServedFromGetRequest()
61+
{
62+
HttpAction<HttpGet> action = get("/index.html?org.ocpsoft.rewrite.gwt.history.contextPath");
63+
Assert.assertEquals(200, action.getResponse().getStatusLine().getStatusCode());
64+
65+
Assert.assertTrue(action.getResponseHeaderValues("org.ocpsoft.rewrite.gwt.history.contextPath").isEmpty());
66+
}
67+
68+
@Test
69+
public void testContextPathServedFromCookieOnNormalRequest()
70+
{
71+
HttpAction<HttpGet> action = get("/index.html");
72+
Assert.assertEquals(200, action.getResponse().getStatusLine().getStatusCode());
73+
74+
String cookie = action.getResponseHeaderValues("Set-Cookie").get(0);
75+
Assert.assertTrue(cookie.contains("org.ocpsoft.rewrite.gwt.history.contextPath=" + action.getContextPath()));
76+
}
77+
78+
}

‎test-harness/src/main/java/org/ocpsoft/rewrite/test/RewriteTest.java‎

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,50 +36,59 @@ public class RewriteTest extends RewriteTestBase
3636
@Deployment(testable = false)
3737
public static WebArchive getDeployment()
3838
{
39-
WebArchive archive = getDeploymentNoWebXml();
40-
if(isJetty()) {
39+
return getDeployment("rewrite-test.war");
40+
}
41+
42+
public static WebArchive getDeployment(String name)
43+
{
44+
WebArchive archive = getDeploymentNoWebXml(name);
45+
if (isJetty()) {
4146
archive.setWebXML("jetty-web.xml");
4247
}
4348
return archive;
4449
}
4550

4651
public static WebArchive getDeploymentNoWebXml()
52+
{
53+
return getDeploymentNoWebXml("rewrite-test.war");
54+
}
55+
56+
public static WebArchive getDeploymentNoWebXml(String name)
4757
{
4858

4959
WebArchive archive = ShrinkWrap
50-
.create(WebArchive.class, "rewrite-test.war")
60+
.create(WebArchive.class, name)
5161
.addPackages(true, MockBinding.class.getPackage())
5262
.addAsLibraries(resolveDependencies("org.ocpsoft.logging:logging-api:1.0.1.Final"))
5363
.addAsLibraries(getRewriteArchive())
5464
.addAsLibraries(getCurrentArchive());
5565

5666
// Jetty specific stuff
57-
if(isJetty()) {
58-
59-
archive.addAsLibraries(resolveDependencies("org.jboss.weld.servlet:weld-servlet:1.1.4.Final"));
60-
61-
/*
62-
* Set the EL implementation
63-
*/
67+
if (isJetty()) {
68+
69+
archive.addAsLibraries(resolveDependencies("org.jboss.weld.servlet:weld-servlet:1.1.4.Final"));
70+
71+
/*
72+
* Set the EL implementation
73+
*/
6474
archive.addAsLibraries(resolveDependencies("org.glassfish.web:el-impl:jar:2.2"));
6575
archive.add(new StringAsset("com.sun.el.ExpressionFactoryImpl"),
6676
"/WEB-INF/classes/META-INF/services/javax.el.ExpressionFactory");
67-
68-
/*
69-
* Set up container configuration
70-
*/
71-
archive.addAsWebInfResource(new StringAsset("<beans/>"), "beans.xml");
72-
archive.addAsWebInfResource("jetty-env.xml", "jetty-env.xml");
73-
archive.addAsWebInfResource("jetty-log4j.xml", "log4j.xml");
74-
77+
78+
/*
79+
* Set up container configuration
80+
*/
81+
archive.addAsWebInfResource(new StringAsset("<beans/>"), "beans.xml");
82+
archive.addAsWebInfResource("jetty-env.xml", "jetty-env.xml");
83+
archive.addAsWebInfResource("jetty-log4j.xml", "log4j.xml");
84+
7585
}
76-
77-
System.out.println(archive.toString(true));
78-
86+
7987
return archive;
8088
}
81-
82-
protected static boolean isJetty() {
89+
90+
protected static boolean isJetty()
91+
{
8392
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
8493
try {
8594
classLoader.loadClass("org.jboss.arquillian.container.jetty.embedded_7.JettyEmbeddedContainer");
@@ -101,7 +110,7 @@ protected static JavaArchive getCurrentArchive()
101110
archive.addAsResource(classes);
102111
if (metaInf.exists())
103112
archive.addAsResource(metaInf);
104-
113+
105114
return archive.addAsResource(new StringAsset("placeholder"), "README");
106115
}
107116

‎test-harness/src/main/java/org/ocpsoft/rewrite/test/RewriteTestBase.java‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
/**
2929
* Base utility class for Rewrite Tests.
30-
*
30+
*
3131
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
3232
*/
3333
public abstract class RewriteTestBase
@@ -53,6 +53,7 @@ protected static GenericArchive resolveDependency(final String coords)
5353
.loadMetadataFromPom("pom.xml")
5454
.resolveAs(GenericArchive.class)).get(0);
5555
}
56+
5657
/**
5758
* Request a resource from the deployed test-application. The {@link HttpServletRequest#getContextPath()} will be
5859
* automatically prepended to the given path.
@@ -110,19 +111,18 @@ protected HttpAction<HttpHead> head(final String path)
110111
}
111112
}
112113

114+
@ArquillianResource
115+
URL baseUrl;
116+
113117
protected String getBaseURL()
114118
{
115-
String baseUrl = "http://localhost:8080";
116-
if (baseUrl.endsWith("/"))
117-
{
118-
baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
119-
}
120-
return baseUrl;
119+
return baseUrl.getProtocol() + "://" + baseUrl.getHost()
120+
+ (baseUrl.getPort() == -1 ? "" : ":" + baseUrl.getPort());
121121
}
122122

123123
protected String getContextPath()
124124
{
125-
return "/rewrite-test";
125+
return baseUrl.getPath().replaceAll("^(.*)/$", "$1").replaceAll("ROOT$", "");
126126
}
127127

128128
protected HtmlAction getWebClient(String path) throws FailingHttpStatusCodeException, IOException

0 commit comments

Comments
 (0)