Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
added WebSocketFactory interface and service (#6258)
Browse files Browse the repository at this point in the history
* added WebSocketFactory interface and service

Also-by: Markus Rathgeb <maggu2810@gmail.com>
Signed-off-by: Kai Kreuzer <kai@openhab.org>
  • Loading branch information
kaikreuzer authored and htreu committed Sep 25, 2018
1 parent 029d7d9 commit 19313b3
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 57 deletions.
Expand Up @@ -30,15 +30,16 @@

import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.smarthome.io.net.http.TrustManagerProvider;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mock;

public class SecureHttpClientFactoryTest {
public class WebClientFactoryTest {

private SecureHttpClientFactory secureHttpClientFactory;
private WebClientFactoryImpl webClientFactory;

private static final String TEST_URL = "https://www.eclipse.org/";

Expand All @@ -48,42 +49,53 @@ public class SecureHttpClientFactoryTest {
@Before
public void setup() {
initMocks(this);
secureHttpClientFactory = new SecureHttpClientFactory();
secureHttpClientFactory.setTrustmanagerProvider(trustmanagerProvider);
webClientFactory = new WebClientFactoryImpl();
webClientFactory.setTrustmanagerProvider(trustmanagerProvider);
}

@Test
public void testGetClient() throws Exception {
secureHttpClientFactory.activate(createConfigMap(10, 200, 60, 5, 10, 60));
public void testGetClients() throws Exception {
webClientFactory.activate(createConfigMap(10, 200, 60, 5, 10, 60));

HttpClient client = secureHttpClientFactory.getCommonHttpClient();
HttpClient httpClient = webClientFactory.getCommonHttpClient();
WebSocketClient webSocketClient = webClientFactory.getCommonWebSocketClient();

assertThat(client, is(notNullValue()));
assertThat(httpClient, is(notNullValue()));
assertThat(webSocketClient, is(notNullValue()));

secureHttpClientFactory.deactivate();
webClientFactory.deactivate();
}

@Test
public void testGetClientWithEndpoint() throws Exception {
secureHttpClientFactory.activate(createConfigMap(10, 200, 60, 5, 10, 60));
public void testGetHttpClientWithEndpoint() throws Exception {
webClientFactory.activate(createConfigMap(10, 200, 60, 5, 10, 60));

when(trustmanagerProvider.getTrustManagers("https://www.heise.de")).thenReturn(Stream.empty());
HttpClient httpClient = webClientFactory.createHttpClient("consumer", TEST_URL);
assertThat(httpClient, is(notNullValue()));
verify(trustmanagerProvider).getTrustManagers(TEST_URL);
httpClient.stop();

HttpClient client = secureHttpClientFactory.createHttpClient("consumer", TEST_URL);
webClientFactory.deactivate();
}

assertThat(client, is(notNullValue()));
@Test
public void testGetWebSocketClientWithEndpoint() throws Exception {
webClientFactory.activate(createConfigMap(10, 200, 60, 5, 10, 60));

when(trustmanagerProvider.getTrustManagers("https://www.heise.de")).thenReturn(Stream.empty());
WebSocketClient webSocketClient = webClientFactory.createWebSocketClient("consumer", TEST_URL);
assertThat(webSocketClient, is(notNullValue()));
verify(trustmanagerProvider).getTrustManagers(TEST_URL);
webSocketClient.stop();

client.stop();

secureHttpClientFactory.deactivate();
webClientFactory.deactivate();
}

@Ignore("only for manual test")
@Test
public void testMultiThreadedShared() throws Exception {
secureHttpClientFactory.activate(createConfigMap(10, 200, 60, 5, 10, 60));
webClientFactory.activate(createConfigMap(10, 200, 60, 5, 10, 60));

ThreadPoolExecutor workers = new ThreadPoolExecutor(20, 80, 60, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(50 * 50));
Expand All @@ -94,7 +106,7 @@ public void testMultiThreadedShared() throws Exception {
final int REQUESTS = 2;

for (int i = 0; i < CLIENTS; i++) {
HttpClient httpClient = secureHttpClientFactory.getCommonHttpClient();
HttpClient httpClient = webClientFactory.getCommonHttpClient();
clients.add(httpClient);
}

Expand Down Expand Up @@ -124,13 +136,13 @@ public void run() {
fail(failures.toString());
}

secureHttpClientFactory.deactivate();
webClientFactory.deactivate();
}

@Ignore("only for manual test")
@Test
public void testMultiThreadedCustom() throws Exception {
secureHttpClientFactory.activate(createConfigMap(10, 200, 60, 5, 10, 60));
webClientFactory.activate(createConfigMap(10, 200, 60, 5, 10, 60));

ThreadPoolExecutor workers = new ThreadPoolExecutor(20, 80, 60, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(50 * 50));
Expand All @@ -141,7 +153,7 @@ public void testMultiThreadedCustom() throws Exception {
final int REQUESTS = 2;

for (int i = 0; i < CLIENTS; i++) {
HttpClient httpClient = secureHttpClientFactory.createHttpClient("consumer" + i, "https://www.heise.de");
HttpClient httpClient = webClientFactory.createHttpClient("consumer" + i, "https://www.heise.de");
clients.add(httpClient);
}

Expand Down Expand Up @@ -175,7 +187,7 @@ public void run() {
client.stop();
}

secureHttpClientFactory.deactivate();
webClientFactory.deactivate();
}

private Map<String, Object> createConfigMap(int minThreadsShared, int maxThreadsShared, int keepAliveTimeoutShared,
Expand Down
4 changes: 4 additions & 0 deletions bundles/io/org.eclipse.smarthome.io.net/META-INF/MANIFEST.MF
Expand Up @@ -24,6 +24,10 @@ Import-Package:
org.eclipse.jetty.util.component,
org.eclipse.jetty.util.ssl,
org.eclipse.jetty.util.thread,
org.eclipse.jetty.websocket.api,
org.eclipse.jetty.websocket.api.extensions,
org.eclipse.jetty.websocket.client,
org.eclipse.jetty.websocket.common.scopes,
org.eclipse.smarthome.core.library.types,
org.eclipse.smarthome.io.net.exec,
org.eclipse.smarthome.io.net.http,
Expand Down
Expand Up @@ -16,15 +16,15 @@
import org.eclipse.jetty.client.HttpClient;

/**
* Factory class to create jetty http clients
* Factory class to create Jetty http clients
*
* @author Michael Bock - initial API
* @author Michael Bock - Initial contribution
*/
@NonNullByDefault
public interface HttpClientFactory {

/**
* Creates a new jetty http client.
* 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.
Expand All @@ -33,17 +33,17 @@ public interface HttpClientFactory {
* @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 endpoint the desired endpoint, protocol and host are sufficient
* @return the jetty client
* @return the Jetty client
* @throws NullPointerException if {@code endpoint} or {@code consumerName} is {@code null}
* @throws IllegalArgumentException if {@code consumerName} is invalid
*/
HttpClient createHttpClient(String consumerName, String endpoint);

/**
* Returns the shared jetty http client. You must not call any setter methods or {@code stop()} on it.
* Returns the shared Jetty http client. You must not call any setter methods or {@code stop()} on it.
* The returned client is already started.
*
* @return the shared jetty http client
* @return the shared Jetty http client
*/
HttpClient getCommonHttpClient();
}
@@ -0,0 +1,49 @@
/**
* Copyright (c) 2014,2018 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.smarthome.io.net.http;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jetty.websocket.client.WebSocketClient;

/**
* Factory class to create Jetty web socket clients
*
* @author Kai Kreuzer - Initial contribution
*/
@NonNullByDefault
public interface WebSocketFactory {

/**
* 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 endpoint the desired endpoint, protocol and host are sufficient
* @return the Jetty client
* @throws NullPointerException if {@code endpoint} or {@code consumerName} is {@code null}
* @throws IllegalArgumentException if {@code consumerName} is invalid
*/
WebSocketClient createWebSocketClient(String consumerName, String endpoint);

/**
* 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.
*
* @return a shared Jetty web socket client
*/
WebSocketClient getCommonWebSocketClient();
}

0 comments on commit 19313b3

Please sign in to comment.