Skip to content

Commit

Permalink
Add param sslContextFactory when creating a HTTP or web socket client (
Browse files Browse the repository at this point in the history
…#3356)

* Add param sslContextFactory when creating a HTTP or web socket client

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
  • Loading branch information
lolodomo authored Feb 12, 2023
1 parent 1cad11a commit f977a59
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,26 @@ protected void deactivate() {

@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
Expand Down Expand Up @@ -191,7 +201,7 @@ private synchronized void initialize() {
}

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
Expand All @@ -201,7 +211,7 @@ private synchronized void initialize() {
}

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) {
Expand All @@ -212,12 +222,13 @@ private synchronized void initialize() {
}
}

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
Expand Down Expand Up @@ -276,12 +287,13 @@ private HttpClient createHttpClientInternal(String consumerName, boolean startCl
}
}

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 {
Expand Down

0 comments on commit f977a59

Please sign in to comment.