ParseExceptionUtils.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.exception;

  21. import org.openspcoop2.message.constants.MessageRole;
  22. import org.openspcoop2.utils.LimitExceededIOException;
  23. import org.openspcoop2.utils.LimitedInputStream;
  24. import org.openspcoop2.utils.TimeoutIOException;
  25. import org.openspcoop2.utils.TimeoutInputStream;
  26. import org.openspcoop2.utils.Utilities;

  27. import com.ctc.wstx.exc.WstxException;


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

  37.        
  38.     // Parse-Exception
  39.    
  40.     public static ParseException buildParseException(Throwable e, MessageRole messageRole){
  41.        
  42.         ParseException pe = new ParseException();
  43.         pe.setSourceException(e, messageRole);
  44.        
  45.         if(e==null){
  46.             pe.setParseException(new Exception("Occurs Parsing Error"), messageRole);
  47.             pe.setSourceException(new Exception("Occurs Parsing Error"), messageRole);
  48.             return pe;
  49.         }
  50.        
  51.         if(TimeoutIOException.isTimeoutIOException(e) || Utilities.existsInnerMessageException(e, TimeoutInputStream.ERROR_MSG, true)) {
  52.            
  53.             Throwable timeoutException = null;
  54.             if(e instanceof TimeoutIOException) {
  55.                 timeoutException = e;
  56.             }
  57.             else {
  58.                 timeoutException = Utilities.getInnerInstanceException(e, TimeoutIOException.class, false);
  59.             }
  60.             if(timeoutException==null && Utilities.existsInnerMessageException(e, TimeoutInputStream.ERROR_MSG, true)) {
  61.                 Throwable exceptionMessageTimeout = Utilities.getInnerMessageException(e, TimeoutInputStream.ERROR_MSG, true);
  62.                 if(exceptionMessageTimeout!=null) {
  63.                     timeoutException = new TimeoutIOException(exceptionMessageTimeout.getMessage(), exceptionMessageTimeout);
  64.                 }
  65.             }
  66.             if(timeoutException!=null) {
  67.                 pe.setParseException(timeoutException, messageRole);
  68.                 pe.setSourceException(timeoutException, messageRole);
  69.                 return pe;
  70.             }
  71.         }
  72.        
  73.         if(LimitExceededIOException.isLimitExceededIOException(e) ||
  74.                 Utilities.existsInnerMessageException(e, LimitedInputStream.ERROR_PAYLOAD_TOO_LARGE_MSG, true) ||
  75.                 Utilities.existsInnerMessageException(e, LimitedInputStream.ERROR_CONTENT_LENGTH_EXCEEDED_MSG, true)) {
  76.            
  77.             Throwable limitedException = null;
  78.             if(e instanceof LimitExceededIOException) {
  79.                 limitedException = e;
  80.             }
  81.             else {
  82.                 limitedException = Utilities.getInnerInstanceException(e, LimitExceededIOException.class, false);
  83.             }
  84.             if(limitedException==null && Utilities.existsInnerMessageException(e, LimitedInputStream.ERROR_PAYLOAD_TOO_LARGE_MSG, true)) {
  85.                 Throwable exceptionMessageLimitExceeded = Utilities.getInnerMessageException(e, LimitedInputStream.ERROR_PAYLOAD_TOO_LARGE_MSG, true);
  86.                 if(exceptionMessageLimitExceeded!=null) {
  87.                     limitedException = new LimitExceededIOException(exceptionMessageLimitExceeded.getMessage(), exceptionMessageLimitExceeded);
  88.                 }
  89.             }
  90.             if(limitedException==null && Utilities.existsInnerMessageException(e, LimitedInputStream.ERROR_CONTENT_LENGTH_EXCEEDED_MSG, true)) {
  91.                 Throwable exceptionMessageLimitExceeded = Utilities.getInnerMessageException(e, LimitedInputStream.ERROR_CONTENT_LENGTH_EXCEEDED_MSG, true);
  92.                 if(exceptionMessageLimitExceeded!=null) {
  93.                     limitedException = new LimitExceededIOException(exceptionMessageLimitExceeded.getMessage(), exceptionMessageLimitExceeded);
  94.                 }
  95.             }
  96.             if(limitedException!=null) {
  97.                 pe.setParseException(limitedException, messageRole);
  98.                 pe.setSourceException(limitedException, messageRole);
  99.                 return pe;
  100.             }
  101.         }
  102.        
  103.         Throwable tmp = ParseExceptionUtils.getParseException(e);
  104.         if(tmp!=null){
  105.             pe.setParseException(tmp, messageRole);
  106.             return pe;
  107.         }
  108.        
  109.         pe.setParseException(getInnerNotEmptyMessageException(e), messageRole);
  110.         return pe;
  111.     }
  112.    
  113.     public static Throwable getParseException(Throwable e){
  114.        
  115.         // Prima verifico presenza di eccezioni che sicuramente non evidenziano problemi di parsing
  116.         if(e instanceof java.net.SocketException){
  117.             return null;
  118.         }
  119.         else if(Utilities.existsInnerException(e, java.net.SocketException.class)){
  120.             return null;
  121.         }
  122.        
  123.        
  124.         // Cerco eccezione di parsing
  125.        
  126.         boolean found = false;
  127.        
  128.         Throwable tmp = null;      
  129.                
  130.         if(tmp==null){
  131.             if(Utilities.isExceptionInstanceOf("org.apache.axiom.om.OMException", e)){
  132.                 tmp = e;
  133.             }
  134.             else if(Utilities.existsInnerException(e, "org.apache.axiom.om.OMException")){
  135.                 tmp = Utilities.getInnerException(e, "org.apache.axiom.om.OMException");
  136.             }
  137.             if(tmp!=null){
  138.                 if( ! (tmp.getMessage()!=null && !"".equals(tmp.getMessage()) && !"null".equalsIgnoreCase(tmp.getMessage())) ){
  139.                     // cerco prossima eccezione, in questa c'è null come message
  140.                     tmp = null;
  141.                     found = true;
  142.                 }
  143.             }
  144.         }
  145.        
  146.         if(tmp==null){
  147.             if(e instanceof WstxException){
  148.                 tmp = e;
  149.             }
  150.             else if(Utilities.existsInnerException(e, WstxException.class)){
  151.                 tmp = Utilities.getInnerException(e, WstxException.class);
  152.             }
  153.             if(tmp!=null){
  154.                 if( ! (tmp.getMessage()!=null && !"".equals(tmp.getMessage()) && !"null".equalsIgnoreCase(tmp.getMessage())) ){
  155.                     // cerco prossima eccezione, in questa c'è null come message
  156.                     tmp = null;
  157.                     found = true;
  158.                 }
  159.             }
  160.         }
  161.        
  162.         if(tmp==null){
  163.             if(Utilities.isExceptionInstanceOf("com.ctc.wstx.exc.WstxIOException", e)){
  164.                 tmp = e;
  165.             }
  166.             else if(Utilities.existsInnerException(e, "com.ctc.wstx.exc.WstxIOException")){
  167.                 tmp = Utilities.getInnerException(e, "com.ctc.wstx.exc.WstxIOException");
  168.             }
  169.             if(tmp!=null){
  170.                 if( ! (tmp.getMessage()!=null && !"".equals(tmp.getMessage()) && !"null".equalsIgnoreCase(tmp.getMessage())) ){
  171.                     // cerco prossima eccezione, in questa c'è null come message
  172.                     tmp = null;
  173.                     found = true;
  174.                 }
  175.             }
  176.         }
  177.        
  178.         if(tmp==null){
  179.             if(Utilities.isExceptionInstanceOf("com.ctc.wstx.exc.WstxParsingException", e)){
  180.                 tmp = e;
  181.             }
  182.             else if(Utilities.existsInnerException(e, "com.ctc.wstx.exc.WstxParsingException")){
  183.                 tmp = Utilities.getInnerException(e, "com.ctc.wstx.exc.WstxParsingException");
  184.             }
  185.             if(tmp!=null){
  186.                 if( ! (tmp.getMessage()!=null && !"".equals(tmp.getMessage()) && !"null".equalsIgnoreCase(tmp.getMessage())) ){
  187.                     // cerco prossima eccezione, in questa c'è null come message
  188.                     tmp = null;
  189.                     found = true;
  190.                 }
  191.             }
  192.         }
  193.        
  194.         if(tmp==null){
  195.             if(Utilities.isExceptionInstanceOf("com.ctc.wstx.exc.WstxUnexpectedCharException", e)){
  196.                 tmp = e;
  197.             }
  198.             else if(Utilities.existsInnerException(e, "com.ctc.wstx.exc.WstxUnexpectedCharException")){
  199.                 tmp = Utilities.getInnerException(e, "com.ctc.wstx.exc.WstxUnexpectedCharException");
  200.             }
  201.             if(tmp!=null){
  202.                 if( ! (tmp.getMessage()!=null && !"".equals(tmp.getMessage()) && !"null".equalsIgnoreCase(tmp.getMessage())) ){
  203.                     // cerco prossima eccezione, in questa c'è null come message
  204.                     tmp = null;
  205.                     found = true;
  206.                 }
  207.             }
  208.         }
  209.        
  210.         if(tmp==null){
  211.             if(Utilities.isExceptionInstanceOf("org.xml.sax.SAXParseException", e)){
  212.                 tmp = e;
  213.             }
  214.             else if(Utilities.existsInnerException(e, "org.xml.sax.SAXParseException")){
  215.                 tmp = Utilities.getInnerException(e, "org.xml.sax.SAXParseException");
  216.             }
  217.             if(tmp!=null){
  218.                 if( ! (tmp.getMessage()!=null && !"".equals(tmp.getMessage()) && !"null".equalsIgnoreCase(tmp.getMessage())) ){
  219.                     // cerco prossima eccezione, in questa c'è null come message
  220.                     tmp = null;
  221.                     found = true;
  222.                 }
  223.             }
  224.         }
  225.    
  226.         if(tmp==null){
  227.             if(Utilities.isExceptionInstanceOf("javax.xml.stream.XMLStreamException", e)){
  228.                 tmp = e;
  229.             }
  230.             else if(Utilities.existsInnerException(e, "javax.xml.stream.XMLStreamException")){
  231.                 tmp = Utilities.getInnerException(e, "javax.xml.stream.XMLStreamException");
  232.             }
  233.             if(tmp!=null){
  234.                 if( ! (tmp.getMessage()!=null && !"".equals(tmp.getMessage()) && !"null".equalsIgnoreCase(tmp.getMessage())) ){
  235.                     // cerco prossima eccezione, in questa c'è null come message
  236.                     tmp = null;
  237.                     found = true;
  238.                 }
  239.             }
  240.         }
  241.        
  242.         if(tmp!=null){
  243.             return tmp;
  244.         }
  245.         if(found){
  246.             return getInnerNotEmptyMessageException(e);
  247.         }
  248.         return null;
  249.        
  250.     }
  251.    
  252.     public static boolean isEmpytMessageException(Throwable e){
  253.         if(e.getMessage()==null ||
  254.                 "".equals(e.getMessage()) ||
  255.                 "null".equalsIgnoreCase(e.getMessage()) ||
  256.                 "com.ctc.wstx.exc.WstxIOException: null".equalsIgnoreCase(e.getMessage())){
  257.             return true;
  258.         }
  259.         else{
  260.             return false;
  261.         }
  262.     }
  263.    
  264.     public static Throwable getInnerNotEmptyMessageException(Throwable e){
  265.         if(!isEmpytMessageException(e)){
  266.             return e;
  267.         }
  268.        
  269.         if(e.getCause()!=null){
  270.             //System.out.println("INNER ["+e.getClass().getName()+"]...");
  271.             return getInnerNotEmptyMessageException(e.getCause());
  272.         }
  273.         else{
  274.             return e; // sono nella foglia, ritorno comunque questa eccezione
  275.         }
  276.     }

  277. }