use of io.gravitee.gateway.api.Request in project gravitee-gateway by gravitee-io.
the class ApiKeyAuthenticationHandlerTest method shouldNotHandleRequest.
@Test
public void shouldNotHandleRequest() {
Request request = mock(Request.class);
when(request.headers()).thenReturn(new HttpHeaders());
MultiValueMap<String, String> parameters = mock(MultiValueMap.class);
when(request.parameters()).thenReturn(parameters);
boolean handle = authenticationHandler.canHandle(request);
Assert.assertFalse(handle);
}
use of io.gravitee.gateway.api.Request 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;
}
use of io.gravitee.gateway.api.Request 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.Request 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.Request in project gravitee-gateway by gravitee-io.
the class SecurityProviderManagerTest method shouldNotResolveSecurityPolicy.
@Test
public void shouldNotResolveSecurityPolicy() {
when(securityProviderLoader.getSecurityProviders()).thenReturn(Collections.emptyList());
Request request = mock(Request.class);
securityManager.initializeSecurityProviders();
AuthenticationHandler securityProvider = securityManager.resolve(request);
assertNull(securityProvider);
}
Aggregations