Skip to content

Package: SSLEchoClient$SendMessageFilter$1

SSLEchoClient$SendMessageFilter$1

nameinstructionbranchcomplexitylinemethod
completed(SSLEngine)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
{...}
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Distribution License v. 1.0, which is available at
6: * http://www.eclipse.org/org/documents/edl-v10.php.
7: *
8: * SPDX-License-Identifier: BSD-3-Clause
9: */
10:
11: package org.glassfish.grizzly.samples.ssl;
12:
13: import java.io.IOException;
14: import java.net.URL;
15: import java.nio.charset.Charset;
16:
17: import javax.net.ssl.SSLEngine;
18:
19: import org.glassfish.grizzly.Connection;
20: import org.glassfish.grizzly.EmptyCompletionHandler;
21: import org.glassfish.grizzly.filterchain.BaseFilter;
22: import org.glassfish.grizzly.filterchain.Filter;
23: import org.glassfish.grizzly.filterchain.FilterChain;
24: import org.glassfish.grizzly.filterchain.FilterChainBuilder;
25: import org.glassfish.grizzly.filterchain.FilterChainContext;
26: import org.glassfish.grizzly.filterchain.NextAction;
27: import org.glassfish.grizzly.filterchain.TransportFilter;
28: import org.glassfish.grizzly.nio.transport.TCPNIOTransport;
29: import org.glassfish.grizzly.nio.transport.TCPNIOTransportBuilder;
30: import org.glassfish.grizzly.ssl.SSLContextConfigurator;
31: import org.glassfish.grizzly.ssl.SSLEngineConfigurator;
32: import org.glassfish.grizzly.ssl.SSLFilter;
33: import org.glassfish.grizzly.utils.StringFilter;
34:
35: /**
36: * The simple {@link FilterChain} based SSL client, which sends a message to the echo server and waits for response. In
37: * this sample we add a {@link StringFilter} to a {@link FilterChain}, so there is no need to do Buffer <-> String
38: * transformation explicitly.
39: *
40: * @see StringFilter
41: * @see SSLFilter
42: * @see SSLContextConfigurator
43: * @see SSLEngineConfigurator
44: *
45: * @author Alexey Stashok
46: */
47: public class SSLEchoClient {
48: private static final String MESSAGE = "Hello World!";
49:
50: public static void main(String[] args) throws IOException {
51: // Create a FilterChain using FilterChainBuilder
52: FilterChainBuilder filterChainBuilder = FilterChainBuilder.stateless();
53: // Add TransportFilter, which is responsible
54: // for reading and writing data to the connection
55: filterChainBuilder.add(new TransportFilter());
56:
57: // Initialize and add SSLFilter
58: final SSLEngineConfigurator serverConfig = initializeSSL();
59: final SSLEngineConfigurator clientConfig = serverConfig.copy().setClientMode(true);
60:
61: final SSLFilter sslFilter = new SSLFilter(serverConfig, clientConfig);
62: filterChainBuilder.add(sslFilter);
63:
64: // Add StringFilter, which will be responsible for Buffer <-> String transformation
65: filterChainBuilder.add(new StringFilter(Charset.forName("UTF-8")));
66:
67: // Add Filter, which will send a greeting message and check the result
68: filterChainBuilder.add(new SendMessageFilter(sslFilter));
69:
70: // Create TCP transport
71: final TCPNIOTransport transport = TCPNIOTransportBuilder.newInstance().build();
72: transport.setProcessor(filterChainBuilder.build());
73:
74: try {
75: // start the transport
76: transport.start();
77:
78: // perform async. connect to the server
79: transport.connect(SSLEchoServer.HOST, SSLEchoServer.PORT);
80:
81: System.out.println("Press any key to stop the client...");
82: System.in.read();
83: } finally {
84: System.out.println("Stopping transport...");
85: // stop the transport
86: transport.shutdownNow();
87:
88: System.out.println("Stopped transport...");
89: }
90: }
91:
92: /**
93: * The {@link Filter}, responsible for handling client {@link Connection} events.
94: */
95: private static class SendMessageFilter extends BaseFilter {
96:
97: private final SSLFilter sslFilter;
98:
99: public SendMessageFilter(SSLFilter sslFilter) {
100: this.sslFilter = sslFilter;
101: }
102:
103: /**
104: * Handle newly connected {@link Connection}, perform SSL handshake and send greeting message to a server.
105: *
106: * @param ctx {@link FilterChain} context
107: * @return nextAction
108: * @throws IOException
109: */
110: @Override
111: @SuppressWarnings("unchecked")
112: public NextAction handleConnect(FilterChainContext ctx) throws IOException {
113: final Connection connection = ctx.getConnection();
114:
115: // Execute async SSL handshake
116: sslFilter.handshake(connection, new EmptyCompletionHandler<SSLEngine>() {
117:
118: /**
119: * Once SSL handshake will be completed - send greeting message
120: */
121: @Override
122: public void completed(SSLEngine result) {
123: // Here we send String directly
124: connection.write(MESSAGE);
125: }
126: });
127:
128: return ctx.getInvokeAction();
129: }
130:
131: /**
132: * Handle server response and check, whether it has expected data
133: *
134: * @param ctx {@link FilterChain} context
135: * @return nextAction
136: * @throws IOException
137: */
138: @Override
139: public NextAction handleRead(FilterChainContext ctx) throws IOException {
140:
141: // The received message is String
142: final String message = ctx.getMessage();
143:
144: // Check the message
145: if (MESSAGE.equals(message)) {
146: System.out.println("Got echo message: \"" + message + "\"");
147: } else {
148: System.out.println("Got unexpected echo message: \"" + message + "\"");
149: }
150:
151: return ctx.getStopAction();
152: }
153:
154: }
155:
156: /**
157: * Initialize server side SSL configuration.
158: *
159: * @return server side {@link SSLEngineConfigurator}.
160: */
161: private static SSLEngineConfigurator initializeSSL() {
162: // Initialize SSLContext configuration
163: SSLContextConfigurator sslContextConfig = new SSLContextConfigurator();
164:
165: // Set key store
166: ClassLoader cl = SSLEchoClient.class.getClassLoader();
167: URL cacertsUrl = cl.getResource("ssltest-cacerts.jks");
168: if (cacertsUrl != null) {
169: sslContextConfig.setTrustStoreFile(cacertsUrl.getFile());
170: sslContextConfig.setTrustStorePass("changeit");
171: }
172:
173: // Set trust store
174: URL keystoreUrl = cl.getResource("ssltest-keystore.jks");
175: if (keystoreUrl != null) {
176: sslContextConfig.setKeyStoreFile(keystoreUrl.getFile());
177: sslContextConfig.setKeyStorePass("changeit");
178: }
179:
180: // Create SSLEngine configurator
181: return new SSLEngineConfigurator(sslContextConfig.createSSLContext(), false, false, false);
182: }
183: }