Search in sources :

Example 91 with HttpContent

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpContent in project cdap by caskdata.

the class AuthenticationChannelHandler method channelRead.

/**
 * Decode the AccessTokenIdentifier passed as a header and set it in a ThreadLocal.
 * Returns a 401 if the identifier is malformed.
 */
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof HttpRequest) {
        // TODO: authenticate the user using user id - CDAP-688
        HttpRequest request = (HttpRequest) msg;
        currentUserId = request.headers().get(Constants.Security.Headers.USER_ID);
        currentUserIP = request.headers().get(Constants.Security.Headers.USER_IP);
        SecurityRequestContext.setUserId(currentUserId);
        SecurityRequestContext.setUserIP(currentUserIP);
    } else if (msg instanceof HttpContent) {
        SecurityRequestContext.setUserId(currentUserId);
        SecurityRequestContext.setUserIP(currentUserIP);
    }
    ctx.fireChannelRead(msg);
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpContent(io.netty.handler.codec.http.HttpContent)

Example 92 with HttpContent

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

the class DefaultHttpTradeTestCase method testTradeForCallAbortAfterPartRequestThenPushError.

@Test
public final void testTradeForCallAbortAfterPartRequestThenPushError() {
    final DefaultHttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
    final HttpContent[] req_contents = Nettys4Test.buildContentArray(REQ_CONTENT.getBytes(Charsets.UTF_8), 1);
    final EmbeddedChannel channel = new EmbeddedChannel();
    final HttpTrade trade = new DefaultHttpTrade(channel);
    // trade.inboundHolder().setMaxBlockSize(-1);
    final TestSubscriber<DisposableWrapper<HttpObject>> reqSubscriber = new TestSubscriber<>();
    trade.inbound().subscribe(reqSubscriber);
    writeToInboundAndFlush(channel, request);
    writeToInboundAndFlush(channel, req_contents[0]);
    trade.close();
    assertFalse(trade.isActive());
    /* TODO, fix no terminal event
        reqSubscriber.assertTerminalEvent();
        reqSubscriber.assertError(Exception.class);
        */
    reqSubscriber.assertValueCount(2);
    reqSubscriber.assertValues(RxNettys.<HttpObject>wrap4release(request), RxNettys.<HttpObject>wrap4release(req_contents[0]));
}
Also used : HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) DisposableWrapper(org.jocean.idiom.DisposableWrapper) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) TestSubscriber(rx.observers.TestSubscriber) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Nettys4Test(org.jocean.http.util.Nettys4Test) Test(org.junit.Test)

Example 93 with HttpContent

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

the class DefaultHttpTradeTestCase method testTradeForRequestPartError.

@Test
public final void testTradeForRequestPartError() {
    final DefaultHttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
    final HttpContent[] req_contents = Nettys4Test.buildContentArray(REQ_CONTENT.getBytes(Charsets.UTF_8), 1);
    final List<DisposableWrapper<HttpObject>> part_req = new ArrayList<DisposableWrapper<HttpObject>>() {

        private static final long serialVersionUID = 1L;

        {
            this.add(RxNettys.<HttpObject>wrap4release(request));
            for (int idx = 0; idx < 5; idx++) {
                this.add(RxNettys.<HttpObject>wrap4release(req_contents[idx]));
            }
        }
    };
    final EmbeddedChannel channel = new EmbeddedChannel();
    final HttpTrade trade = new DefaultHttpTrade(channel);
    // trade.inboundHolder().setMaxBlockSize(-1);
    final TestSubscriber<DisposableWrapper<HttpObject>> reqSubscriber = new TestSubscriber<>();
    trade.inbound().subscribe(reqSubscriber);
    Observable.<HttpObject>concat(Observable.<HttpObject>just(request), Observable.<HttpObject>just(req_contents[0]), Observable.<HttpObject>just(req_contents[1]), Observable.<HttpObject>just(req_contents[2]), Observable.<HttpObject>just(req_contents[3]), Observable.<HttpObject>just(req_contents[4])).map(obj -> writeToInboundAndFlush(channel, obj)).last().toBlocking().single().syncUninterruptibly();
    channel.disconnect().syncUninterruptibly();
    assertTrue(!trade.isActive());
    // TODO, fix assert terminal event
    // reqSubscriber.assertTerminalEvent();
    // java.lang.AssertionError: Exceptions differ; expected: java.lang.RuntimeException: RequestPartError,
    // actual: java.lang.RuntimeException: trade unactived
    // reqSubscriber.assertError(Exception.class);
    reqSubscriber.assertNotCompleted();
    reqSubscriber.assertValues(part_req.toArray(new DisposableWrapper[0]));
}
Also used : DisposableWrapper(org.jocean.idiom.DisposableWrapper) ArrayList(java.util.ArrayList) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpObject(io.netty.handler.codec.http.HttpObject) TestSubscriber(rx.observers.TestSubscriber) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Nettys4Test(org.jocean.http.util.Nettys4Test) Test(org.junit.Test)

