Search in sources :

Example 1 with ReadWriteStream

use of io.gravitee.gateway.api.stream.ReadWriteStream in project gravitee-gateway by gravitee-io.

the class StreamablePolicyChain method prepareStreamablePolicyChain.

private void prepareStreamablePolicyChain(final Request request, final Response response) {
    ReadWriteStream<Buffer> previousPolicyStreamer = null;
    for (Policy policy : policies) {
        if (policy.isStreamable()) {
            try {
                // Run OnXXXContent to get ReadWriteStream object
                final ReadWriteStream streamer = stream(policy, request, response, this, executionContext);
                if (streamer != null) {
                    // An handler was never assigned to start the chain, so let's do it
                    if (streamablePolicyHandlerChain == null) {
                        streamablePolicyHandlerChain = streamer;
                    }
                    // Chain policy stream using the previous one
                    if (previousPolicyStreamer != null) {
                        previousPolicyStreamer.bodyHandler(streamer::write);
                        previousPolicyStreamer.endHandler(result1 -> streamer.end());
                    }
                    // Previous stream is now the current policy stream
                    previousPolicyStreamer = streamer;
                }
            } catch (Exception ex) {
                logger.error("Unexpected error while running onXXXXContent for policy {}", policy, ex);
            }
        }
    }
    ReadWriteStream<Buffer> tailPolicyStreamer = previousPolicyStreamer;
    if (streamablePolicyHandlerChain != null && tailPolicyStreamer != null) {
        tailPolicyStreamer.bodyHandler(bodyPart -> {
            if (bodyHandler != null)
                bodyHandler.handle(bodyPart);
        });
        tailPolicyStreamer.endHandler(result -> {
            if (endHandler != null)
                endHandler.handle(result);
        });
    }
}
Also used : Buffer(io.gravitee.gateway.api.buffer.Buffer) Policy(io.gravitee.gateway.policy.Policy) ReadWriteStream(io.gravitee.gateway.api.stream.ReadWriteStream)

Example 2 with ReadWriteStream

use of io.gravitee.gateway.api.stream.ReadWriteStream in project gravitee-gateway by gravitee-io.

the class RequestPolicyChainTest method doNext_streamablePolicies_streaming.

@Test
public void doNext_streamablePolicies_streaming() throws Exception {
    StreamablePolicy policy4 = spy(new StreamablePolicy());
    StreamablePolicy policy5 = spy(new StreamablePolicy());
    ExecutionContext executionContext = mock(ExecutionContext.class);
    ReadWriteStream streamPolicy4 = spy(new BufferedReadWriteStream());
    when(policy4.onRequestContent(any(Request.class), any(Response.class), any(io.gravitee.policy.api.PolicyChain.class), eq(executionContext))).thenReturn(streamPolicy4);
    ReadWriteStream streamPolicy5 = spy(new BufferedReadWriteStream());
    when(policy5.onRequestContent(any(Request.class), any(Response.class), any(io.gravitee.policy.api.PolicyChain.class), eq(executionContext))).thenReturn(streamPolicy5);
    InOrder inOrder = inOrder(streamPolicy4, streamPolicy5);
    PolicyChain chain = RequestPolicyChain.create(Arrays.asList(policy4, policy5), executionContext);
    chain.setResultHandler(result -> {
    });
    chain.bodyHandler(mock(Handler.class));
    chain.endHandler(mock(Handler.class));
    chain.doNext(null, null);
    chain.write(Buffer.buffer("TEST"));
    chain.write(Buffer.buffer("TEST"));
    chain.end();
    inOrder.verify(streamPolicy4, atLeastOnce()).bodyHandler(any(Handler.class));
    inOrder.verify(streamPolicy4, atLeastOnce()).endHandler(any(Handler.class));
    inOrder.verify(streamPolicy5, atLeastOnce()).bodyHandler(any(Handler.class));
    inOrder.verify(streamPolicy5, atLeastOnce()).endHandler(any(Handler.class));
    verify(policy4, atLeastOnce()).onRequest(null, null, chain, executionContext);
}
Also used : ReadWriteStream(io.gravitee.gateway.api.stream.ReadWriteStream) BufferedReadWriteStream(io.gravitee.gateway.api.stream.BufferedReadWriteStream) Response(io.gravitee.gateway.api.Response) PolicyChain(io.gravitee.gateway.policy.impl.PolicyChain) RequestPolicyChain(io.gravitee.gateway.policy.impl.RequestPolicyChain) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) InOrder(org.mockito.InOrder) BufferedReadWriteStream(io.gravitee.gateway.api.stream.BufferedReadWriteStream) Request(io.gravitee.gateway.api.Request) Handler(io.gravitee.gateway.api.handler.Handler) Test(org.junit.Test)

