Search in sources :

Example 1 with ServletRequestBindingException

use of org.springframework.web.bind.ServletRequestBindingException in project esup-papercut by EsupPortail.

the class PayBoxResourceController method getStats.

@RequestMapping(value = "/statsPapercut")
public void getStats(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletRequestBindingException {
    HttpSession session = request.getSession();
    String sharedSessionId = request.getParameter("sharedSessionId");
    if (sharedSessionId != null) {
        EsupPapercutSessionObject objectShared = (EsupPapercutSessionObject) session.getAttribute(sharedSessionId);
        String requeteNbTransactions = objectShared.getRequeteNbTransactions();
        String requeteMontantTransactions = objectShared.getRequeteMontantTransactions();
        String requeteCumulTransactions = objectShared.getRequeteCumulTransactions();
        String requeteCumulMontants = objectShared.getRequeteCumulMontants();
        if (objectShared.isIsAdmin()) {
            String flexJsonString = "Aucune statistique à récupérer";
            try {
                JSONSerializer serializer = new JSONSerializer();
                flexJsonString = serializer.deepSerialize(statsService.getStatsPapercut(requeteNbTransactions, requeteMontantTransactions, requeteCumulTransactions, requeteCumulMontants));
            } catch (Exception e) {
                log.warn("Impossible de récupérer les statistiques", e);
            }
            InputStream jsonStream = IOUtils.toInputStream(flexJsonString, "utf-8");
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            FileCopyUtils.copy(jsonStream, response.getOutputStream());
        }
    }
}
Also used : EsupPapercutSessionObject(org.esupportail.papercut.domain.EsupPapercutSessionObject) HttpSession(javax.servlet.http.HttpSession) InputStream(java.io.InputStream) IOException(java.io.IOException) ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) JSONSerializer(flexjson.JSONSerializer) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with ServletRequestBindingException

use of org.springframework.web.bind.ServletRequestBindingException in project CILManagement-Server by LiuinStein.

the class GlobalExceptionResolver method resolveException.

/**
 * Resolve the exception form controller
 *
 * @param request   http request
 * @param response  http response
 * @param o         the executed handler, or null if none chosen at the time of the exception (for example, if multipart resolution failed)
 * @param exception the exception that threw from controller
 * @return a new ModelAndView
 * @implNote https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/HandlerExceptionResolver.html
 */
@NotNull
@Override
public ModelAndView resolveException(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @Nullable Object o, @NotNull Exception exception) {
    RestfulResult result = new RestfulResult(1, exception.getMessage(), new HashMap<>());
    response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
    if (exception instanceof SimpleException) {
        result.setCode(((SimpleException) exception).getCode());
    }
    if (exception instanceof SimpleHttpException) {
        response.setStatus(((SimpleHttpException) exception).getHttpStatusToReturn().value());
    }
    if (exception instanceof HttpRequestMethodNotSupportedException) {
        result.setCode(405);
        response.setStatus(HttpStatus.METHOD_NOT_ALLOWED.value());
    }
    if (exception instanceof ValidationException || exception instanceof ServletRequestBindingException || exception instanceof IllegalArgumentException) {
        response.setStatus(HttpStatus.BAD_REQUEST.value());
    }
    if (exception instanceof DataAccessException) {
        result.setMessage("database access error");
    }
    try {
        if ("application/xml".equals(request.getHeader("Accept"))) {
            response.setHeader("Content-Type", "application/xml;charset=UTF-8");
            response.getWriter().print(result.toXmlString());
        } else {
            response.setHeader("Content-Type", "application/json;charset=UTF-8");
            response.getWriter().print(result.toJsonString());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new ModelAndView();
}
Also used : ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) ValidationException(com.shaoqunliu.validation.ValidationException) RestfulResult(cn.opencil.vo.RestfulResult) ModelAndView(org.springframework.web.servlet.ModelAndView) IOException(java.io.IOException) HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException) DataAccessException(org.springframework.dao.DataAccessException) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with ServletRequestBindingException

use of org.springframework.web.bind.ServletRequestBindingException in project spring-framework by spring-projects.

the class DefaultHandlerExceptionResolverTests method handleServletRequestBindingException.

