Skip to content

Commit

Permalink
Merge pull request #189 from NiteshKant/master
Browse files Browse the repository at this point in the history
Fixes #187
  • Loading branch information
NiteshKant committed Jul 22, 2014
2 parents 1f012bc + ca710a6 commit 284a039
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 46 deletions.
4 changes: 0 additions & 4 deletions rx-netty-examples/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@







sourceCompatibility = JavaVersion.VERSION_1_6
targetCompatibility = JavaVersion.VERSION_1_6

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@
package io.reactivex.netty.examples.http.helloworld;

import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.reactivex.netty.RxNetty;
import io.reactivex.netty.protocol.http.client.FlatResponseOperator;
import io.reactivex.netty.protocol.http.client.HttpClientResponse;
import rx.Observable;
import rx.functions.Action0;
import io.reactivex.netty.protocol.http.client.ResponseHolder;
import rx.functions.Func1;
import rx.functions.Func2;

import java.nio.charset.Charset;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static io.reactivex.netty.examples.http.helloworld.HelloWorldServer.DEFAULT_PORT;

Expand All @@ -41,29 +42,18 @@ public HelloWorldClient(int port) {
this.port = port;
}

public HttpResponseStatus sendHelloRequest() {
HttpResponseStatus statusCode = RxNetty.createHttpGet("http://localhost:" + port + "/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) {
printResponseHeader(response);
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 HttpClientResponse<ByteBuf> sendHelloRequest() throws InterruptedException, ExecutionException, TimeoutException {
return RxNetty.createHttpGet("http://localhost:" + port + "/hello")
.lift(FlatResponseOperator.<ByteBuf>flatResponse())
.map(new Func1<ResponseHolder<ByteBuf>, HttpClientResponse<ByteBuf>>() {
@Override
public HttpClientResponse<ByteBuf> call(ResponseHolder<ByteBuf> holder) {
printResponseHeader(holder.getResponse());
System.out.println(holder.getContent().toString(Charset.defaultCharset()));
System.out.println("========================");
return holder.getResponse();
}
}).toBlocking().toFuture().get(1, TimeUnit.MINUTES);
}

public void printResponseHeader(HttpClientResponse<ByteBuf> response) {
Expand All @@ -76,7 +66,7 @@ public void printResponseHeader(HttpClientResponse<ByteBuf> response) {
}
}

public static void main(String[] args) {
public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException {
int port = DEFAULT_PORT;
if (args.length > 0) {
port = Integer.parseInt(args[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

import static io.reactivex.netty.examples.http.cpuintensive.CPUIntensiveServer.DEFAULT_PORT;

/**
Expand All @@ -47,9 +50,9 @@ public void stopServer() throws InterruptedException {
}

@Test
public void testRequestReplySequence() {
public void testRequestReplySequence() throws InterruptedException, ExecutionException, TimeoutException {
HelloWorldClient client = new HelloWorldClient(DEFAULT_PORT); // The client is no different than hello world.
HttpResponseStatus statusCode = client.sendHelloRequest();
HttpResponseStatus statusCode = client.sendHelloRequest().getStatus();
Assert.assertEquals(HttpResponseStatus.OK, statusCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

import static io.reactivex.netty.examples.http.helloworld.HelloWorldServer.DEFAULT_PORT;

/**
Expand All @@ -46,9 +49,9 @@ public void stopServer() throws InterruptedException {
}

@Test
public void testRequestReplySequence() {
public void testRequestReplySequence() throws InterruptedException, ExecutionException, TimeoutException {
HelloWorldClient client = new HelloWorldClient(DEFAULT_PORT);
HttpResponseStatus statusCode = client.sendHelloRequest();
HttpResponseStatus statusCode = client.sendHelloRequest().getStatus();
Assert.assertEquals(HttpResponseStatus.OK, statusCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

import static io.reactivex.netty.examples.http.plaintext.PlainTextServer.DEFAULT_PORT;

/**
Expand All @@ -47,9 +50,9 @@ public void stopServer() throws InterruptedException {
}

@Test
public void testRequestReplySequence() {
public void testRequestReplySequence() throws InterruptedException, ExecutionException, TimeoutException {
HelloWorldClient client = new HelloWorldClient(DEFAULT_PORT); // The client is no different than hello world.
HttpResponseStatus statusCode = client.sendHelloRequest();
HttpResponseStatus statusCode = client.sendHelloRequest().getStatus();
Assert.assertEquals(HttpResponseStatus.OK, statusCode);
}
}
15 changes: 7 additions & 8 deletions rx-netty/build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
apply plugin: 'osgi'
apply plugin: 'groovy'

sourceCompatibility = JavaVersion.VERSION_1_6
targetCompatibility = JavaVersion.VERSION_1_6


/*
* Copyright 2014 Netflix, Inc.
*
Expand All @@ -20,7 +13,13 @@ targetCompatibility = JavaVersion.VERSION_1_6
* See the License for the specific language governing permissions and
* limitations under the License.
*/
sourceSets.test.java.srcDir 'src/main/java'


apply plugin: 'osgi'
apply plugin: 'groovy'

sourceCompatibility = JavaVersion.VERSION_1_6
targetCompatibility = JavaVersion.VERSION_1_6

tasks.withType(Javadoc).each {
it.classpath = sourceSets.main.compileClasspath
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.protocol.http.client;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Func1;

/**
* An operator to be used for a source of {@link HttpClientResponse} containing aggregated responses i.e. which does not
* have multiple HTTP chunks. This operator simplifies the handling of such a responses by flattening the content
* {@link Observable} into a single element producing a {@link ResponseHolder} object.
* See <a href="https://github.com/Netflix/RxNetty/issues/187">related issue</a> for details.
*
* @author Nitesh Kant
*/
public class FlatResponseOperator<T>
implements Observable.Operator<ResponseHolder<T>, HttpClientResponse<T>> {

public static <T> FlatResponseOperator<T> flatResponse() {
return new FlatResponseOperator<T>();
}

@Override
public Subscriber<? super HttpClientResponse<T>> call(final Subscriber<? super ResponseHolder<T>> child) {
return new Subscriber<HttpClientResponse<T>>() {
@Override
public void onCompleted() {
// Content complete propagates to the child subscriber.
}

@Override
public void onError(Throwable e) {
child.onError(e);
}

@Override
public void onNext(final HttpClientResponse<T> response) {
response.getContent()
.take(1)
.map(new Func1<T, ResponseHolder<T>>() {
@Override
public ResponseHolder<T> call(T t) {
return new ResponseHolder<T>(response, t);
}
}).subscribe(child);
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.protocol.http.client;

/**
* @author Nitesh Kant
*/
public class ResponseHolder<T> {

private final HttpClientResponse<T> response;
private final T content;

public ResponseHolder(HttpClientResponse<T> response, T content) {
this.response = response;
this.content = content;
}

public HttpClientResponse<T> getResponse() {
return response;
}

public T getContent() {
return content;
}
}

0 comments on commit 284a039

Please sign in to comment.