ValidationExceptionMapper.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.utils.service.fault.jaxrs;

  21. import javax.validation.ConstraintViolation;
  22. import javax.validation.ConstraintViolationException;
  23. import javax.validation.ValidationException;
  24. import javax.ws.rs.core.Response;
  25. import javax.ws.rs.ext.ExceptionMapper;
  26. import javax.ws.rs.ext.Provider;

  27. import org.openspcoop2.utils.transport.http.HttpConstants;
  28. import org.openspcoop2.utils.transport.http.HttpUtilities;
  29. import org.slf4j.Logger;

  30. /**
  31.  * ValidationExceptionMapper
  32.  *
  33.  * @author Poli Andrea (apoli@link.it)
  34.  * @author $Author$
  35.  * @version $Rev$, $Date$
  36.  */
  37. @Provider
  38. public class ValidationExceptionMapper implements ExceptionMapper<ValidationException> {

  39.     private Logger log = org.openspcoop2.utils.LoggerWrapperFactory.getLogger(ValidationExceptionMapper.class);
  40.    
  41.     private boolean updateTitle = true;
  42.     private ITypeGenerator typeGenerator;
  43.    
  44.     public boolean isUpdateTitle() {
  45.         return this.updateTitle;
  46.     }
  47.     public void setUpdateTitle(boolean updateTitle) {
  48.         this.updateTitle = updateTitle;
  49.     }
  50.    
  51.     public ITypeGenerator getTypeGenerator() {
  52.         return this.typeGenerator;
  53.     }
  54.     public void setTypeGenerator(ITypeGenerator typeGenerator) {
  55.         this.typeGenerator = typeGenerator;
  56.     }
  57.    
  58.     @Override
  59.     public Response toResponse(ValidationException exception) {
  60.        
  61.         this.log.error(exception.getMessage(),exception);
  62.        
  63.         ProblemValidation problem = new ProblemValidation(FaultCode.RICHIESTA_NON_VALIDA.toFault());
  64.         if (exception instanceof ConstraintViolationException) {
  65.             final ConstraintViolationException constraint = (ConstraintViolationException) exception;

  66.             for (final ConstraintViolation< ? > violation: constraint.getConstraintViolations()) {
  67.                 problem.addInvalidParam(violation.getPropertyPath().toString(), violation.getMessage(), violation.getInvalidValue());
  68.             }
  69.         }
  70.         this.updateProblem(problem, exception); // customizzabile
  71.         this._setType(problem, exception);
  72.         if(this.updateTitle) {
  73.             // aggiorno rispetto al code modificabile nel metodo sopra
  74.             problem.setTitle(HttpUtilities.getHttpReason(problem.getStatus()));
  75.         }
  76.         return Response.status(problem.getStatus()).entity(problem).type(HttpConstants.CONTENT_TYPE_JSON_PROBLEM_DETAILS_RFC_7807).build();
  77.     }
  78.    
  79.     public void _setType(ProblemRFC7807 problem, ValidationException e) {
  80.         if(this.typeGenerator==null) {
  81.             return;
  82.         }
  83.         problem.setType(this.typeGenerator.getType(problem.getStatus(), e));
  84.     }
  85.    
  86.    
  87.     public void updateProblem(ProblemRFC7807 problem, ValidationException e) {
  88.        
  89.     }
  90. }