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);
}
}
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());
}
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();
}
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");
}
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();
}
Aggregations