Example usage for org.springframework.web.context.request WebRequest getDescription

List of usage examples for org.springframework.web.context.request WebRequest getDescription

Introduction

In this page you can find the example usage for org.springframework.web.context.request WebRequest getDescription.

Prototype

String getDescription(boolean includeClientInfo);

Source Link

Document

Get a short description of this request, typically containing request URI and session id.

Usage

From source file:uk.gov.nationalarchives.discovery.taxonomy.ws.controller.TaxonomyExceptionHandler.java

private String extractPathFromWebRequest(WebRequest webRequest) {
    String[] uriElements = webRequest.getDescription(false).split("uri=/taxonomy");
    String requestPath;/*from  w w  w.  j  a  v  a  2s.com*/
    if (uriElements.length == 2) {
        requestPath = uriElements[1];
    } else {
        requestPath = uriElements[0];
    }
    return requestPath;
}

From source file:se.kth.csc.controller.ExceptionHandlerAdvice.java

@ExceptionHandler(value = DataIntegrityViolationException.class)
@ResponseBody//  w w  w  . j av a 2 s  . c o m
public String dataIntegrityViolation(Exception exception, WebRequest request, HttpServletResponse response) {
    response.setStatus(409); // Conflict
    return String.format("%s: %s", request.getDescription(true), exception.getMessage());
}

From source file:se.kth.csc.controller.ExceptionHandlerAdvice.java

/**
 * Handle exceptions thrown by handlers.
 *///from w w w .ja va  2  s  .co  m
@ExceptionHandler(value = NotFoundException.class)
@ResponseBody
public String notFound(Exception exception, WebRequest request, HttpServletResponse response) {
    response.setStatus(404); // Not found
    return String.format("%s: %s", request.getDescription(true), exception.getMessage());
}

From source file:com.epam.catgenome.controller.ExceptionHandlerAdvice.java

@ResponseBody
@Order(Ordered.HIGHEST_PRECEDENCE)/*from   ww  w .j  a  v a 2 s .c om*/
@ExceptionHandler(Throwable.class)
public final ResponseEntity<Result<String>> handleUncaughtException(final Throwable exception,
        final WebRequest request) {
    // adds information about encountered error to application log
    LOG.error(MessageHelper.getMessage("logger.error", request.getDescription(true)), exception);
    HttpStatus code = HttpStatus.OK;

    String message;
    if (exception instanceof FileNotFoundException) {
        // any details about real path of a resource should be normally prevented to send to the client
        message = MessageHelper.getMessage("error.io.not.found");
    } else if (exception instanceof DataAccessException) {
        // any details about data access error should be normally prevented to send to the client,
        // as its message can contain information about failed SQL query or/and database schema
        if (exception instanceof BadSqlGrammarException) {
            // for convenience we need to provide detailed information about occurred BadSqlGrammarException,
            // but it can be retrieved
            SQLException root = ((BadSqlGrammarException) exception).getSQLException();
            if (root.getNextException() != null) {
                LOG.error(MessageHelper.getMessage("logger.error.root.cause", request.getDescription(true)),
                        root.getNextException());
            }
            message = MessageHelper.getMessage("error.sql.bad.grammar");
        } else {
            message = MessageHelper.getMessage("error.sql");
        }
    } else if (exception instanceof UnauthorizedClientException) {
        message = exception.getMessage();
        code = HttpStatus.UNAUTHORIZED;
    } else {
        message = exception.getMessage();
    }

    return new ResponseEntity<>(Result.error(StringUtils.defaultString(StringUtils.trimToNull(message),
            MessageHelper.getMessage("error" + ".default"))), code);
}