Search in sources :

Example 1 with ProxyRequest

use of io.gravitee.gateway.api.proxy.ProxyRequest 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

UrlEscapers (com.google.common.net.UrlEscapers)1 HttpHeaders (io.gravitee.common.http.HttpHeaders)1 HttpMethod (io.gravitee.common.http.HttpMethod)1 HttpStatusCode (io.gravitee.common.http.HttpStatusCode)1 Api (io.gravitee.definition.model.Api)1 ExecutionContext (io.gravitee.gateway.api.ExecutionContext)1 Invoker (io.gravitee.gateway.api.Invoker)1 Request (io.gravitee.gateway.api.Request)1 Buffer (io.gravitee.gateway.api.buffer.Buffer)1 Endpoint (io.gravitee.gateway.api.endpoint.Endpoint)1 EndpointManager (io.gravitee.gateway.api.endpoint.EndpointManager)1 Handler (io.gravitee.gateway.api.handler.Handler)1 ProxyConnection (io.gravitee.gateway.api.proxy.ProxyConnection)1 ProxyRequest (io.gravitee.gateway.api.proxy.ProxyRequest)1 ProxyRequestBuilder (io.gravitee.gateway.api.proxy.builder.ProxyRequestBuilder)1 ReadStream (io.gravitee.gateway.api.stream.ReadStream)1 DirectProxyConnection (io.gravitee.gateway.http.core.direct.DirectProxyConnection)1 LoggableProxyConnection (io.gravitee.gateway.http.core.invoker.logging.LoggableProxyConnection)1 QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)1 QueryStringEncoder (io.netty.handler.codec.http.QueryStringEncoder)1