Search in sources :

Example 6 with ServletRequestBindingException

use of org.springframework.web.bind.ServletRequestBindingException in project Asqatasun by Asqatasun.

the class PageListController method pageLinkDispatcher.

/**
 * This method dispatches the result depending on the parameters passed to
 * the request. Only multi-pages audit are considered here.
 *
 * @param request
 * @param model
 * @return
 * @throws Exception
 */
private String pageLinkDispatcher(HttpServletRequest request, Audit audit, Model model) throws Exception {
    if (audit.getSubject() instanceof Page) {
        throw new ForbiddenPageException();
    }
    String status = ServletRequestUtils.getStringParameter(request, STATUS_KEY);
    HttpStatusCodeFamily httpStatusCode = getHttpStatusCodeFamily(status);
    // the repartion of the pages regarding the httpStatusCode
    if (httpStatusCode == null) {
        if (!isAuthorizedScopeForPageList(audit)) {
            throw new ForbiddenScopeException();
        }
        try {
            Contract currentContract = retrieveContractFromAudit(audit);
            model.addAttribute(CONTRACT_NAME_KEY, currentContract.getLabel());
            model.addAttribute(CONTRACT_ID_KEY, currentContract.getId());
            String testLabel = ServletRequestUtils.getStringParameter(request, TEST_KEY);
            if (StringUtils.isNotBlank(testLabel)) {
                model.addAttribute(TEST_CODE_KEY, testDataService.getTestFromAuditAndLabel(audit, testLabel));
            }
            return this.preparePageListData(audit, model);
        } catch (ServletRequestBindingException e) {
            return OUPS_VIEW_REDIRECT_NAME;
        }
    } else {
        boolean isAuthorizedScopeForPageList = isAuthorizedScopeForPageList(audit);
        Contract currentContract = retrieveContractFromAudit(audit);
        model.addAttribute(CONTRACT_NAME_KEY, currentContract.getLabel());
        model.addAttribute(CONTRACT_ID_KEY, currentContract.getId());
        // used in the jsp
        if (!isAuthorizedScopeForPageList) {
            model.addAttribute(AUDIT_NUMBER_KEY, true);
        }
        String testLabel = ServletRequestUtils.getStringParameter(request, TEST_KEY);
        if (StringUtils.isNotBlank(testLabel)) {
            model.addAttribute(TEST_CODE_KEY, testDataService.getTestFromAuditAndLabel(audit, testLabel));
        }
        return this.preparePageListStatsByHttpStatusCode(audit, model, httpStatusCode, request, false);
    }
}
Also used : ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) HttpStatusCodeFamily(org.asqatasun.webapp.util.HttpStatusCodeFamily) Page(org.asqatasun.entity.subject.Page) ForbiddenScopeException(org.asqatasun.webapp.exception.ForbiddenScopeException) Contract(org.asqatasun.entity.contract.Contract) ForbiddenPageException(org.asqatasun.webapp.exception.ForbiddenPageException)

Example 7 with ServletRequestBindingException

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

the class AdminController method getStatsInfos.

@RequestMapping(value = "/statsPapercut")
public void getStatsInfos(HttpServletResponse response) throws IOException, ServletRequestBindingException {
    String flexJsonString = "Aucune statistique à récupérer";
    EsupPapercutContext context = config.getContext(ContextHelper.getCurrentContext());
    try {
        JSONSerializer serializer = new JSONSerializer();
        flexJsonString = serializer.deepSerialize(statsService.getStatsPapercut(context));
    } 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 : InputStream(java.io.InputStream) EsupPapercutContext(org.esupportail.papercut.config.EsupPapercutContext) IOException(java.io.IOException) ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) JSONSerializer(flexjson.JSONSerializer) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 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 ValidationInternalException) {
        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.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.exception.ValidationException) RestfulResult(cn.opencil.vo.RestfulResult) ValidationInternalException(com.shaoqunliu.validation.exception.ValidationInternalException) 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 9 with ServletRequestBindingException

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

the class ResponseEntityExceptionHandlerTests method controllerAdvice.

@Test
public void controllerAdvice() throws Exception {
    StaticWebApplicationContext ctx = new StaticWebApplicationContext();
    ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
    ctx.refresh();
    ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver();
    resolver.setApplicationContext(ctx);
    resolver.afterPropertiesSet();
    ServletRequestBindingException ex = new ServletRequestBindingException("message");
    assertThat(resolver.resolveException(this.servletRequest, this.servletResponse, null, ex)).isNotNull();
    assertThat(this.servletResponse.getStatus()).isEqualTo(400);
    assertThat(this.servletResponse.getContentAsString()).isEqualTo("error content");
    assertThat(this.servletResponse.getHeader("someHeader")).isEqualTo("someHeaderValue");
}
Also used : ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 10 with ServletRequestBindingException

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

the class ResponseEntityExceptionHandlerTests method controllerAdviceWithNestedException.

@Test
public void controllerAdviceWithNestedException() {
    StaticWebApplicationContext ctx = new StaticWebApplicationContext();
    ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
    ctx.refresh();
    ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver();
    resolver.setApplicationContext(ctx);
    resolver.afterPropertiesSet();
    IllegalStateException ex = new IllegalStateException(new ServletRequestBindingException("message"));
    assertThat(resolver.resolveException(this.servletRequest, this.servletResponse, null, ex)).isNull();
}
Also used : ServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Test(org.junit.jupiter.api.Test)

Aggregations

ServletRequestBindingException (org.springframework.web.bind.ServletRequestBindingException)10 Test (org.junit.jupiter.api.Test)5 IOException (java.io.IOException)3 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)3 JSONSerializer (flexjson.JSONSerializer)2 ServletException (jakarta.servlet.ServletException)2 InputStream (java.io.InputStream)2 HttpRequestMethodNotSupportedException (org.springframework.web.HttpRequestMethodNotSupportedException)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ModelAndView (org.springframework.web.servlet.ModelAndView)2 RestfulResult (cn.opencil.vo.RestfulResult)1 ValidationException (com.shaoqunliu.validation.exception.ValidationException)1 ValidationInternalException (com.shaoqunliu.validation.exception.ValidationInternalException)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