ErrorHandler.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.pdd.core.dynamic;

  21. import java.util.List;
  22. import java.util.Map;

  23. import org.openspcoop2.message.OpenSPCoop2Message;
  24. import org.openspcoop2.pdd.services.error.AbstractErrorGenerator;
  25. import org.openspcoop2.protocol.sdk.Context;
  26. import org.openspcoop2.protocol.sdk.constants.IntegrationFunctionError;
  27. import org.openspcoop2.utils.transport.TransportUtils;

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

  36.     public ErrorHandler() {} // per funzionalità dove la gestione dell'errore non serve
  37.     public ErrorHandler(AbstractErrorGenerator errorGenerator, IntegrationFunctionError functionErrorDefault, Context context) {
  38.         this.errorGenerator = errorGenerator;
  39.         this.functionErrorDefault = functionErrorDefault;
  40.         this.context = context;
  41.     }

  42.     private AbstractErrorGenerator errorGenerator;
  43.     private IntegrationFunctionError functionErrorDefault;
  44.     private Context context;
  45.    
  46.     private boolean error = false;
  47.     private String detail = null;
  48.     private ErrorMessage message = null;
  49.     private OpenSPCoop2Message op2Message = null;
  50.     private IntegrationFunctionError op2IntegrationFunctionError;
  51.    
  52.        
  53.     public String getDetail() {
  54.         return this.detail;
  55.     }
  56.     public boolean isError() {
  57.         return this.error;
  58.     }

  59.     public ErrorMessage getMessage() {
  60.         return this.message;
  61.     }
  62.     public OpenSPCoop2Message getOp2Message() {
  63.         return this.op2Message;
  64.     }
  65.     public IntegrationFunctionError getOp2IntegrationFunctionError() {
  66.         return this.op2IntegrationFunctionError;
  67.     }
  68.    
  69.     public void setMessage(String detail, int responseCode) {
  70.         this._setMessage(detail, null, null, responseCode+"", null);
  71.     }
  72.     public void setMessage(String detail, int responseCode, Map<String, String> headers) {
  73.         this._setMessage(detail, null, null, responseCode+"", TransportUtils.convertToMapListValues(headers));
  74.     }
  75.     public void setMessageWithHeaders(String detail, int responseCode, Map<String, List<String>> headers) {
  76.         this._setMessage(detail, null, null, responseCode+"", headers);
  77.     }
  78.     public void setMessage(String detail, String responseCode) {
  79.         this._setMessage(detail, null, null, responseCode, null);
  80.     }
  81.     public void setMessage(String detail, String responseCode, Map<String, String> headers) {
  82.         this._setMessage(detail, null, null, responseCode, TransportUtils.convertToMapListValues(headers));
  83.     }
  84.     public void setMessageWithHeaders(String detail, String responseCode, Map<String, List<String>> headers) {
  85.         this._setMessage(detail, null, null, responseCode, headers);
  86.     }
  87.    
  88.     public void setMessage(String detail, String content, String contentType, int responseCode) {
  89.         this._setMessage(detail, content.getBytes(), contentType, responseCode+"", null);
  90.     }
  91.     public void setMessage(String detail, String content, String contentType, int responseCode, Map<String, String> headers) {
  92.         this._setMessage(detail, content.getBytes(), contentType, responseCode+"", TransportUtils.convertToMapListValues(headers));
  93.     }
  94.     public void setMessageWithHeaders(String detail, String content, String contentType, int responseCode, Map<String, List<String>> headers) {
  95.         this._setMessage(detail, content.getBytes(), contentType, responseCode+"", headers);
  96.     }
  97.     public void setMessage(String detail, String content, String contentType, String responseCode) {
  98.         this._setMessage(detail, content.getBytes(), contentType, responseCode, null);
  99.     }
  100.     public void setMessage(String detail, String content, String contentType, String responseCode, Map<String, String> headers) {
  101.         this._setMessage(detail, content.getBytes(), contentType, responseCode, TransportUtils.convertToMapListValues(headers));
  102.     }
  103.     public void setMessageWithHeaders(String detail, String content, String contentType, String responseCode, Map<String, List<String>> headers) {
  104.         this._setMessage(detail, content.getBytes(), contentType, responseCode, headers);
  105.     }
  106.    
  107.     public void setMessage(String detail, byte[] content, String contentType, int responseCode) {
  108.         this._setMessage(detail, content, contentType, responseCode+"", null);
  109.     }
  110.     public void setMessage(String detail, byte[] content, String contentType, int responseCode, Map<String, String> headers) {
  111.         this._setMessage(detail, content, contentType, responseCode+"", TransportUtils.convertToMapListValues(headers));
  112.     }
  113.     public void setMessageWithHeaders(String detail, byte[] content, String contentType, int responseCode, Map<String, List<String>> headers) {
  114.         this._setMessage(detail, content, contentType, responseCode+"", headers);
  115.     }
  116.     public void setMessage(String detail, byte[] content, String contentType, String responseCode) {
  117.         this._setMessage(detail, content, contentType, responseCode, null);
  118.     }
  119.     public void setMessage(String detail, byte[] content, String contentType, String responseCode, Map<String, String> headers) {
  120.         this._setMessage(detail, content, contentType, responseCode, TransportUtils.convertToMapListValues(headers));
  121.     }
  122.     public void setMessageWithHeaders(String detail, byte[] content, String contentType, String responseCode, Map<String, List<String>> headers) {
  123.         this._setMessage(detail, content, contentType, responseCode, headers);
  124.     }
  125.    
  126.     public void setError(String detail) throws DynamicException {
  127.         this.setError(detail, this.functionErrorDefault);
  128.     }
  129.     public void setError(String detail, String integrationFunctionError) throws DynamicException {
  130.         this.setError(detail, IntegrationFunctionError.valueOf(integrationFunctionError));
  131.     }
  132.     public void setError(String detail, IntegrationFunctionError integrationFunctionError) throws DynamicException {
  133.         if(this.errorGenerator==null) {
  134.             throw new DynamicException("Funzionalità non supportata");
  135.         }
  136.         this.detail = detail;
  137.         this.op2Message = this.errorGenerator.buildFault(detail, this.context, integrationFunctionError);
  138.         this.op2IntegrationFunctionError = integrationFunctionError;
  139.         this.error = true;
  140.     }
  141.    
  142.     private void _setMessage(String detail, byte[] content, String contentType, String responseCode, Map<String, List<String>> headers) {  
  143.         ErrorMessage messageError = new ErrorMessage();
  144.         messageError.setContent(content);
  145.         messageError.setContentType(contentType);
  146.         messageError.setResponseCode(responseCode);
  147.         messageError.setHeadersValues(headers);
  148.         this.setMessage(detail, messageError);
  149.     }
  150.     public void setMessage(String detail, ErrorMessage message) {
  151.         this.detail = detail;
  152.         this.message = message;
  153.         if(message!=null) {
  154.             this.error = true;
  155.         }
  156.         else {
  157.             this.error = false;
  158.         }
  159.     }
  160.    
  161.    
  162.    

  163. }