Search in sources :

Example 1 with Policy

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());
}
Also used : Path(io.gravitee.gateway.handlers.api.path.Path) Objects(java.util.Objects) Policy(io.gravitee.gateway.policy.Policy) List(java.util.List) Response(io.gravitee.gateway.api.Response) Path(io.gravitee.gateway.handlers.api.path.Path) StreamType(io.gravitee.gateway.policy.StreamType) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) Request(io.gravitee.gateway.api.Request) AbstractPolicyChainResolver(io.gravitee.gateway.policy.AbstractPolicyChainResolver) PathResolver(io.gravitee.gateway.handlers.api.path.PathResolver) Autowired(org.springframework.beans.factory.annotation.Autowired) Collectors(java.util.stream.Collectors)

Example 2 with Policy

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);
        });
    }
}
Also used : Buffer(io.gravitee.gateway.api.buffer.Buffer) Policy(io.gravitee.gateway.policy.Policy) ReadWriteStream(io.gravitee.gateway.api.stream.ReadWriteStream)

Aggregations

Policy (io.gravitee.gateway.policy.Policy)2 ExecutionContext (io.gravitee.gateway.api.ExecutionContext)1 Request (io.gravitee.gateway.api.Request)1 Response (io.gravitee.gateway.api.Response)1 Buffer (io.gravitee.gateway.api.buffer.Buffer)1 ReadWriteStream (io.gravitee.gateway.api.stream.ReadWriteStream)1 Path (io.gravitee.gateway.handlers.api.path.Path)1 PathResolver (io.gravitee.gateway.handlers.api.path.PathResolver)1 AbstractPolicyChainResolver (io.gravitee.gateway.policy.AbstractPolicyChainResolver)1 StreamType (io.gravitee.gateway.policy.StreamType)1 List (java.util.List)1 Objects (java.util.Objects)1 Collectors (java.util.stream.Collectors)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1