mirror of
https://github.com/danieldemus/openhab-core.git
synced 2025-01-10 21:31:53 +01:00
Add param sslContextFactory when creating a HTTP or web socket client (#3356)
* Add param sslContextFactory when creating a HTTP or web socket client Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
parent
1cad11a1ba
commit
f977a59ebe
@ -13,7 +13,9 @@
|
||||
package org.openhab.core.io.net.http;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.eclipse.jetty.client.HttpClient;
|
||||
import org.eclipse.jetty.util.ssl.SslContextFactory;
|
||||
|
||||
/**
|
||||
* Factory class to create Jetty http clients
|
||||
@ -39,6 +41,22 @@ public interface HttpClientFactory {
|
||||
*/
|
||||
HttpClient createHttpClient(String consumerName);
|
||||
|
||||
/**
|
||||
* Creates a new Jetty http client.
|
||||
* The returned client is not started yet. You have to start it yourself before using.
|
||||
* Don't forget to stop a started client again after its usage.
|
||||
* The client lifecycle should be the same as for your service.
|
||||
* DO NOT CREATE NEW CLIENTS FOR EACH REQUEST!
|
||||
*
|
||||
* @param consumerName the for identifying the consumer in the Jetty thread pool.
|
||||
* Must be between 4 and 20 characters long and must contain only the following characters [a-zA-Z0-9-_]
|
||||
* @param sslContextFactory the SSL factory managing TLS encryption
|
||||
* @return the Jetty client
|
||||
* @throws NullPointerException if {@code consumerName} is {@code null}
|
||||
* @throws IllegalArgumentException if {@code consumerName} is invalid
|
||||
*/
|
||||
HttpClient createHttpClient(String consumerName, @Nullable SslContextFactory sslContextFactory);
|
||||
|
||||
/**
|
||||
* Returns the shared Jetty http client. You must not call any setter methods or {@code stop()} on it.
|
||||
* The returned client is already started.
|
||||
|
@ -13,6 +13,8 @@
|
||||
package org.openhab.core.io.net.http;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.eclipse.jetty.util.ssl.SslContextFactory;
|
||||
import org.eclipse.jetty.websocket.client.WebSocketClient;
|
||||
|
||||
/**
|
||||
@ -38,6 +40,22 @@ public interface WebSocketFactory {
|
||||
*/
|
||||
WebSocketClient createWebSocketClient(String consumerName);
|
||||
|
||||
/**
|
||||
* Creates a new Jetty web socket client.
|
||||
* The returned client is not started yet. You have to start it yourself before using.
|
||||
* Don't forget to stop a started client again after its usage.
|
||||
* The client lifecycle should be the same as for your service.
|
||||
* DO NOT CREATE NEW CLIENTS FOR EACH REQUEST!
|
||||
*
|
||||
* @param consumerName the for identifying the consumer in the Jetty thread pool.
|
||||
* Must be between 4 and 20 characters long and must contain only the following characters [a-zA-Z0-9-_]
|
||||
* @param sslContextFactory the SSL factory managing TLS encryption
|
||||
* @return the Jetty client
|
||||
* @throws NullPointerException if {@code consumerName} is {@code null}
|
||||
* @throws IllegalArgumentException if {@code consumerName} is invalid
|
||||
*/
|
||||
WebSocketClient createWebSocketClient(String consumerName, @Nullable SslContextFactory sslContextFactory);
|
||||
|
||||
/**
|
||||
* Returns a shared Jetty web socket client. You must not call any setter methods or {@code stop()} on it.
|
||||
* The returned client is already started.
|
||||
|
@ -124,16 +124,26 @@ public class WebClientFactoryImpl implements HttpClientFactory, WebSocketFactory
|
||||
|
||||
@Override
|
||||
public HttpClient createHttpClient(String consumerName) {
|
||||
return createHttpClient(consumerName, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpClient createHttpClient(String consumerName, @Nullable SslContextFactory sslContextFactory) {
|
||||
logger.debug("http client for consumer {} requested", consumerName);
|
||||
checkConsumerName(consumerName);
|
||||
return createHttpClientInternal(consumerName, false, null);
|
||||
return createHttpClientInternal(consumerName, sslContextFactory, false, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebSocketClient createWebSocketClient(String consumerName) {
|
||||
return createWebSocketClient(consumerName, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebSocketClient createWebSocketClient(String consumerName, @Nullable SslContextFactory sslContextFactory) {
|
||||
logger.debug("web socket client for consumer {} requested", consumerName);
|
||||
checkConsumerName(consumerName);
|
||||
return createWebSocketClientInternal(consumerName, false, null);
|
||||
return createWebSocketClientInternal(consumerName, sslContextFactory, false, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -191,7 +201,7 @@ public class WebClientFactoryImpl implements HttpClientFactory, WebSocketFactory
|
||||
}
|
||||
|
||||
if (commonHttpClient == null) {
|
||||
commonHttpClient = createHttpClientInternal("common", true, threadPool);
|
||||
commonHttpClient = createHttpClientInternal("common", null, true, threadPool);
|
||||
// we need to set the stop timeout AFTER the client has been started, because
|
||||
// otherwise the Jetty client sets it back to the default value.
|
||||
// We need the stop timeout in order to prevent blocking the deactivation of this
|
||||
@ -201,7 +211,7 @@ public class WebClientFactoryImpl implements HttpClientFactory, WebSocketFactory
|
||||
}
|
||||
|
||||
if (commonWebSocketClient == null) {
|
||||
commonWebSocketClient = createWebSocketClientInternal("common", true, threadPool);
|
||||
commonWebSocketClient = createWebSocketClientInternal("common", null, true, threadPool);
|
||||
logger.debug("Jetty shared web socket client created");
|
||||
}
|
||||
} catch (RuntimeException e) {
|
||||
@ -212,12 +222,13 @@ public class WebClientFactoryImpl implements HttpClientFactory, WebSocketFactory
|
||||
}
|
||||
}
|
||||
|
||||
private HttpClient createHttpClientInternal(String consumerName, boolean startClient,
|
||||
@Nullable QueuedThreadPool threadPool) {
|
||||
private HttpClient createHttpClientInternal(String consumerName, @Nullable SslContextFactory sslContextFactory,
|
||||
boolean startClient, @Nullable QueuedThreadPool threadPool) {
|
||||
try {
|
||||
logger.debug("creating http client for consumer {}", consumerName);
|
||||
|
||||
HttpClient httpClient = new HttpClient(createSslContextFactory());
|
||||
HttpClient httpClient = new HttpClient(
|
||||
sslContextFactory != null ? sslContextFactory : createSslContextFactory());
|
||||
|
||||
// If proxy is set as property (standard Java property), provide the proxy information to Jetty HTTP
|
||||
// Client
|
||||
@ -276,12 +287,13 @@ public class WebClientFactoryImpl implements HttpClientFactory, WebSocketFactory
|
||||
}
|
||||
}
|
||||
|
||||
private WebSocketClient createWebSocketClientInternal(String consumerName, boolean startClient,
|
||||
@Nullable QueuedThreadPool threadPool) {
|
||||
private WebSocketClient createWebSocketClientInternal(String consumerName,
|
||||
@Nullable SslContextFactory sslContextFactory, boolean startClient, @Nullable QueuedThreadPool threadPool) {
|
||||
try {
|
||||
logger.debug("creating web socket client for consumer {}", consumerName);
|
||||
|
||||
HttpClient httpClient = new HttpClient(createSslContextFactory());
|
||||
HttpClient httpClient = new HttpClient(
|
||||
sslContextFactory != null ? sslContextFactory : createSslContextFactory());
|
||||
if (threadPool != null) {
|
||||
httpClient.setExecutor(threadPool);
|
||||
} else {
|
||||
|
Loading…
Reference in New Issue
Block a user