Search in sources :

Example 1 with LoggableClientResponse

use of io.gravitee.gateway.handlers.api.logging.LoggableClientResponse in project gravitee-gateway by gravitee-io.

the class ApiReactorHandler method doHandle.

@Override
protected void doHandle(Request serverRequest, Response serverResponse, Handler<Response> handler) {
    // Prepare request execution context
    ExecutionContext executionContext = executionContextFactory.create(serverRequest);
    executionContext.setAttribute(ExecutionContext.ATTR_CONTEXT_PATH, serverRequest.contextPath());
    try {
        // Set execution context attributes and metrics specific to this handler
        serverRequest.metrics().setApi(api.getId());
        executionContext.setAttribute(ExecutionContext.ATTR_API, api.getId());
        executionContext.setAttribute(ExecutionContext.ATTR_INVOKER, invoker);
        // Enable logging at client level
        if (api.getProxy().getLoggingMode().isClientMode()) {
            serverRequest = new LoggableClientRequest(serverRequest);
            serverResponse = new LoggableClientResponse(serverRequest, serverResponse);
        }
        Cors cors = api.getProxy().getCors();
        if (cors != null && cors.isEnabled()) {
            Request finalServerRequest = serverRequest;
            Response finalServerResponse = serverResponse;
            new CorsHandler(cors).responseHandler(new Handler<Response>() {

                @Override
                public void handle(Response response) {
                    handleClientRequest(finalServerRequest, finalServerResponse, executionContext, new CorsResponseHandler(handler));
                }
            }).handle(serverRequest, serverResponse, handler);
        } else {
            handleClientRequest(serverRequest, serverResponse, executionContext, handler);
        }
    } catch (Exception ex) {
        logger.error("An unexpected error occurs while processing request", ex);
        serverRequest.metrics().setMessage(Throwables.getStackTraceAsString(ex));
        // Send an INTERNAL_SERVER_ERROR (500)
        serverResponse.status(HttpStatusCode.INTERNAL_SERVER_ERROR_500);
        serverResponse.headers().set(HttpHeaders.CONNECTION, HttpHeadersValues.CONNECTION_CLOSE);
        serverResponse.end();
        handler.handle(serverResponse);
    }
}
Also used : LoggableClientResponse(io.gravitee.gateway.handlers.api.logging.LoggableClientResponse) Response(io.gravitee.gateway.api.Response) ProxyResponse(io.gravitee.gateway.api.proxy.ProxyResponse) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) LoggableClientRequest(io.gravitee.gateway.handlers.api.logging.LoggableClientRequest) LoggableClientRequest(io.gravitee.gateway.handlers.api.logging.LoggableClientRequest) Request(io.gravitee.gateway.api.Request) CorsHandler(io.gravitee.gateway.handlers.api.cors.CorsHandler) Handler(io.gravitee.gateway.api.handler.Handler) CorsResponseHandler(io.gravitee.gateway.handlers.api.cors.CorsResponseHandler) AbstractReactorHandler(io.gravitee.gateway.reactor.handler.AbstractReactorHandler) CorsHandler(io.gravitee.gateway.handlers.api.cors.CorsHandler) LoggableClientResponse(io.gravitee.gateway.handlers.api.logging.LoggableClientResponse) Cors(io.gravitee.definition.model.Cors) CorsResponseHandler(io.gravitee.gateway.handlers.api.cors.CorsResponseHandler) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Cors (io.gravitee.definition.model.Cors)1 ExecutionContext (io.gravitee.gateway.api.ExecutionContext)1 Request (io.gravitee.gateway.api.Request)1 Response (io.gravitee.gateway.api.Response)1 Handler (io.gravitee.gateway.api.handler.Handler)1 ProxyResponse (io.gravitee.gateway.api.proxy.ProxyResponse)1 CorsHandler (io.gravitee.gateway.handlers.api.cors.CorsHandler)1 CorsResponseHandler (io.gravitee.gateway.handlers.api.cors.CorsResponseHandler)1 LoggableClientRequest (io.gravitee.gateway.handlers.api.logging.LoggableClientRequest)1 LoggableClientResponse (io.gravitee.gateway.handlers.api.logging.LoggableClientResponse)1 AbstractReactorHandler (io.gravitee.gateway.reactor.handler.AbstractReactorHandler)1