Issue #3730 - Removing HttpContainerScope

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2019-12-18 14:38:37 -06:00
parent 7d65183e2c
commit 9da1820f13
No known key found for this signature in database
GPG Key ID: 2D0E1FB8FE4B68B4
3 changed files with 58 additions and 146 deletions

View File

@ -52,7 +52,6 @@ import org.eclipse.jetty.websocket.api.WebSocketBehavior;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.api.extensions.ExtensionFactory;
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
import org.eclipse.jetty.websocket.client.HttpContainerScope;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.jetty.websocket.client.io.UpgradeListener;
import org.eclipse.jetty.websocket.common.WebSocketSession;
@ -124,7 +123,7 @@ public class ClientContainer extends ContainerLifeCycle implements WebSocketCont
*/
public ClientContainer(final HttpClient httpClient)
{
this(new HttpContainerScope(httpClient));
this(new WebSocketClient(httpClient));
}
/**

View File

@ -1,119 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.websocket.client;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Executor;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.util.DecoratedObjectFactory;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.common.WebSocketSessionListener;
import org.eclipse.jetty.websocket.common.scopes.WebSocketContainerScope;
/**
* A simple Scope that is focused around a HttpClient, DecoratedObjectFactory, and Client WebSocketPolicy.
*/
public class HttpContainerScope extends ContainerLifeCycle implements WebSocketContainerScope
{
private final HttpClient httpClient;
private final DecoratedObjectFactory decoratedObjectFactory;
private final WebSocketPolicy webSocketPolicy;
private List<WebSocketSessionListener> sessionListeners = new ArrayList<>();
public HttpContainerScope(HttpClient httpClient)
{
this(httpClient, new DecoratedObjectFactory());
}
public HttpContainerScope(HttpClient httpClient, DecoratedObjectFactory decoratedObjectFactory)
{
this.httpClient = Objects.requireNonNull(httpClient, "HttpClient");
this.decoratedObjectFactory = decoratedObjectFactory != null ? decoratedObjectFactory : new DecoratedObjectFactory();
this.webSocketPolicy = WebSocketPolicy.newClientPolicy();
}
public HttpContainerScope(SslContextFactory sslContextFactory, Executor executor, ByteBufferPool bufferPool, DecoratedObjectFactory decoratedObjectFactory)
{
this.httpClient = new HttpClient(sslContextFactory);
this.httpClient.setExecutor(executor);
this.httpClient.setByteBufferPool(bufferPool);
this.decoratedObjectFactory = decoratedObjectFactory != null ? decoratedObjectFactory : new DecoratedObjectFactory();
this.webSocketPolicy = WebSocketPolicy.newClientPolicy();
}
public HttpClient getHttpClient()
{
return httpClient;
}
@Override
public ByteBufferPool getBufferPool()
{
return httpClient.getByteBufferPool();
}
@Override
public Executor getExecutor()
{
return httpClient.getExecutor();
}
@Override
public DecoratedObjectFactory getObjectFactory()
{
return decoratedObjectFactory;
}
@Override
public WebSocketPolicy getPolicy()
{
return webSocketPolicy;
}
@Override
public SslContextFactory getSslContextFactory()
{
return httpClient.getSslContextFactory();
}
@Override
public void addSessionListener(WebSocketSessionListener listener)
{
this.sessionListeners.add(listener);
}
@Override
public void removeSessionListener(WebSocketSessionListener listener)
{
this.sessionListeners.remove(listener);
}
@Override
public Collection<WebSocketSessionListener> getSessionListeners()
{
return sessionListeners;
}
}

View File

