-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tomasz Bak
committed
Jun 24, 2014
1 parent
c30e833
commit 07c39de
Showing
26 changed files
with
717 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
version=0.3.6 | ||
netty_version=4.0.14.Final | ||
netty_version=4.0.20.Final | ||
slf4j_version=1.7.6 | ||
rxjava_version=[0.17,) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
rx-netty-examples/src/main/java/io/reactivex/netty/examples/http/ssl/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
Overview | ||
======== | ||
|
||
TO enable HTTPS connection, configure ```io.reactivex.netty.pipeline.ssl.SSLEngineFactory``` using | ||
HttpClientBuilder. For testing purposes ```io.reactivex.netty.pipeline.ssl.DefaultFactories``` class is provided | ||
that makes it easy to build SSLEngineFactory instances for client and server endpoints. These are good only for testing environment. | ||
|
||
Running | ||
======= | ||
|
||
To run the example execute: | ||
|
||
``` | ||
$ cd RxNetty/rx-netty-examples | ||
$ ../gradlew runSslHelloWorldServer | ||
``` | ||
|
||
and in another console: | ||
|
||
``` | ||
$ cd RxNetty/rx-netty-examples | ||
$ ../gradlew runSslHelloWorldClient | ||
``` | ||
|
||
HTTP client | ||
=========== | ||
|
||
Here is the snippet from [SslHelloWordClient](SslHelloWorldClient.java): | ||
|
||
```java | ||
public HttpResponseStatus sendHelloRequest() throws Exception { | ||
HttpClient<ByteBuf, ByteBuf> rxClient = RxNetty.<ByteBuf, ByteBuf>newHttpClientBuilder("localhost", port) | ||
.withSslEngineFactory(DefaultFactories.TRUST_ALL) | ||
.build(); | ||
|
||
HttpResponseStatus statusCode = rxClient.submit(HttpClientRequest.createGet("/hello")) | ||
... | ||
} | ||
} | ||
``` | ||
|
||
The client code is identical to its non-encrypted counterpart ([HelloWorldClient](../helloworld/README.md)), except | ||
the HTTPClient creation step. The predefined DefaultFactories.TRUST_ALL SSLEngineFactory used in the example | ||
will accept all certificates without validation. It is good only for testing purposes. | ||
|
||
HTTP server | ||
=========== | ||
|
||
Here is the snippet from [SslHelloWordClient](SslHelloWorldServer.java): | ||
|
||
```java | ||
public HttpServer<ByteBuf, ByteBuf> createServer() throws CertificateException, SSLException { | ||
HttpServer<ByteBuf, ByteBuf> server = RxNetty.newHttpServerBuilder(port, new RequestHandler<ByteBuf, ByteBuf>() { | ||
@Override | ||
public Observable<Void> handle(HttpServerRequest<ByteBuf> request, final HttpServerResponse<ByteBuf> response) { | ||
response.writeStringAndFlush("Welcome!!"); | ||
return response.close(); | ||
} | ||
}).withSslEngineFactory(DefaultFactories.SELF_SIGNED).build(); | ||
... | ||
} | ||
``` | ||
|
||
On the server side, SSLEngineFactory is configured with a temporary self signed certificate/private key generated automatically. | ||
It will be accepted on the client side, since in it is configured to trusts all certificates. | ||
This setup should NEVER be used in the production deployment. Its usage is limited to test code/examples. |
80 changes: 80 additions & 0 deletions
80
...etty-examples/src/main/java/io/reactivex/netty/examples/http/ssl/SslHelloWorldClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright 2014 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.ssl; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.handler.codec.http.HttpResponseStatus; | ||
import io.reactivex.netty.RxNetty; | ||
import io.reactivex.netty.pipeline.ssl.DefaultFactories; | ||
import io.reactivex.netty.protocol.http.client.HttpClient; | ||
import io.reactivex.netty.protocol.http.client.HttpClientRequest; | ||
import io.reactivex.netty.protocol.http.client.HttpClientResponse; | ||
import rx.Observable; | ||
import rx.functions.Action0; | ||
import rx.functions.Func1; | ||
import rx.functions.Func2; | ||
|
||
import java.nio.charset.Charset; | ||
|
||
import static io.reactivex.netty.examples.http.ssl.SslHelloWorldServer.*; | ||
|
||
/** | ||
* @author Tomasz Bak | ||
*/ | ||
public class SslHelloWorldClient { | ||
|
||
private final int port; | ||
|
||
public SslHelloWorldClient(int port) { | ||
this.port = port; | ||
} | ||
|
||
public HttpResponseStatus sendHelloRequest() throws Exception { | ||
HttpClient<ByteBuf, ByteBuf> rxClient = RxNetty.<ByteBuf, ByteBuf>newHttpClientBuilder("localhost", port) | ||
.withSslEngineFactory(DefaultFactories.TRUST_ALL) | ||
.build(); | ||
|
||
HttpResponseStatus statusCode = rxClient.submit(HttpClientRequest.createGet("/hello")) | ||
.mergeMap(new Func1<HttpClientResponse<ByteBuf>, Observable<ByteBuf>>() { | ||
@Override | ||
public Observable<ByteBuf> call(HttpClientResponse<ByteBuf> response) { | ||
return response.getContent(); | ||
} | ||
}, new Func2<HttpClientResponse<ByteBuf>, ByteBuf, HttpResponseStatus>() { | ||
@Override | ||
public HttpResponseStatus call(HttpClientResponse<ByteBuf> response, ByteBuf content) { | ||
System.out.println(content.toString(Charset.defaultCharset())); | ||
return response.getStatus(); | ||
} | ||
}) | ||
.doOnTerminate(new Action0() { | ||
@Override | ||
public void call() { | ||
System.out.println("======================="); | ||
} | ||
}).toBlocking().last(); | ||
|
||
return statusCode; | ||
} | ||
|
||
public static void main(String[] args) { | ||
try { | ||
new SslHelloWorldClient(DEFAULT_PORT).sendHelloRequest(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...etty-examples/src/main/java/io/reactivex/netty/examples/http/ssl/SslHelloWorldServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2014 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.ssl; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.reactivex.netty.RxNetty; | ||
import io.reactivex.netty.pipeline.ssl.DefaultFactories; | ||
import io.reactivex.netty.protocol.http.server.HttpServer; | ||
import io.reactivex.netty.protocol.http.server.HttpServerRequest; | ||
import io.reactivex.netty.protocol.http.server.HttpServerResponse; | ||
import io.reactivex.netty.protocol.http.server.RequestHandler; | ||
import rx.Observable; | ||
|
||
import javax.net.ssl.SSLException; | ||
import java.security.cert.CertificateException; | ||
|
||
/** | ||
* @author Tomasz Bak | ||
*/ | ||
public final class SslHelloWorldServer { | ||
|
||
static final int DEFAULT_PORT = 8105; | ||
|
||
private int port; | ||
|
||
public SslHelloWorldServer(int port) throws CertificateException { | ||
this.port = port; | ||
} | ||
|
||
public HttpServer<ByteBuf, ByteBuf> createServer() throws CertificateException, SSLException { | ||
HttpServer<ByteBuf, ByteBuf> server = RxNetty.newHttpServerBuilder(port, new RequestHandler<ByteBuf, ByteBuf>() { | ||
@Override | ||
public Observable<Void> handle(HttpServerRequest<ByteBuf> request, final HttpServerResponse<ByteBuf> response) { | ||
response.writeStringAndFlush("Welcome!!"); | ||
return response.close(); | ||
} | ||
}).withSslEngineFactory(DefaultFactories.SELF_SIGNED).build(); | ||
|
||
System.out.println("HTTP hello world server started..."); | ||
return server; | ||
} | ||
|
||
public static void main(final String[] args) { | ||
try { | ||
new SslHelloWorldServer(DEFAULT_PORT).createServer().startAndWait(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
rx-netty-examples/src/main/java/io/reactivex/netty/examples/tcp/ssl/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Overview | ||
======== | ||
|
||
This example is a small modification of [TCP echo](../echo/README.md) example that demonstrates how to setup SSL connection. | ||
More detailed explanation of SSL handling is provided with the [SSL HelloWorld](../../ssl/README.md) example. | ||
|
||
Running | ||
======= | ||
|
||
To run the example execute: | ||
|
||
``` | ||
$ cd RxNetty/rx-netty-examples | ||
$ ../gradlew runSslTcpEchoServer | ||
``` | ||
|
||
and in another console: | ||
|
||
``` | ||
$ cd RxNetty/rx-netty-examples | ||
$ ../gradlew runSslTcpEchoClient | ||
``` |
Oops, something went wrong.