Merge remote-tracking branch 'eclipse/jetty-10.0.x-3167-2175-websocket-close' into jetty-10.0.x-3167-2175-websocket-close

This commit is contained in:
Lachlan Roberts 2019-01-15 13:05:23 +11:00
commit 5d31612ec2
11 changed files with 144 additions and 108 deletions

View File

@ -97,6 +97,18 @@ public class ServletContextHandler extends ContextHandler
public final static int NO_SESSIONS=0;
public final static int NO_SECURITY=0;
public static ServletContextHandler getServletContextHandler(ServletContext servletContext, String purpose)
{
ContextHandler contextHandler = ContextHandler.getContextHandler(servletContext);
if (contextHandler == null)
throw new IllegalStateException("No Jetty ContextHandler, " + purpose+ " unavailable");
if (!(contextHandler instanceof ServletContextHandler))
throw new IllegalStateException("No Jetty ServletContextHandler, " + purpose + " unavailable");
return (ServletContextHandler)contextHandler;
}
public interface ServletContainerInitializerCaller extends LifeCycle {};
protected final DecoratedObjectFactory _objFactory;

View File

@ -25,6 +25,7 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.function.Supplier;
import javax.websocket.ClientEndpoint;
import javax.websocket.ClientEndpointConfig;
import javax.websocket.DeploymentException;
@ -61,7 +62,11 @@ public class JavaxWebSocketClientContainer extends JavaxWebSocketContainer imple
public JavaxWebSocketClientContainer()
{
this(() -> new WebSocketCoreClient());
this(() -> {
WebSocketCoreClient coreClient = new WebSocketCoreClient();
coreClient.getHttpClient().setName("Javax-WebSocketClient@" + Integer.toHexString(coreClient.getHttpClient().hashCode()));
return coreClient;
});
}
public JavaxWebSocketClientContainer(Supplier<WebSocketCoreClient> coreClientFactory)
@ -75,7 +80,7 @@ public class JavaxWebSocketClientContainer extends JavaxWebSocketContainer imple
{
super();
this.coreClient = coreClient;
this.addBean(this.coreClient);
this.addBean(coreClient);
this.objectFactory = new DecoratedObjectFactory();
this.extensionRegistry = new WebSocketExtensionRegistry();
this.frameHandlerFactory = new JavaxWebSocketClientFrameHandlerFactory(this);
@ -103,8 +108,10 @@ public class JavaxWebSocketClientContainer extends JavaxWebSocketContainer imple
if (coreClient == null)
{
coreClient = coreClientFactory.get();
this.coreClient.getHttpClient().setName("Javax-WebSocketClient@" + Integer.toHexString(this.coreClient.getHttpClient().hashCode()));
addManaged(coreClient);
if (coreClient.isRunning())
addBean(coreClient,false);
else
addManaged(coreClient);
}
return coreClient;

View File

@ -18,6 +18,25 @@
package org.eclipse.jetty.websocket.javax.common;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import javax.websocket.CloseReason;
import javax.websocket.Decoder;
import javax.websocket.EndpointConfig;
import javax.websocket.MessageHandler;
import javax.websocket.PongMessage;
import javax.websocket.Session;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.log.Log;
@ -39,24 +58,6 @@ import org.eclipse.jetty.websocket.javax.common.messages.PartialByteBufferMessag
import org.eclipse.jetty.websocket.javax.common.messages.PartialStringMessageSink;
import org.eclipse.jetty.websocket.javax.common.util.InvokerUtils;
import javax.websocket.CloseReason;
import javax.websocket.Decoder;
import javax.websocket.EndpointConfig;
import javax.websocket.MessageHandler;
import javax.websocket.PongMessage;
import javax.websocket.Session;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
public class JavaxWebSocketFrameHandler implements FrameHandler
{
private final Logger LOG;
@ -217,9 +218,7 @@ public class JavaxWebSocketFrameHandler implements FrameHandler
if (errorHandle == null)
{
LOG.warn("Unhandled Error: Endpoint " + endpointInstance.getClass().getName() + " : " + cause);
if (LOG.isDebugEnabled())
LOG.debug("unhandled", cause);
LOG.warn("Unhandled Error: " + endpointInstance, cause);
return;
}

View File

@ -24,8 +24,6 @@ import java.util.List;
import java.util.concurrent.Executor;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.websocket.DeploymentException;
import javax.websocket.EndpointConfig;
@ -80,12 +78,13 @@ public class JavaxWebSocketServerContainer
if (!(handler instanceof ServletContextHandler))
return null;
return (javax.websocket.WebSocketContainer)handler.getServletContext().getAttribute(ServerContainer.class.getName());
return (javax.websocket.WebSocketContainer)handler.getServletContext().getAttribute("javax.websocket.server.ServerContainer");
}
public static JavaxWebSocketServerContainer ensureContainer(ServletContext servletContext) throws ServletException
{
ContextHandler contextHandler = ContextHandler.getContextHandler(servletContext);
ContextHandler contextHandler = ServletContextHandler.getServletContextHandler(servletContext, "Javax Websocket");
JavaxWebSocketServerContainer container = contextHandler.getBean(JavaxWebSocketServerContainer.class);
if (container==null)
{
@ -116,7 +115,7 @@ public class JavaxWebSocketServerContainer
return container;
}
private final WebSocketMapping _webSocketMapping;
private final WebSocketMapping webSocketMapping;
private final JavaxWebSocketServerFrameHandlerFactory frameHandlerFactory;
private final Executor executor;
private final FrameHandler.ConfigurationCustomizer customizer = new FrameHandler.ConfigurationCustomizer();
@ -138,7 +137,7 @@ public class JavaxWebSocketServerContainer
client.getHttpClient().setExecutor(executor);
return client;
});
this._webSocketMapping = webSocketMapping;
this.webSocketMapping = webSocketMapping;
this.executor = executor;
this.frameHandlerFactory = new JavaxWebSocketServerFrameHandlerFactory(this);
}
@ -148,7 +147,7 @@ public class JavaxWebSocketServerContainer
{
ContextHandler contextHandler = (ContextHandler) context;
JavaxWebSocketServerContainer container = contextHandler.getBean(JavaxWebSocketServerContainer.class);
if (container==null)
if (container==this)
{
contextHandler.removeBean(container);
LifeCycle.stop(container);
@ -159,7 +158,7 @@ public class JavaxWebSocketServerContainer
@Override
public ByteBufferPool getBufferPool()
{
return this._webSocketMapping.getBufferPool();
return this.webSocketMapping.getBufferPool();
}
@Override
@ -171,7 +170,7 @@ public class JavaxWebSocketServerContainer
@Override
public WebSocketExtensionRegistry getExtensionRegistry()
{
return this._webSocketMapping.getExtensionRegistry();
return this.webSocketMapping.getExtensionRegistry();
}
@Override
@ -183,7 +182,7 @@ public class JavaxWebSocketServerContainer
@Override
public DecoratedObjectFactory getObjectFactory()
{
return this._webSocketMapping.getObjectFactory();
return this.webSocketMapping.getObjectFactory();
}
@Override
@ -269,10 +268,11 @@ public class JavaxWebSocketServerContainer
{
frameHandlerFactory.getMetadata(config.getEndpointClass(), config);
JavaxWebSocketCreator creator = new JavaxWebSocketCreator(this, config, this._webSocketMapping
JavaxWebSocketCreator creator = new JavaxWebSocketCreator(this, config, this.webSocketMapping
.getExtensionRegistry());
this._webSocketMapping.addMapping(new UriTemplatePathSpec(config.getPath()), creator, frameHandlerFactory, customizer);
this.webSocketMapping
.addMapping(new UriTemplatePathSpec(config.getPath()), creator, frameHandlerFactory, customizer);
}
@Override

View File

@ -20,11 +20,9 @@ package org.eclipse.jetty.websocket.javax.server;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Executor;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.annotation.HandlesTypes;
import javax.websocket.DeploymentException;
@ -33,8 +31,6 @@ import javax.websocket.server.ServerApplicationConfig;
import javax.websocket.server.ServerEndpoint;
import javax.websocket.server.ServerEndpointConfig;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.TypeUtil;
@ -126,7 +122,7 @@ public class JavaxWebSocketServerContainerInitializer implements ServletContaine
return;
}
JavaxWebSocketServerContainer container = configureContext((ServletContextHandler)(ContextHandler.getContextHandler(context)));
JavaxWebSocketServerContainer container = configureContext(ServletContextHandler.getServletContextHandler(context,"Javax WebSocket SCI"));
try (ThreadClassLoaderScope scope = new ThreadClassLoaderScope(context.getClassLoader()))
{

View File

@ -18,25 +18,9 @@
package org.eclipse.jetty.websocket.javax.tests.client;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.websocket.core.FrameHandler;
import org.eclipse.jetty.websocket.core.MessageHandler;
import org.eclipse.jetty.websocket.core.server.Negotiation;
import org.eclipse.jetty.websocket.javax.tests.CoreServer;
import org.eclipse.jetty.websocket.javax.tests.WSEventTracker;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import javax.websocket.ClientEndpoint;
import javax.websocket.ContainerProvider;
import javax.websocket.DecodeException;
import javax.websocket.Decoder;
import javax.websocket.EndpointConfig;
import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
@ -47,8 +31,25 @@ import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import javax.websocket.ClientEndpoint;
import javax.websocket.ContainerProvider;
import javax.websocket.DecodeException;
import javax.websocket.Decoder;
import javax.websocket.EndpointConfig;
import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.websocket.core.FrameHandler;
import org.eclipse.jetty.websocket.core.MessageHandler;
import org.eclipse.jetty.websocket.core.server.Negotiation;
import org.eclipse.jetty.websocket.javax.tests.CoreServer;
import org.eclipse.jetty.websocket.javax.tests.WSEventTracker;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
public class DecoderReaderManySmallTest
{

View File

@ -18,11 +18,14 @@
package org.eclipse.jetty.websocket.javax.tests.server.sockets;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import org.eclipse.jetty.websocket.core.WebSocketTimeoutException;
@ServerEndpoint(value = "/idle-onopen-socket")
public class IdleTimeoutOnOpenSocket
{
@ -37,4 +40,11 @@ public class IdleTimeoutOnOpenSocket
{
return msg;
}
@OnError
public void onError(Throwable cause)
{
if (!(cause instanceof WebSocketTimeoutException))
throw new RuntimeException(cause);
}
}

View File

@ -18,6 +18,23 @@
package org.eclipse.jetty.websocket.common;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.lang.annotation.Annotation;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.nio.ByteBuffer;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.websocket.api.InvalidWebSocketException;
import org.eclipse.jetty.websocket.api.Session;
@ -46,24 +63,21 @@ import org.eclipse.jetty.websocket.common.message.StringMessageSink;
import org.eclipse.jetty.websocket.common.util.ReflectUtils;
import org.eclipse.jetty.websocket.core.Frame;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.lang.annotation.Annotation;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.nio.ByteBuffer;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
/**
* Factory to create {@link JettyWebSocketFrameHandler} instances suitable for
* use with jetty-native websocket API.
* <p>
* Will create a {@link FrameHandler} suitable for use with classes/objects that:
* </p>
* <ul>
* <li>Is &#64;{@link org.eclipse.jetty.websocket.api.annotations.WebSocket} annotated</li>
* <li>Extends {@link org.eclipse.jetty.websocket.api.WebSocketAdapter}</li>
* <li>Implements {@link org.eclipse.jetty.websocket.api.WebSocketListener}</li>
* <li>Implements {@link org.eclipse.jetty.websocket.api.WebSocketConnectionListener}</li>
* <li>Implements {@link org.eclipse.jetty.websocket.api.WebSocketPartialListener}</li>
* <li>Implements {@link org.eclipse.jetty.websocket.api.WebSocketPingPongListener}</li>
* <li>Implements {@link org.eclipse.jetty.websocket.api.WebSocketFrameListener}</li>
* </ul>
*/
public class JettyWebSocketFrameHandlerFactory extends ContainerLifeCycle
{

View File

@ -18,7 +18,14 @@
package org.eclipse.jetty.websocket.server;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.websocket.common.JettyWebSocketFrameHandlerFactory;
import org.eclipse.jetty.websocket.core.FrameHandler;
@ -28,18 +35,15 @@ import org.eclipse.jetty.websocket.servlet.FrameHandlerFactory;
import org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest;
import org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import javax.servlet.ServletContext;
public class JettyServerFrameHandlerFactory
extends JettyWebSocketFrameHandlerFactory
implements FrameHandlerFactory, LifeCycle.Listener
{
public static JettyServerFrameHandlerFactory ensureFactory(ServletContext servletContext)
throws ServletException
{
ContextHandler contextHandler = ContextHandler.getContextHandler(servletContext);
ContextHandler contextHandler = ServletContextHandler.getServletContextHandler(servletContext, "Jetty Websocket");
JettyServerFrameHandlerFactory factory = contextHandler.getBean(JettyServerFrameHandlerFactory.class);
if (factory == null)
{

View File

@ -18,14 +18,19 @@
package org.eclipse.jetty.websocket.servlet;
import static javax.servlet.http.HttpServletResponse.SC_SERVICE_UNAVAILABLE;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.pathmap.MappedResource;
import org.eclipse.jetty.http.pathmap.PathMappings;
import org.eclipse.jetty.http.pathmap.PathSpec;
@ -36,9 +41,6 @@ import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.io.MappedByteBufferPool;
import org.eclipse.jetty.io.RuntimeIOException;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.util.DecoratedObjectFactory;
import org.eclipse.jetty.util.component.Dumpable;
import org.eclipse.jetty.util.component.LifeCycle;
@ -50,18 +52,6 @@ import org.eclipse.jetty.websocket.core.server.Handshaker;
import org.eclipse.jetty.websocket.core.server.Negotiation;
import org.eclipse.jetty.websocket.core.server.WebSocketNegotiator;
import static javax.servlet.http.HttpServletResponse.SC_SERVICE_UNAVAILABLE;
import javax.servlet.DispatcherType;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Mapping of pathSpec to a tupple of {@link WebSocketCreator}, {@link FrameHandlerFactory} and
* {@link org.eclipse.jetty.websocket.core.FrameHandler.Customizer}.
@ -119,8 +109,11 @@ public class WebSocketMapping implements Dumpable, LifeCycle.Listener
{
ContextHandler contextHandler = (ContextHandler) context;
WebSocketMapping mapping = contextHandler.getBean(WebSocketMapping.class);
if (mapping == null)
if (mapping == this)
{
contextHandler.removeBean(mapping);
mappings.reset();
}
}
/**

View File

@ -29,6 +29,7 @@ import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.pathmap.PathSpec;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.core.FrameHandler;
@ -153,10 +154,9 @@ public abstract class WebSocketServlet extends HttpServlet
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
// Typically this servlet is used together with the WebSocketUpgradeFilter,
// Often this servlet is used together with the WebSocketUpgradeFilter,
// so upgrade requests will normally be upgraded by the filter. But we
// can do it here as well if for some reason the filter did not match.
if (mapping.upgrade(req, resp, null))
return;
@ -256,7 +256,7 @@ public abstract class WebSocketServlet extends HttpServlet
{
// TODO a bit fragile. This code knows that only the JettyFHF is added directly as a been
ServletContext servletContext = getServletContext();
ContextHandler contextHandler = ContextHandler.getContextHandler(servletContext);
ContextHandler contextHandler = ServletContextHandler.getServletContextHandler(servletContext, "WebSocketServlet");
FrameHandlerFactory frameHandlerFactory = contextHandler.getBean(FrameHandlerFactory.class);
if (frameHandlerFactory==null)