Search in sources :

Example 11 with ExecutionContext

use of io.gravitee.gateway.api.ExecutionContext 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 12 with ExecutionContext

use of io.gravitee.gateway.api.ExecutionContext 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 13 with ExecutionContext

use of io.gravitee.gateway.api.ExecutionContext in project gravitee-gateway by gravitee-io.

the class ResponsePolicyChainTest method doNext_multiplePolicy_throwError.

@Test
public void doNext_multiplePolicy_throwError() throws Exception {
    ExecutionContext executionContext = mock(ExecutionContext.class);
    Request request = mock(Request.class);
    Metrics metrics = Metrics.on(System.currentTimeMillis()).build();
    when(request.metrics()).thenReturn(metrics);
    PolicyChain chain = ResponsePolicyChain.create(Arrays.asList(policy2, policy3), executionContext);
    chain.setResultHandler(result -> {
    });
    chain.doNext(request, null);
    verify(request, atLeastOnce()).metrics();
    verify(policy3, atLeastOnce()).onResponse(request, null, chain, executionContext);
    verify(policy2, never()).onResponse(request, null, chain, executionContext);
}
Also used : PolicyChain(io.gravitee.gateway.policy.impl.PolicyChain) ResponsePolicyChain(io.gravitee.gateway.policy.impl.ResponsePolicyChain) Metrics(io.gravitee.reporter.api.http.Metrics) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) Request(io.gravitee.gateway.api.Request) Test(org.junit.Test)

Example 14 with ExecutionContext

use of io.gravitee.gateway.api.ExecutionContext in project gravitee-gateway by gravitee-io.

the class ApiKeyAuthenticationHandlerTest method shouldReturnPolicies.

@Test
public void shouldReturnPolicies() {
    ExecutionContext executionContext = mock(ExecutionContext.class);
    List<AuthenticationPolicy> apikeyProviderPolicies = authenticationHandler.handle(executionContext);
    Assert.assertEquals(1, apikeyProviderPolicies.size());
    PluginAuthenticationPolicy policy = (PluginAuthenticationPolicy) apikeyProviderPolicies.iterator().next();
    Assert.assertEquals(policy.name(), ApiKeyAuthenticationHandler.API_KEY_POLICY);
}
Also used : ExecutionContext(io.gravitee.gateway.api.ExecutionContext) AuthenticationPolicy(io.gravitee.gateway.security.core.AuthenticationPolicy) PluginAuthenticationPolicy(io.gravitee.gateway.security.core.PluginAuthenticationPolicy) PluginAuthenticationPolicy(io.gravitee.gateway.security.core.PluginAuthenticationPolicy) Test(org.junit.Test)

Example 15 with ExecutionContext

use of io.gravitee.gateway.api.ExecutionContext 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;
}
Also used : java.util(java.util) HttpHeaders(io.gravitee.common.http.HttpHeaders) URISyntaxException(java.net.URISyntaxException) ProxyRequest(io.gravitee.gateway.api.proxy.ProxyRequest) Autowired(org.springframework.beans.factory.annotation.Autowired) Invoker(io.gravitee.gateway.api.Invoker) LoggableProxyConnection(io.gravitee.gateway.http.core.invoker.logging.LoggableProxyConnection) HttpStatusCode(io.gravitee.common.http.HttpStatusCode) ProxyRequestBuilder(io.gravitee.gateway.api.proxy.builder.ProxyRequestBuilder) Endpoint(io.gravitee.gateway.api.endpoint.Endpoint) Request(io.gravitee.gateway.api.Request) ReadStream(io.gravitee.gateway.api.stream.ReadStream) URI(java.net.URI) Api(io.gravitee.definition.model.Api) PrintWriter(java.io.PrintWriter) QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) MalformedURLException(java.net.MalformedURLException) UrlEscapers(com.google.common.net.UrlEscapers) StringWriter(java.io.StringWriter) DirectProxyConnection(io.gravitee.gateway.http.core.direct.DirectProxyConnection) ProxyConnection(io.gravitee.gateway.api.proxy.ProxyConnection) EndpointManager(io.gravitee.gateway.api.endpoint.EndpointManager) Buffer(io.gravitee.gateway.api.buffer.Buffer) Handler(io.gravitee.gateway.api.handler.Handler) HttpMethod(io.gravitee.common.http.HttpMethod) QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) Pattern(java.util.regex.Pattern) DirectProxyConnection(io.gravitee.gateway.http.core.direct.DirectProxyConnection) LoggableProxyConnection(io.gravitee.gateway.http.core.invoker.logging.LoggableProxyConnection) DirectProxyConnection(io.gravitee.gateway.http.core.direct.DirectProxyConnection) ProxyConnection(io.gravitee.gateway.api.proxy.ProxyConnection) LoggableProxyConnection(io.gravitee.gateway.http.core.invoker.logging.LoggableProxyConnection) ProxyRequest(io.gravitee.gateway.api.proxy.ProxyRequest) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) HttpMethod(io.gravitee.common.http.HttpMethod)

Aggregations

ExecutionContext (io.gravitee.gateway.api.ExecutionContext)28 Request (io.gravitee.gateway.api.Request)22 Test (org.junit.Test)21 Response (io.gravitee.gateway.api.Response)19 PolicyChain (io.gravitee.gateway.policy.impl.PolicyChain)12 Handler (io.gravitee.gateway.api.handler.Handler)9 PolicyChain (io.gravitee.policy.api.PolicyChain)9 SubscriptionRepository (io.gravitee.repository.management.api.SubscriptionRepository)7 SubscriptionCriteria (io.gravitee.repository.management.api.search.SubscriptionCriteria)7 BufferedReadWriteStream (io.gravitee.gateway.api.stream.BufferedReadWriteStream)6 ReadWriteStream (io.gravitee.gateway.api.stream.ReadWriteStream)6 ResponsePolicyChain (io.gravitee.gateway.policy.impl.ResponsePolicyChain)6 RequestPolicyChain (io.gravitee.gateway.policy.impl.RequestPolicyChain)5 Subscription (io.gravitee.repository.management.model.Subscription)5 AuthenticationPolicy (io.gravitee.gateway.security.core.AuthenticationPolicy)4 PluginAuthenticationPolicy (io.gravitee.gateway.security.core.PluginAuthenticationPolicy)4 InOrder (org.mockito.InOrder)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 HttpHeaders (io.gravitee.common.http.HttpHeaders)3 HttpStatusCode (io.gravitee.common.http.HttpStatusCode)3