Search in sources :

Example 11 with Response

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

the class AbstractPolicy method onRequest.

public void onRequest(Object... args) throws PolicyException {
    ExecutionContext executionContext = getParameterAssignableTo(ExecutionContext.class, args);
    PolicyChain policyChain = getParameterAssignableTo(PolicyChain.class, args);
    Request request = getParameterAssignableTo(Request.class, args);
    Response response = getParameterAssignableTo(Response.class, args);
    this.onRequest(request, response, policyChain, executionContext);
}
Also used : Response(io.gravitee.gateway.api.Response) PolicyChain(io.gravitee.policy.api.PolicyChain) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) Request(io.gravitee.gateway.api.Request)

Example 12 with Response

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

the class ApiReactorHandler method handleClientRequest.

private void handleClientRequest(Request serverRequest, Response serverResponse, ExecutionContext executionContext, Handler<Response> handler) {
    serverRequest.pause();
    // Apply request policies
    RequestPolicyChainProcessor requestPolicyChain = new RequestPolicyChainProcessor(policyResolvers);
    requestPolicyChain.setResultHandler(requestPolicyChainResult -> {
        if (requestPolicyChainResult.isFailure()) {
            sendPolicyFailure(requestPolicyChainResult.getPolicyResult(), serverResponse);
            handler.handle(serverResponse);
        } else {
            // Call an invoker to get a proxy connection (connection to an underlying backend, mainly HTTP)
            Invoker upstreamInvoker = (Invoker) executionContext.getAttribute(ExecutionContext.ATTR_INVOKER);
            long serviceInvocationStart = System.currentTimeMillis();
            AtomicReference<ProxyConnection> proxyConnection = new AtomicReference<>();
            Request invokeRequest = upstreamInvoker.invoke(executionContext, serverRequest, requestPolicyChainResult.getPolicyChain(), connection -> {
                proxyConnection.set(connection);
                connection.responseHandler(proxyResponse -> handleProxyResponse(serverRequest, serverResponse, executionContext, proxyResponse, serviceInvocationStart, handler));
                requestPolicyChain.setStreamErrorHandler(result -> {
                    connection.cancel();
                    sendPolicyFailure(result.getPolicyResult(), serverResponse);
                    handler.handle(serverResponse);
                });
            });
            // Plug server request stream to request policy chain stream
            invokeRequest.bodyHandler(chunk -> requestPolicyChainResult.getPolicyChain().write(chunk)).endHandler(aVoid -> requestPolicyChainResult.getPolicyChain().end());
        }
    });
    requestPolicyChain.execute(serverRequest, serverResponse, executionContext);
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) CorsHandler(io.gravitee.gateway.handlers.api.cors.CorsHandler) HttpHeaders(io.gravitee.common.http.HttpHeaders) PolicyChain(io.gravitee.gateway.policy.impl.PolicyChain) LoggerFactory(org.slf4j.LoggerFactory) HttpHeadersValues(io.gravitee.common.http.HttpHeadersValues) Autowired(org.springframework.beans.factory.annotation.Autowired) Invoker(io.gravitee.gateway.api.Invoker) PolicyManager(io.gravitee.gateway.policy.PolicyManager) AtomicReference(java.util.concurrent.atomic.AtomicReference) InitializingBean(org.springframework.beans.factory.InitializingBean) ArrayList(java.util.ArrayList) HttpStatusCode(io.gravitee.common.http.HttpStatusCode) LoggableClientResponse(io.gravitee.gateway.handlers.api.logging.LoggableClientResponse) RequestPolicyChainProcessor(io.gravitee.gateway.policy.impl.RequestPolicyChainProcessor) LoggableClientRequest(io.gravitee.gateway.handlers.api.logging.LoggableClientRequest) Response(io.gravitee.gateway.api.Response) StreamType(io.gravitee.gateway.policy.StreamType) Reactable(io.gravitee.gateway.reactor.Reactable) Request(io.gravitee.gateway.api.Request) DefaultInvoker(io.gravitee.gateway.http.core.invoker.DefaultInvoker) Logger(org.slf4j.Logger) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) PolicyChainResolver(io.gravitee.gateway.policy.PolicyChainResolver) TemplateContext(io.gravitee.gateway.api.expression.TemplateContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DirectProxyConnection(io.gravitee.gateway.http.core.direct.DirectProxyConnection) Throwables(com.google.common.base.Throwables) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ProxyConnection(io.gravitee.gateway.api.proxy.ProxyConnection) Buffer(io.gravitee.gateway.api.buffer.Buffer) Handler(io.gravitee.gateway.api.handler.Handler) CorsResponseHandler(io.gravitee.gateway.handlers.api.cors.CorsResponseHandler) EndpointLifecycleManager(io.gravitee.gateway.http.core.endpoint.EndpointLifecycleManager) ResourceLifecycleManager(io.gravitee.gateway.resource.ResourceLifecycleManager) List(java.util.List) MediaType(io.gravitee.common.http.MediaType) ProxyResponse(io.gravitee.gateway.api.proxy.ProxyResponse) ExecutionContextFactory(io.gravitee.gateway.handlers.api.context.ExecutionContextFactory) Cors(io.gravitee.definition.model.Cors) TemplateVariableProvider(io.gravitee.gateway.api.expression.TemplateVariableProvider) PlanPolicyChainResolver(io.gravitee.gateway.handlers.api.policy.plan.PlanPolicyChainResolver) Api(io.gravitee.gateway.handlers.api.definition.Api) AbstractReactorHandler(io.gravitee.gateway.reactor.handler.AbstractReactorHandler) PolicyResult(io.gravitee.policy.api.PolicyResult) SecurityPolicyChainResolver(io.gravitee.gateway.security.core.SecurityPolicyChainResolver) ApiPolicyChainResolver(io.gravitee.gateway.handlers.api.policy.api.ApiPolicyChainResolver) Invoker(io.gravitee.gateway.api.Invoker) DefaultInvoker(io.gravitee.gateway.http.core.invoker.DefaultInvoker) DirectProxyConnection(io.gravitee.gateway.http.core.direct.DirectProxyConnection) ProxyConnection(io.gravitee.gateway.api.proxy.ProxyConnection) LoggableClientRequest(io.gravitee.gateway.handlers.api.logging.LoggableClientRequest) Request(io.gravitee.gateway.api.Request) AtomicReference(java.util.concurrent.atomic.AtomicReference) RequestPolicyChainProcessor(io.gravitee.gateway.policy.impl.RequestPolicyChainProcessor)

Example 13 with Response

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

the class CheckSubscriptionPolicyTest method shouldReturnUnauthorized_onException.

@Test
public void shouldReturnUnauthorized_onException() throws PolicyException, TechnicalException {
    CheckSubscriptionPolicy policy = new CheckSubscriptionPolicy();
    Request request = mock(Request.class);
    Response response = mock(Response.class);
    PolicyChain policyChain = mock(PolicyChain.class);
    ExecutionContext executionContext = mock(ExecutionContext.class);
    SubscriptionRepository subscriptionRepository = mock(SubscriptionRepository.class);
    when(executionContext.getComponent(SubscriptionRepository.class)).thenReturn(subscriptionRepository);
    when(subscriptionRepository.search(any(SubscriptionCriteria.class))).thenThrow(TechnicalException.class);
    policy.onRequest(request, response, policyChain, executionContext);
    verify(policyChain, times(1)).failWith(argThat(statusCode(HttpStatusCode.UNAUTHORIZED_401)));
}
Also used : Response(io.gravitee.gateway.api.Response) PolicyChain(io.gravitee.policy.api.PolicyChain) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) SubscriptionRepository(io.gravitee.repository.management.api.SubscriptionRepository) Request(io.gravitee.gateway.api.Request) SubscriptionCriteria(io.gravitee.repository.management.api.search.SubscriptionCriteria) Test(org.junit.Test)

