Utilities.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.protocol.basic;

  21. import javax.xml.soap.SOAPElement;

  22. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  23. import org.openspcoop2.message.constants.ServiceBinding;
  24. import org.openspcoop2.message.xml.MessageXMLUtils;
  25. import org.openspcoop2.protocol.sdk.ProtocolException;

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

  34.     public static String toString(OpenSPCoop2MessageFactory messageFactory, SOAPElement soapElement,boolean consume)
  35.             throws ProtocolException {
  36.         if(soapElement == null){
  37.             throw new ProtocolException("Conversione in element non riuscita");
  38.         }
  39.         try{
  40.             MessageXMLUtils xmlUtils = MessageXMLUtils.getInstance(messageFactory);
  41.             String xml = OpenSPCoop2MessageFactory.getAsString(messageFactory, soapElement,consume);
  42.             if(xml==null){
  43.                 xml = xmlUtils.toString(soapElement,true);
  44.                 if(xml==null){
  45.                     throw new Exception("Conversione in stringa non riuscita");
  46.                 }
  47.             }
  48.             return xml;
  49.         }catch(ProtocolException pe){
  50.             throw pe;
  51.         }catch(Exception e){
  52.             throw new ProtocolException(e.getMessage(),e);
  53.         }
  54.     }

  55.     public static byte[] toByteArray(OpenSPCoop2MessageFactory messageFactory, SOAPElement soapElement,boolean consume)
  56.             throws ProtocolException {
  57.         if(soapElement == null){
  58.             throw new ProtocolException("Conversione in element non riuscita");
  59.         }
  60.         try{
  61.             MessageXMLUtils xmlUtils = MessageXMLUtils.getInstance(messageFactory);
  62.             byte[] xml = OpenSPCoop2MessageFactory.getAsByte(messageFactory, soapElement,consume);
  63.             if(xml==null){
  64.                 xml = xmlUtils.toByteArray(soapElement,true);
  65.                 if(xml==null){
  66.                     throw new Exception("Conversione in bytes non riuscita");
  67.                 }
  68.             }
  69.             return xml;
  70.         }catch(ProtocolException pe){
  71.             throw pe;
  72.         }catch(Exception e){
  73.             throw new ProtocolException(e.getMessage(),e);
  74.         }
  75.     }


  76.    
  77.     public static ServiceBinding convert(org.openspcoop2.core.registry.constants.ServiceBinding serviceBinding){
  78.         switch (serviceBinding) {
  79.         case SOAP:
  80.             return ServiceBinding.SOAP;
  81.         case REST:
  82.             return ServiceBinding.REST;
  83.         }
  84.         return null;
  85.     }
  86.     public static ServiceBinding convert(org.openspcoop2.protocol.manifest.constants.ServiceBinding serviceBinding){
  87.         switch (serviceBinding) {
  88.         case SOAP:
  89.             return ServiceBinding.SOAP;
  90.         case REST:
  91.             return ServiceBinding.REST;
  92.         }
  93.         return null;
  94.     }
  95. }