Search in sources :

Example 61 with FullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project jocean-http by isdom.

the class DefaultSignalClientTestCase method buildResponse.

private static Observable<HttpObject> buildResponse(final Object responseBean, final Action1<Action0> onTerminate) {
    final byte[] responseBytes = JSON.toJSONBytes(responseBean);
    final FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, OK, Unpooled.wrappedBuffer(responseBytes));
    onTerminate.call(new Action0() {

        @Override
        public void call() {
            final boolean released = response.release();
            if (LOG.isDebugEnabled()) {
                LOG.debug("buildBytesResponse release {} released({})", response, released);
            }
        }
    });
    response.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json");
    response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
    return Observable.<HttpObject>just(response);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) Action0(rx.functions.Action0) HttpObject(io.netty.handler.codec.http.HttpObject) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse)

Example 62 with FullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project jocean-http by isdom.

the class DefaultSignalClientTestCase method buildBytesResponse.

private static Observable<HttpObject> buildBytesResponse(final byte[] bodyAsBytes, final Action1<Action0> onTerminate) {
    final FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, OK, Unpooled.wrappedBuffer(bodyAsBytes));
    onTerminate.call(new Action0() {

        @Override
        public void call() {
            final boolean released = response.release();
            if (LOG.isDebugEnabled()) {
                LOG.debug("buildBytesResponse release {} released({})", response, released);
            }
        }
    });
    response.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json");
    response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
    return Observable.<HttpObject>just(response);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) Action0(rx.functions.Action0) HttpObject(io.netty.handler.codec.http.HttpObject) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse)

Example 63 with FullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project jocean-http by isdom.

the class NettysTestCase method test_httpobjs2fullresp_misshttpresp.

@Test
public final void test_httpobjs2fullresp_misshttpresp() throws Exception {
    final HttpContent[] resp_contents = Nettys4Test.buildContentArray(REQ_CONTENT.getBytes(Charsets.UTF_8), 1);
    final List<HttpObject> resps = new ArrayList<HttpObject>() {

        private static final long serialVersionUID = 1L;

        {
            this.addAll(Arrays.asList(resp_contents));
            this.add(LastHttpContent.EMPTY_LAST_CONTENT);
        }
    };
    RxActions.applyArrayBy(resp_contents, new Action1<HttpContent>() {

        @Override
        public void call(final HttpContent c) {
            assertEquals(1, c.content().refCnt());
        }
    });
    FullHttpResponse fullresp = null;
    thrown.expect(RuntimeException.class);
    try {
        fullresp = Nettys.httpobjs2fullresp(resps);
    } finally {
        assertNull(fullresp);
        RxActions.applyArrayBy(resp_contents, new Action1<HttpContent>() {

            @Override
            public void call(final HttpContent c) {
                assertEquals(1, c.content().refCnt());
            }
        });
    }
}
Also used : HttpObject(io.netty.handler.codec.http.HttpObject) ArrayList(java.util.ArrayList) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) Test(org.junit.Test)

Example 64 with FullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project jocean-http by isdom.

the class NettysTestCase method test_httpobjs2fullresp_latercontentdisposed.

@Test
public final void test_httpobjs2fullresp_latercontentdisposed() throws Exception {
    final DefaultHttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    final HttpContent[] resp_contents = Nettys4Test.buildContentArray(REQ_CONTENT.getBytes(Charsets.UTF_8), 1);
    final List<HttpObject> resps = new ArrayList<HttpObject>() {

        private static final long serialVersionUID = 1L;

        {
            this.add(response);
            this.addAll(Arrays.asList(resp_contents));
            this.add(LastHttpContent.EMPTY_LAST_CONTENT);
        }
    };
    RxActions.applyArrayBy(resp_contents, new Action1<HttpContent>() {

        @Override
        public void call(final HttpContent c) {
            assertEquals(1, c.content().refCnt());
        }
    });
    // release [0]'s content
    resp_contents[resp_contents.length - 1].release();
    FullHttpResponse fullresp = null;
    thrown.expect(IllegalReferenceCountException.class);
    try {
        fullresp = Nettys.httpobjs2fullresp(resps);
    } finally {
        assertNull(fullresp);
        RxActions.applyArrayBy(Arrays.copyOfRange(resp_contents, 0, resp_contents.length - 1), new Action1<HttpContent>() {

            @Override
            public void call(final HttpContent c) {
                assertEquals(1, c.content().refCnt());
            }
        });
    }
}
Also used : DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) HttpObject(io.netty.handler.codec.http.HttpObject) ArrayList(java.util.ArrayList) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) Test(org.junit.Test)

Example 65 with FullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpResponse in project jocean-http by isdom.

the class NettysTestCase method test_httpobjs2fullresp_success2.

@Test
public final void test_httpobjs2fullresp_success2() throws Exception {
    final FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, OK, Nettys4Test.buildByteBuf(REQ_CONTENT));
    response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
    response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
    assertEquals(1, response.refCnt());
    final FullHttpResponse fullresp = Nettys.httpobjs2fullresp(Arrays.<HttpObject>asList(response));
    assertNotNull(fullresp);
    assertEquals(REQ_CONTENT, new String(Nettys.dumpByteBufAsBytes(fullresp.content()), Charsets.UTF_8));
    assertEquals(2, response.refCnt());
    fullresp.release();
    assertEquals(1, response.refCnt());
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) Test(org.junit.Test)

Aggregations

FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)261 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)174 ByteBuf (io.netty.buffer.ByteBuf)53 Test (org.junit.Test)50 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)38 HttpRequest (io.netty.handler.codec.http.HttpRequest)34 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)24 HttpObject (io.netty.handler.codec.http.HttpObject)23 IOException (java.io.IOException)23 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)23 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)22 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)21 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)21 Test (org.junit.jupiter.api.Test)21 ChannelFuture (io.netty.channel.ChannelFuture)20 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)20 HttpResponse (io.netty.handler.codec.http.HttpResponse)20 HttpInitiator (org.jocean.http.client.HttpClient.HttpInitiator)20 Subscription (rx.Subscription)20 LocalAddress (io.netty.channel.local.LocalAddress)19