Issue #1528 - WebSocketClient can use jetty-websocket-httpclient.xml

+ Will use XML resource (if jetty-xml is present, and resource found)
  to load the HttpClient configuration that should be used by the
  new WebSocketClient() instance.
This commit is contained in:
Joakim Erdfelt 2017-05-18 15:06:22 -07:00
parent 9b4bfbc680
commit 75101dfa0c
7 changed files with 325 additions and 19 deletions

View File

@ -20,6 +20,12 @@
<artifactId>jetty-util</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-xml</artifactId>
<version>${project.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-io</artifactId>

View File

@ -0,0 +1,58 @@
//
// ========================================================================
// Copyright (c) 1995-2017 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.concurrent.Executor;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.websocket.common.scopes.WebSocketContainerScope;
class DefaultHttpClientProvider
{
public static HttpClient newHttpClient(WebSocketContainerScope scope)
{
SslContextFactory sslContextFactory = null;
Executor executor = null;
if (scope != null)
{
sslContextFactory = scope.getSslContextFactory();
executor = scope.getExecutor();
}
if (sslContextFactory == null)
{
sslContextFactory = new SslContextFactory();
}
HttpClient client = new HttpClient(sslContextFactory);
if (executor == null)
{
QueuedThreadPool threadPool = new QueuedThreadPool();
String name = "WebSocketClient@" + client.hashCode();
threadPool.setName(name);
threadPool.setDaemon(true);
executor = threadPool;
}
client.setExecutor(executor);
return client;
}
}

View File

@ -0,0 +1,51 @@
//
// ========================================================================
// Copyright (c) 1995-2017 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.lang.reflect.Method;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.websocket.common.scopes.WebSocketContainerScope;
public final class HttpClientProvider
{
public static HttpClient get(WebSocketContainerScope scope)
{
try
{
if (Class.forName("org.eclipse.jetty.xml.XmlConfiguration") != null)
{
Class<?> xmlClazz = Class.forName("org.eclipse.jetty.websocket.client.XmlBasedHttpClientProvider");
Method getMethod = xmlClazz.getMethod("get", WebSocketContainerScope.class);
Object ret = getMethod.invoke(null, scope);
if ((ret != null) && (ret instanceof HttpClient))
{
return (HttpClient) ret;
}
}
}
catch (Throwable ignore)
{
Log.getLogger(HttpClientProvider.class).warn(ignore);
}
return DefaultHttpClientProvider.newHttpClient(scope);
}
}

View File

@ -39,7 +39,6 @@ import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.util.thread.Scheduler;
import org.eclipse.jetty.util.thread.ShutdownThread;
import org.eclipse.jetty.websocket.api.Session;
@ -84,7 +83,7 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont
public WebSocketClient()
{
// Create synthetic HttpClient
this(new HttpClient());
this(HttpClientProvider.get(null));
addBean(this.httpClient);
}
@ -262,23 +261,8 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont
}
this.containerScope = clientScope;
SslContextFactory sslContextFactory = scope.getSslContextFactory();
if(sslContextFactory == null)
{
sslContextFactory = new SslContextFactory();
}
this.httpClient = new HttpClient(sslContextFactory);
Executor executor = scope.getExecutor();
if (executor == null)
{
QueuedThreadPool threadPool = new QueuedThreadPool();
String name = "WebSocketClient@" + hashCode();
threadPool.setName(name);
threadPool.setDaemon(true);
executor = threadPool;
}
this.httpClient.setExecutor(executor);
this.httpClient = HttpClientProvider.get(scope);
addBean(this.httpClient);
this.extensionRegistry = new WebSocketExtensionFactory(containerScope);

View File

@ -0,0 +1,51 @@
//
// ========================================================================
// Copyright (c) 1995-2017 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.io.InputStream;
import java.net.URL;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.websocket.common.scopes.WebSocketContainerScope;
import org.eclipse.jetty.xml.XmlConfiguration;
class XmlBasedHttpClientProvider
{
public static HttpClient get(@SuppressWarnings("unused") WebSocketContainerScope scope)
{
URL resource = Thread.currentThread().getContextClassLoader().getResource("jetty-websocket-httpclient.xml");
if (resource == null)
{
return null;
}
try (InputStream in = resource.openStream())
{
XmlConfiguration configuration = new XmlConfiguration(in);
return (HttpClient) configuration.configure();
}
catch (Throwable t)
{
Log.getLogger(XmlBasedHttpClientProvider.class).warn("Unable to load: " + resource, t);
}
return null;
}
}

