Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

WICKET-5095 Upgrade Wicket Native WebSocket to Jetty 9.0.0

  • Loading branch information...
commit 55b916f0eac2cf262d4fa49355c860684a89a297 1 parent 74e7767
@martin-g martin-g authored
View
2  pom.xml
@@ -30,7 +30,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jetty.version>7.6.3.v20120416</jetty.version>
- <jetty9.version>9.0.0.M0</jetty9.version>
+ <jetty9.version>9.0.0.v20130308</jetty9.version>
<spring.version>3.0.7.RELEASE</spring.version>
<maven.javadoc.version>2.8.1</maven.javadoc.version>
<maven.surefire.version>2.12.2</maven.surefire.version>
View
74 ...native-websocket-jetty9/src/main/java/org/apache/wicket/protocol/ws/jetty9/Jetty9UpgradeHttpRequest.java
@@ -0,0 +1,74 @@
+/*
+ * 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.jetty9;
+
+import java.lang.reflect.Field;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletRequestWrapper;
+
+import org.eclipse.jetty.websocket.api.UpgradeRequest;
+import org.eclipse.jetty.websocket.server.ServletWebSocketRequest;
+
+/**
+ * An HttpServletRequest that wraps the original HttpServletRequest
+ * hidden hard by Jetty 9.x UpgradeRequest.
+ */
+class Jetty9UpgradeHttpRequest extends HttpServletRequestWrapper
+{
+ private static final Field REQ;
+ static
+ {
+ try
+ {
+ REQ = ServletWebSocketRequest.class.getDeclaredField("req");
+ } catch (NoSuchFieldException nsfx)
+ {
+ throw new IllegalStateException(ServletWebSocketRequest.class.getName() +
+ " has no 'req' field!", nsfx);
+ }
+ REQ.setAccessible(true);
+ }
+
+ Jetty9UpgradeHttpRequest(UpgradeRequest upgradeRequest)
+ {
+ super(extractHttpRequest(upgradeRequest));
+ }
+
+ private static HttpServletRequest extractHttpRequest(UpgradeRequest upgradeRequest)
+ {
+ if (upgradeRequest instanceof ServletWebSocketRequest == false)
+ {
+ throw new IllegalArgumentException(Jetty9UpgradeHttpRequest.class.getName() +
+ " can work only with " + ServletWebSocketRequest.class.getName());
+ }
+
+ ServletWebSocketRequest servletWebSocketRequest = (ServletWebSocketRequest) upgradeRequest;
+ HttpServletRequest request;
+ try
+ {
+ request = (HttpServletRequest) REQ.get(servletWebSocketRequest);
+ }
+ catch (IllegalAccessException iax)
+ {
+ throw new IllegalStateException("Cannot get the HttpServletRequest after the protocol upgrade", iax);
+ }
+
+ return request;
+ }
+
+}
View
54 ...ative-websocket-jetty9/src/main/java/org/apache/wicket/protocol/ws/jetty9/Jetty9WebSocketConnection.java
@@ -17,16 +17,15 @@
package org.apache.wicket.protocol.ws.jetty9;
import java.io.IOException;
-import java.io.InterruptedIOException;
-import java.util.concurrent.ExecutionException;
+import java.nio.ByteBuffer;
import org.apache.wicket.protocol.ws.api.AbstractWebSocketConnection;
import org.apache.wicket.protocol.ws.api.AbstractWebSocketProcessor;
import org.apache.wicket.protocol.ws.api.IWebSocketConnection;
import org.apache.wicket.util.lang.Args;
-import org.eclipse.jetty.util.Callback;
-import org.eclipse.jetty.util.FutureCallback;
-import org.eclipse.jetty.websocket.core.api.WebSocketConnection;
+import org.eclipse.jetty.websocket.api.Session;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* A wrapper around Jetty9's native WebSocketConnection.
@@ -35,24 +34,26 @@
*/
public class Jetty9WebSocketConnection extends AbstractWebSocketConnection
{
- private final WebSocketConnection connection;
+ private static final Logger LOG = LoggerFactory.getLogger(Jetty9WebSocketConnection.class);
+
+ private final Session session;
/**
* Constructor.
*
- * @param connection
+ * @param session
* the jetty websocket connection
*/
- public Jetty9WebSocketConnection(WebSocketConnection connection, AbstractWebSocketProcessor webSocketProcessor)
+ public Jetty9WebSocketConnection(Session session, AbstractWebSocketProcessor webSocketProcessor)
{
super(webSocketProcessor);
- this.connection = Args.notNull(connection, "connection");
+ this.session = Args.notNull(session, "connection");
}
@Override
public boolean isOpen()
{
- return connection.isOpen();
+ return session.isOpen();
}
@Override
@@ -60,7 +61,13 @@ public void close(int code, String reason)
{
if (isOpen())
{
- connection.close(code, reason);
+ try
+ {
+ session.close(code, reason);
+ } catch (IOException iox)
+ {
+ LOG.error("An error occurred while closing WebSocket session", iox);
+ }
}
}
@@ -69,9 +76,7 @@ public IWebSocketConnection sendMessage(String message) throws IOException
{
checkClosed();
- FutureCallback<Void> waiter = new FutureCallback<Void>();
- connection.write(null, waiter, message);
- waitForMessageSent(waiter);
+ session.getRemote().sendString(message);
return this;
}
@@ -81,28 +86,11 @@ public IWebSocketConnection sendMessage(byte[] message, int offset, int length)
{
checkClosed();
- FutureCallback<Void> waiter = new FutureCallback<Void>();
- connection.write(null, new Callback.Empty<Void>(), message, offset, length);
- waitForMessageSent(waiter);
+ ByteBuffer buf = ByteBuffer.wrap(message, offset, length);
+ session.getRemote().sendBytes(buf);
return this;
}
- private void waitForMessageSent(FutureCallback<?> waiter) throws IOException
- {
- try
- {
- waiter.get();
- }
- catch (InterruptedException e)
- {
- throw new InterruptedIOException();
- }
- catch (ExecutionException e)
- {
- FutureCallback.rethrow(e);
- }
- }
-
private void checkClosed()
{
if (!isOpen())
View
19 ...et-native-websocket-jetty9/src/main/java/org/apache/wicket/protocol/ws/jetty9/Jetty9WebSocketFilter.java
@@ -24,11 +24,11 @@
import javax.servlet.http.HttpServletResponse;
import org.apache.wicket.protocol.ws.AbstractUpgradeFilter;
-import org.eclipse.jetty.websocket.core.api.UpgradeRequest;
-import org.eclipse.jetty.websocket.core.api.UpgradeResponse;
-import org.eclipse.jetty.websocket.core.api.WebSocketPolicy;
-import org.eclipse.jetty.websocket.server.WebSocketCreator;
+import org.eclipse.jetty.websocket.api.UpgradeRequest;
+import org.eclipse.jetty.websocket.api.UpgradeResponse;
+import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.server.WebSocketServerFactory;
+import org.eclipse.jetty.websocket.servlet.WebSocketCreator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -50,20 +50,17 @@ public void init(final boolean isServlet, final FilterConfig filterConfig)
try
{
WebSocketPolicy serverPolicy = WebSocketPolicy.newServerPolicy();
- String bs = filterConfig.getInitParameter("bufferSize");
+ String bs = filterConfig.getInitParameter("inputBufferSize");
if (bs != null)
- serverPolicy.setBufferSize(Integer.parseInt(bs));
+ serverPolicy.setInputBufferSize(Integer.parseInt(bs));
String max = filterConfig.getInitParameter("maxIdleTime");
if (max != null)
serverPolicy.setIdleTimeout(Integer.parseInt(max));
- max = filterConfig.getInitParameter("maxTextMessageSize");
+ max = filterConfig.getInitParameter("maxMessageSize");
if (max != null)
- serverPolicy.setMaxTextMessageSize(Integer.parseInt(max));
+ serverPolicy.setMaxMessageSize(Integer.parseInt(max));
- max = filterConfig.getInitParameter("maxBinaryMessageSize");
- if (max != null)
- serverPolicy.setMaxBinaryMessageSize(Integer.parseInt(max));
_webSocketFactory = new WebSocketServerFactory(serverPolicy);
_webSocketFactory.setCreator(new WebSocketCreator()
View
34 ...native-websocket-jetty9/src/main/java/org/apache/wicket/protocol/ws/jetty9/Jetty9WebSocketProcessor.java
@@ -16,23 +16,18 @@
*/
package org.apache.wicket.protocol.ws.jetty9;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletRequestWrapper;
-
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.protocol.ws.api.AbstractWebSocketProcessor;
-import org.eclipse.jetty.websocket.core.annotations.WebSocket;
-import org.eclipse.jetty.websocket.core.api.UpgradeRequest;
-import org.eclipse.jetty.websocket.core.api.UpgradeResponse;
-import org.eclipse.jetty.websocket.core.api.WebSocketConnection;
-import org.eclipse.jetty.websocket.core.api.WebSocketException;
-import org.eclipse.jetty.websocket.core.api.WebSocketListener;
+import org.eclipse.jetty.websocket.api.Session;
+import org.eclipse.jetty.websocket.api.UpgradeRequest;
+import org.eclipse.jetty.websocket.api.UpgradeResponse;
+import org.eclipse.jetty.websocket.api.WebSocketListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* An {@link org.apache.wicket.protocol.ws.api.IWebSocketProcessor processor} that integrates with
- * Jetty 9.x {@link WebSocket web socket} implementation.
+ * Jetty 9.x {@link Session web socket} implementation.
*
* @since 6.2
*/
@@ -55,14 +50,13 @@
public Jetty9WebSocketProcessor(final UpgradeRequest upgradeRequest,
final UpgradeResponse upgradeResponse, final WebApplication application)
{
- super((HttpServletRequest)((HttpServletRequestWrapper)upgradeRequest).getRequest(),
- application);
+ super(new Jetty9UpgradeHttpRequest(upgradeRequest), application);
}
@Override
- public void onWebSocketConnect(WebSocketConnection connection)
+ public void onWebSocketConnect(Session session)
{
- onConnect(new Jetty9WebSocketConnection(connection, this));
+ onConnect(new Jetty9WebSocketConnection(session, this));
}
@Override
@@ -84,19 +78,19 @@ public void onWebSocketClose(int statusCode, String reason)
}
@Override
- public void onWebSocketException(WebSocketException error)
+ public void onWebSocketError(Throwable throwable)
{
- LOG.error("An error occurred when using WebSocket.", error);
+ LOG.error("An error occurred when using WebSocket.", throwable);
}
@Override
public void onOpen(Object connection)
{
- if (!(connection instanceof WebSocketConnection))
+ if (!(connection instanceof Session))
{
- throw new IllegalArgumentException(WebSocketConnection.class.getName() +
- " can work only with " + WebSocketConnection.class.getName());
+ throw new IllegalArgumentException(Jetty9WebSocketProcessor.class.getName() +
+ " can work only with " + Session.class.getName());
}
- onWebSocketConnect((WebSocketConnection)connection);
+ onWebSocketConnect((Session)connection);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.