use of io.gravitee.gateway.handlers.api.logging.LoggableClientRequest 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);
}
}
Aggregations