use of org.slf4j.MDC.MDCCloseable in project open-kilda by telstra.
the class NorthboundExceptionHandler method handleMessageException.
/**
* Handles NorthboundException exception.
*
* @param exception the NorthboundException instance
* @param request the WebRequest caused exception
* @return the ResponseEntity object instance
*/
@ExceptionHandler(MessageException.class)
protected ResponseEntity<Object> handleMessageException(MessageException exception, WebRequest request) {
HttpStatus status;
switch(exception.getErrorType()) {
case NOT_FOUND:
status = HttpStatus.NOT_FOUND;
break;
case DATA_INVALID:
status = HttpStatus.BAD_REQUEST;
break;
case PARAMETERS_INVALID:
status = HttpStatus.BAD_REQUEST;
break;
case REQUEST_INVALID:
status = HttpStatus.BAD_REQUEST;
break;
case OPERATION_TIMED_OUT:
status = HttpStatus.REQUEST_TIMEOUT;
break;
case ALREADY_EXISTS:
status = HttpStatus.CONFLICT;
break;
case AUTH_FAILED:
status = HttpStatus.UNAUTHORIZED;
break;
case UNPROCESSABLE_REQUEST:
status = HttpStatus.UNPROCESSABLE_ENTITY;
break;
case INTERNAL_ERROR:
status = HttpStatus.INTERNAL_SERVER_ERROR;
break;
case NOT_PERMITTED:
status = HttpStatus.FORBIDDEN;
break;
case NOT_IMPLEMENTED:
status = HttpStatus.NOT_IMPLEMENTED;
break;
default:
status = HttpStatus.INTERNAL_SERVER_ERROR;
break;
}
MessageError error = new MessageError(exception.getCorrelationId(), exception.getTimestamp(), exception.getErrorType().toString(), exception.getMessage(), exception.getErrorDescription());
try (MDCCloseable mdcCloseable = MDC.putCloseable(CORRELATION_ID, exception.getCorrelationId())) {
logger.warn(format("Error %s caught.", error), exception);
}
return super.handleExceptionInternal(exception, error, new HttpHeaders(), status, request);
}
use of org.slf4j.MDC.MDCCloseable in project open-kilda by telstra.
the class RequestCorrelationFilter method doFilterInternal.
/**
* Generates new correlation_id and add it into passing correlation id in the header.
*/
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
String correlationId = request.getHeader(CORRELATION_ID);
boolean emptyCorrelationId = StringUtils.isBlank(correlationId);
if (emptyCorrelationId) {
correlationId = UUID.randomUUID().toString();
} else {
correlationId = RequestCorrelationId.chain(UUID.randomUUID().toString(), correlationId);
}
try (RequestCorrelationClosable requestCorrelation = RequestCorrelationId.create(correlationId)) {
// MDC is picked up by the %X in log4j2 formatter .. resources/log4j2.xml
try (MDCCloseable closable = MDC.putCloseable(CORRELATION_ID, correlationId)) {
if (emptyCorrelationId) {
logger.warn("CorrelationId was not sent, generated one: {}", correlationId);
} else {
logger.trace("Found correlationId in header. Chaining: {}", correlationId);
}
response.addHeader(CORRELATION_ID, correlationId);
filterChain.doFilter(request, response);
}
}
}
use of org.slf4j.MDC.MDCCloseable in project open-kilda by telstra.
the class KafkaMessageListener method onMessage.
/**
* Handles all messages from kafka and sends to corresponding component for further processing.
*
* @param message received message.
*/
@KafkaHandler
public void onMessage(@Header(name = KafkaHeaders.RECEIVED_MESSAGE_KEY, required = false) String key, Message message) {
try (MDCCloseable closable = MDC.putCloseable(Utils.CORRELATION_ID, message.getCorrelationId())) {
log.debug("Message received: {} - {}", Thread.currentThread().getId(), message);
messageProcessor.processRequest(message, key);
}
}
use of org.slf4j.MDC.MDCCloseable in project open-kilda by telstra.
the class KafkaMessageListener method onMessage.
/**
* Handles all messages from kafka and sends to corresponding component for further processing.
* <p/>
* @param message received message.
*/
@KafkaHandler
public void onMessage(Message message) {
try (MDCCloseable closable = MDC.putCloseable(CORRELATION_ID, message.getCorrelationId())) {
logger.debug("Message received: {} - {}", Thread.currentThread().getId(), message);
messagingChannel.onResponse(message);
}
}
Aggregations