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);
}
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);
}
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)));
}
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)));
}
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));
}
Aggregations