Example usage for io.netty.handler.codec.http HttpContent duplicate

List of usage examples for io.netty.handler.codec.http HttpContent duplicate

Introduction

In this page you can find the example usage for io.netty.handler.codec.http HttpContent duplicate.

Prototype

@Override
    HttpContent duplicate();

Source Link

Usage

From source file:com.mastfrog.netty.http.client.HttpClientTest.java

License:Open Source License

@Test
public void test() throws Throwable {
    //        if (true) return;
    HttpClient client = HttpClient.builder().build();
    final CookieStore store = new CookieStore();

    //        ResponseFuture h = client.get()setCookieStore(store).setURL(URL.parse("https://timboudreau.com/")).execute(new ResponseHandler<String>(String.class) {
    //        ResponseFuture h = client.get().setURL(URL.parse("http://timboudreau.com/files/INTRNET2.TXT")).execute(new ResponseHandler<String>(String.class){
    //        ResponseFuture h = client.get().setURL(URL.parse("http://mail-vm.timboudreau.org/blog/api-list")).execute(new ResponseHandler<String>(String.class) {
    //        ResponseFuture h = client.get().setURL(URL.parse("http://mail-vm.timboudreau.org")).execute(new ResponseHandler<String>(String.class){
    //        ResponseFuture h = client.get().setURL(URL.parse("http://www.google.com")).execute(new ResponseHandler<String>(String.class){
    //        ResponseFuture h = client.get().setCookieStore(store).setURL(URL.parse("http://hp.timboudreau.org/blog/latest/read")).execute(new ResponseHandler<String>(String.class){
    ResponseFuture h = client.get().setCookieStore(store).setURL(URL.parse("https://www.google.com/"))
            .execute(new ResponseHandler<String>(String.class) {

                @Override//from   w  w w.  j av a  2s. c o m
                protected void receive(HttpResponseStatus status, HttpHeaders headers, String obj) {
                    System.out.println("CALLED BACK WITH '" + obj + "'");
                }

            });
    h.on(State.HeadersReceived.class, new Receiver<HttpResponse>() {

        @Override
        public void receive(HttpResponse object) {
            for (Map.Entry<String, String> e : object.headers().entries()) {
                System.out.println(e.getKey() + ": " + e.getValue());
            }
            System.out.println("COOKIES: " + store);
        }
    });
    h.onAnyEvent(new Receiver<State<?>>() {
        Set<StateType> seen = new HashSet<>();

        @Override
        public void receive(State<?> state) {
            System.out.println("STATE " + state);
            seen.add(state.stateType());
            if (state.get() instanceof HttpContent) {
                HttpContent content = (HttpContent) state.get();
                ByteBuf bb = content.duplicate().content();
                System.out.println("CHUNK " + bb.readableBytes() + " bytes");
            } else if (state.get() instanceof State.FullContentReceived) {
                System.out.println("COOKIES: " + store);
            }
        }
    });
    h.await().throwIfError();
}

From source file:io.urmia.proxy.HttpProxyFrontendHandler.java

License:Open Source License

private void writeToAllOutbounds(final ChannelHandlerContext ctx, final HttpContent msg) {

    writeSet.clear();//  ww w  .j  av a2 s. c om

    final int contentSize = msg.content().writableBytes();

    int i = 0;

    for (final Channel outboundChannel : outboundChannels) {

        final int chIdx = i++;

        outboundChannel.writeAndFlush(msg.duplicate()) // duplicate because different widx
                .addListener(new GenericFutureListener<ChannelFuture>() {
                    @Override
                    public void operationComplete(ChannelFuture future) throws Exception {
                        if (future.isSuccess()) {
                            writtenSizes[chIdx].addAndGet(contentSize);
                            onSuccessfulWrite(ctx, chIdx);
                        } else {
                            log.error("error to write to outbound", future.cause());
                            future.channel().close();
                        }

                    }
                });
    }
}

From source file:org.ebayopensource.scc.filter.NettyResponseProxyFilterTest.java

License:Apache License

@Test
public void testFilterChunkeResponses() throws InterruptedException {
    PolicyManager policyManager = mock(PolicyManager.class);
    AppConfiguration appConfig = mock(AppConfiguration.class);
    when(appConfig.getBoolean(AppConfiguration.KEY_DEBUG_INFO)).thenReturn(true);

    ExecutorService tp = Executors.newCachedThreadPool();
    NettyResponseProxyFilter filter = new NettyResponseProxyFilter(policyManager, tp);

    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.attr(any(AttributeKey.class))).thenReturn(mock(Attribute.class));
    Attribute cachable = mock(Attribute.class);
    when(ctx.attr(NettyRequestProxyFilter.IS_CACHABLE)).thenReturn(cachable);
    when(cachable.get()).thenReturn(Boolean.TRUE);

    Attribute cacheKey = mock(Attribute.class);
    when(ctx.attr(NettyRequestProxyFilter.CACHE_KEY)).thenReturn(cacheKey);
    when(cacheKey.get()).thenReturn("key");

    HttpResponse resp = mock(HttpResponse.class);
    when(resp.getStatus()).thenReturn(HttpResponseStatus.OK);
    filter.filterResponse(resp, ctx);//from w w w . ja v  a 2s . c o  m
    HttpContent httpContent = mock(HttpContent.class);
    when(httpContent.duplicate()).thenReturn(mock(HttpContent.class));
    filter.filterResponse(httpContent, ctx);
    LastHttpContent lastHttpContent = mock(LastHttpContent.class);
    when(lastHttpContent.duplicate()).thenReturn(mock(LastHttpContent.class));
    filter.filterResponse(lastHttpContent, ctx);

    filter.filterResponse(resp, ctx);
    filter.filterResponse(lastHttpContent, ctx);

    tp.awaitTermination(10, TimeUnit.SECONDS);
}