View File

@ -0,0 +1,133 @@
//
// ========================================================================
// Copyright (c) 1995-2017 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 static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executor;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.util.thread.ThreadClassLoaderScope;
import org.junit.Test;
public class HttpClientInitTest
{
@Test
public void testDefaultInit() throws Exception
{
WebSocketClient client = new WebSocketClient();
try
{
client.start();
HttpClient httpClient = client.getHttpClient();
assertThat("HttpClient exists", httpClient, notNullValue());
assertThat("HttpClient is started", httpClient.isStarted(), is(true));
Executor executor = httpClient.getExecutor();
assertThat("Executor exists", executor, notNullValue());
assertThat("Executor instanceof", executor, instanceOf(QueuedThreadPool.class));
QueuedThreadPool threadPool = (QueuedThreadPool) executor;
assertThat("QueuedThreadPool.name", threadPool.getName(), startsWith("WebSocketClient@"));
}
finally
{
client.stop();
}
}
@Test
public void testManualInit() throws Exception
{
HttpClient http = new HttpClient();
{
QueuedThreadPool threadPool = new QueuedThreadPool();
threadPool.setName("ManualWSClient@" + http.hashCode());
http.setExecutor(threadPool);
http.setConnectTimeout(7777);
}
WebSocketClient client = new WebSocketClient(http);
client.addBean(http);
try
{
client.start();
HttpClient httpClient = client.getHttpClient();
assertThat("HttpClient exists", httpClient, notNullValue());
assertThat("HttpClient is started", httpClient.isStarted(), is(true));
assertThat("HttpClient.connectTimeout", httpClient.getConnectTimeout(), is(7777L));
Executor executor = httpClient.getExecutor();
assertThat("Executor exists", executor, notNullValue());
assertThat("Executor instanceof", executor, instanceOf(QueuedThreadPool.class));
QueuedThreadPool threadPool = (QueuedThreadPool) executor;
assertThat("QueuedThreadPool.name", threadPool.getName(), startsWith("ManualWSClient@"));
}
finally
{
client.stop();
}
}
@Test
public void testXmlResourceInit() throws Exception
{
ClassLoader parent = Thread.currentThread().getContextClassLoader();
URL urls[] = new URL[]{
MavenTestingUtils.getTestResourceDir("httpclient/simple").toURI().toURL()
};
URLClassLoader classLoader = new URLClassLoader(urls, parent);
try (ThreadClassLoaderScope scope = new ThreadClassLoaderScope(classLoader))
{
WebSocketClient client = new WebSocketClient();
try
{
client.start();
HttpClient httpClient = client.getHttpClient();
assertThat("HttpClient exists", httpClient, notNullValue());
assertThat("HttpClient is started", httpClient.isStarted(), is(true));
assertThat("HttpClient.connectTimeout", httpClient.getConnectTimeout(), is(5555L));
SslContextFactory sslContextFactory = httpClient.getSslContextFactory();
List<String> actualExcludedProtocols = Arrays.asList(sslContextFactory.getExcludeProtocols());
assertThat("HttpClient.sslContextFactory.excludedProtocols", actualExcludedProtocols, hasItem("TLS/0.1"));
Executor executor = httpClient.getExecutor();
assertThat("Executor exists", executor, notNullValue());
assertThat("Executor instanceof", executor, instanceOf(QueuedThreadPool.class));
QueuedThreadPool threadPool = (QueuedThreadPool) executor;
assertThat("QueuedThreadPool.name", threadPool.getName(), startsWith("XmlBasedClient@"));
}
finally
{
client.stop();
}
}
}
}

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure class="org.eclipse.jetty.client.HttpClient">
<Arg>
<New class="org.eclipse.jetty.util.ssl.SslContextFactory">
<Set name="trustAll" type="java.lang.Boolean">false</Set>
<Call name="addExcludeProtocols">
<Arg>
<Array type="java.lang.String">
<Item>TLS/0.1</Item>
</Array>
</Arg>
</Call>
</New>
</Arg>
<Set name="connectTimeout">5555</Set>
<Set name="executor">
<New class="org.eclipse.jetty.util.thread.QueuedThreadPool">
<Set name="name">XmlBasedClient@</Set>
</New>
</Set>
</Configure>