Skip to content

Commit 55901d9

Browse files
committed
Enabled distributable mode in web-fragment.xml, adding initial GWT history support
1 parent ceae87b commit 55901d9

14 files changed

Lines changed: 546 additions & 0 deletions

File tree

‎impl-servlet/src/main/resources/META-INF/web-fragment.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
version="3.0">
77

88
<name id="prettyfaces">com_ocpsoft_rewrite</name>
9+
<distributable />
910

1011
<ordering>
1112
<before>

‎integration-gwt/pom.xml‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?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"
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>../pom.xml</relativePath>
10+
</parent>
11+
12+
<artifactId>rewrite-integration-gwt</artifactId>
13+
14+
<properties>
15+
<gwt.version>2.3.0</gwt.version>
16+
</properties>
17+
18+
<dependencies>
19+
20+
<dependency>
21+
<groupId>org.jboss.spec.javax.servlet</groupId>
22+
<artifactId>jboss-servlet-api_3.0_spec</artifactId>
23+
<scope>provided</scope>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>com.ocpsoft.rewrite</groupId>
28+
<artifactId>rewrite-impl-servlet</artifactId>
29+
<scope>provided</scope>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>com.google.gwt</groupId>
34+
<artifactId>gwt-user</artifactId>
35+
<version>${gwt.version}</version>
36+
<scope>provided</scope>
37+
</dependency>
38+
</dependencies>
39+
40+
<build>
41+
<plugins>
42+
<plugin>
43+
<artifactId>maven-source-plugin</artifactId>
44+
<version>2.1.2</version>
45+
<executions>
46+
<execution>
47+
<goals>
48+
<goal>jar</goal>
49+
</goals>
50+
</execution>
51+
</executions>
52+
</plugin>
53+
</plugins>
54+
</build>
55+
56+
</project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 1.6//EN"
3+
"http://google-web-toolkit.googlecode.com/svn/releases/1.6/distro-source/core/src/gwt-module.dtd">
4+
<module rename-to="Rewrite">
5+
<source path="client" />
6+
7+
<inherits name='com.google.gwt.user.User' />
8+
9+
<!-- History Push State Implementation -->
10+
<define-property name="history.push.state" values="supported, notsupported" />
11+
12+
<property-provider name="history.push.state">
13+
<![CDATA[
14+
if (typeof(window.history.pushState) == "function") {
15+
return "supported";
16+
} else {
17+
return "notsupported";
18+
}
19+
]]>
20+
</property-provider>
21+
22+
<replace-with class="com.ocpsoft.rewrite.gwt.client.history.HistoryStateImpl">
23+
<when-type-is class="com.google.gwt.user.client.impl.HistoryImpl" />
24+
<all>
25+
<when-property-is name="history.push.state" value="supported" />
26+
</all>
27+
</replace-with>
28+
</module>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.ocpsoft.rewrite.gwt.client.history;
2+
3+
/**
4+
* Listener to be notified when the application context path is set.
5+
*/
6+
public interface ContextPathListener
7+
{
8+
public void onContextPathSet(String contextPath);
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.ocpsoft.rewrite.gwt.client.history;
2+
3+
4+
/**
5+
* Provide a method of retrieving the root context path from which this application is hosted.
6+
*
7+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
8+
*/
9+
public interface ContextPathProvider
10+
{
11+
void getContextPath(HistoryStateImpl historyState);
12+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.ocpsoft.rewrite.gwt.client.history;
2+
3+
import com.google.gwt.user.client.Cookies;
4+
5+
/**
6+
* Standard {@link ContextPathProvider} for retrieving the context path via a cookie set when the server is accessed.
7+
*
8+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
9+
*/
10+
public class CookieContextPathProvider implements ContextPathProvider
11+
{
12+
public void getContextPath(HistoryStateImpl historyState)
13+
{
14+
String contextPath = Cookies.getCookie("com.ocpsoft.rewrite.gwt.history.contextPath");
15+
if (contextPath != null)
16+
HistoryStateImpl.setContextPath(contextPath);
17+
}
18+
19+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package com.ocpsoft.rewrite.gwt.client.history;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
import com.google.gwt.user.client.impl.HistoryImpl;
8+
9+
/**
10+
* History implementation based on pushState
11+
*/
12+
public class HistoryStateImpl extends HistoryImpl
13+
{
14+
private static List<ContextPathProvider> providers = Arrays.asList((ContextPathProvider) new CookieContextPathProvider(),
15+
new RequestContextPathProvider());
16+
17+
static String contextPath = null;
18+
19+
private static List<ContextPathListener> listeners = new ArrayList<ContextPathListener>();
20+
21+
@Override
22+
public boolean init()
23+
{
24+
for (ContextPathProvider provider : providers) {
25+
provider.getContextPath(this);
26+
if (contextPath != null)
27+
{
28+
break;
29+
}
30+
}
31+
32+
return initNative();
33+
}
34+
35+
static void setContextPath(String contextPath)
36+
{
37+
HistoryStateImpl.contextPath = contextPath;
38+
System.out.println("Set contextPath: " + contextPath);
39+
for (ContextPathListener listner : listeners) {
40+
listner.onContextPathSet(contextPath);
41+
}
42+
}
43+
44+
public static String getContextPath()
45+
{
46+
if (contextPath != null && !contextPath.endsWith("/"))
47+
{
48+
contextPath += "/";
49+
}
50+
System.out.println("Context path: " + contextPath);
51+
return contextPath;
52+
}
53+
54+
public native boolean initNative() /*-{
55+
var token = '';
56+
var historyImpl = this;
57+
58+
var path = $wnd.location.pathname;
59+
if (path.length > 0) {
60+
token = historyImpl.@com.google.gwt.user.client.impl.HistoryImpl::decodeFragment(Ljava/lang/String;)(path);
61+
token = historyImpl.@com.ocpsoft.rewrite.gwt.client.history.HistoryStateImpl::cleanToken(Ljava/lang/String;)(token);
62+
}
63+
64+
@com.google.gwt.user.client.impl.HistoryImpl::setToken(Ljava/lang/String;)(token);
65+
66+
var oldHandler = $wnd.history.onpopstate;
67+
68+
$wnd.onpopstate = $entry(function() {
69+
var token = '';
70+
var path = $wnd.location.pathname;
71+
if (path.length > 0) {
72+
token = historyImpl.@com.google.gwt.user.client.impl.HistoryImpl::decodeFragment(Ljava/lang/String;)(path);
73+
token = historyImpl.@com.ocpsoft.rewrite.gwt.client.history.HistoryStateImpl::cleanToken(Ljava/lang/String;)(token);
74+
}
75+
76+
historyImpl.@com.google.gwt.user.client.impl.HistoryImpl::newItemOnEvent(Ljava/lang/String;)(token);
77+
78+
if (oldHandler) {
79+
oldHandler();
80+
}
81+
});
82+
83+
return true;
84+
}-*/;
85+
86+
protected String cleanToken(String historyToken)
87+
{
88+
String contextPath = HistoryStateImpl.getContextPath();
89+
if (!contextPath.equals(historyToken) && historyToken.startsWith(contextPath))
90+
historyToken = historyToken.substring(contextPath.length());
91+
92+
System.out.println("History.pop() = [" + historyToken + "]");
93+
return historyToken;
94+
}
95+
96+
@Override
97+
protected void nativeUpdate(String historyToken)
98+
{
99+
String contextPath = HistoryStateImpl.getContextPath();
100+
if (!contextPath.equals(historyToken))
101+
historyToken = contextPath + historyToken;
102+
103+
System.out.println("History.push(" + historyToken + ")");
104+
update(historyToken);
105+
}
106+
107+
protected native void update(String historyToken) /*-{
108+
var encodedToken = this.@com.google.gwt.user.client.impl.HistoryImpl::encodeFragment(Ljava/lang/String;)(historyToken);
109+
$wnd.history.pushState(encodedToken, $wnd.document.title, encodedToken);
110+
}-*/;
111+
112+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.ocpsoft.rewrite.gwt.client.history;
2+
3+
import com.google.gwt.core.client.GWT;
4+
import com.google.gwt.http.client.Request;
5+
import com.google.gwt.http.client.RequestBuilder;
6+
import com.google.gwt.http.client.RequestCallback;
7+
import com.google.gwt.http.client.Response;
8+
9+
public class RequestContextPathProvider implements ContextPathProvider
10+
{
11+
12+
@Override
13+
public void getContextPath(HistoryStateImpl historyState)
14+
{
15+
new RequestBuilder(RequestBuilder.HEAD, GWT.getHostPageBaseURL()).setCallback(new RequestCallback() {
16+
17+
@Override
18+
public void onResponseReceived(Request request, Response response)
19+
{
20+
HistoryStateImpl.setContextPath(response.getHeader("com.ocpsoft.rewrite.gwt.history.contextPath"));
21+
}
22+
23+
@Override
24+
public void onError(Request request, Throwable exception)
25+
{}
26+
});
27+
}
28+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.ocpsoft.rewrite.gwt.server.history;
2+
3+
import javax.servlet.ServletContext;
4+
import javax.servlet.http.Cookie;
5+
6+
import com.ocpsoft.logging.Logger;
7+
import com.ocpsoft.rewrite.config.Configuration;
8+
import com.ocpsoft.rewrite.config.ConfigurationBuilder;
9+
import com.ocpsoft.rewrite.servlet.config.HttpConfigurationProvider;
10+
import com.ocpsoft.rewrite.servlet.config.Method;
11+
import com.ocpsoft.rewrite.servlet.config.Path;
12+
import com.ocpsoft.rewrite.servlet.config.QueryString;
13+
import com.ocpsoft.rewrite.servlet.config.Response;
14+
import com.ocpsoft.rewrite.servlet.util.ContextUtil;
15+
16+
public class HistoryRewriteConfiguration extends HttpConfigurationProvider
17+
{
18+
private static final String DISABLED_PARAM = "com.ocpsoft.rewrite.gwt.history.disableCookies";
19+
public static Logger log = Logger.getLogger(HistoryRewriteConfiguration.class);
20+
21+
Configuration config = null;
22+
23+
public Configuration getConfiguration(ServletContext context)
24+
{
25+
if (!ContextUtil.getInitParamBoolean(context, DISABLED_PARAM))
26+
{
27+
if (log.isDebugEnabled())
28+
log.debug(DISABLED_PARAM + " [false]");
29+
30+
if (config == null)
31+
{
32+
config = ConfigurationBuilder
33+
.begin()
34+
.defineRule()
35+
.perform(Response.addCookie(new Cookie("com.ocpsoft.rewrite.gwt.history.contextPath", context
36+
.getContextPath())))
37+
38+
.defineRule()
39+
.when(Method.isHead().and(
40+
QueryString.parameterExists("com.ocpsoft.rewrite.gwt.history.contextPath")))
41+
.perform(Response.setCode(200).and(
42+
Response.addHeader("com.ocpsoft.rewrite.gwt.history.contextPath",
43+
context.getContextPath())));
44+
}
45+
}
46+
return config;
47+
}
48+
49+
public int priority()
50+
{
51+
return -5;
52+
}
53+
}

0 commit comments

Comments
 (0)