use of io.gravitee.gateway.api.stream.BufferedReadWriteStream in project gravitee-gateway by gravitee-io.
the class OverrideRequestContentPolicy method onRequestContent.
@OnRequestContent
public ReadWriteStream onRequestContent(Request request) {
return new BufferedReadWriteStream() {
@Override
public SimpleReadWriteStream<Buffer> write(Buffer content) {
// We dot want to get the request content, skipping
return this;
}
@Override
public void end() {
Buffer content = Buffer.buffer(STREAM_POLICY_CONTENT);
request.headers().set(HttpHeaders.CONTENT_LENGTH, Integer.toString(content.length()));
// Write content
super.write(content);
// Mark the end of content
super.end();
}
};
}
use of io.gravitee.gateway.api.stream.BufferedReadWriteStream 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);
}
use of io.gravitee.gateway.api.stream.BufferedReadWriteStream 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);
}
use of io.gravitee.gateway.api.stream.BufferedReadWriteStream in project gravitee-gateway by gravitee-io.
the class OverrideResponseContentPolicy method onResponseContent.
@OnResponseContent
public ReadWriteStream onResponseContent(Request request) {
return new BufferedReadWriteStream() {
@Override
public SimpleReadWriteStream<Buffer> write(Buffer content) {
// We dot want to get the request content, skipping
return this;
}
@Override
public void end() {
Buffer content = Buffer.buffer(STREAM_POLICY_CONTENT);
// Write content
super.write(content);
// Mark the end of content
super.end();
}
};
}
use of io.gravitee.gateway.api.stream.BufferedReadWriteStream in project gravitee-gateway by gravitee-io.
the class TransformRequestContentPolicy method onRequestContent.
@OnRequestContent
public ReadWriteStream onRequestContent(Request request, ExecutionContext executionContext) {
return new BufferedReadWriteStream() {
Buffer buffer = Buffer.buffer();
@Override
public SimpleReadWriteStream<Buffer> write(Buffer content) {
// We dot want to get the request content, skipping
buffer.appendBuffer(content);
return this;
}
@Override
public void end() {
String content = executionContext.getTemplateEngine().convert(buffer.toString());
Buffer contentBuf = Buffer.buffer(content);
request.headers().set(HttpHeaders.CONTENT_LENGTH, Integer.toString(contentBuf.length()));
// Write content
super.write(contentBuf);
// Mark the end of content
super.end();
}
};
}
Aggregations