Example 14 with Response

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

the class CheckSubscriptionPolicyTest method shouldReturnUnauthorized_badClient.

@Test
public void shouldReturnUnauthorized_badClient() throws PolicyException, TechnicalException {
    CheckSubscriptionPolicy policy = new CheckSubscriptionPolicy();
    Request request = mock(Request.class);
    Response response = mock(Response.class);
    PolicyChain policyChain = mock(PolicyChain.class);
    ExecutionContext executionContext = mock(ExecutionContext.class);
    when(executionContext.getAttribute(CheckSubscriptionPolicy.CONTEXT_ATTRIBUTE_CLIENT_ID)).thenReturn("my-client-id");
    SubscriptionRepository subscriptionRepository = mock(SubscriptionRepository.class);
    when(executionContext.getComponent(SubscriptionRepository.class)).thenReturn(subscriptionRepository);
    Subscription subscription = mock(Subscription.class);
    when(subscription.getClientId()).thenReturn("my-bad-client-id");
    when(subscriptionRepository.search(any(SubscriptionCriteria.class))).thenReturn(Collections.singletonList(subscription));
    policy.onRequest(request, response, policyChain, executionContext);
    verify(policyChain, times(1)).failWith(argThat(statusCode(HttpStatusCode.UNAUTHORIZED_401)));
}
Also used : Response(io.gravitee.gateway.api.Response) PolicyChain(io.gravitee.policy.api.PolicyChain) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) SubscriptionRepository(io.gravitee.repository.management.api.SubscriptionRepository) Request(io.gravitee.gateway.api.Request) SubscriptionCriteria(io.gravitee.repository.management.api.search.SubscriptionCriteria) Subscription(io.gravitee.repository.management.model.Subscription) Test(org.junit.Test)

