use of io.gravitee.gateway.policy.Policy in project gravitee-gateway by gravitee-io.
the class ApiPolicyChainResolver method calculate.
@Override
protected List<Policy> calculate(StreamType streamType, Request request, Response response, ExecutionContext executionContext) {
// Resolve the "configured" path according to the inbound request
Path path = pathResolver.resolve(request.path());
executionContext.setAttribute(ExecutionContext.ATTR_RESOLVED_PATH, path.getResolvedPath());
return path.getRules().stream().filter(rule -> rule.isEnabled() && rule.getMethods().contains(request.method())).map(rule -> create(streamType, rule.getPolicy().getName(), rule.getPolicy().getConfiguration())).filter(Objects::nonNull).collect(Collectors.toList());
}
use of io.gravitee.gateway.policy.Policy 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);
});
}
}
Aggregations