FaultCode.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 java.util.Map;

  22. import javax.ws.rs.core.Response;
  23. import javax.ws.rs.core.Response.ResponseBuilder;

  24. import org.openspcoop2.utils.transport.http.HttpConstants;
  25. import org.openspcoop2.utils.transport.http.HttpUtilities;


  26. /**
  27.  * FaultCode
  28.  *
  29.  * @author Poli Andrea (apoli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class FaultCode {

  34.     public static final FaultCode RICHIESTA_NON_VALIDA = new FaultCode(400, "RICHIESTA NON VALIDA", "Richiesta non correttamente formata");
  35.     public static final FaultCode AUTENTICAZIONE = new FaultCode(401, "AUTENTICAZIONE", "Mittente della richiesta non autenticato");
  36.     public static final FaultCode AUTORIZZAZIONE = new FaultCode(403, "AUTORIZZAZIONE", "Richiesta non permessa");
  37.     public static final FaultCode NOT_FOUND = new FaultCode(404, "NOT_FOUND", "Risorsa non trovata");
  38.     public static final FaultCode CONFLITTO = new FaultCode(409,"CONFLITTO","L'entità che si vuole creare risulta già esistente");
  39.     public static final FaultCode RICHIESTA_NON_VALIDA_SEMANTICAMENTE = new FaultCode(422,"RICHIESTA NON VALIDA SEMANTICAMENTE",
  40.             "Richiesta non processabile");
  41.     public static final FaultCode ERRORE_INTERNO = new FaultCode(500,"ERRORE INTERNO","Errore interno");
  42.    
  43.     public static final FaultCode STATUS_OK = new FaultCode(200,"STATUS OK","Il servizio funziona correttamente");


  44.     private final String name;
  45.     private final String descrizione;
  46.     private final int code;

  47.     public FaultCode(int code, String name, String descrizione)
  48.     {
  49.         this.code = code;
  50.         this.name = name;
  51.         this.descrizione = descrizione;
  52.     }

  53.     public int getCode() {
  54.         return this.code;
  55.     }
  56.     public String getName() {
  57.         return this.name;
  58.     }
  59.     public String getDescrizione()
  60.     {
  61.         return this.descrizione;
  62.     }

  63.     @Override
  64.     public String toString() {
  65.         return this.descrizione;
  66.     }

  67.     public ProblemRFC7807 toFault() {
  68.         ProblemRFC7807 problem = new ProblemRFC7807();
  69.         problem.setType("https://httpstatuses.com/"+this.code);
  70.         problem.setStatus(this.code);
  71.         problem.setTitle(HttpUtilities.getHttpReason(this.code));
  72.         problem.setDetail(this.descrizione);
  73.         return problem;
  74.     }
  75.     public ProblemRFC7807 toFault(String dettaglio) {
  76.         ProblemRFC7807 problem = this.toFault();
  77.         problem.setDetail(dettaglio);
  78.         return problem;
  79.     }
  80.     public ProblemRFC7807 toFault(Throwable e) {
  81.         ProblemRFC7807 problem = this.toFault();
  82.         problem.setDetail(e.getMessage());
  83.         return problem;
  84.     }

  85.     public ResponseBuilder toFaultResponseBuilder() {
  86.         return this.toFaultResponseBuilder(true);
  87.     }
  88.     public ResponseBuilder toFaultResponseBuilder(boolean addFault) {
  89.         ProblemRFC7807 problem = this.toFault();
  90.         ResponseBuilder rb = Response.status(this.code);
  91.         if(addFault) {
  92.             rb.entity(problem).type(HttpConstants.CONTENT_TYPE_JSON_PROBLEM_DETAILS_RFC_7807);
  93.         }
  94.         return rb;
  95.     }
  96.     public ResponseBuilder toFaultResponseBuilder(String dettaglio) {
  97.         ProblemRFC7807 problem = this.toFault(dettaglio);
  98.         return Response.status(this.code).entity(problem).type(HttpConstants.CONTENT_TYPE_JSON_PROBLEM_DETAILS_RFC_7807);
  99.     }
  100.     public ResponseBuilder toFaultResponseBuilder(Throwable e) {
  101.         ProblemRFC7807 problem = this.toFault(e);
  102.         return Response.status(this.code).entity(problem).type(HttpConstants.CONTENT_TYPE_JSON_PROBLEM_DETAILS_RFC_7807);
  103.     }
  104.    
  105.     public Response toFaultResponse() {
  106.         return this.toFaultResponse(true);
  107.     }
  108.     public Response toFaultResponse(boolean addFault) {
  109.         return this.toFaultResponseBuilder(addFault).build();
  110.     }
  111.     public Response toFaultResponse(String dettaglio) {
  112.         return this.toFaultResponseBuilder(dettaglio).build();
  113.     }
  114.     public Response toFaultResponse(Throwable e) {
  115.         return this.toFaultResponseBuilder(e).build();
  116.     }

  117.     public javax.ws.rs.WebApplicationException toException(ResponseBuilder responseBuilder){
  118.         return new javax.ws.rs.WebApplicationException(responseBuilder.build());
  119.     }
  120.     public javax.ws.rs.WebApplicationException toException(ResponseBuilder responseBuilder, Map<String, String> headers){
  121.         if(headers!=null && !headers.isEmpty()) {
  122.             headers.keySet().stream().forEach(k -> {
  123.                 responseBuilder.header(k, headers.get(k));
  124.             });
  125.         }
  126.         return new javax.ws.rs.WebApplicationException(responseBuilder.build());
  127.     }
  128.     public javax.ws.rs.WebApplicationException toException(Response response){
  129.         return toException(response, null);
  130.     }
  131.     public javax.ws.rs.WebApplicationException toException(Response response, Throwable e){
  132.         // Aggiunta eccezione nel costruttore, in modo che cxf chiami la classe WebApplicationExceptionMapper
  133.         Exception exception = null;
  134.         String msgException = response.getEntity().toString();
  135.         if(e!=null) {
  136.             exception = new Exception(msgException,e);
  137.         }
  138.         else {
  139.             exception = new Exception(msgException);
  140.         }
  141.         return new javax.ws.rs.WebApplicationException(exception,response);
  142.     }
  143.     public javax.ws.rs.WebApplicationException toException(){
  144.         return this.toException(true);
  145.     }
  146.     public javax.ws.rs.WebApplicationException toException(boolean addFault){
  147.         Response r = this.toFaultResponse(addFault);
  148.         return this.toException(r);
  149.     }
  150.     public javax.ws.rs.WebApplicationException toException(String dettaglio){
  151.         Response r = this.toFaultResponse(dettaglio);
  152.         return this.toException(r);
  153.     }
  154.     public javax.ws.rs.WebApplicationException toException(Throwable e){
  155.         Response r = this.toFaultResponse(e);
  156.         return this.toException(r,e);
  157.     }

  158.     public void throwException(ResponseBuilder responseBuilder) throws javax.ws.rs.WebApplicationException{
  159.         throw this.toException(responseBuilder);
  160.     }
  161.     public void throwException(Response response) throws javax.ws.rs.WebApplicationException{
  162.         throw this.toException(response);
  163.     }
  164.     public void throwException() throws javax.ws.rs.WebApplicationException{
  165.         throw this.toException();
  166.     }
  167.     public void throwException(boolean addFault) throws javax.ws.rs.WebApplicationException{
  168.         throw toException(addFault);
  169.     }
  170.     public void throwException(String dettaglio) throws javax.ws.rs.WebApplicationException{
  171.         throw toException(dettaglio);
  172.     }
  173.     public void throwException(Throwable e) throws javax.ws.rs.WebApplicationException{
  174.         throw toException(e);
  175.     }
  176.    

  177. }