Created
December 16, 2014 23:53
-
-
Save NiteshKant/bf4b31d94e67cd3d791e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /* | |
| * 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; | |
| import io.netty.buffer.ByteBuf; | |
| import io.netty.channel.ChannelDuplexHandler; | |
| import io.netty.channel.ChannelHandlerContext; | |
| import io.netty.channel.ChannelPipeline; | |
| import io.netty.channel.ChannelPromise; | |
| import io.netty.handler.codec.http.HttpRequest; | |
| import io.netty.handler.codec.http.HttpResponse; | |
| import io.reactivex.netty.client.RxClient; | |
| import io.reactivex.netty.pipeline.PipelineConfigurator; | |
| import io.reactivex.netty.protocol.http.client.CompositeHttpClientBuilder; | |
| 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 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 rx.functions.Action1; | |
| import rx.functions.Func1; | |
| import java.nio.charset.Charset; | |
| /** | |
| * @author Nitesh Kant | |
| */ | |
| public class MantisDuplicateResponseDebug { | |
| public static void main(String[] args) { | |
| RxNetty.createHttpServer(8888, new RequestHandler<ByteBuf, ByteBuf>() { | |
| @Override | |
| public Observable<Void> handle(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) { | |
| return response.writeStringAndFlush("Hello World!!"); | |
| } | |
| }).start(); | |
| new CompositeHttpClientBuilder<ByteBuf, ByteBuf>() | |
| .appendPipelineConfigurator( | |
| new PipelineConfigurator<HttpClientResponse<ByteBuf>, HttpClientRequest<ByteBuf>>() { | |
| @Override | |
| public void configureNewPipeline(ChannelPipeline pipeline) { | |
| pipeline.addLast("introspecting-handler", new ChannelDuplexHandler() { | |
| private String uri = "<undefined>"; | |
| @Override | |
| public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) | |
| throws Exception { | |
| if (msg instanceof HttpRequest) { | |
| HttpRequest request = (HttpRequest) msg; | |
| uri = request.getUri(); | |
| // TODO: Replace with logging | |
| System.out.println("Sending request on channel id: " + ctx.channel().toString() + | |
| ", request URI: " + uri); | |
| } | |
| super.write(ctx, msg, promise); | |
| } | |
| @Override | |
| public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { | |
| if (msg instanceof HttpResponse) { | |
| // TODO: Replace with logging | |
| System.out.println("Received response on channel id: " + ctx.channel().toString() + | |
| ", request URI: " + uri); | |
| } | |
| super.channelRead(ctx, msg); | |
| } | |
| }); | |
| } | |
| }) | |
| .build() | |
| .submit(new RxClient.ServerInfo("localhost", 8888), | |
| HttpClientRequest.createGet("/hello"), | |
| new HttpClient.HttpClientConfig.Builder().setFollowRedirect(true).followRedirect(2).build()) | |
| .flatMap(new Func1<HttpClientResponse<ByteBuf>, Observable<ByteBuf>>() { | |
| @Override | |
| public Observable<ByteBuf> call(HttpClientResponse<ByteBuf> r) { | |
| return r.getContent(); | |
| } | |
| }) | |
| .map(new Func1<ByteBuf, String>() { | |
| @Override | |
| public String call(ByteBuf o) { | |
| return o.toString(Charset.defaultCharset()); | |
| } | |
| }) | |
| .toBlocking() | |
| .forEach(new Action1<String>() { | |
| @Override | |
| public void call(String s) { | |
| System.out.println("s = " + s); | |
| } | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment