Updated Branches: refs/heads/sandbox/wicket-native-websocket-glassfish [created] a950aa1a9
Add module for Glassfish3 Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/a950aa1a Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/a950aa1a Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/a950aa1a Branch: refs/heads/sandbox/wicket-native-websocket-glassfish Commit: a950aa1a9847ed412332098a8fb9e92b9d0b24ea Parents: 74c0e98 Author: Martin Tzvetanov Grigorov <mgrigo...@apache.org> Authored: Tue Jul 3 22:51:05 2012 +0200 Committer: Martin Tzvetanov Grigorov <mgrigo...@apache.org> Committed: Tue Jul 3 22:51:05 2012 +0200 ---------------------------------------------------------------------- .../wicket-native-websocket/pom.xml | 3 +- .../wicket-native-websocket-glassfish/pom.xml | 47 +++++++ .../protocol/http/Glassfish3WebSocketFilter.java | 98 +++++++++++++++ .../glassfish3/GlassfishWebSocketConnection.java | 83 ++++++++++++ .../ws/glassfish3/GlassfishWebSocketProcessor.java | 95 ++++++++++++++ .../ws/util/license/ApacheLicenceHeaderTest.java | 34 +++++ 6 files changed, 359 insertions(+), 1 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/a950aa1a/wicket-experimental/wicket-native-websocket/pom.xml ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-native-websocket/pom.xml b/wicket-experimental/wicket-native-websocket/pom.xml index ca6aa66..af0e5c5 100644 --- a/wicket-experimental/wicket-native-websocket/pom.xml +++ b/wicket-experimental/wicket-native-websocket/pom.xml @@ -24,7 +24,7 @@ <relativePath>../pom.xml</relativePath> </parent> <artifactId>wicket-native-websocket</artifactId> - <version>0.1-SNAPSHOT</version> + <version>0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>Wicket Native WebSocket Parent</name> <description>Wicket Native WebSocket provides native integration for WebSocket support with Servlet web containers.</description> @@ -32,6 +32,7 @@ <module>wicket-native-websocket-core</module> <module>wicket-native-websocket-jetty</module> <module>wicket-native-websocket-tomcat</module> + <module>wicket-native-websocket-glassfish</module> </modules> <dependencyManagement> <dependencies> http://git-wip-us.apache.org/repos/asf/wicket/blob/a950aa1a/wicket-experimental/wicket-native-websocket/wicket-native-websocket-glassfish/pom.xml ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-native-websocket/wicket-native-websocket-glassfish/pom.xml b/wicket-experimental/wicket-native-websocket/wicket-native-websocket-glassfish/pom.xml new file mode 100644 index 0000000..15f67a8 --- /dev/null +++ b/wicket-experimental/wicket-native-websocket/wicket-native-websocket-glassfish/pom.xml @@ -0,0 +1,47 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<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/maven-v4_0_0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.wicket</groupId> + <artifactId>wicket-native-websocket</artifactId> + <version>0.1-SNAPSHOT</version> + <relativePath>../pom.xml</relativePath> + </parent> + <artifactId>wicket-native-websocket-glassfish</artifactId> + <packaging>jar</packaging> + <version>0.1-SNAPSHOT</version> + <name>Wicket Native WebSocket Glassfish</name> + <description>Provides the code specific to integrate with Glassfish web container</description> + <dependencies> + <dependency> + <groupId>org.apache.wicket</groupId> + <artifactId>wicket-native-websocket-core</artifactId> + </dependency> + + <dependency> + <groupId>org.glassfish.websocket</groupId> + <artifactId>websocket-impl</artifactId> + <version>0.1</version> + <scope>provided</scope> + </dependency> + + + </dependencies> +</project> http://git-wip-us.apache.org/repos/asf/wicket/blob/a950aa1a/wicket-experimental/wicket-native-websocket/wicket-native-websocket-glassfish/src/main/java/org/apache/wicket/protocol/http/Glassfish3WebSocketFilter.java ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-native-websocket/wicket-native-websocket-glassfish/src/main/java/org/apache/wicket/protocol/http/Glassfish3WebSocketFilter.java b/wicket-experimental/wicket-native-websocket/wicket-native-websocket-glassfish/src/main/java/org/apache/wicket/protocol/http/Glassfish3WebSocketFilter.java new file mode 100644 index 0000000..4fe203e --- /dev/null +++ b/wicket-experimental/wicket-native-websocket/wicket-native-websocket-glassfish/src/main/java/org/apache/wicket/protocol/http/Glassfish3WebSocketFilter.java @@ -0,0 +1,98 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.protocol.http; + +import org.apache.wicket.Application; +import org.apache.wicket.protocol.ws.glassfish3.GlassfishWebSocketProcessor; +import org.glassfish.grizzly.http.HttpRequestPacket; +import org.glassfish.grizzly.websockets.ProtocolHandler; +import org.glassfish.grizzly.websockets.Version; +import org.glassfish.grizzly.websockets.WebSocket; +import org.glassfish.grizzly.websockets.WebSocketApplication; +import org.glassfish.grizzly.websockets.WebSocketEngine; +import org.glassfish.grizzly.websockets.WebSocketListener; + +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * @since + */ +public class Glassfish3WebSocketFilter extends AbstractUpgradeFilter +{ + private final GlassfishWebSocketApplication webSocketApplication = new GlassfishWebSocketApplication(); + + @Override + public void init(boolean isServlet, FilterConfig filterConfig) throws ServletException + { + super.init(isServlet, filterConfig); + + WebSocketEngine.getEngine().register(webSocketApplication); + } + + @Override + public void destroy() + { + WebSocketEngine.getEngine().unregisterAll(); + + super.destroy(); + } + + protected boolean acceptWebSocket(HttpServletRequest req, HttpServletResponse resp, Application application) + throws ServletException, IOException + { + if (!super.acceptWebSocket(req, resp, application)) + { + return false; + } + + GlassfishWebSocketProcessor webSocketHandle = new GlassfishWebSocketProcessor(req, application); + ProtocolHandler handler = Version.DRAFT17.createHandler(false); + GlassfishWebSocketProcessor.GlassfishWebSocket webSocket = webSocketHandle.new GlassfishWebSocket(handler); + + webSocketApplication.add(webSocket); + webSocket.onConnect(); + return true; + } + + private static class GlassfishWebSocketApplication extends WebSocketApplication + { + @Override + public WebSocket createSocket(ProtocolHandler handler, WebSocketListener... listeners) { + return null; + } + + @Override + public boolean add(WebSocket socket) + { + return super.add(socket); + } + + @Override + public boolean isApplicationRequest(HttpRequestPacket request) { + return true; + } + + @Override + public void onConnect(WebSocket socket) { + super.onConnect(socket); + } + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/a950aa1a/wicket-experimental/wicket-native-websocket/wicket-native-websocket-glassfish/src/main/java/org/apache/wicket/protocol/ws/glassfish3/GlassfishWebSocketConnection.java ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-native-websocket/wicket-native-websocket-glassfish/src/main/java/org/apache/wicket/protocol/ws/glassfish3/GlassfishWebSocketConnection.java b/wicket-experimental/wicket-native-websocket/wicket-native-websocket-glassfish/src/main/java/org/apache/wicket/protocol/ws/glassfish3/GlassfishWebSocketConnection.java new file mode 100644 index 0000000..101e73a --- /dev/null +++ b/wicket-experimental/wicket-native-websocket/wicket-native-websocket-glassfish/src/main/java/org/apache/wicket/protocol/ws/glassfish3/GlassfishWebSocketConnection.java @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.protocol.ws.glassfish3; + +import java.io.IOException; + +import org.apache.wicket.protocol.ws.api.IWebSocketConnection; +import org.apache.wicket.util.lang.Args; +import org.glassfish.grizzly.websockets.WebSocket; + +/** + * @since 6.0 + */ +public class GlassfishWebSocketConnection implements IWebSocketConnection +{ + private final WebSocket connection; + + /** + * Constructor + * + * @param connection + * the native web socket connection + */ + public GlassfishWebSocketConnection(final WebSocket connection) + { + this.connection = Args.notNull(connection, "connection"); + } + + @Override + public boolean isOpen() + { + return connection.isConnected(); + } + + @Override + public void close(int code, String message) + { + if (isOpen()) + { + connection.close(code, message); + } + } + + @Override + public IWebSocketConnection sendMessage(String message) throws IOException + { + checkClosed(); + + connection.send(message); + return this; + } + + @Override + public IWebSocketConnection sendMessage(byte[] message, int offset, int length) throws IOException + { + checkClosed(); + + connection.send(message); + return this; + } + + private void checkClosed() + { + if (!isOpen()) + { + throw new IllegalStateException("The connection is closed."); + } + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/a950aa1a/wicket-experimental/wicket-native-websocket/wicket-native-websocket-glassfish/src/main/java/org/apache/wicket/protocol/ws/glassfish3/GlassfishWebSocketProcessor.java ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-native-websocket/wicket-native-websocket-glassfish/src/main/java/org/apache/wicket/protocol/ws/glassfish3/GlassfishWebSocketProcessor.java b/wicket-experimental/wicket-native-websocket/wicket-native-websocket-glassfish/src/main/java/org/apache/wicket/protocol/ws/glassfish3/GlassfishWebSocketProcessor.java new file mode 100644 index 0000000..dfff615 --- /dev/null +++ b/wicket-experimental/wicket-native-websocket/wicket-native-websocket-glassfish/src/main/java/org/apache/wicket/protocol/ws/glassfish3/GlassfishWebSocketProcessor.java @@ -0,0 +1,95 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.protocol.ws.glassfish3; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.wicket.Application; +import org.apache.wicket.protocol.ws.api.AbstractWebSocketProcessor; +import org.glassfish.grizzly.websockets.DataFrame; +import org.glassfish.grizzly.websockets.DefaultWebSocket; +import org.glassfish.grizzly.websockets.ProtocolHandler; +import org.glassfish.grizzly.websockets.WebSocket; +import org.glassfish.grizzly.websockets.WebSocketListener; +import org.glassfish.grizzly.websockets.draft06.ClosingFrame; + +/** + * @since 0.1 + */ +public class GlassfishWebSocketProcessor extends AbstractWebSocketProcessor +{ + public class GlassfishWebSocket extends DefaultWebSocket + { + public GlassfishWebSocket(ProtocolHandler protocolHandler, WebSocketListener... listeners) { + super(protocolHandler, listeners); + } + + @Override + public void onClose(DataFrame frame) + { + int code; + String reason; + if (frame instanceof ClosingFrame) + { + ClosingFrame closingFrame = (ClosingFrame) frame; + code = closingFrame.getCode(); + reason = closingFrame.getReason(); + } + else + { + code = 0; + reason = frame.getTextPayload(); + } + + GlassfishWebSocketProcessor.this.onClose(code, reason); + } + + @Override + public void onConnect() + { + super.onConnect(); + GlassfishWebSocketProcessor.this.onOpen(this); + } + + @Override + public void onMessage(String message) + { + GlassfishWebSocketProcessor.this.onMessage(message); + } + + @Override + public void onMessage(byte[] message) + { + GlassfishWebSocketProcessor.this.onMessage(message, 0, message.length); + } + } + + public GlassfishWebSocketProcessor(final HttpServletRequest request, Application application) + { + super(request, application); + } + + @Override + public void onOpen(Object connection) + { + if (!(connection instanceof WebSocket)) + { + throw new IllegalArgumentException(GlassfishWebSocketProcessor.class.getName() + " can work only with " + WebSocket.class.getName()); + } + onConnect(new GlassfishWebSocketConnection((WebSocket) connection)); + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/a950aa1a/wicket-experimental/wicket-native-websocket/wicket-native-websocket-glassfish/src/test/java/org/apache/wicket/protocol/ws/util/license/ApacheLicenceHeaderTest.java ---------------------------------------------------------------------- diff --git a/wicket-experimental/wicket-native-websocket/wicket-native-websocket-glassfish/src/test/java/org/apache/wicket/protocol/ws/util/license/ApacheLicenceHeaderTest.java b/wicket-experimental/wicket-native-websocket/wicket-native-websocket-glassfish/src/test/java/org/apache/wicket/protocol/ws/util/license/ApacheLicenceHeaderTest.java new file mode 100644 index 0000000..9e3759d --- /dev/null +++ b/wicket-experimental/wicket-native-websocket/wicket-native-websocket-glassfish/src/test/java/org/apache/wicket/protocol/ws/util/license/ApacheLicenceHeaderTest.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.protocol.ws.util.license; + +import org.apache.wicket.util.license.ApacheLicenseHeaderTestCase; + +/** + * Test that the license headers are in place in this project. The tests are run from + * {@link org.apache.wicket.util.license.ApacheLicenseHeaderTestCase}, but you can add project specific tests here if needed. + */ +public class ApacheLicenceHeaderTest extends ApacheLicenseHeaderTestCase +{ + /** + * Construct. + */ + public ApacheLicenceHeaderTest() + { + // addHeaders = true; + } +}