@ -89,8 +89,8 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont
*/
public WebSocketClient()
{
this(new HttpContainerScope(HttpClientProvider.get(null)));
// Add as bean, as HttpClient was created in this class
this(null, HttpClientProvider.get(null));
// Add as bean, as HttpClient was created in this constructor
addBean(this.httpClient);
}
@ -102,7 +102,7 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont
public WebSocketClient(HttpClient httpClient)
{
// Use external HttpClient
this(new HttpContainerScope(Objects.requireNonNull(httpClient)));
this(null, Objects.requireNonNull(httpClient));
}
/**
@ -114,7 +114,7 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont
public WebSocketClient(HttpClient httpClient, DecoratedObjectFactory objectFactory)
{
// Use external HttpClient
this(new HttpContainerScope(Objects.requireNonNull(httpClient), objectFactory));
this(objectFactory, Objects.requireNonNull(httpClient));
}
/**
@ -126,8 +126,8 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont
@Deprecated
public WebSocketClient(SslContextFactory sslContextFactory)
{
this(new HttpContainerScope(sslContextFactory, null, null, null));
// Add as bean, as HttpClient was created in this class
this(null, newHttpClient(sslContextFactory, null, null));
// Add as bean, as HttpClient was created in this constructor
addBean(this.httpClient);
}
@ -140,8 +140,8 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont
@Deprecated
public WebSocketClient(Executor executor)
{
this(new HttpContainerScope(null, executor, null, null));
// Add as bean, as HttpClient was created in this class
this(null, newHttpClient(null, executor, null));
// Add as bean, as HttpClient was created in this constructor
addBean(this.httpClient);
}
@ -154,8 +154,8 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont
@Deprecated
public WebSocketClient(ByteBufferPool bufferPool)
{
this(new HttpContainerScope(null, null, bufferPool, null));
// Add as bean, as HttpClient was created in this class
this(null, newHttpClient(null, null, bufferPool));
// Add as bean, as HttpClient was created in this constructor
addBean(this.httpClient);
}
@ -169,8 +169,8 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont
@Deprecated
public WebSocketClient(SslContextFactory sslContextFactory, Executor executor)
{
this(new HttpContainerScope(sslContextFactory, executor, null, null));
// Add as bean, as HttpClient was created in this class
this(null, newHttpClient(sslContextFactory, executor, null));
// Add as bean, as HttpClient was created in this constructor
addBean(this.httpClient);
}
@ -182,7 +182,9 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont
*/
public WebSocketClient(WebSocketContainerScope scope)
{
this(scope, null, null, null);
this(scope.getObjectFactory(), newHttpClient(scope.getSslContextFactory(), scope.getExecutor(), scope.getBufferPool()));
// Add as bean, as HttpClient was created in this constructor
addBean(this.httpClient);
}
/**
@ -197,8 +199,18 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont
@Deprecated
public WebSocketClient(WebSocketContainerScope scope, SslContextFactory sslContextFactory)
{
this(new HttpContainerScope(sslContextFactory, scope.getExecutor(), scope.getBufferPool(), scope.getObjectFactory()));
// Add as bean, as HttpClient was created in this class
/* This is constructor is in an awful place, it's got a signature that has a scope,
* a concept that javax.websocket ServerContainer uses to share its buffer pools / executors / etc
* with the underlying HttpClient.
* This means that the constructor should go through the HttpClientProvider.get(scope) behaviors.
* but it also has an arbitrary SslContextFactory parameter, which isn't in the scope that
* HttpClientProvider uses.
* Since this isn't used by Jetty's implementation of the javax.websocket ServerContainer
* this behavior has been changed to be non-scoped so as to be able to use the provided
* SslContextFactory for the underlying HttpClient instance.
*/
this(scope.getObjectFactory(), newHttpClient(sslContextFactory, scope.getExecutor(), scope.getBufferPool()));
// Add as bean, as HttpClient was created in this constructor
addBean(this.httpClient);
}
@ -214,7 +226,7 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont
@Deprecated
public WebSocketClient(SslContextFactory sslContextFactory, Executor executor, ByteBufferPool bufferPool)
{
this(new HttpContainerScope(sslContextFactory, executor, bufferPool, null));
this(null, newHttpClient(sslContextFactory, executor, bufferPool));
// Add as bean, as HttpClient was created in this class
addBean(this.httpClient);
}
@ -231,8 +243,11 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont
@Deprecated
public WebSocketClient(final WebSocketContainerScope scope, EventDriverFactory eventDriverFactory, SessionFactory sessionFactory)
{
/* Nothing in Jetty uses this constructor anymore.
* It's left in for backwards compat reasons.
*/
this(scope, eventDriverFactory, sessionFactory, HttpClientProvider.get(scope));
// Add as bean, as HttpClient was created in this class
// Add as bean, as HttpClient was created in this constructor
addBean(this.httpClient);
}
@ -250,7 +265,7 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont
if (httpClient == null)
{
this.httpClient = HttpClientProvider.get(scope);
// Add as bean, as HttpClient was created in this class
// Add as bean, as HttpClient was created in this constructor
addBean(this.httpClient);
}
else
@ -263,8 +278,9 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont
// Ensure we get a Client version of the policy.
this.policy = scope.getPolicy().delegateAs(WebSocketBehavior.CLIENT);
// Support Late Binding of Object Factory (for CDI)
// Support Late Binding of DecoratedObjectFactory (that CDI establishes in its own servlet context listeners)
this.objectFactorySupplier = () -> scope.getObjectFactory();
this.extensionRegistry = new WebSocketExtensionFactory(this);
addBean(extensionRegistry);
@ -272,17 +288,25 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont
this.sessionFactory = sessionFactory == null ? new WebSocketSessionFactory(this) : sessionFactory;
}
WebSocketClient(final HttpContainerScope httpContainerScope)
/**
* Internal constructor (not public)
*
* @param decoratedObjectFactory the decorated Object Factory to use
* @param httpClient the HttpClient underpinnings
*/
WebSocketClient(DecoratedObjectFactory decoratedObjectFactory, HttpClient httpClient)
{
this.httpClient = httpContainerScope.getHttpClient();
this.httpClient = httpClient;
addBean(sessionTracker);
addSessionListener(sessionTracker);
// Ensure we get a Client version of the policy.
this.policy = httpContainerScope.getPolicy();
// Support Late Binding of Object Factory (for CDI)
this.objectFactorySupplier = httpContainerScope::getObjectFactory;
// Always a pristine Client policy
this.policy = WebSocketPolicy.newClientPolicy();
// We do not support late binding of DecoratedObjectFactory in this WebSocketClient
DecoratedObjectFactory objectFactory = decoratedObjectFactory == null ? new DecoratedObjectFactory() : decoratedObjectFactory;
this.objectFactorySupplier = () -> objectFactory;
this.extensionRegistry = new WebSocketExtensionFactory(this);
addBean(extensionRegistry);
@ -290,6 +314,14 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont
this.sessionFactory = new WebSocketSessionFactory(this);
}
public static HttpClient newHttpClient(SslContextFactory sslContextFactory, Executor executor, ByteBufferPool bufferPool)
{
HttpClient httpClient = new HttpClient(sslContextFactory);
httpClient.setExecutor(executor);
httpClient.setByteBufferPool(bufferPool);
return httpClient;
}
public Future<Session> connect(Object websocket, URI toUri) throws IOException
{
ClientUpgradeRequest request = new ClientUpgradeRequest(toUri);