@Test
public void handleServletRequestBindingException() {
    String message = "Missing required value - header, cookie, or pathvar";
    ServletRequestBindingException ex = new ServletRequestBindingException(message);
    ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
    assertThat(mav).as("No ModelAndView returned").isNotNull();
    assertThat(mav.isEmpty()).as("No Empty ModelAndView returned").isTrue();
    assertThat(response.getStatus()).as("Invalid status code").isEqualTo(400);
}
Also used : ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) ModelAndView(org.springframework.web.servlet.ModelAndView) Test(org.junit.jupiter.api.Test)

Example 4 with ServletRequestBindingException

use of org.springframework.web.bind.ServletRequestBindingException in project spring-framework by spring-projects.

the class ResponseEntityExceptionHandlerTests method controllerAdviceWithNestedExceptionWithinDispatcherServlet.

@Test
public void controllerAdviceWithNestedExceptionWithinDispatcherServlet() throws Exception {
    StaticWebApplicationContext ctx = new StaticWebApplicationContext();
    ctx.registerSingleton("controller", NestedExceptionThrowingController.class);
    ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
    ctx.refresh();
    DispatcherServlet servlet = new DispatcherServlet(ctx);
    servlet.init(new MockServletConfig());
    try {
        servlet.service(this.servletRequest, this.servletResponse);
    } catch (ServletException ex) {
        boolean condition1 = ex.getCause() instanceof IllegalStateException;
        assertThat(condition1).isTrue();
        boolean condition = ex.getCause().getCause() instanceof ServletRequestBindingException;
        assertThat(condition).isTrue();
    }
}
Also used : ServletException(jakarta.servlet.ServletException) ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 5 with ServletRequestBindingException

use of org.springframework.web.bind.ServletRequestBindingException in project spring-framework by spring-projects.

the class ResponseEntityExceptionHandlerTests method servletRequestBindingException.

@Test
public void servletRequestBindingException() {
    Exception ex = new ServletRequestBindingException("message");
    testException(ex);
}
Also used : ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) MissingPathVariableException(org.springframework.web.bind.MissingPathVariableException) ServletException(jakarta.servlet.ServletException) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException) MissingServletRequestPartException(org.springframework.web.multipart.support.MissingServletRequestPartException) BindException(org.springframework.validation.BindException) ConversionNotSupportedException(org.springframework.beans.ConversionNotSupportedException) AsyncRequestTimeoutException(org.springframework.web.context.request.async.AsyncRequestTimeoutException) MissingServletRequestParameterException(org.springframework.web.bind.MissingServletRequestParameterException) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) TypeMismatchException(org.springframework.beans.TypeMismatchException) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) HttpMediaTypeNotSupportedException(org.springframework.web.HttpMediaTypeNotSupportedException) HttpRequestMethodNotSupportedException(org.springframework.web.HttpRequestMethodNotSupportedException) HttpMediaTypeNotAcceptableException(org.springframework.web.HttpMediaTypeNotAcceptableException) Test(org.junit.jupiter.api.Test)

Aggregations

ServletRequestBindingException (org.springframework.web.bind.ServletRequestBindingException)9 Test (org.junit.jupiter.api.Test)5 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)3 ServletException (jakarta.servlet.ServletException)2 IOException (java.io.IOException)2 HttpRequestMethodNotSupportedException (org.springframework.web.HttpRequestMethodNotSupportedException)2 ModelAndView (org.springframework.web.servlet.ModelAndView)2 RestfulResult (cn.opencil.vo.RestfulResult)1 ValidationException (com.shaoqunliu.validation.ValidationException)1 JSONSerializer (flexjson.JSONSerializer)1 InputStream (java.io.InputStream)1 Map (java.util.Map)1 HttpSession (javax.servlet.http.HttpSession)1 Contract (org.asqatasun.entity.contract.Contract)1 Page (org.asqatasun.entity.subject.Page)1 ForbiddenPageException (org.asqatasun.webapp.exception.ForbiddenPageException)1 ForbiddenScopeException (org.asqatasun.webapp.exception.ForbiddenScopeException)1 HttpStatusCodeFamily (org.asqatasun.webapp.util.HttpStatusCodeFamily)1 EsupPapercutSessionObject (org.esupportail.papercut.domain.EsupPapercutSessionObject)1 NotNull (org.jetbrains.annotations.NotNull)1