SOAPInfo.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.modipa.utils;

  21. import javax.xml.soap.SOAPBody;
  22. import javax.xml.soap.SOAPElement;
  23. import javax.xml.soap.SOAPEnvelope;
  24. import javax.xml.soap.SOAPHeader;
  25. import javax.xml.soap.SOAPPart;

  26. import org.openspcoop2.message.MessageUtils;
  27. import org.openspcoop2.message.OpenSPCoop2SoapMessage;
  28. import org.openspcoop2.message.soap.AbstractOpenSPCoop2Message_soap_impl;
  29. import org.openspcoop2.message.soap.SoapUtils;

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

  38.     private SOAPHeader header = null;
  39.     private String rootElementNamespace = null;
  40.     private String rootElementPrefix = null;
  41.     private String envelopeNamespace = null;
  42.    
  43.     public SOAPHeader getHeader() {
  44.         return this.header;
  45.     }
  46.     public String getRootElementNamespace() {
  47.         return this.rootElementNamespace;
  48.     }
  49.     public String getRootElementPrefix() {
  50.         return this.rootElementPrefix;
  51.     }
  52.     public String getEnvelopeNamespace() {
  53.         return this.envelopeNamespace;
  54.     }
  55.    
  56.     public void read(boolean useSoapReader, OpenSPCoop2SoapMessage soapMessage, boolean bufferMessage_readOnly, String idTransazione,
  57.             boolean readEnvelopeNamespace, boolean readHeader, boolean readRootElementInfo) throws Exception {
  58.        
  59.         boolean read = false;
  60.         if(useSoapReader && soapMessage instanceof AbstractOpenSPCoop2Message_soap_impl<?>) {
  61.             AbstractOpenSPCoop2Message_soap_impl<?> soap = (AbstractOpenSPCoop2Message_soap_impl<?>)soapMessage;
  62.             if(readHeader) {
  63.                 if(soap.isSoapHeaderOptimizable()) {
  64.                     this.header = soap.getSOAPHeader();
  65.                     if(readRootElementInfo) {
  66.                         this.rootElementNamespace = soap.getSoapReader().getRootElementNamespace();
  67.                         this.rootElementPrefix = soap.getSoapReader().getRootElementPrefix();
  68.                     }
  69.                     if(readEnvelopeNamespace) {
  70.                         this.envelopeNamespace = soap.getSoapReader().getNamespace();
  71.                     }
  72.                     read = true;
  73.                 }
  74.             }
  75.             else if(readRootElementInfo || readEnvelopeNamespace) {
  76.                 if(soap.getSoapReader()!=null && soap.getSoapReader().isParsingComplete()) {
  77.                     if(readRootElementInfo) {
  78.                         this.rootElementNamespace = soap.getSoapReader().getRootElementNamespace();
  79.                         this.rootElementPrefix = soap.getSoapReader().getRootElementPrefix();
  80.                     }
  81.                     if(readEnvelopeNamespace) {
  82.                         this.envelopeNamespace = soap.getSoapReader().getNamespace();
  83.                     }
  84.                 }
  85.                 read = true;
  86.             }
  87.         }
  88.        
  89.         if(!read) {
  90.             SOAPPart soapPart = MessageUtils.getSOAPPart(soapMessage, bufferMessage_readOnly, idTransazione);
  91.             SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
  92.             SOAPBody body = null;
  93.             if(soapEnvelope!=null) {
  94.                 if(readHeader) {
  95.                     this.header = soapEnvelope.getHeader();
  96.                 }
  97.                
  98.                 if(readRootElementInfo) {
  99.                     body = soapEnvelope.getBody();
  100.                     if(readRootElementInfo) {
  101.                         this.readSOAPChildBodyInfo(body);
  102.                     }
  103.                 }
  104.                
  105.                 if(readEnvelopeNamespace) {
  106.                     this.envelopeNamespace = soapEnvelope.getNamespaceURI();
  107.                 }
  108.             }
  109.         }
  110.     }
  111.    
  112.     private void readSOAPChildBodyInfo(SOAPBody soapBody) throws Exception {
  113.        
  114.         if(soapBody==null) {
  115.             throw new Exception("Messaggio senza Body");
  116.         }
  117.         SOAPElement child = SoapUtils.getNotEmptyFirstChildSOAPElement(soapBody);
  118.         if(child==null) {
  119.             throw new Exception("Messaggio senza un contenuto nel Body");
  120.         }
  121.         this.rootElementNamespace = child.getNamespaceURI();
  122.         this.rootElementPrefix = child.getPrefix();
  123.     }
  124.    
  125. }