/
SecureClientContainerExample.java
90 lines (80 loc) · 3.73 KB
/
SecureClientContainerExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//
// ========================================================================
// Copyright (c) 1995-2021 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package examples;
import java.lang.management.ManagementFactory;
import java.net.URI;
import java.util.concurrent.TimeUnit;
import javax.websocket.ClientEndpointConfig;
import javax.websocket.WebSocketContainer;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.http.HttpClientTransportOverHTTP;
import org.eclipse.jetty.io.ClientConnector;
import org.eclipse.jetty.jmx.MBeanContainer;
import org.eclipse.jetty.util.component.Container;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.websocket.javax.client.JavaxWebSocketClientContainerProvider;
public class SecureClientContainerExample
{
public static void main(String[] args)
{
WebSocketContainer client = null;
try
{
URI echoUri = URI.create("wss://echo.websocket.org");
client = getConfiguredWebSocketContainer();
ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create()
.configurator(new OriginServerConfigurator("https://websocket.org"))
.build();
EchoEndpoint echoEndpoint = new EchoEndpoint();
client.connectToServer(echoEndpoint, clientEndpointConfig, echoUri);
System.out.printf("Connecting to : %s%n", echoUri);
// wait for closed socket connection.
echoEndpoint.awaitClose(5, TimeUnit.SECONDS);
}
catch (Throwable t)
{
t.printStackTrace();
}
finally
{
/* Since javax.websocket clients have no defined LifeCycle we
* want to either close/stop the client, or exit the JVM
* via a System.exit(), otherwise the threads this client keeps
* open will prevent the JVM from terminating naturally.
* @see https://github.com/eclipse-ee4j/websocket-api/issues/212
*/
LifeCycle.stop(client);
}
}
/**
* Since javax.websocket does not have an API for configuring SSL, each implementation
* of javax.websocket has to come up with their own SSL configuration mechanism.
*
* @return the client WebSocketContainer
* @see <a href="https://github.com/eclipse-ee4j/websocket-api/issues/210">javax.websocket issue #210</a>
*/
public static WebSocketContainer getConfiguredWebSocketContainer() throws Exception
{
SslContextFactory.Client ssl = new SslContextFactory.Client();
ssl.setExcludeCipherSuites(); // echo.websocket.org use WEAK cipher suites
ClientConnector clientConnector = new ClientConnector();
clientConnector.setSslContextFactory(ssl);
HttpClient httpClient = new HttpClient(new HttpClientTransportOverHTTP(clientConnector));
WebSocketContainer clientContainer = JavaxWebSocketClientContainerProvider.getContainer(httpClient);
// Components can be added as a bean to the WebSocketContainer with the Container static method.
MBeanContainer mbeanContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
Container.addBean(clientContainer, mbeanContainer);
return clientContainer;
}
}