Example 3 with ReadWriteStream

use of io.gravitee.gateway.api.stream.ReadWriteStream in project gravitee-gateway by gravitee-io.

the class RequestPolicyChainTest method doNext_streamablePolicies.

@Test
public void doNext_streamablePolicies() throws Exception {
    StreamablePolicy policy4 = spy(new StreamablePolicy());
    StreamablePolicy policy5 = spy(new StreamablePolicy());
    ExecutionContext executionContext = mock(ExecutionContext.class);
    ReadWriteStream streamPolicy4 = spy(new BufferedReadWriteStream());
    when(policy4.onRequestContent(any(Request.class), any(Response.class), any(io.gravitee.policy.api.PolicyChain.class), eq(executionContext))).thenReturn(streamPolicy4);
    ReadWriteStream streamPolicy5 = spy(new BufferedReadWriteStream());
    when(policy5.onRequestContent(any(Request.class), any(Response.class), any(io.gravitee.policy.api.PolicyChain.class), eq(executionContext))).thenReturn(streamPolicy5);
    InOrder inOrder = inOrder(streamPolicy4, streamPolicy5);
    PolicyChain chain = RequestPolicyChain.create(Arrays.asList(policy4, policy5), executionContext);
    chain.setResultHandler(result -> {
    });
    chain.doNext(null, null);
    inOrder.verify(streamPolicy4, atLeastOnce()).bodyHandler(any(Handler.class));
    inOrder.verify(streamPolicy4, atLeastOnce()).endHandler(any(Handler.class));
    inOrder.verify(streamPolicy5, atLeastOnce()).bodyHandler(any(Handler.class));
    inOrder.verify(streamPolicy5, atLeastOnce()).endHandler(any(Handler.class));
    verify(policy4, atLeastOnce()).onRequest(null, null, chain, executionContext);
}
Also used : ReadWriteStream(io.gravitee.gateway.api.stream.ReadWriteStream) BufferedReadWriteStream(io.gravitee.gateway.api.stream.BufferedReadWriteStream) Response(io.gravitee.gateway.api.Response) PolicyChain(io.gravitee.gateway.policy.impl.PolicyChain) RequestPolicyChain(io.gravitee.gateway.policy.impl.RequestPolicyChain) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) InOrder(org.mockito.InOrder) BufferedReadWriteStream(io.gravitee.gateway.api.stream.BufferedReadWriteStream) Request(io.gravitee.gateway.api.Request) Handler(io.gravitee.gateway.api.handler.Handler) Test(org.junit.Test)

Example 4 with ReadWriteStream

use of io.gravitee.gateway.api.stream.ReadWriteStream in project gravitee-gateway by gravitee-io.

the class RequestPolicyChainTest method doNext_streamablePolicy.

@Test
public void doNext_streamablePolicy() throws Exception {
    StreamablePolicy policy4 = spy(new StreamablePolicy());
    ExecutionContext executionContext = mock(ExecutionContext.class);
    ReadWriteStream stream = spy(new BufferedReadWriteStream());
    when(policy4.onRequestContent(any(Request.class), any(Response.class), any(io.gravitee.policy.api.PolicyChain.class), eq(executionContext))).thenReturn(stream);
    PolicyChain chain = RequestPolicyChain.create(Collections.singletonList(policy4), executionContext);
    chain.setResultHandler(result -> {
    });
    chain.doNext(null, null);
    verify(stream, atLeastOnce()).bodyHandler(any(Handler.class));
    verify(stream, atLeastOnce()).endHandler(any(Handler.class));
    verify(policy4, atLeastOnce()).onRequest(null, null, chain, executionContext);
}
Also used : ReadWriteStream(io.gravitee.gateway.api.stream.ReadWriteStream) BufferedReadWriteStream(io.gravitee.gateway.api.stream.BufferedReadWriteStream) Response(io.gravitee.gateway.api.Response) PolicyChain(io.gravitee.gateway.policy.impl.PolicyChain) RequestPolicyChain(io.gravitee.gateway.policy.impl.RequestPolicyChain) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) BufferedReadWriteStream(io.gravitee.gateway.api.stream.BufferedReadWriteStream) Request(io.gravitee.gateway.api.Request) Handler(io.gravitee.gateway.api.handler.Handler) Test(org.junit.Test)

