Skip to content

Commit

Permalink
Simple example that connects to github securely. (#553)
Browse files Browse the repository at this point in the history
  • Loading branch information
groodt authored and NiteshKant committed Sep 12, 2016
1 parent f918e0e commit 9a1ebe8
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions rxnetty-examples/HTTP.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and an [Hello Server](src/main/java/io/reactivex/netty/examples/http/helloworld/
This example constitutes of an [Hello Client](src/main/java/io/reactivex/netty/examples/http/secure/SecureHelloWorldClient.java)
and an [Hello Server](src/main/java/io/reactivex/netty/examples/http/secure/SecureHelloWorldServer.java)

- __Secure Client Default SSLContext__: A very simple example that demonstrates one way of connecting a client securely to a public HTTPS server using the system defaults [SecureDefaultHttpClient](src/main/java/io/reactivex/netty/examples/http/secure/SecureDefaultHttpClient.java)

- __Simple Interception__: A example that demonstrates how to write basic interceptors for HTTP server and client.
This example constitutes of an [Intercepting Client](src/main/java/io/reactivex/netty/examples/http/interceptors/simple/InterceptingClient.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2015 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package io.reactivex.netty.examples.http.secure;

import io.netty.handler.logging.LogLevel;
import io.reactivex.netty.examples.ExamplesEnvironment;
import io.reactivex.netty.protocol.http.client.HttpClient;
import org.slf4j.Logger;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import java.nio.charset.Charset;
import java.security.NoSuchAlgorithmException;

/**
* An example showing how to setup an HttpClient that uses the
* <a href="https://docs.oracle.com/javase/8/docs/api/javax/net/ssl/SSLContext.html#getDefault--">Default SSLContext</a>
* to make an example request against a secure host on the public internet.
* <p>
* For most people, this will be using the default Protocols, CipherSuites and Certificate bundles that come with the
* system and/or JDK. This should be doing the 'right thing' in most cases, but of course, this depends on the specifics
* of how the system and/or JDK has been installed or configured.
* </p>
* <h2>IMPORTANT:</h2>
* We are not security experts and you should do your own research or consult an expert when configuring your own
* secure HttpClients for Production use.
*/
public final class SecureDefaultHttpClient {

private final static String HOST = "github.com";
private final static Integer PORT = 443;

public static void main(String[] args) {

ExamplesEnvironment env = ExamplesEnvironment.newEnvironment(SecureDefaultHttpClient.class);
Logger logger = env.getLogger();

SSLEngine sslEngine = null;
try {
sslEngine = defaultSSLEngineForClient();
} catch (NoSuchAlgorithmException nsae) {
logger.error("Failed to create SSLEngine.", nsae);
System.exit(-1);
}

HttpClient.newClient(HOST, PORT)
.enableWireLogging("http-secure-default-client", LogLevel.DEBUG)
.secure(sslEngine)
.createGet("/")
.doOnNext(resp -> logger.info(resp.toString()))
.flatMap(resp -> {
System.out.println(resp.getStatus());
return resp.getContent()
.map(bb -> bb.toString(Charset.defaultCharset()));
}
)
.toBlocking()
.forEach(logger::info);
}

private static SSLEngine defaultSSLEngineForClient() throws NoSuchAlgorithmException {
SSLContext sslCtx = SSLContext.getDefault();
SSLEngine sslEngine = sslCtx.createSSLEngine(HOST, PORT);
sslEngine.setUseClientMode(true);
return sslEngine;
}
}

0 comments on commit 9a1ebe8

Please sign in to comment.