MessageContent.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 org.openspcoop2.message.MessageUtils;
  22. import org.openspcoop2.message.OpenSPCoop2Message;
  23. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  24. import org.openspcoop2.message.OpenSPCoop2RestJsonMessage;
  25. import org.openspcoop2.message.OpenSPCoop2RestMimeMultipartMessage;
  26. import org.openspcoop2.message.OpenSPCoop2RestXmlMessage;
  27. import org.openspcoop2.message.OpenSPCoop2SoapMessage;
  28. import org.openspcoop2.message.exception.MessageException;
  29. import org.openspcoop2.message.exception.MessageNotSupportedException;
  30. import org.openspcoop2.protocol.sdk.Context;
  31. import org.w3c.dom.Element;

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

  40.     private OpenSPCoop2MessageFactory messageFactory;
  41.    
  42.     private OpenSPCoop2SoapMessage soapMessage;
  43.     private OpenSPCoop2RestXmlMessage restXmlMessage;
  44.     private OpenSPCoop2RestJsonMessage restJsonMessage;
  45.     private OpenSPCoop2RestMimeMultipartMessage restMultipartMessage;
  46.    
  47.     private String idTransazione;
  48.     private boolean bufferMessage_readOnly;
  49.    
  50.     public MessageContent(OpenSPCoop2SoapMessage soapMessage, boolean bufferMessage_readOnly, Context context) {
  51.         this.soapMessage = soapMessage;
  52.         init(this.soapMessage, bufferMessage_readOnly, context);
  53.     }
  54.     public MessageContent(OpenSPCoop2RestXmlMessage restXmlMessage, boolean bufferMessage_readOnly, Context context) {
  55.         this.restXmlMessage = restXmlMessage;
  56.         init(this.restXmlMessage, bufferMessage_readOnly, context);
  57.     }
  58.     public MessageContent(OpenSPCoop2RestJsonMessage restJsonMessage, boolean bufferMessage_readOnly, Context context) {
  59.         this.restJsonMessage = restJsonMessage;
  60.         init(this.restJsonMessage, bufferMessage_readOnly, context);
  61.     }
  62.     public MessageContent(OpenSPCoop2RestMimeMultipartMessage restMultipartMessage, boolean bufferMessage_readOnly, Context context) {
  63.         this.restMultipartMessage = restMultipartMessage;
  64.         init(this.restMultipartMessage, bufferMessage_readOnly, context);
  65.     }
  66.     private void init(OpenSPCoop2Message msg, boolean bufferMessage_readOnly, Context context) {
  67.         this.messageFactory = msg.getFactory();
  68.         this.bufferMessage_readOnly = bufferMessage_readOnly;
  69.         if(context!=null) {
  70.             this.idTransazione = (String)context.getObject(org.openspcoop2.core.constants.Costanti.ID_TRANSAZIONE);
  71.         }
  72.     }
  73.    
  74.     public OpenSPCoop2MessageFactory getMessageFactory() {
  75.         return this.messageFactory;
  76.     }

  77.     public boolean isJson() {
  78.         return this.restJsonMessage!=null;
  79.     }
  80.     public OpenSPCoop2RestJsonMessage getJsonMessage() {
  81.         return this.restJsonMessage;
  82.     }
  83.     public boolean isXml() {
  84.         return this.soapMessage!=null || this.restXmlMessage!=null;
  85.     }
  86.     public boolean isRestMultipart() {
  87.         return this.restMultipartMessage!=null;
  88.     }
  89.    
  90.     private Element element = null;
  91.     public Element getElement() throws MessageException, MessageNotSupportedException {
  92.         if(this.element!=null) {
  93.             return this.element;
  94.         }
  95.        
  96.         boolean checkSoapBodyEmpty = false; // devo poter fare xpath anche su soapBody empty
  97.         if(this.soapMessage!=null) {
  98.             this.element = MessageUtils.getContentElement(this.soapMessage, checkSoapBodyEmpty, this.bufferMessage_readOnly, this.idTransazione);
  99.         }
  100.         else if(this.restXmlMessage!=null) {
  101.             this.element = MessageUtils.getContentElement(this.restXmlMessage, checkSoapBodyEmpty, this.bufferMessage_readOnly, this.idTransazione);
  102.         }
  103.         else if(this.restMultipartMessage!=null) {
  104.             this.element = MessageUtils.getContentElement(this.restMultipartMessage, checkSoapBodyEmpty, this.bufferMessage_readOnly, this.idTransazione);
  105.         }
  106.        
  107.         return this.element;
  108.     }
  109.    
  110.     private String elementJson = null;
  111.     public String getElementJson() throws MessageException, MessageNotSupportedException {
  112.         if(this.elementJson!=null) {
  113.             return this.elementJson;
  114.         }
  115.        
  116.         if(this.restJsonMessage!=null) {
  117.             this.elementJson = MessageUtils.getContentString(this.restJsonMessage, this.bufferMessage_readOnly, this.idTransazione);
  118.         }
  119.         else if(this.restMultipartMessage!=null) {
  120.             this.elementJson = MessageUtils.getContentString(this.restMultipartMessage, this.bufferMessage_readOnly, this.idTransazione);
  121.         }
  122.        
  123.         return this.elementJson;
  124.     }
  125.    
  126.     public void setUpdatable() throws MessageException {
  127.         OpenSPCoop2Message msg = null;
  128.         if(this.soapMessage!=null) {
  129.             msg = this.soapMessage;
  130.         }
  131.         else if(this.restXmlMessage!=null) {
  132.             msg = this.restXmlMessage;
  133.         }
  134.         else if(this.restJsonMessage!=null) {
  135.             msg = this.restJsonMessage;
  136.         }
  137.         else if(this.restMultipartMessage!=null) {
  138.             msg = this.restMultipartMessage;
  139.         }
  140.         MessageUtils.setUpdatable(msg);
  141.     }
  142. }