Merge pull request #3357 from lachlan-roberts/jetty-10.0.x-3341-HttpClientProvider
Issue #3341 - WebSocket HttpClientProvider
This commit is contained in:
commit
42290828a6
|
@ -32,13 +32,6 @@
|
||||||
<artifactId>jetty-client</artifactId>
|
<artifactId>jetty-client</artifactId>
|
||||||
<version>${project.version}</version>
|
<version>${project.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>org.eclipse.jetty</groupId>
|
|
||||||
<artifactId>jetty-xml</artifactId>
|
|
||||||
<version>${project.version}</version>
|
|
||||||
<optional>true</optional>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.eclipse.jetty</groupId>
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
<artifactId>jetty-server</artifactId>
|
<artifactId>jetty-server</artifactId>
|
||||||
|
|
|
@ -27,7 +27,4 @@ module org.eclipse.jetty.websocket.jetty.client
|
||||||
requires org.eclipse.jetty.websocket.jetty.api;
|
requires org.eclipse.jetty.websocket.jetty.api;
|
||||||
requires org.eclipse.jetty.websocket.core;
|
requires org.eclipse.jetty.websocket.core;
|
||||||
requires org.eclipse.jetty.websocket.jetty.common;
|
requires org.eclipse.jetty.websocket.jetty.common;
|
||||||
|
|
||||||
// Only required if using XmlBasedHttpClientProvider.
|
|
||||||
requires static org.eclipse.jetty.xml;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,50 +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.impl;
|
|
||||||
|
|
||||||
import org.eclipse.jetty.client.HttpClient;
|
|
||||||
import org.eclipse.jetty.util.log.Log;
|
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
|
|
||||||
public final class HttpClientProvider
|
|
||||||
{
|
|
||||||
public static HttpClient get()
|
|
||||||
{
|
|
||||||
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");
|
|
||||||
Object ret = getMethod.invoke(null);
|
|
||||||
if ((ret != null) && (ret instanceof HttpClient))
|
|
||||||
{
|
|
||||||
return (HttpClient)ret;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Throwable ignore)
|
|
||||||
{
|
|
||||||
Log.getLogger(HttpClientProvider.class).ignore(ignore);
|
|
||||||
}
|
|
||||||
|
|
||||||
return DefaultHttpClientProvider.newHttpClient();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -46,7 +46,7 @@ public class HttpClientInitTest
|
||||||
assertThat("Executor exists", executor, notNullValue());
|
assertThat("Executor exists", executor, notNullValue());
|
||||||
assertThat("Executor instanceof", executor, instanceOf(QueuedThreadPool.class));
|
assertThat("Executor instanceof", executor, instanceOf(QueuedThreadPool.class));
|
||||||
QueuedThreadPool threadPool = (QueuedThreadPool)executor;
|
QueuedThreadPool threadPool = (QueuedThreadPool)executor;
|
||||||
assertThat("QueuedThreadPool.name", threadPool.getName(), startsWith("Jetty-WebSocketClient@"));
|
assertThat("QueuedThreadPool.name", threadPool.getName(), startsWith("WebSocketClient@"));
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
|
|
|
@ -31,6 +31,12 @@
|
||||||
<artifactId>jetty-http</artifactId>
|
<artifactId>jetty-http</artifactId>
|
||||||
<version>${project.version}</version>
|
<version>${project.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
|
<artifactId>jetty-xml</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.eclipse.jetty</groupId>
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
<artifactId>jetty-client</artifactId>
|
<artifactId>jetty-client</artifactId>
|
||||||
|
|
|
@ -39,6 +39,9 @@ module org.eclipse.jetty.websocket.core
|
||||||
requires org.eclipse.jetty.server;
|
requires org.eclipse.jetty.server;
|
||||||
requires org.eclipse.jetty.util;
|
requires org.eclipse.jetty.util;
|
||||||
|
|
||||||
|
// Only required if using XmlBasedHttpClientProvider.
|
||||||
|
requires static org.eclipse.jetty.xml;
|
||||||
|
|
||||||
uses Extension;
|
uses Extension;
|
||||||
|
|
||||||
provides Extension with
|
provides Extension with
|
||||||
|
|
|
@ -16,23 +16,43 @@
|
||||||
// ========================================================================
|
// ========================================================================
|
||||||
//
|
//
|
||||||
|
|
||||||
package org.eclipse.jetty.websocket.client.impl;
|
package org.eclipse.jetty.websocket.core.client;
|
||||||
|
|
||||||
import org.eclipse.jetty.client.HttpClient;
|
import org.eclipse.jetty.client.HttpClient;
|
||||||
|
import org.eclipse.jetty.util.log.Log;
|
||||||
import org.eclipse.jetty.util.ssl.SslContextFactory;
|
import org.eclipse.jetty.util.ssl.SslContextFactory;
|
||||||
import org.eclipse.jetty.util.thread.QueuedThreadPool;
|
import org.eclipse.jetty.util.thread.QueuedThreadPool;
|
||||||
|
|
||||||
class DefaultHttpClientProvider
|
public interface HttpClientProvider
|
||||||
{
|
{
|
||||||
public static HttpClient newHttpClient()
|
static HttpClient get()
|
||||||
{
|
{
|
||||||
SslContextFactory sslContextFactory = new SslContextFactory();
|
try
|
||||||
HttpClient client = new HttpClient(sslContextFactory);
|
{
|
||||||
|
HttpClientProvider xmlProvider = new XmlHttpClientProvider();
|
||||||
|
HttpClient client = xmlProvider.newHttpClient();
|
||||||
|
if (client != null)
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
catch (Throwable ignore)
|
||||||
|
{
|
||||||
|
Log.getLogger(HttpClientProvider.class).ignore(ignore);
|
||||||
|
}
|
||||||
|
|
||||||
|
return HttpClientProvider.newDefaultHttpClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static HttpClient newDefaultHttpClient()
|
||||||
|
{
|
||||||
|
HttpClient client = new HttpClient(new SslContextFactory());
|
||||||
QueuedThreadPool threadPool = new QueuedThreadPool();
|
QueuedThreadPool threadPool = new QueuedThreadPool();
|
||||||
String name = "WebSocketClient@" + client.hashCode();
|
threadPool.setName("WebSocketClient@" + client.hashCode());
|
||||||
threadPool.setName(name);
|
|
||||||
threadPool.setDaemon(true);
|
|
||||||
client.setExecutor(threadPool);
|
client.setExecutor(threadPool);
|
||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default HttpClient newHttpClient()
|
||||||
|
{
|
||||||
|
return newDefaultHttpClient();
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -20,6 +20,7 @@ package org.eclipse.jetty.websocket.core.client;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
import org.eclipse.jetty.client.HttpClient;
|
import org.eclipse.jetty.client.HttpClient;
|
||||||
|
@ -27,7 +28,6 @@ import org.eclipse.jetty.util.DecoratedObjectFactory;
|
||||||
import org.eclipse.jetty.util.component.ContainerLifeCycle;
|
import org.eclipse.jetty.util.component.ContainerLifeCycle;
|
||||||
import org.eclipse.jetty.util.log.Log;
|
import org.eclipse.jetty.util.log.Log;
|
||||||
import org.eclipse.jetty.util.log.Logger;
|
import org.eclipse.jetty.util.log.Logger;
|
||||||
import org.eclipse.jetty.util.ssl.SslContextFactory;
|
|
||||||
import org.eclipse.jetty.util.thread.ShutdownThread;
|
import org.eclipse.jetty.util.thread.ShutdownThread;
|
||||||
import org.eclipse.jetty.websocket.core.ExtensionConfig;
|
import org.eclipse.jetty.websocket.core.ExtensionConfig;
|
||||||
import org.eclipse.jetty.websocket.core.FrameHandler;
|
import org.eclipse.jetty.websocket.core.FrameHandler;
|
||||||
|
@ -62,11 +62,8 @@ public class WebSocketCoreClient extends ContainerLifeCycle implements FrameHand
|
||||||
public WebSocketCoreClient(HttpClient httpClient, FrameHandler.Customizer customizer)
|
public WebSocketCoreClient(HttpClient httpClient, FrameHandler.Customizer customizer)
|
||||||
{
|
{
|
||||||
if (httpClient == null)
|
if (httpClient == null)
|
||||||
{
|
httpClient = Objects.requireNonNull(HttpClientProvider.get());
|
||||||
httpClient = new HttpClient(new SslContextFactory());
|
|
||||||
httpClient.getSslContextFactory().setEndpointIdentificationAlgorithm("HTTPS");
|
|
||||||
httpClient.setName(String.format("%s@%x",getClass().getSimpleName(),hashCode()));
|
|
||||||
}
|
|
||||||
this.httpClient = httpClient;
|
this.httpClient = httpClient;
|
||||||
this.extensionRegistry = new WebSocketExtensionRegistry();
|
this.extensionRegistry = new WebSocketExtensionRegistry();
|
||||||
this.objectFactory = new DecoratedObjectFactory();
|
this.objectFactory = new DecoratedObjectFactory();
|
||||||
|
|
|
@ -16,18 +16,19 @@
|
||||||
// ========================================================================
|
// ========================================================================
|
||||||
//
|
//
|
||||||
|
|
||||||
package org.eclipse.jetty.websocket.client.impl;
|
package org.eclipse.jetty.websocket.core.client;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
import org.eclipse.jetty.client.HttpClient;
|
import org.eclipse.jetty.client.HttpClient;
|
||||||
import org.eclipse.jetty.util.log.Log;
|
import org.eclipse.jetty.util.log.Log;
|
||||||
import org.eclipse.jetty.xml.XmlConfiguration;
|
import org.eclipse.jetty.xml.XmlConfiguration;
|
||||||
|
|
||||||
import java.io.InputStream;
|
class XmlHttpClientProvider implements HttpClientProvider
|
||||||
import java.net.URL;
|
|
||||||
|
|
||||||
class XmlBasedHttpClientProvider
|
|
||||||
{
|
{
|
||||||
public static HttpClient get()
|
@Override
|
||||||
|
public HttpClient newHttpClient()
|
||||||
{
|
{
|
||||||
URL resource = Thread.currentThread().getContextClassLoader().getResource("jetty-websocket-httpclient.xml");
|
URL resource = Thread.currentThread().getContextClassLoader().getResource("jetty-websocket-httpclient.xml");
|
||||||
if (resource == null)
|
if (resource == null)
|
||||||
|
@ -42,7 +43,7 @@ class XmlBasedHttpClientProvider
|
||||||
}
|
}
|
||||||
catch (Throwable t)
|
catch (Throwable t)
|
||||||
{
|
{
|
||||||
Log.getLogger(XmlBasedHttpClientProvider.class).warn("Unable to load: " + resource, t);
|
Log.getLogger(XmlHttpClientProvider.class).warn("Unable to load: " + resource, t);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
Loading…
Reference in New Issue