use of io.gravitee.gateway.api.buffer.Buffer 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.buffer.Buffer 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);
});
}
}
use of io.gravitee.gateway.api.buffer.Buffer in project gravitee-gateway by gravitee-io.
the class DefaultInvoker method invoke.
@Override
public Request invoke(ExecutionContext executionContext, Request serverRequest, ReadStream<Buffer> stream, Handler<ProxyConnection> connectionHandler) {
TargetEndpoint targetEndpoint = selectEndpoint(serverRequest, executionContext);
if (!targetEndpoint.isReachable()) {
DirectProxyConnection statusOnlyConnection = new DirectProxyConnection(HttpStatusCode.SERVICE_UNAVAILABLE_503);
connectionHandler.handle(statusOnlyConnection);
statusOnlyConnection.sendResponse();
} else {
// Remove duplicate slash
String uri = DUPLICATE_SLASH_REMOVER.matcher(targetEndpoint.uri).replaceAll(URI_PATH_SEPARATOR);
URI requestUri = null;
try {
requestUri = encodeQueryParameters(serverRequest, uri);
} catch (Exception ex) {
serverRequest.metrics().setMessage(getStackTraceAsString(ex));
// Request URI is not correct nor correctly encoded, returning a bad request
DirectProxyConnection statusOnlyConnection = new DirectProxyConnection(HttpStatusCode.BAD_REQUEST_400);
connectionHandler.handle(statusOnlyConnection);
statusOnlyConnection.sendResponse();
}
if (requestUri != null) {
uri = requestUri.toString();
// Add the endpoint reference in metrics to know which endpoint has been invoked while serving the request
serverRequest.metrics().setEndpoint(uri);
final HttpMethod httpMethod = extractHttpMethod(executionContext, serverRequest);
ProxyRequest proxyRequest = ProxyRequestBuilder.from(serverRequest).uri(requestUri).method(httpMethod).headers(setProxyHeaders(serverRequest.headers(), requestUri, targetEndpoint.endpoint)).build();
ProxyConnection proxyConnection = targetEndpoint.endpoint.connector().request(proxyRequest);
// Enable logging at proxy level
if (api.getProxy().getLoggingMode().isProxyMode()) {
proxyConnection = new LoggableProxyConnection(proxyConnection, proxyRequest);
}
connectionHandler.handle(proxyConnection);
// Plug underlying stream to connection stream
ProxyConnection finalProxyConnection = proxyConnection;
stream.bodyHandler(buffer -> {
finalProxyConnection.write(buffer);
if (finalProxyConnection.writeQueueFull()) {
serverRequest.pause();
finalProxyConnection.drainHandler(aVoid -> serverRequest.resume());
}
}).endHandler(aVoid -> finalProxyConnection.end());
}
}
// Resume the incoming request to handle content and end
serverRequest.resume();
return serverRequest;
}
use of io.gravitee.gateway.api.buffer.Buffer in project gravitee-gateway by gravitee-io.
the class ApiReactorHandler method sendPolicyFailure.
private void sendPolicyFailure(PolicyResult policyResult, Response response) {
response.status(policyResult.httpStatusCode());
response.headers().set(HttpHeaders.CONNECTION, HttpHeadersValues.CONNECTION_CLOSE);
if (policyResult.message() != null) {
try {
Buffer payload;
if (policyResult.contentType().equalsIgnoreCase(MediaType.APPLICATION_JSON)) {
payload = Buffer.buffer(policyResult.message());
} else {
String contentAsJson = mapper.writeValueAsString(new PolicyResultAsJson(policyResult));
payload = Buffer.buffer(contentAsJson);
}
response.headers().set(HttpHeaders.CONTENT_LENGTH, Integer.toString(payload.length()));
response.headers().set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
response.write(payload);
} catch (JsonProcessingException jpe) {
logger.error("Unable to transform a policy result into a json payload", jpe);
}
}
response.end();
}
use of io.gravitee.gateway.api.buffer.Buffer 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();
}
};
}
Aggregations