Example 15 with Response

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

the class PolicyTest method onResponse_mockParameters.

@Test
public void onResponse_mockParameters() throws Exception {
    PolicyMetadata policyDefinition = mock(PolicyMetadata.class);
    when(policyDefinition.policy()).then((Answer<Class>) invocationOnMock -> DummyPolicy.class);
    DummyPolicy policyInst = Mockito.spy((DummyPolicy) policyFactory.create(policyDefinition, null));
    Method onResponseMethod = resolvePolicyMethod(DummyPolicy.class, OnResponse.class);
    when(policyDefinition.method(OnResponse.class)).thenReturn(onResponseMethod);
    Policy policy = PolicyImpl.target(policyInst).definition(policyDefinition).build();
    Request mockRequest = mock(Request.class);
    Response mockResponse = mock(Response.class);
    policy.onResponse(mockRequest, mockResponse);
    verify(policyInst, atLeastOnce()).onResponse(eq(mockRequest), eq(mockResponse), any(PolicyChain.class));
}
Also used : PolicyImpl(io.gravitee.gateway.policy.impl.PolicyImpl) OnResponse(io.gravitee.policy.api.annotations.OnResponse) OnRequest(io.gravitee.policy.api.annotations.OnRequest) ReflectionUtils.withAnnotation(org.reflections.ReflectionUtils.withAnnotation) PolicyFactoryImpl(io.gravitee.gateway.policy.impl.PolicyFactoryImpl) Set(java.util.Set) Test(org.junit.Test) ReflectionUtils.withModifier(org.reflections.ReflectionUtils.withModifier) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) Answer(org.mockito.stubbing.Answer) Response(io.gravitee.gateway.api.Response) Request(io.gravitee.gateway.api.Request) Modifier(java.lang.reflect.Modifier) PolicyChain(io.gravitee.policy.api.PolicyChain) Annotation(java.lang.annotation.Annotation) ReflectionUtils(org.reflections.ReflectionUtils) Method(java.lang.reflect.Method) Before(org.junit.Before) OnResponse(io.gravitee.policy.api.annotations.OnResponse) Response(io.gravitee.gateway.api.Response) PolicyChain(io.gravitee.policy.api.PolicyChain) OnRequest(io.gravitee.policy.api.annotations.OnRequest) Request(io.gravitee.gateway.api.Request) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

Request (io.gravitee.gateway.api.Request)21 Response (io.gravitee.gateway.api.Response)21 Test (org.junit.Test)14 ExecutionContext (io.gravitee.gateway.api.ExecutionContext)13 PolicyChain (io.gravitee.policy.api.PolicyChain)11 HttpHeaders (io.gravitee.common.http.HttpHeaders)7 SubscriptionRepository (io.gravitee.repository.management.api.SubscriptionRepository)7 SubscriptionCriteria (io.gravitee.repository.management.api.search.SubscriptionCriteria)7 Before (org.junit.Before)6 Subscription (io.gravitee.repository.management.model.Subscription)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 UUID (io.gravitee.common.utils.UUID)4 Metrics (io.gravitee.reporter.api.http.Metrics)4 TimeUnit (java.util.concurrent.TimeUnit)4 Assert.assertEquals (org.junit.Assert.assertEquals)4 Assert.assertNotNull (org.junit.Assert.assertNotNull)4 Mock (org.mockito.Mock)4 Mockito.when (org.mockito.Mockito.when)4 MockitoAnnotations (org.mockito.MockitoAnnotations)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2