Skip to content

Commit

Permalink
fixes networknt#612 manually merge the integration with REST based Li…
Browse files Browse the repository at this point in the history
…ght config server
  • Loading branch information
santoshaherkar authored and stevehu committed Nov 23, 2019
1 parent 0e51ede commit 45925b7
Show file tree
Hide file tree
Showing 19 changed files with 393 additions and 132 deletions.
Expand Up @@ -159,7 +159,7 @@ public void testAuditWithTrace() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
Expand Down
22 changes: 11 additions & 11 deletions body/src/test/java/com/networknt/body/BodyHandlerTest.java
Expand Up @@ -132,7 +132,7 @@ public void testGet() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
Expand Down Expand Up @@ -161,7 +161,7 @@ public void testPostNonJson() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
Expand Down Expand Up @@ -202,7 +202,7 @@ public void testPostInvalidJson() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
Expand Down Expand Up @@ -243,7 +243,7 @@ public void testPostJsonList() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
Expand Down Expand Up @@ -278,7 +278,7 @@ public void testPostJsonListEmpty() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
Expand Down Expand Up @@ -313,7 +313,7 @@ public void testPostJsonMap() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
Expand Down Expand Up @@ -348,7 +348,7 @@ public void testPostJsonMapEmpty() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
Expand Down Expand Up @@ -383,7 +383,7 @@ public void testPostJsonMapWithoutContentTypeHeader() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
Expand Down Expand Up @@ -416,7 +416,7 @@ public void testPostFormWithoutContentTypeHeader() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
Expand Down Expand Up @@ -449,7 +449,7 @@ public void testPostFormUrlEncoded() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
Expand Down Expand Up @@ -482,7 +482,7 @@ public void testPostFormMultipart() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
Expand Down
Expand Up @@ -79,7 +79,7 @@ public void testEnableCacheRequestBody() throws Exception {
final ClientConnection connection;
String post;
try {
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
Expand Down Expand Up @@ -115,7 +115,7 @@ public void testDisableCacheRequestBody() throws Exception {
final ClientConnection connection;
String post;
try {
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
Expand Down
38 changes: 36 additions & 2 deletions client/src/main/java/com/networknt/client/Http2Client.java
Expand Up @@ -49,6 +49,7 @@
import io.undertow.util.Headers;
import io.undertow.util.StringReadChannelListener;
import io.undertow.util.StringWriteChannelListener;
import org.omg.SendingContext.RunTime;
import org.owasp.encoder.Encode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -147,7 +148,6 @@ private Http2Client(final ClassLoader classLoader) {
try {
final Xnio xnio = Xnio.getInstance(Undertow.class.getClassLoader());
WORKER = xnio.createWorker(null, Http2Client.DEFAULT_OPTIONS);
SSL = new UndertowXnioSsl(WORKER.getXnio(), OptionMap.EMPTY, BUFFER_POOL, createSSLContext());
} catch (Exception e) {
logger.error("Exception: ", e);
}
Expand All @@ -167,6 +167,18 @@ private void addProvider(Map<String, ClientProvider> map, String scheme, ClientP
}
}

/**
* Create an XnioSsl object with the given sslContext. This is used to create the normal client context
* and the light-config-server bootstrap context separately. the XnioSsl object can be used to create
* an Https connection to the downstream services.
*
* @param sslContext SslContext
* @return XnioSsl
*/
public XnioSsl createXnioSsl(SSLContext sslContext) {
return new UndertowXnioSsl(WORKER.getXnio(), OptionMap.EMPTY, BUFFER_POOL, sslContext);
}

public IoFuture<ClientConnection> connect(final URI uri, final XnioWorker worker, ByteBufferPool bufferPool, OptionMap options) {
return connect(uri, worker, null, bufferPool, options);
}
Expand All @@ -175,11 +187,25 @@ public IoFuture<ClientConnection> connect(InetSocketAddress bindAddress, final U
return connect(bindAddress, uri, worker, null, bufferPool, options);
}

private XnioSsl getDefaultXnioSsl() {
if(SSL == null) {
try {
SSL = createXnioSsl(createSSLContext());
} catch (Exception e) {
logger.error("Exception", e);
throw new RuntimeException(e);
}
}
return SSL;
}

public IoFuture<ClientConnection> connect(final URI uri, final XnioWorker worker, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
if("https".equals(uri.getScheme()) && ssl == null) ssl = getDefaultXnioSsl();
return connect((InetSocketAddress) null, uri, worker, ssl, bufferPool, options);
}

public IoFuture<ClientConnection> connect(InetSocketAddress bindAddress, final URI uri, final XnioWorker worker, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
if("https".equals(uri.getScheme()) && ssl == null) ssl = getDefaultXnioSsl();
ClientProvider provider = getClientProvider(uri);
final FutureResult<ClientConnection> result = new FutureResult<>();
provider.connect(new ClientCallback<ClientConnection>() {
Expand All @@ -206,10 +232,12 @@ public IoFuture<ClientConnection> connect(InetSocketAddress bindAddress, final U
}

public IoFuture<ClientConnection> connect(final URI uri, final XnioIoThread ioThread, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
if("https".equals(uri.getScheme()) && ssl == null) ssl = getDefaultXnioSsl();
return connect((InetSocketAddress) null, uri, ioThread, ssl, bufferPool, options);
}

public IoFuture<ClientConnection> connect(InetSocketAddress bindAddress, final URI uri, final XnioIoThread ioThread, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
if("https".equals(uri.getScheme()) && ssl == null) ssl = getDefaultXnioSsl();
ClientProvider provider = getClientProvider(uri);
final FutureResult<ClientConnection> result = new FutureResult<>();
provider.connect(new ClientCallback<ClientConnection>() {
Expand All @@ -235,11 +263,13 @@ public void connect(final ClientCallback<ClientConnection> listener, InetSocketA
}

public void connect(final ClientCallback<ClientConnection> listener, final URI uri, final XnioWorker worker, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
if("https".equals(uri.getScheme()) && ssl == null) ssl = getDefaultXnioSsl();
ClientProvider provider = getClientProvider(uri);
provider.connect(listener, uri, worker, ssl, bufferPool, options);
}

public void connect(final ClientCallback<ClientConnection> listener, InetSocketAddress bindAddress, final URI uri, final XnioWorker worker, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
if("https".equals(uri.getScheme()) && ssl == null) ssl = getDefaultXnioSsl();
ClientProvider provider = getClientProvider(uri);
provider.connect(listener, bindAddress, uri, worker, ssl, bufferPool, options);
}
Expand All @@ -254,11 +284,13 @@ public void connect(final ClientCallback<ClientConnection> listener, InetSocketA
}

public void connect(final ClientCallback<ClientConnection> listener, final URI uri, final XnioIoThread ioThread, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
if("https".equals(uri.getScheme()) && ssl == null) ssl = getDefaultXnioSsl();
ClientProvider provider = getClientProvider(uri);
provider.connect(listener, uri, ioThread, ssl, bufferPool, options);
}

public void connect(final ClientCallback<ClientConnection> listener, InetSocketAddress bindAddress, final URI uri, final XnioIoThread ioThread, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
if("https".equals(uri.getScheme()) && ssl == null) ssl = getDefaultXnioSsl();
ClientProvider provider = getClientProvider(uri);
provider.connect(listener, bindAddress, uri, ioThread, ssl, bufferPool, options);
}
Expand Down Expand Up @@ -850,11 +882,13 @@ public CompletableFuture<ClientResponse> callService(String protocol, String ser
* @return CompletableFuture
*/
public CompletableFuture<ClientConnection> connectAsync(URI uri) {
return this.connectAsync(null, uri, com.networknt.client.Http2Client.WORKER, com.networknt.client.Http2Client.SSL, com.networknt.client.Http2Client.BUFFER_POOL,
if("https".equals(uri.getScheme()) && SSL == null) SSL = getDefaultXnioSsl();
return this.connectAsync(null, uri, WORKER, SSL, com.networknt.client.Http2Client.BUFFER_POOL,
ClientConfig.get().getRequestEnableHttp2() ? OptionMap.create(UndertowOptions.ENABLE_HTTP2, true) : OptionMap.EMPTY);
}

public CompletableFuture<ClientConnection> connectAsync(InetSocketAddress bindAddress, final URI uri, final XnioWorker worker, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) {
if("https".equals(uri.getScheme()) && SSL == null) SSL = getDefaultXnioSsl();
CompletableFuture<ClientConnection> completableFuture = new CompletableFuture<>();
ClientProvider provider = clientProviders.get(uri.getScheme());
try {
Expand Down
Expand Up @@ -101,7 +101,7 @@ public void testWithCid() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
Expand Down Expand Up @@ -130,7 +130,7 @@ public void testGetWithoutTid() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
Expand Down Expand Up @@ -162,7 +162,7 @@ public void testGetWithoutTidNoAutogen() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
Expand Down
2 changes: 1 addition & 1 deletion dump/src/test/java/com/networknt/dump/DumpHandlerTest.java
Expand Up @@ -100,7 +100,7 @@ public void tesLog() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
Expand Down
Expand Up @@ -104,7 +104,7 @@ public void testNormal() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
Expand Down Expand Up @@ -132,7 +132,7 @@ public void testRuntimeException() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
Expand Down Expand Up @@ -164,7 +164,7 @@ public void testApiException() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
Expand All @@ -191,7 +191,7 @@ public void testUncaughtException() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
Expand Down
Expand Up @@ -118,7 +118,7 @@ public void testRequestHeader() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection;
try {
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.BUFFER_POOL, OptionMap.EMPTY).get();
} catch (Exception e) {
throw new ClientException(e);
}
Expand Down

0 comments on commit 45925b7

Please sign in to comment.