Example 5 with ReadWriteStream

use of io.gravitee.gateway.api.stream.ReadWriteStream in project gravitee-gateway by gravitee-io.

the class ResponsePolicyChainTest method doNext_streamablePolicies.

@Test
public void doNext_streamablePolicies() throws Exception {
    StreamablePolicy policy4 = spy(new StreamablePolicy());
    StreamablePolicy policy5 = spy(new StreamablePolicy());
    ExecutionContext executionContext = mock(ExecutionContext.class);
    ReadWriteStream streamPolicy4 = spy(new BufferedReadWriteStream());
    when(policy4.onResponseContent(any(Request.class), any(Response.class), any(io.gravitee.policy.api.PolicyChain.class), eq(executionContext))).thenReturn(streamPolicy4);
    ReadWriteStream streamPolicy5 = spy(new BufferedReadWriteStream());
    when(policy5.onResponseContent(any(Request.class), any(Response.class), any(io.gravitee.policy.api.PolicyChain.class), eq(executionContext))).thenReturn(streamPolicy5);
    InOrder inOrder = inOrder(streamPolicy4, streamPolicy5);
    PolicyChain chain = ResponsePolicyChain.create(Arrays.asList(policy4, policy5), executionContext);
    chain.setResultHandler(result -> {
    });
    chain.doNext(null, null);
    inOrder.verify(streamPolicy4, atLeastOnce()).bodyHandler(any(Handler.class));
    inOrder.verify(streamPolicy4, atLeastOnce()).endHandler(any(Handler.class));
    inOrder.verify(streamPolicy5, atLeastOnce()).bodyHandler(any(Handler.class));
    inOrder.verify(streamPolicy5, atLeastOnce()).endHandler(any(Handler.class));
    verify(policy4, atLeastOnce()).onResponse(null, null, chain, executionContext);
}
Also used : ReadWriteStream(io.gravitee.gateway.api.stream.ReadWriteStream) BufferedReadWriteStream(io.gravitee.gateway.api.stream.BufferedReadWriteStream) Response(io.gravitee.gateway.api.Response) PolicyChain(io.gravitee.gateway.policy.impl.PolicyChain) ResponsePolicyChain(io.gravitee.gateway.policy.impl.ResponsePolicyChain) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) InOrder(org.mockito.InOrder) BufferedReadWriteStream(io.gravitee.gateway.api.stream.BufferedReadWriteStream) Request(io.gravitee.gateway.api.Request) Handler(io.gravitee.gateway.api.handler.Handler) Test(org.junit.Test)

Aggregations

ReadWriteStream (io.gravitee.gateway.api.stream.ReadWriteStream)7 ExecutionContext (io.gravitee.gateway.api.ExecutionContext)6 Request (io.gravitee.gateway.api.Request)6 Response (io.gravitee.gateway.api.Response)6 Handler (io.gravitee.gateway.api.handler.Handler)6 BufferedReadWriteStream (io.gravitee.gateway.api.stream.BufferedReadWriteStream)6 PolicyChain (io.gravitee.gateway.policy.impl.PolicyChain)6 Test (org.junit.Test)6 InOrder (org.mockito.InOrder)4 RequestPolicyChain (io.gravitee.gateway.policy.impl.RequestPolicyChain)3 ResponsePolicyChain (io.gravitee.gateway.policy.impl.ResponsePolicyChain)3 Buffer (io.gravitee.gateway.api.buffer.Buffer)1 Policy (io.gravitee.gateway.policy.Policy)1