IntegrationErrorConfiguration.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.message.config;

  21. import java.util.List;

  22. import org.openspcoop2.message.constants.IntegrationErrorMessageType;
  23. import org.openspcoop2.message.constants.MessageType;
  24. import org.openspcoop2.message.constants.ServiceBinding;
  25. import org.openspcoop2.utils.transport.http.HttpConstants;
  26. import org.openspcoop2.utils.transport.http.HttpServletTransportRequestContext;

  27. /**
  28.  * IntegrationErrorConfiguration
  29.  *
  30.  *
  31.  * @author Poli Andrea (apoli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */

  35. public class IntegrationErrorConfiguration implements java.io.Serializable {

  36.     /**
  37.      * serialVersionUID
  38.      */
  39.     private static final long serialVersionUID = 1L;

  40.     private IntegrationErrorMessageType errorType;
  41.     private IntegrationErrorMessageType defaultErrorType;
  42.     private ConfigurationRFC7807 rfc7807;
  43.     private IntegrationErrorReturnConfiguration errorReturnConfig;
  44.     private boolean useInternalFault; // in cooperazione
  45.        
  46.     public IntegrationErrorConfiguration(ConfigurationRFC7807 rfc7807, IntegrationErrorMessageType errorType,
  47.             IntegrationErrorReturnConfiguration errorReturnConfig,  
  48.             boolean useInternalFault){
  49.         this.rfc7807 = rfc7807;
  50.         this.errorType = errorType;
  51.         this.errorReturnConfig = errorReturnConfig;
  52.         this.useInternalFault = useInternalFault;
  53.     }
  54.    
  55.     void setDefaultErrorType(IntegrationErrorMessageType defaultErrorType) {
  56.         this.defaultErrorType = defaultErrorType;
  57.     }

  58.     public ConfigurationRFC7807 getRfc7807() {
  59.         return this.rfc7807;
  60.     }
  61.     public IntegrationErrorReturnConfiguration getErrorReturnConfig() {
  62.         return this.errorReturnConfig;
  63.     }
  64.     public boolean isUseInternalFault() {
  65.         return this.useInternalFault;
  66.     }
  67.    
  68.     public MessageType getMessageType(HttpServletTransportRequestContext request, ServiceBinding serviceBinding, MessageType requestMsgType){
  69.         if(ServiceBinding.REST.equals(serviceBinding) && this.rfc7807!=null) {
  70.             if(this.rfc7807.isUseAcceptHeader() && request!=null && request.getHeaderValues(HttpConstants.ACCEPT)!=null && !request.getHeaderValues(HttpConstants.ACCEPT).isEmpty()) {
  71.                 List<String> values = request.getHeaderValues(HttpConstants.ACCEPT);
  72.                 // https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
  73.                 /*
  74.                  * Example from rfc:
  75.                     Accept: text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c
  76.                     Verbally, this would be interpreted as "text/html and text/x-c are the preferred media types, but if they do not exist, then send the text/x-dvi entity, and if that does not exist, send the text/plain entity."
  77.                  **/
  78.                 boolean readQparameter = true;
  79.                 MessageType mt = readMessageTypeFromAccept(values, !readQparameter); // prima priorità a quelli senza q parameter
  80.                 if(mt!=null) {
  81.                     return mt;
  82.                 }
  83.                
  84.                 // altrimenti esamino quelli con q parameter
  85.                 mt = readMessageTypeFromAccept(values, readQparameter); // prima priorità a quelli senza q parameter
  86.                 if(mt!=null) {
  87.                     return mt;
  88.                 }
  89.             }
  90.         }
  91.        
  92.         switch (this.errorType) {
  93.         case SOAP_AS_REQUEST:
  94.             if(MessageType.SOAP_11.equals(requestMsgType) || MessageType.SOAP_12.equals(requestMsgType) )
  95.                 return requestMsgType;          
  96.             return getDefaultMessageType(requestMsgType);
  97.         case SOAP_11:
  98.             return MessageType.SOAP_11;
  99.         case SOAP_12:
  100.             return MessageType.SOAP_12;
  101.         case XML:
  102.             return MessageType.XML;
  103.         case JSON:
  104.             return MessageType.JSON;
  105.         case SAME_AS_REQUEST:
  106.             if(MessageType.SOAP_11.equals(requestMsgType) || MessageType.SOAP_12.equals(requestMsgType) )
  107.                 return requestMsgType;
  108.             if(MessageType.XML.equals(requestMsgType) || MessageType.JSON.equals(requestMsgType) )
  109.                 return requestMsgType;
  110.             return getDefaultMessageType(requestMsgType);
  111.         default:
  112.             return MessageType.BINARY; // content is null
  113.         }
  114.     }
  115.     public MessageType getDefaultMessageType(MessageType requestMsgType){
  116.         switch (this.defaultErrorType) {
  117.         case SOAP_11:
  118.             return MessageType.SOAP_11;
  119.         case SOAP_12:
  120.             return MessageType.SOAP_12;
  121.         case XML:
  122.             return MessageType.XML;
  123.         case JSON:
  124.             return MessageType.JSON;
  125.         default:
  126.             return MessageType.BINARY; // content is null
  127.         }
  128.     }
  129.    
  130.     private MessageType readMessageTypeFromAccept(List<String> values, boolean readQparameter) {
  131.         boolean asJson = false;
  132.         boolean asXml = false;
  133.         // https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
  134.         for (String value : values) {
  135.             String [] acceptHeaders = null;
  136.             if(value.contains(",")) {
  137.                 acceptHeaders = value.split(",");
  138.                 for (int i = 0; i < acceptHeaders.length; i++) {
  139.                     acceptHeaders[i] = acceptHeaders[i].trim();
  140.                 }
  141.             }
  142.             else {
  143.                 acceptHeaders = new String [] {value.trim()};
  144.             }
  145.             double asJsonParamterQ = 0;
  146.             double asXmlParamterQ = 0;
  147.             for (String hdr : acceptHeaders) {
  148.                 if(readQparameter) {
  149.                     if(hdr.contains(";")) {
  150.                         String [] tmp = hdr.split(";");
  151.                         if(tmp!=null && tmp.length==2) {
  152.                             String mediaType = tmp[0];
  153.                             String q = tmp[1];
  154.                             if(mediaType!=null && q!=null) {
  155.                                 q = q.trim();
  156.                                 mediaType = mediaType.trim();
  157.                                 if(q.contains("=")) {
  158.                                     String [] tmpQ = q.split("=");
  159.                                     if(tmpQ!=null && tmpQ.length==2) {
  160.                                         String qParam = tmpQ[0];
  161.                                         String qParamValue = tmpQ[1];
  162.                                         if(qParam!=null && "q".equalsIgnoreCase(qParam.trim()) && qParamValue!=null) {
  163.                                             double d = 0;
  164.                                             try {
  165.                                                 d = Double.valueOf(qParamValue.trim());
  166.                                                 if(d>0) {
  167.                                                     if(isJsonMediaType(mediaType)){
  168.                                                         if(d>asJsonParamterQ) {
  169.                                                             asJsonParamterQ = d;
  170.                                                             asJson = true;
  171.                                                         }
  172.                                                     }
  173.                                                     else if(isXmlMediaType(mediaType)){
  174.                                                         if(d>asXmlParamterQ) {
  175.                                                             asXmlParamterQ = d;
  176.                                                             asXml = true;
  177.                                                         }
  178.                                                     }                  
  179.                                                 }
  180.                                             }catch(Throwable t) {}
  181.                                         }
  182.                                     }
  183.                                 }
  184.                             }
  185.                         }
  186.                     }
  187.                 }
  188.                 else {
  189.                     if(isJsonMediaType(hdr)){
  190.                         asJson = true;
  191.                         break;
  192.                     }
  193.                     else if(isXmlMediaType(hdr)){
  194.                         asXml = true;
  195.                         break;
  196.                     }
  197.                 }
  198.             }
  199.            
  200.             if(asJson && asXml && readQparameter) {
  201.                 if(asJsonParamterQ>asXmlParamterQ) {
  202.                     return MessageType.JSON;
  203.                 }
  204.                 else {
  205.                     return MessageType.XML;
  206.                 }
  207.             }
  208.            
  209.             if(asJson) {
  210.                 return MessageType.JSON;
  211.             }
  212.             else if(asXml) {
  213.                 return MessageType.XML;
  214.             }
  215.         }
  216.         return null;
  217.     }
  218.     private boolean isJsonMediaType(String hdrValue) {
  219.         if(hdrValue.toLowerCase().endsWith("/x-json") || hdrValue.toLowerCase().endsWith("/json") || hdrValue.toLowerCase().endsWith("+json")){
  220.             return true;
  221.         }
  222.         return false;
  223.     }
  224.     private boolean isXmlMediaType(String hdrValue) {
  225.         if(hdrValue.toLowerCase().endsWith("/x-xml") || hdrValue.toLowerCase().endsWith("/xml") || hdrValue.toLowerCase().endsWith("+xml")){
  226.             return true;
  227.         }
  228.         return false;
  229.     }
  230. }