use of io.gravitee.gateway.api.endpoint.Endpoint 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.endpoint.Endpoint in project gravitee-gateway by gravitee-io.
the class MultiTenantAwareEndpointLifecycleManagerTest method shouldStartEndpoint_noTenant.
@Test
public void shouldStartEndpoint_noTenant() throws Exception {
io.gravitee.definition.model.Endpoint endpoint = mock(io.gravitee.definition.model.endpoint.HttpEndpoint.class);
when(endpoint.getName()).thenReturn("endpoint");
when(endpoint.isBackup()).thenReturn(false);
when(endpoint.getType()).thenReturn(EndpointType.HTTP);
when(proxy.getEndpoints()).thenReturn(Collections.singleton(endpoint));
when(applicationContext.getBean(Connector.class, endpoint)).thenReturn(mock(Connector.class));
endpointLifecycleManager.start();
HttpEndpoint httpClientEndpoint = (HttpEndpoint) endpointLifecycleManager.get("endpoint");
assertNotNull(httpClientEndpoint);
verify(applicationContext, times(1)).getBean(eq(Connector.class), any(Endpoint.class));
verify(httpClientEndpoint.connector(), times(1)).start();
assertEquals(httpClientEndpoint, endpointLifecycleManager.get("endpoint"));
assertNull(endpointLifecycleManager.get("unknown"));
assertFalse(endpointLifecycleManager.endpoints().isEmpty());
}
Aggregations