ContentExtractor.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 java.util.Iterator;
  22. import java.util.List;

  23. import javax.xml.soap.SOAPBody;
  24. import javax.xml.soap.SOAPElement;
  25. import javax.xml.soap.SOAPException;
  26. import javax.xml.soap.SOAPHeader;
  27. import javax.xml.soap.SOAPHeaderElement;

  28. import org.openspcoop2.core.commons.CoreRuntimeException;
  29. import org.openspcoop2.message.OpenSPCoop2Message;
  30. import org.openspcoop2.message.OpenSPCoop2SoapMessage;
  31. import org.openspcoop2.message.constants.ServiceBinding;
  32. import org.openspcoop2.message.soap.SoapUtils;
  33. import org.openspcoop2.message.xml.XPathExpressionEngine;
  34. import org.openspcoop2.pdd.core.CostantiPdD;
  35. import org.openspcoop2.protocol.sdk.Context;
  36. import org.openspcoop2.utils.xml.DynamicNamespaceContext;
  37. import org.openspcoop2.utils.xml.XPathReturnType;
  38. import org.slf4j.Logger;
  39. import org.w3c.dom.Node;

  40. /**
  41.  * ContentExtractor
  42.  *
  43.  * @author Andrea Poli (apoli@link.it)
  44.  * @author $Author$
  45.  * @version $Rev$, $Date$
  46.  */
  47. public class ContentExtractor extends ContentReader {


  48.     public ContentExtractor(OpenSPCoop2Message message, Context pddContext, Logger log) {
  49.         super(message, pddContext, log);
  50.     }


  51.    
  52.    
  53.     // TRANSPORT
  54.    

  55.     public void addTransportHeader(String name, String value) {
  56.         if(this.message!=null) {
  57.             this.message.forceTransportHeader(name, value);
  58.         }
  59.     }
  60.     public void addTransportHeader(String name, List<String> values) {
  61.         if(this.message!=null) {
  62.             this.message.forceTransportHeader(name, values);
  63.         }
  64.     }
  65.     public void removeTransportHeader(String name) {
  66.         if(this.message!=null) {
  67.             if(this.message.getTransportRequestContext()!=null) {
  68.                 this.message.getTransportRequestContext().removeHeader(name);
  69.             }
  70.             else if(this.message.getTransportResponseContext()!=null) {
  71.                 this.message.getTransportResponseContext().removeHeader(name);
  72.             }
  73.         }
  74.     }
  75.    
  76.     public void addUrlProperty(String name, String value) {
  77.         if(this.message!=null) {
  78.             this.message.forceUrlProperty(name, value);
  79.         }
  80.     }
  81.     public void addUrlProperty(String name, List<String> values) {
  82.         if(this.message!=null) {
  83.             this.message.forceUrlProperty(name, values);
  84.         }
  85.     }
  86.     public void removeUrlProperty(String name) {
  87.         if(this.message!=null &&
  88.             this.message.getTransportRequestContext()!=null) {
  89.             this.message.getTransportRequestContext().removeParameter(name);
  90.         }
  91.     }

  92.    
  93.    
  94.     // SOAP
  95.    
  96.     // imposta in tutti gli header
  97.     public void setMustUnderstand(boolean value) throws DynamicException {
  98.         setMustUnderstand(value, null, null);
  99.     }
  100.     public void setMustUnderstand(boolean value, String localName, String namespace) throws DynamicException {
  101.         if(this.message!=null && ServiceBinding.SOAP.equals(this.message.getServiceBinding())) {
  102.             try {
  103.            
  104.                 OpenSPCoop2SoapMessage soapMsg = this.message.castAsSoap();
  105.                
  106.                 SOAPHeader header = soapMsg.getSOAPHeader();
  107.                 if(header!=null) {
  108.                     Iterator<javax.xml.soap.Node> nodes = header.getChildElements();
  109.                     if(nodes!=null) {
  110.                         while (nodes.hasNext()) {
  111.                             Node n = nodes.next();
  112.                             setMustUnderstandEngine(n, value, localName, namespace);
  113.                         }
  114.                     }
  115.                 }
  116.                
  117.             }catch(Exception t) {
  118.                 throw new DynamicException(t.getMessage(),t);
  119.             }
  120.            
  121.         }
  122.     }
  123.     private void setMustUnderstandEngine(Node n, boolean value, String localName, String namespace) {
  124.         if(n instanceof SOAPHeaderElement) {
  125.             SOAPHeaderElement hdrE = (SOAPHeaderElement) n;
  126.             boolean setValue = true;
  127.             if(localName!=null && !localName.equals(hdrE.getLocalName())) {
  128.                 setValue = false;
  129.             }
  130.             if(setValue && namespace!=null && !namespace.equals(hdrE.getNamespaceURI())) {
  131.                 setValue = false;
  132.             }
  133.             if(setValue) {
  134.                 hdrE.setMustUnderstand(value);
  135.             }
  136.         }
  137.     }
  138.    
  139.     // imposta in tutti gli header
  140.     public void setActor(String value) throws DynamicException {
  141.         setActor(value, null, null);
  142.     }
  143.     public void setActor(String value, String localName, String namespace) throws DynamicException {
  144.         if(this.message!=null && ServiceBinding.SOAP.equals(this.message.getServiceBinding())) {
  145.             try {
  146.            
  147.                 OpenSPCoop2SoapMessage soapMsg = this.message.castAsSoap();
  148.                
  149.                 SOAPHeader header = soapMsg.getSOAPHeader();
  150.                 if(header!=null) {
  151.                     Iterator<javax.xml.soap.Node> nodes = header.getChildElements();
  152.                     if(nodes!=null) {
  153.                         while (nodes.hasNext()) {
  154.                             Node n = nodes.next();
  155.                             setActorEngine(n, value, localName, namespace);
  156.                         }
  157.                     }
  158.                 }
  159.                
  160.             }catch(Exception t) {
  161.                 throw new DynamicException(t.getMessage(),t);
  162.             }
  163.            
  164.         }
  165.     }
  166.     private void setActorEngine(Node n, String value, String localName, String namespace) {
  167.         if(n instanceof SOAPHeaderElement) {
  168.             SOAPHeaderElement hdrE = (SOAPHeaderElement) n;
  169.             boolean setValue = true;
  170.             if(localName!=null && !localName.equals(hdrE.getLocalName())) {
  171.                 setValue = false;
  172.             }
  173.             if(setValue && namespace!=null && !namespace.equals(hdrE.getNamespaceURI())) {
  174.                 setValue = false;
  175.             }
  176.             if(setValue) {
  177.                 hdrE.setActor(value);
  178.             }
  179.         }
  180.     }
  181.    
  182.     // imposta in tutti gli header
  183.     public void setRole(String value) throws DynamicException {
  184.         setRole(value, null, null);
  185.     }
  186.     public void setRole(String value, String localName, String namespace) throws DynamicException {
  187.         if(this.message!=null && ServiceBinding.SOAP.equals(this.message.getServiceBinding())) {
  188.             try {
  189.            
  190.                 OpenSPCoop2SoapMessage soapMsg = this.message.castAsSoap();
  191.                
  192.                 SOAPHeader header = soapMsg.getSOAPHeader();
  193.                 if(header!=null) {
  194.                     Iterator<javax.xml.soap.Node> nodes = header.getChildElements();
  195.                     if(nodes!=null) {
  196.                         while (nodes.hasNext()) {
  197.                             Node n = nodes.next();
  198.                             setRoleEngine(n, value, localName, namespace);
  199.                         }
  200.                     }
  201.                 }
  202.                
  203.             }catch(Exception t) {
  204.                 throw new DynamicException(t.getMessage(),t);
  205.             }
  206.            
  207.         }
  208.     }
  209.     private void setRoleEngine(Node n, String value, String localName, String namespace) throws SOAPException {
  210.         if(n instanceof SOAPHeaderElement) {
  211.             SOAPHeaderElement hdrE = (SOAPHeaderElement) n;
  212.             boolean setValue = true;
  213.             if(localName!=null && !localName.equals(hdrE.getLocalName())) {
  214.                 setValue = false;
  215.             }
  216.             if(setValue && namespace!=null && !namespace.equals(hdrE.getNamespaceURI())) {
  217.                 setValue = false;
  218.             }
  219.             if(setValue) {
  220.                 hdrE.setRole(value);
  221.             }
  222.         }
  223.     }
  224.    
  225.     public void disableExceptionIfFoundMoreSecurityHeader() {
  226.         if(this.message!=null && ServiceBinding.SOAP.equals(this.message.getServiceBinding())) {
  227.             try {
  228.                 this.message.castAsSoap().setThrowExceptionIfFoundMoreSecurityHeader(false);
  229.             }catch(Exception t) {
  230.                 throw new CoreRuntimeException(t.getMessage(),t); // non dovrebbe mai avvenire
  231.             }
  232.         }
  233.     }
  234.     public void addSoapHeader(String xml) throws DynamicException {
  235.         this.addSoapHeader(xml.getBytes());
  236.     }
  237.     public void addSoapHeader(byte[] xml) throws DynamicException {
  238.         if(this.message!=null && ServiceBinding.SOAP.equals(this.message.getServiceBinding())) {
  239.            
  240.             try {
  241.            
  242.                 OpenSPCoop2SoapMessage soapMsg = this.message.castAsSoap();
  243.                
  244.                 SOAPHeader header = soapMsg.getSOAPHeader();
  245.                 if(header==null) {
  246.                     header = soapMsg.getSOAPPart().getEnvelope().addHeader();
  247.                 }
  248.                
  249.                 SOAPElement soapElement = soapMsg.createSOAPElement(xml);
  250.                
  251.                 header.addChildElement(soapElement);
  252.                
  253.             }catch(Exception t) {
  254.                 throw new DynamicException(t.getMessage(),t);
  255.             }
  256.            
  257.         }
  258.     }
  259.    
  260.     public void setSoapBody(String xml) throws DynamicException {
  261.         this.setSoapBody(xml.getBytes());
  262.     }
  263.     public void setSoapBody(byte[] xml) throws DynamicException {
  264.         if(this.message!=null && ServiceBinding.SOAP.equals(this.message.getServiceBinding())) {
  265.            
  266.             try {
  267.            
  268.                 OpenSPCoop2SoapMessage soapMsg = this.message.castAsSoap();
  269.                
  270.                 SOAPElement soapElement = soapMsg.createSOAPElement(xml);
  271.                
  272.                 soapMsg.getSOAPBody().removeContents();
  273.                 soapMsg.getSOAPBody().addChildElement(soapElement);
  274.                
  275.             }catch(Exception t) {
  276.                 throw new DynamicException(t.getMessage(),t);
  277.             }
  278.            
  279.         }
  280.     }
  281.    
  282.     public void addSoapBody(String xml, String xpath) throws DynamicException {
  283.         this.addSoapBody(xml.getBytes(), xpath);
  284.     }
  285.     public void addSoapBody(byte[] xml, String xpath) throws DynamicException {
  286.         if(this.message!=null && ServiceBinding.SOAP.equals(this.message.getServiceBinding())) {
  287.            
  288.             try {
  289.            
  290.                 OpenSPCoop2SoapMessage soapMsg = this.message.castAsSoap();
  291.                
  292.                 SOAPBody body = soapMsg.getSOAPBody();
  293.                 XPathExpressionEngine xpathEngine = new XPathExpressionEngine(this.message.getFactory());
  294.                 DynamicNamespaceContext dnc = new DynamicNamespaceContext();
  295.                 dnc.findPrefixNamespace(body);
  296.                 Node n = (Node) xpathEngine.getMatchPattern(body, dnc, xpath, XPathReturnType.NODE);
  297.                
  298.                 SOAPElement soapElement = soapMsg.createSOAPElement(xml);
  299.                
  300.                 if(n instanceof SOAPElement) {
  301.                     SOAPElement s = (SOAPElement) n;
  302.                     s.addChildElement(soapElement);
  303.                 }
  304.                 else {
  305.                     Node nImported = body.getOwnerDocument().importNode(soapElement, true);
  306.                     n.appendChild(nImported);
  307.                 }
  308.                
  309.             }catch(Exception t) {
  310.                 throw new DynamicException(t.getMessage(),t);
  311.             }
  312.            
  313.         }
  314.     }
  315.    
  316.     public void addSWAStartParameterIfNotPresentBeforeSend() {
  317.         CostantiPdD.addSWAStartParameterIfNotPresent(this.message);
  318.     }
  319.     public void addSWAStartParameterIfNotPresent() throws DynamicException {
  320.         try {
  321.             SoapUtils.addSWAStartParameterIfNotPresent(this.message);
  322.         }catch(Exception t) {
  323.             throw new DynamicException(t.getMessage(),t);
  324.         }
  325.     }
  326.     public void addSWAStartParameterIfNotPresent(boolean addOnlyIfExistsContentIdRootPart) throws DynamicException {
  327.         try {
  328.             SoapUtils.addSWAStartParameterIfNotPresent(this.message, addOnlyIfExistsContentIdRootPart);
  329.         }catch(Exception t) {
  330.             throw new DynamicException(t.getMessage(),t);
  331.         }
  332.     }
  333.     public void addSWAStartParameterIfNotPresent(boolean addOnlyIfExistsContentIdRootPart, boolean forceAddStartParameter) throws DynamicException {
  334.         try {
  335.             SoapUtils.addSWAStartParameterIfNotPresent(this.message, addOnlyIfExistsContentIdRootPart, forceAddStartParameter);
  336.         }catch(Exception t) {
  337.             throw new DynamicException(t.getMessage(),t);
  338.         }
  339.     }
  340.    
  341.    
  342.    
  343.     // Utility JSON
  344.    
  345.     public void prettyFormatJsonContent() throws DynamicException {
  346.         if(!isRestJson()) {
  347.             throw new DynamicException(ContentReader.FUNZIONALITA_RICHIEDE_JSON);
  348.         }
  349.         try {
  350.             this.message.castAsRestJson().prettyFormatContent();
  351.         }catch(Exception e) {
  352.             throw new DynamicException("Operazione fallita: "+e.getMessage(),e);
  353.         }
  354.     }
  355.    
  356.     public void addSimpleJsonElement(String name, Object value) throws DynamicException {
  357.         if(!isRestJson()) {
  358.             throw new DynamicException(ContentReader.FUNZIONALITA_RICHIEDE_JSON);
  359.         }
  360.         try {
  361.             this.message.castAsRestJson().addSimpleElement(name, value);
  362.         }catch(Exception e) {
  363.             throw new DynamicException(e.getMessage(),e);
  364.         }
  365.     }
  366.     public void addSimpleJsonElement(String jsonPath, String name, Object value) throws DynamicException {
  367.         if(!isRestJson()) {
  368.             throw new DynamicException(ContentReader.FUNZIONALITA_RICHIEDE_JSON);
  369.         }
  370.         try {
  371.             this.message.castAsRestJson().addSimpleElement(jsonPath, name, value);
  372.         }catch(Exception e) {
  373.             throw new DynamicException(e.getMessage(),e);
  374.         }
  375.     }
  376.     public void addObjectJsonElement(String name, Object value) throws DynamicException {
  377.         if(!isRestJson()) {
  378.             throw new DynamicException(ContentReader.FUNZIONALITA_RICHIEDE_JSON);
  379.         }
  380.         try {
  381.             this.message.castAsRestJson().addObjectElement(name, value);
  382.         }catch(Exception e) {
  383.             throw new DynamicException(e.getMessage(),e);
  384.         }
  385.     }
  386.     public void addObjectJsonElement(String jsonPath, String name, Object value) throws DynamicException {
  387.         if(!isRestJson()) {
  388.             throw new DynamicException(ContentReader.FUNZIONALITA_RICHIEDE_JSON);
  389.         }
  390.         try {
  391.             this.message.castAsRestJson().addObjectElement(jsonPath, name, value);
  392.         }catch(Exception e) {
  393.             throw new DynamicException(e.getMessage(),e);
  394.         }
  395.     }
  396.     public void addArrayJsonElement(String name, Object value) throws DynamicException {
  397.         if(!isRestJson()) {
  398.             throw new DynamicException(ContentReader.FUNZIONALITA_RICHIEDE_JSON);
  399.         }
  400.         try {
  401.             this.message.castAsRestJson().addArrayElement(name, value);
  402.         }catch(Exception e) {
  403.             throw new DynamicException(e.getMessage(),e);
  404.         }
  405.     }
  406.     public void addArrayJsonElement(String jsonPath, String name, Object value) throws DynamicException {
  407.         if(!isRestJson()) {
  408.             throw new DynamicException(ContentReader.FUNZIONALITA_RICHIEDE_JSON);
  409.         }
  410.         try {
  411.             this.message.castAsRestJson().addArrayElement(jsonPath, name, value);
  412.         }catch(Exception e) {
  413.             throw new DynamicException(e.getMessage(),e);
  414.         }
  415.     }
  416.     public void removeJsonField(String name) throws DynamicException {
  417.         if(!isRestJson()) {
  418.             throw new DynamicException(ContentReader.FUNZIONALITA_RICHIEDE_JSON);
  419.         }
  420.         try {
  421.             this.message.castAsRestJson().removeElement(name);
  422.         }catch(Exception e) {
  423.             throw new DynamicException(e.getMessage(),e);
  424.         }
  425.     }
  426.     public void removeJsonField(String jsonPath, String name) throws DynamicException {
  427.         if(!isRestJson()) {
  428.             throw new DynamicException(ContentReader.FUNZIONALITA_RICHIEDE_JSON);
  429.         }
  430.         try {
  431.             this.message.castAsRestJson().removeElement(jsonPath, name);
  432.         }catch(Exception e) {
  433.             throw new DynamicException(e.getMessage(),e);
  434.         }
  435.     }
  436.    

  437.     // Utility XML
  438.    
  439.     public void addXmlElement(String name, String value) throws DynamicException {
  440.         if(!isRestXml()) {
  441.             throw new DynamicException(ContentReader.FUNZIONALITA_RICHIEDE_XML);
  442.         }
  443.         try {
  444.             this.message.castAsRestXml().addElement(name, value);
  445.         }catch(Exception e) {
  446.             throw new DynamicException(e.getMessage(),e);
  447.         }
  448.     }
  449.     public void addXmlElement(String name, String namespace, String value) throws DynamicException {
  450.         if(!isRestXml()) {
  451.             throw new DynamicException(ContentReader.FUNZIONALITA_RICHIEDE_XML);
  452.         }
  453.         try {
  454.             this.message.castAsRestXml().addElement(name, namespace, value);
  455.         }catch(Exception e) {
  456.             throw new DynamicException(e.getMessage(),e);
  457.         }
  458.     }
  459.    
  460.     public void addXmlElementIn(String pattern, String name, String value) throws DynamicException {
  461.         if(!isRestXml()) {
  462.             throw new DynamicException(ContentReader.FUNZIONALITA_RICHIEDE_XML);
  463.         }
  464.         try {
  465.             this.message.castAsRestXml().addElementIn(pattern, name, value);
  466.         }catch(Exception e) {
  467.             throw new DynamicException(e.getMessage(),e);
  468.         }
  469.     }
  470.     public void addXmlElementIn(String pattern, String name, String namespace, String value) throws DynamicException {
  471.         if(!isRestXml()) {
  472.             throw new DynamicException(ContentReader.FUNZIONALITA_RICHIEDE_XML);
  473.         }
  474.         try {
  475.             this.message.castAsRestXml().addElementIn(pattern, name, namespace, value);
  476.         }catch(Exception e) {
  477.             throw new DynamicException(e.getMessage(),e);
  478.         }
  479.     }
  480.    
  481.     public void removeXmlElement(String name) throws DynamicException {
  482.         if(!isRestXml()) {
  483.             throw new DynamicException(ContentReader.FUNZIONALITA_RICHIEDE_XML);
  484.         }
  485.         try {
  486.             this.message.castAsRestXml().removeElement(name);
  487.         }catch(Exception e) {
  488.             throw new DynamicException(e.getMessage(),e);
  489.         }
  490.     }
  491.     public void removeXmlElement(String name, String namespace) throws DynamicException {
  492.         if(!isRestXml()) {
  493.             throw new DynamicException(ContentReader.FUNZIONALITA_RICHIEDE_XML);
  494.         }
  495.         try {
  496.             this.message.castAsRestXml().removeElement(name, namespace);
  497.         }catch(Exception e) {
  498.             throw new DynamicException(e.getMessage(),e);
  499.         }
  500.     }
  501.    
  502.     public void removeXmlElementIn(String pattern, String name) throws DynamicException {
  503.         if(!isRestXml()) {
  504.             throw new DynamicException(ContentReader.FUNZIONALITA_RICHIEDE_XML);
  505.         }
  506.         try {
  507.             this.message.castAsRestXml().removeElementIn(pattern, name);
  508.         }catch(Exception e) {
  509.             throw new DynamicException(e.getMessage(),e);
  510.         }
  511.     }
  512.     public void removeXmlElementIn(String pattern, String name, String namespace) throws DynamicException {
  513.         if(!isRestXml()) {
  514.             throw new DynamicException(ContentReader.FUNZIONALITA_RICHIEDE_XML);
  515.         }
  516.         try {
  517.             this.message.castAsRestXml().removeElementIn(pattern, name, namespace);
  518.         }catch(Exception e) {
  519.             throw new DynamicException(e.getMessage(),e);
  520.         }
  521.     }

  522. }