Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit d0383fb

Browse files
committed
Netty Connector - Wrapping Java SSLContext produced by Jersey to Netty JdkSslContext.
Change-Id: I0a6f69b7446970e6cbc9088d0ca6e98dba3c7e4b
1 parent 9f017bf commit d0383fb

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

connectors/netty-connector/src/main/java/org/glassfish/jersey/netty/connector/JerseyClientHandler.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,14 @@ public void run() {
168168
@Override
169169
public void exceptionCaught(ChannelHandlerContext ctx, final Throwable cause) {
170170
if (asyncConnectorCallback != null) {
171-
172-
if (asyncConnectorCallback != null) {
173-
connector.executorService.execute(new Runnable() {
174-
@Override
175-
public void run() {
176-
asyncConnectorCallback.failure(cause);
177-
}
178-
});
179-
}
171+
connector.executorService.execute(new Runnable() {
172+
@Override
173+
public void run() {
174+
asyncConnectorCallback.failure(cause);
175+
}
176+
});
180177
}
178+
future.setException(cause);
181179
isList.add(NettyInputStream.END_OF_INPUT_ERROR);
182180
}
183181
}

connectors/netty-connector/src/main/java/org/glassfish/jersey/netty/connector/NettyConnector.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@
7777
import io.netty.handler.codec.http.HttpUtil;
7878
import io.netty.handler.codec.http.HttpVersion;
7979
import io.netty.handler.proxy.HttpProxyHandler;
80-
import io.netty.handler.ssl.SslContextBuilder;
80+
import io.netty.handler.ssl.ClientAuth;
81+
import io.netty.handler.ssl.JdkSslContext;
8182
import io.netty.handler.stream.ChunkedWriteHandler;
83+
import io.netty.util.concurrent.GenericFutureListener;
8284
import jersey.repackaged.com.google.common.util.concurrent.SettableFuture;
8385
import org.glassfish.jersey.client.ClientProperties;
8486
import org.glassfish.jersey.client.ClientRequest;
@@ -97,6 +99,7 @@ class NettyConnector implements Connector {
9799

98100
final ExecutorService executorService;
99101
final EventLoopGroup group;
102+
final Client client;
100103

101104
NettyConnector(Client client) {
102105

@@ -109,6 +112,7 @@ class NettyConnector implements Connector {
109112
}
110113

111114
this.group = new NioEventLoopGroup();
115+
this.client = client;
112116
}
113117

114118
@Override
@@ -173,8 +177,9 @@ protected void initChannel(SocketChannel ch) throws Exception {
173177

174178
// Enable HTTPS if necessary.
175179
if ("https".equals(requestUri.getScheme())) {
176-
// TODO how to transform client.getSslContext (JDK) to Netty SslContext?
177-
p.addLast(SslContextBuilder.forClient().build().newHandler(ch.alloc()));
180+
// making client authentication optional for now; it could be extracted to configurable property
181+
JdkSslContext jdkSslContext = new JdkSslContext(client.getSslContext(), true, ClientAuth.NONE);
182+
p.addLast(jdkSslContext.newHandler(ch.alloc()));
178183
}
179184

180185
// http proxy
@@ -208,7 +213,20 @@ protected void initChannel(SocketChannel ch) throws Exception {
208213
}
209214

210215
// Make the connection attempt.
211-
Channel ch = b.connect(host, port).sync().channel();
216+
final Channel ch = b.connect(host, port).sync().channel();
217+
218+
// guard against prematurely closed channel
219+
final GenericFutureListener<io.netty.util.concurrent.Future<? super Void>> closeListener =
220+
new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {
221+
@Override
222+
public void operationComplete(io.netty.util.concurrent.Future<? super Void> future) throws Exception {
223+
if (!settableFuture.isDone()) {
224+
settableFuture.setException(new IOException("Channel closed."));
225+
}
226+
}
227+
};
228+
229+
ch.closeFuture().addListener(closeListener);
212230

213231
HttpRequest nettyRequest;
214232

@@ -238,10 +256,9 @@ protected void initChannel(SocketChannel ch) throws Exception {
238256
}
239257
}
240258

241-
// Send the HTTP request.
242-
ch.writeAndFlush(nettyRequest);
243-
244259
if (jerseyRequest.hasEntity()) {
260+
// Send the HTTP request.
261+
ch.writeAndFlush(nettyRequest);
245262

246263
final JerseyChunkedInput jerseyChunkedInput = new JerseyChunkedInput(ch);
247264
jerseyRequest.setStreamProvider(new OutboundMessageContext.StreamProvider() {
@@ -260,6 +277,9 @@ public OutputStream getOutputStream(int contentLength) throws IOException {
260277
executorService.execute(new Runnable() {
261278
@Override
262279
public void run() {
280+
// close listener is not needed any more.
281+
ch.closeFuture().removeListener(closeListener);
282+
263283
try {
264284
jerseyRequest.writeEntity();
265285
} catch (IOException e) {
@@ -270,10 +290,14 @@ public void run() {
270290
});
271291

272292
ch.flush();
293+
} else {
294+
// close listener is not needed any more.
295+
ch.closeFuture().removeListener(closeListener);
296+
297+
// Send the HTTP request.
298+
ch.writeAndFlush(nettyRequest);
273299
}
274300

275-
// Wait for the server to close the connection.
276-
// ch.closeFuture().sync();
277301
} catch (InterruptedException e) {
278302
settableFuture.setException(e);
279303
return settableFuture;

0 commit comments

Comments
 (0)