WebApplicationExceptionMapper.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.ws.rs.core.MultivaluedMap;
  22. import javax.ws.rs.core.Response;
  23. import javax.ws.rs.core.Response.ResponseBuilder;
  24. import javax.ws.rs.ext.ExceptionMapper;

  25. import org.openspcoop2.utils.transport.http.HttpConstants;
  26. import org.openspcoop2.utils.transport.http.HttpUtilities;
  27. import org.slf4j.Logger;

  28. import com.fasterxml.jackson.core.JsonParseException;
  29. import com.fasterxml.jackson.databind.JsonMappingException;

  30. /**
  31.  * WebApplicationExceptionMapper
  32.  *
  33.  * @author Poli Andrea (apoli@link.it)
  34.  * @author $Author$
  35.  * @version $Rev$, $Date$
  36.  */
  37. public class WebApplicationExceptionMapper implements ExceptionMapper<javax.ws.rs.WebApplicationException> {

  38.     private Logger log = org.openspcoop2.utils.LoggerWrapperFactory.getLogger(WebApplicationExceptionMapper.class);
  39.    
  40.     private boolean excludeFaultBean;
  41.     private boolean updateTitle = true;
  42.     private ITypeGenerator typeGenerator;
  43.    
  44.     public boolean isExcludeFaultBean() {
  45.         return this.excludeFaultBean;
  46.     }
  47.     public void setExcludeFaultBean(boolean excludeFaultBean) {
  48.         this.excludeFaultBean = excludeFaultBean;
  49.     }
  50.    
  51.     public boolean isUpdateTitle() {
  52.         return this.updateTitle;
  53.     }
  54.     public void setUpdateTitle(boolean updateTitle) {
  55.         this.updateTitle = updateTitle;
  56.     }
  57.    
  58.     public ITypeGenerator getTypeGenerator() {
  59.         return this.typeGenerator;
  60.     }
  61.     public void setTypeGenerator(ITypeGenerator typeGenerator) {
  62.         this.typeGenerator = typeGenerator;
  63.     }
  64.    
  65.     @Override
  66.     public Response toResponse(javax.ws.rs.WebApplicationException e) {
  67.         if(e.getResponse()==null || e.getResponse().getEntity()==null || !(e.getResponse().getEntity() instanceof ProblemRFC7807)) {
  68.            
  69.             this.log.error(e.getMessage(),e);
  70.            
  71.             ProblemRFC7807 problem = null;
  72.            
  73.             if(e.getCause() instanceof JsonMappingException) {
  74.                 JsonMappingException jsonMappingException = (JsonMappingException) e.getCause();
  75.                 problem = new ProblemValidation(FaultCode.RICHIESTA_NON_VALIDA.toFault());
  76.                 ((ProblemValidation) problem).addInvalidParam(jsonMappingException.getPathReference(), jsonMappingException.getMessage(), null);
  77.             }
  78.             else if(e.getCause() instanceof JsonParseException) {
  79.                 JsonParseException jsonParseException = (JsonParseException) e.getCause();
  80.                 problem = new ProblemValidation(FaultCode.RICHIESTA_NON_VALIDA.toFault());
  81.                 if(jsonParseException.getOriginalMessage()!=null) {
  82.                     ((ProblemValidation) problem).addInvalidParam("body", jsonParseException.getOriginalMessage(), null);
  83.                 }
  84.                 else {
  85.                     ((ProblemValidation) problem).addInvalidParam("body", jsonParseException.getMessage(), null);
  86.                 }
  87.                 if(jsonParseException.getLocation()!=null) {
  88.                     StringBuilder bf = new StringBuilder();
  89.                     bf.append("line: ").append(jsonParseException.getLocation().getLineNr()).append(", column: ").append(jsonParseException.getLocation().getColumnNr());
  90.                     ((ProblemValidation) problem).addInvalidParam("position", bf.toString(), null);
  91.                 }
  92.             }
  93.             else {
  94.                 problem = FaultCode.RICHIESTA_NON_VALIDA.toFault();
  95.             }
  96.            
  97.             this.updateProblem(problem, e); // customizzabile
  98.             this._setType(problem, e);
  99.             if(this.updateTitle) {
  100.                 // aggiorno rispetto al code modificabile nel metodo sopra
  101.                 problem.setTitle(HttpUtilities.getHttpReason(problem.getStatus()));
  102.             }
  103.             return Response.status(problem.getStatus()).entity(problem).type(HttpConstants.CONTENT_TYPE_JSON_PROBLEM_DETAILS_RFC_7807).build();

  104.         }
  105.         else {
  106.             if(this.excludeFaultBean) {
  107.                 ResponseBuilder responseBuilder = Response.status(e.getResponse().getStatus());
  108.                 if(e.getResponse().getHeaders()!=null) {
  109.                     MultivaluedMap<String, Object> map = e.getResponse().getHeaders();
  110.                     if(!map.isEmpty()) {
  111.                         map.keySet().stream().forEach(k -> {
  112.                             responseBuilder.header(k, map.get(k));
  113.                         });
  114.                     }
  115.                 }
  116.                 return responseBuilder.build();
  117.             } else {
  118.                
  119.                 ProblemRFC7807 problem = this._getProblem(e);
  120.                 if(problem!=null) {
  121.                     this.updateProblem(problem, e); // customizzabile
  122.                     this._setType(problem, e);
  123.                     if(this.updateTitle) {
  124.                         // aggiorno rispetto al code modificabile nel metodo sopra
  125.                         problem.setTitle(HttpUtilities.getHttpReason(problem.getStatus()));
  126.                     }
  127.                 }
  128.                
  129.                 return e.getResponse();
  130.             }
  131.         }
  132.     }
  133.    
  134.     private ProblemRFC7807 _getProblem(javax.ws.rs.WebApplicationException e) {
  135.         if(e==null || e.getResponse()==null || e.getResponse().getEntity()==null) {
  136.             return null;
  137.         }
  138.         if(!(e.getResponse().getEntity() instanceof ProblemRFC7807)) {
  139.             return null;
  140.         }
  141.         ProblemRFC7807 problem = (ProblemRFC7807) e.getResponse().getEntity();
  142.         return problem;
  143.     }
  144.     private void _setType(ProblemRFC7807 problem, javax.ws.rs.WebApplicationException e) {
  145.         if(this.typeGenerator==null) {
  146.             return;
  147.         }
  148.         problem.setType(this.typeGenerator.getType(problem.getStatus(), e));
  149.     }
  150.    
  151.    
  152.     public void updateProblem(ProblemRFC7807 problem, javax.ws.rs.WebApplicationException e) {
  153.        
  154.     }

  155. }