Search in sources :

Example 11 with HttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponse in project riposte by Nike-Inc.

the class ProcessFinalResponseOutputHandler method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    // Deal with the final outbound HttpResponse
    if (msg instanceof HttpResponse) {
        HttpProcessingState state = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
        if (state != null)
            state.setActualResponseObject((HttpResponse) msg);
    }
    // Deal with the final outbound body content
    if (msg instanceof HttpContent) {
        HttpProcessingState state = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
        if (state != null && state.getResponseInfo() != null) {
            ResponseInfo<?> responseInfo = state.getResponseInfo();
            long contentBytes = ((HttpContent) msg).content().readableBytes();
            if (responseInfo.getFinalContentLength() == null)
                responseInfo.setFinalContentLength(contentBytes);
            else
                responseInfo.setFinalContentLength(responseInfo.getFinalContentLength() + contentBytes);
        }
    }
    super.write(ctx, msg, promise);
}
Also used : HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) HttpResponse(io.netty.handler.codec.http.HttpResponse) HttpContent(io.netty.handler.codec.http.HttpContent)

Example 12 with HttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponse in project riposte by Nike-Inc.

the class SmartHttpContentCompressor method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    HttpProcessingState state = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
    allowCompressionForThisRequest = false;
    if (state != null) {
        // We only want to allow compression if the endpoint being hit is *not* a ProxyRouterEndpoint, the response is full, and the response size
        // is greater than the threshold
        boolean isFull = msg instanceof HttpResponse && msg instanceof LastHttpContent;
        boolean endpointAllowed = endpointAllowsCompression(state.getEndpointForExecution());
        boolean responseInfoAllowed = state.getResponseInfo() == null || !state.getResponseInfo().isPreventCompressedOutput();
        if (isFull && endpointAllowed && responseInfoAllowed && ((LastHttpContent) msg).content().readableBytes() > responseSizeThresholdBytes) {
            allowCompressionForThisRequest = true;
        }
    }
    super.write(ctx, msg, promise);
}
Also used : HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) HttpResponse(io.netty.handler.codec.http.HttpResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Example 13 with HttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponse in project riposte by Nike-Inc.

the class ProcessFinalResponseOutputHandlerTest method write_calls_setActualResponseObject_on_state_if_msg_is_HttpResponse.

@Test
public void write_calls_setActualResponseObject_on_state_if_msg_is_HttpResponse() throws Exception {
    // given
    HttpResponse msgMock = mock(HttpResponse.class);
    // when
    handler.write(ctxMock, msgMock, promiseMock);
    // then
    verify(stateMock).setActualResponseObject(msgMock);
}
Also used : HttpResponse(io.netty.handler.codec.http.HttpResponse) Test(org.junit.Test)

Example 14 with HttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponse in project riposte by Nike-Inc.

the class ProcessFinalResponseOutputHandlerTest method write_does_not_do_anything_with_state_if_msg_is_HttpResponse_but_state_is_null.

@Test
public void write_does_not_do_anything_with_state_if_msg_is_HttpResponse_but_state_is_null() throws Exception {
    // given
    HttpResponse msgMock = mock(HttpResponse.class);
    doReturn(null).when(stateAttrMock).get();
    // when
    handler.write(ctxMock, msgMock, promiseMock);
    // then
    verifyNoMoreInteractions(stateMock);
}
Also used : HttpResponse(io.netty.handler.codec.http.HttpResponse) Test(org.junit.Test)

Example 15 with HttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponse in project apn-proxy by apn-proxy.

the class ApnProxyRemoteForwardHandler method channelRead.

public void channelRead(final ChannelHandlerContext remoteChannelCtx, final Object msg) throws Exception {
    LoggerUtil.debug(logger, uaChannel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY), "Remote msg", msg);
    remainMsgCount++;
    if (remainMsgCount <= 5) {
        remoteChannelCtx.read();
    }
    HttpObject ho = (HttpObject) msg;
    if (ho instanceof HttpResponse) {
        HttpResponse httpResponse = (HttpResponse) ho;
        LoggerUtil.info(forwardRestLogger, uaChannel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY), httpResponse.getStatus(), httpResponse.getProtocolVersion());
        httpResponse.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
        httpResponse.headers().set("Proxy-Connection", HttpHeaders.Values.KEEP_ALIVE);
    }
    if (uaChannel.isActive()) {
        uaChannel.writeAndFlush(ho).addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                LoggerUtil.debug(logger, uaChannel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY), "Write to UA finished: " + future.isSuccess());
                if (future.isSuccess()) {
                    remainMsgCount--;
                    remoteChannelCtx.read();
                    LoggerUtil.debug(logger, uaChannel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY), "Fire read again");
                } else {
                    remoteChannelCtx.close();
                }
            }
        });
    } else {
        remoteChannelCtx.close();
    }
}
Also used : HttpObject(io.netty.handler.codec.http.HttpObject) HttpResponse(io.netty.handler.codec.http.HttpResponse)

Aggregations

HttpResponse (io.netty.handler.codec.http.HttpResponse)230 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)103 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)56 Test (org.junit.jupiter.api.Test)56 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)54 HttpRequest (io.netty.handler.codec.http.HttpRequest)52 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)47 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)40 Test (org.junit.Test)39 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)38 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)35 HttpContent (io.netty.handler.codec.http.HttpContent)32 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)25 ByteBuf (io.netty.buffer.ByteBuf)18 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)17 ChannelFuture (io.netty.channel.ChannelFuture)16 UtilsTest (com.github.ambry.utils.UtilsTest)15 IOException (java.io.IOException)15 Map (java.util.Map)15 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)13