Example 94 with HttpContent

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

the class DefaultHttpTradeTestCase method testTradeForCompleteRoundAndWithCacheOperator.

@Test
public final void testTradeForCompleteRoundAndWithCacheOperator() {
    final DefaultHttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
    final HttpContent[] req_contents = Nettys4Test.buildContentArray(REQ_CONTENT.getBytes(Charsets.UTF_8), 1);
    final EmbeddedChannel channel = new EmbeddedChannel();
    final HttpTrade trade = new DefaultHttpTrade(channel);
    Observable.<HttpObject>concat(Observable.<HttpObject>just(request), Observable.<HttpObject>from(req_contents), Observable.<HttpObject>just(LastHttpContent.EMPTY_LAST_CONTENT)).map(obj -> writeToInboundAndFlush(channel, obj)).last().toBlocking().single().syncUninterruptibly();
    // Error, will cause : exception when invoke visitor(org.jocean.http.util.RxNettys$2@722c517b), detail:
    // java.lang.ClassCastException: io.netty.handler.codec.http.DefaultHttpRequest cannot be cast to
    // io.netty.handler.codec.http.HttpContent
    // inbound.subscribe();    double subscribe holder.assembleAndHold()
    final FullHttpRequest recvreq = trade.inbound().compose(RxNettys.message2fullreq(trade)).toBlocking().single().unwrap();
    assertNotNull(recvreq);
    trade.close();
}
Also used : HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpObject(io.netty.handler.codec.http.HttpObject) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) Nettys4Test(org.jocean.http.util.Nettys4Test) Test(org.junit.Test)

Example 95 with HttpContent

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

the class NettysTestCase method test_httpobjs2fullresp_firstcontentdisposed.

@Test
public final void test_httpobjs2fullresp_firstcontentdisposed() 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[0].release();
    FullHttpResponse fullresp = null;
    thrown.expect(IllegalReferenceCountException.class);
    try {
        fullresp = Nettys.httpobjs2fullresp(resps);
    } finally {
        assertNull(fullresp);
        RxActions.applyArrayBy(Arrays.copyOfRange(resp_contents, 1, resp_contents.length), 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)

Aggregations

HttpContent (io.netty.handler.codec.http.HttpContent)158 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)122 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)62 DefaultHttpContent (io.netty.handler.codec.http.DefaultHttpContent)59 Test (org.junit.Test)59 ByteBuf (io.netty.buffer.ByteBuf)40 HttpResponse (io.netty.handler.codec.http.HttpResponse)37 HttpRequest (io.netty.handler.codec.http.HttpRequest)35 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)34 ArrayList (java.util.ArrayList)30 HttpObject (io.netty.handler.codec.http.HttpObject)28 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)24 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)20 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)17 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)17 Channel (io.netty.channel.Channel)16 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)15 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)14 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)13 IOException (java.io.IOException)13