PatternExtractor.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.List;

  22. import org.apache.commons.lang.StringUtils;
  23. import org.openspcoop2.message.OpenSPCoop2Message;
  24. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  25. import org.openspcoop2.message.OpenSPCoop2RestJsonMessage;
  26. import org.openspcoop2.message.constants.MessageRole;
  27. import org.openspcoop2.message.constants.MessageType;
  28. import org.openspcoop2.message.xml.MessageXMLUtils;
  29. import org.openspcoop2.protocol.sdk.Context;
  30. import org.openspcoop2.utils.SemaphoreLock;
  31. import org.openspcoop2.utils.json.JsonPathNotFoundException;
  32. import org.openspcoop2.utils.json.JsonPathNotValidException;
  33. import org.openspcoop2.utils.transport.http.HttpConstants;
  34. import org.openspcoop2.utils.xml.AbstractXPathExpressionEngine;
  35. import org.openspcoop2.utils.xml.DynamicNamespaceContext;
  36. import org.openspcoop2.utils.xml.XPathNotFoundException;
  37. import org.openspcoop2.utils.xml.XPathNotValidException;
  38. import org.openspcoop2.utils.xml.XPathReturnType;
  39. import org.openspcoop2.utils.xml2json.JsonXmlPathExpressionEngine;
  40. import org.slf4j.Logger;
  41. import org.w3c.dom.Element;
  42. import org.w3c.dom.Node;
  43. import org.w3c.dom.NodeList;

  44. /**
  45.  * GestorePathEstrazione
  46.  *
  47.  * @author Andrea Poli (apoli@link.it)
  48.  * @author $Author$
  49.  * @version $Rev$, $Date$
  50.  */
  51. public class PatternExtractor {

  52.     public static PatternExtractor getJsonPatternExtractor(String json, Logger log, boolean bufferMessage_readOnly, Context context) throws DynamicException {
  53.         try {
  54.             OpenSPCoop2MessageFactory messageFactory = OpenSPCoop2MessageFactory.getDefaultMessageFactory();
  55.             OpenSPCoop2Message jsonMessageVerifica = messageFactory.createMessage(MessageType.JSON, MessageRole.NONE,
  56.                     HttpConstants.CONTENT_TYPE_JSON, json.getBytes()).getMessage();
  57.             MessageContent messageContent = new MessageContent(jsonMessageVerifica.castAsRestJson(), bufferMessage_readOnly, context);
  58.             return new PatternExtractor(messageFactory, messageContent, log);
  59.         }catch(Exception e) {
  60.             throw new DynamicException(e.getMessage(),e);
  61.         }
  62.     }
  63.     public static PatternExtractor getXmlPatternExtractor(Element element, Logger log, boolean bufferMessage_readOnly, Context context) throws DynamicException {
  64.         try {
  65.             OpenSPCoop2MessageFactory messageFactory = OpenSPCoop2MessageFactory.getDefaultMessageFactory();
  66.             OpenSPCoop2Message jsonMessageVerifica = messageFactory.createMessage(MessageType.XML, MessageRole.NONE,
  67.                     HttpConstants.CONTENT_TYPE_XML, MessageXMLUtils.getInstance(messageFactory).toByteArray(element, true)).getMessage();
  68.             MessageContent messageContent = new MessageContent(jsonMessageVerifica.castAsRestXml(), bufferMessage_readOnly, context);
  69.             return new PatternExtractor(messageFactory, messageContent, log);
  70.         }catch(Exception e) {
  71.             throw new DynamicException(e.getMessage(),e);
  72.         }
  73.     }
  74.    
  75.     private Logger log;
  76.    
  77.     private OpenSPCoop2MessageFactory messageFactory;
  78.     private Element element = null;
  79.     private Boolean refresh = null;
  80.     private DynamicNamespaceContext dnc;
  81.    
  82.     private String elementJson = null;
  83.    
  84.     private MessageContent messageContent = null;
  85.     private boolean messageContent_initialized = false;
  86.    
  87.     public PatternExtractor(OpenSPCoop2MessageFactory messageFactory, MessageContent messageContent, Logger log) {
  88.         this.messageFactory = messageFactory;
  89.         this.messageContent = messageContent;
  90.         this.log = log;
  91.     }
  92.    
  93.     // I seguenti costruttori li lascio per backward compatibility se usati dentro delle trasformazioni
  94.     @Deprecated
  95.     public PatternExtractor(OpenSPCoop2MessageFactory messageFactory, Element element, Logger log) {
  96.         try {
  97.             PatternExtractor pe = getXmlPatternExtractor(element, log, false, null);
  98.             this.messageFactory = pe.messageFactory;
  99.             this.messageContent = pe.messageContent;
  100.             this.log = pe.log;
  101.         }catch(Exception e) {
  102.             throw new RuntimeException(e.getMessage(),e);
  103.         }
  104.     }
  105.     @Deprecated
  106.     public PatternExtractor(String elementJson, Logger log) {
  107.         try {
  108.             PatternExtractor pe = getJsonPatternExtractor(elementJson, log, false, null);
  109.             this.messageFactory = pe.messageFactory;
  110.             this.messageContent = pe.messageContent;
  111.             this.log = pe.log;
  112.         }catch(Exception e) {
  113.             throw new RuntimeException(e.getMessage(),e);
  114.         }
  115.     }
  116.    
  117.    
  118.     private void init() throws DynamicException {
  119.         if(!this.messageContent_initialized) {
  120.             _init();
  121.         }
  122.     }
  123.     private org.openspcoop2.utils.Semaphore semaphore = new org.openspcoop2.utils.Semaphore("PatternExtractor");
  124.     private void _init() throws DynamicException {
  125.         SemaphoreLock slock = this.semaphore.acquireThrowRuntime("init");
  126.         try {
  127.             if(!this.messageContent_initialized) {
  128.                 if(this.messageContent!=null) {
  129.                     try {
  130.                         //this.messageFactory = this.messageContent.getMessageFactory();
  131.                         if(this.messageContent.isJson()) {
  132.                             this.elementJson = this.messageContent.getElementJson();
  133.                         }
  134.                         else if(this.messageContent.isXml()) {
  135.                             this.element = this.messageContent.getElement();
  136.                             this.dnc = new DynamicNamespaceContext();
  137.                             this.dnc.findPrefixNamespace(this.element);
  138.                         }
  139.                         else if(this.messageContent.isRestMultipart()) {
  140.                             this.elementJson = this.messageContent.getElementJson();
  141.                             if(this.elementJson==null) {
  142.                                 this.element = this.messageContent.getElement();
  143.                                 if(this.element!=null) {
  144.                                     this.dnc = new DynamicNamespaceContext();
  145.                                     this.dnc.findPrefixNamespace(this.element);
  146.                                 }
  147.                                 else {
  148.                                     throw new Exception("Invalid multipart content");
  149.                                 }
  150.                             }
  151.                         }
  152.                     }catch(Exception e) {
  153.                         throw new DynamicException(e.getMessage(),e);
  154.                     }
  155.                 }
  156.                 this.messageContent_initialized = true;
  157.             }
  158.         }finally {
  159.             this.semaphore.release(slock, "init");
  160.         }
  161.     }
  162.    
  163.    
  164.     public void refreshContent() {
  165.         if(this.element!=null && this.refresh==null) {
  166.             this._refreshContent();
  167.         }
  168.     }
  169.     private void _refreshContent() {
  170.         SemaphoreLock slock = this.semaphore.acquireThrowRuntime("refreshContent");
  171.         // effettuo il refresh, altrimenti le regole xpath applicate sulla richiesta, nel flusso di risposta (es. header http della risposta) non funzionano.
  172.         try {
  173.             this.refresh = true;
  174.             if(this.element!=null) {
  175.                 MessageXMLUtils xmlUtils = MessageXMLUtils.getInstance(this.messageFactory);
  176.                 this.element = xmlUtils.newElement(xmlUtils.toByteArray(this.element));
  177.             }
  178.         }catch(Exception e){
  179.             this.log.error("Refresh fallito: "+e.getMessage(),e);
  180.         }finally {
  181.             this.semaphore.release(slock, "refreshContent");
  182.         }
  183.     }
  184.    
  185.     public boolean match(String pattern) throws DynamicException {
  186.         init();
  187.        
  188.         String v = read(pattern);
  189.         return v!=null && !"".equals(v);
  190.     }
  191.    
  192.     public String read(String pattern) throws DynamicException {
  193.         init();
  194.        
  195.         String operazione = "Read (pattern: "+pattern+")";
  196.        
  197.         String valore = null;
  198.         try {
  199.             if(this.element!=null) {
  200.                 AbstractXPathExpressionEngine xPathEngine = new org.openspcoop2.message.xml.XPathExpressionEngine(this.messageFactory);
  201.                 valore = AbstractXPathExpressionEngine.extractAndConvertResultAsString(this.element, this.dnc, xPathEngine, pattern, this.log);
  202.             }
  203.             else {
  204.                 valore = JsonXmlPathExpressionEngine.extractAndConvertResultAsString(this.elementJson, pattern, this.log);
  205.             }
  206.         }
  207.         catch(XPathNotFoundException e){
  208.             this.log.debug(operazione+" failed, results not found: "+e.getMessage(),e);
  209.         }
  210.         catch(XPathNotValidException e){
  211.             throw new DynamicException(e.getMessage(),e);
  212.         }
  213.         catch(JsonPathNotFoundException e){
  214.             this.log.debug(operazione+" failed, results not found: "+e.getMessage(),e);
  215.         }
  216.         catch(JsonPathNotValidException e){
  217.             throw new DynamicException(e.getMessage(),e);
  218.         }
  219.         catch(org.openspcoop2.utils.UtilsMultiException e) {
  220.             int index = 0;
  221.             boolean notFound = true;
  222.             boolean notValid = true;
  223.             for (Throwable t : e.getExceptions()) {
  224.                 if(t instanceof XPathNotFoundException || t instanceof JsonPathNotFoundException) {
  225.                     this.log.debug("["+index+"] "+operazione+" failed: "+t.getMessage(),t);
  226.                 }
  227.                 else {
  228.                     notFound = false;
  229.                 }
  230.                
  231.                 if(!(t instanceof XPathNotValidException) && !(t instanceof JsonPathNotValidException)) {
  232.                     notValid = false;
  233.                 }
  234.                
  235.                 index++;
  236.             }
  237.             if(!notFound) {
  238.                 if(notValid) {
  239.                     throw new DynamicException(e.getMessage(),e);
  240.                 }
  241.                 else {
  242.                     throw new DynamicException(operazione+" failed: "+e.getMessage(),e);
  243.                 }
  244.             }
  245.         }
  246.         catch(Exception e){
  247.             throw new DynamicException(operazione+" failed: "+e.getMessage(),e);
  248.         }
  249.         return valore;
  250.     }
  251.    
  252.     public List<String> readList(String pattern) throws DynamicException {
  253.         init();
  254.        
  255.         String operazione = "ReadList (pattern: "+pattern+")";
  256.        
  257.         List<String> valore = null;
  258.         try {
  259.             if(this.element!=null) {
  260.                 AbstractXPathExpressionEngine xPathEngine = new org.openspcoop2.message.xml.XPathExpressionEngine(this.messageFactory);
  261.                 xPathEngine.getMatchPattern(this.element, this.dnc, pattern, XPathReturnType.BOOLEAN);
  262.                 valore = AbstractXPathExpressionEngine.extractAndConvertResultAsList(this.element, this.dnc, xPathEngine, pattern, this.log);
  263.             }
  264.             else {
  265.                 valore = JsonXmlPathExpressionEngine.extractAndConvertResultAsList(this.elementJson, pattern, this.log);
  266.             }
  267.         }
  268.         catch(XPathNotFoundException e){
  269.             this.log.debug(operazione+" failed, results not found: "+e.getMessage(),e);
  270.         }
  271.         catch(XPathNotValidException e){
  272.             throw new DynamicException(e.getMessage(),e);
  273.         }
  274.         catch(JsonPathNotFoundException e){
  275.             this.log.debug(operazione+" failed, results not found: "+e.getMessage(),e);
  276.         }
  277.         catch(JsonPathNotValidException e){
  278.             throw new DynamicException(e.getMessage(),e);
  279.         }
  280.         catch(org.openspcoop2.utils.UtilsMultiException e) {
  281.             int index = 0;
  282.             boolean notFound = true;
  283.             boolean notValid = true;
  284.             for (Throwable t : e.getExceptions()) {
  285.                 if(t instanceof XPathNotFoundException || t instanceof JsonPathNotFoundException) {
  286.                     this.log.debug("["+index+"] "+operazione+" failed: "+t.getMessage(),t);
  287.                 }
  288.                 else {
  289.                     notFound = false;
  290.                 }
  291.                
  292.                 if(!(t instanceof XPathNotValidException) && !(t instanceof JsonPathNotValidException)) {
  293.                     notValid = false;
  294.                 }
  295.                
  296.                 index++;
  297.             }
  298.             if(!notFound) {
  299.                 if(notValid) {
  300.                     throw new DynamicException(e.getMessage(),e);
  301.                 }
  302.                 else {
  303.                     throw new DynamicException(operazione+" failed: "+e.getMessage(),e);
  304.                 }
  305.             }
  306.         }
  307.         catch(Exception e){
  308.             throw new DynamicException(operazione+" failed: "+e.getMessage(),e);
  309.         }
  310.         return valore;
  311.     }
  312.    
  313.     // Lascio per utilizzi in vecchie trasformazioni
  314.     @Deprecated
  315.     public void remove(String pattern) throws DynamicException {
  316.         this._remove(pattern, null);
  317.     }
  318.     public void removeByXPath(String pattern) throws DynamicException {
  319.         if(pattern==null || StringUtils.isEmpty(pattern)) {
  320.             throw new DynamicException("XPath pattern undefined");
  321.         }
  322.         this._remove(pattern, null);
  323.     }
  324.     public void removeJsonElement(String name) throws DynamicException {
  325.         if(name==null || StringUtils.isEmpty(name)) {
  326.             throw new DynamicException("JsonElement name undefined");
  327.         }
  328.         this._remove(null, name);
  329.     }
  330.     public void removeByJsonPath(String pattern, String name) throws DynamicException {
  331.         if(pattern==null || StringUtils.isEmpty(pattern)) {
  332.             throw new DynamicException("JsonPath pattern undefined");
  333.         }
  334.         if(name==null || StringUtils.isEmpty(name)) {
  335.             throw new DynamicException("JsonElement name undefined");
  336.         }
  337.         this._remove(pattern, name);
  338.     }
  339.     private void _remove(String pattern, String name) throws DynamicException {
  340.         init();
  341.        
  342.         String opName = "";
  343.         if(name!=null) {
  344.             opName="name:"+name+"";
  345.         }
  346.         String opPattern = "";
  347.         if(pattern!=null) {
  348.             opPattern="pattern:"+pattern+"";
  349.             if(name!=null) {
  350.                 opPattern+=" ";
  351.             }
  352.         }
  353.         String operazione = "Remove ("+opPattern+""+opName+")";
  354.        
  355.         try {
  356.             if(this.messageContent!=null) {
  357.                 this.messageContent.setUpdatable();
  358.             }
  359.            
  360.             if(this.element!=null) {
  361.                 AbstractXPathExpressionEngine xPathEngine = new org.openspcoop2.message.xml.XPathExpressionEngine(this.messageFactory);
  362.                 Node n = (Node) xPathEngine.getMatchPattern(this.element, this.dnc, pattern, XPathReturnType.NODE);
  363.                 if(n.getParentNode()!=null) {
  364.                     n.getParentNode().removeChild(n);  
  365.                 }
  366.                 else {
  367.                     this.element.removeChild(n);
  368.                 }
  369.             }
  370.             else {
  371.                 if(this.messageContent!=null && this.messageContent.isJson() && this.messageContent.getJsonMessage()!=null) {
  372.                     OpenSPCoop2RestJsonMessage json = this.messageContent.getJsonMessage();
  373.                     if(pattern!=null) {
  374.                         json.removeElement(pattern, name);
  375.                     }
  376.                     else {
  377.                         json.removeElement(name);
  378.                     }
  379.                 }
  380.                 else {
  381.                     // retrocompatibilita'
  382.                     String s = JsonXmlPathExpressionEngine.extractAndConvertResultAsString(this.elementJson, pattern, this.log);
  383.                     this.elementJson = this.elementJson.replace(s, "");
  384.                 }
  385.             }
  386.         }
  387.         catch(XPathNotFoundException e){
  388.             this.log.debug(operazione+" failed, results not found: "+e.getMessage(),e);
  389.         }
  390.         catch(XPathNotValidException e){
  391.             throw new DynamicException(e.getMessage(),e);
  392.         }
  393.         catch(JsonPathNotFoundException e){
  394.             this.log.debug(operazione+" failed, results not found: "+e.getMessage(),e);
  395.         }
  396.         catch(JsonPathNotValidException e){
  397.             throw new DynamicException(e.getMessage(),e);
  398.         }
  399.         catch(org.openspcoop2.utils.UtilsMultiException e) {
  400.             int index = 0;
  401.             boolean notFound = true;
  402.             boolean notValid = true;
  403.             for (Throwable t : e.getExceptions()) {
  404.                 if(t instanceof XPathNotFoundException || t instanceof JsonPathNotFoundException) {
  405.                     this.log.debug("["+index+"] "+operazione+" failed: "+t.getMessage(),t);
  406.                 }
  407.                 else {
  408.                     notFound = false;
  409.                 }
  410.                
  411.                 if(!(t instanceof XPathNotValidException) && !(t instanceof JsonPathNotValidException)) {
  412.                     notValid = false;
  413.                 }
  414.                
  415.                 index++;
  416.             }
  417.             if(!notFound) {
  418.                 if(notValid) {
  419.                     throw new DynamicException(e.getMessage(),e);
  420.                 }
  421.                 else {
  422.                     throw new DynamicException(operazione+" failed: "+e.getMessage(),e);
  423.                 }
  424.             }
  425.         }
  426.         catch(Exception e){
  427.             throw new DynamicException(operazione+" failed: "+e.getMessage(),e);
  428.         }
  429.     }
  430.    
  431.     // Lascio per utilizzi in vecchie trasformazioni
  432.     @Deprecated
  433.     public void removeList(String pattern) throws DynamicException {
  434.         this._removeList(pattern);
  435.     }
  436.     public void removeListByXPath(String pattern) throws DynamicException {
  437.         if(pattern==null || StringUtils.isEmpty(pattern)) {
  438.             throw new DynamicException("XPath pattern undefined");
  439.         }
  440.         this._remove(pattern, null);
  441.     }
  442.     private void _removeList(String pattern) throws DynamicException {
  443.         init();
  444.        
  445.         String operazione = "RemoveList (pattern: "+pattern+")";
  446.        
  447.         try {
  448.             this.messageContent.setUpdatable();
  449.            
  450.             if(this.element!=null) {
  451.                 AbstractXPathExpressionEngine xPathEngine = new org.openspcoop2.message.xml.XPathExpressionEngine(this.messageFactory);
  452.                 NodeList nList = (NodeList) xPathEngine.getMatchPattern(this.element, this.dnc, pattern, XPathReturnType.NODESET);
  453.                 if(nList!=null && nList.getLength()>0) {
  454.                     for (int i = 0; i < nList.getLength(); i++) {
  455.                         Node n = nList.item(i);
  456.                         if(n.getParentNode()!=null) {
  457.                             n.getParentNode().removeChild(n);  
  458.                         }
  459.                         else {
  460.                             this.element.removeChild(n);
  461.                         }          
  462.                     }
  463.                 }
  464.             }
  465.             else {
  466.                 // Soluzione deprecata: usare removeByJsonPath
  467.                 List<String> valori = JsonXmlPathExpressionEngine.extractAndConvertResultAsList(this.elementJson, pattern, this.log);
  468.                 if(valori!=null && !valori.isEmpty()) {
  469.                     for (String valore : valori) {
  470.                         this.elementJson = this.elementJson.replace(valore, "");            
  471.                     }
  472.                 }
  473.             }
  474.         }
  475.         catch(XPathNotFoundException e){
  476.             this.log.debug(operazione+" failed, results not found: "+e.getMessage(),e);
  477.         }
  478.         catch(XPathNotValidException e){
  479.             throw new DynamicException(e.getMessage(),e);
  480.         }
  481.         catch(JsonPathNotFoundException e){
  482.             this.log.debug(operazione+" failed, results not found: "+e.getMessage(),e);
  483.         }
  484.         catch(JsonPathNotValidException e){
  485.             throw new DynamicException(e.getMessage(),e);
  486.         }
  487.         catch(org.openspcoop2.utils.UtilsMultiException e) {
  488.             int index = 0;
  489.             boolean notFound = true;
  490.             boolean notValid = true;
  491.             for (Throwable t : e.getExceptions()) {
  492.                 if(t instanceof XPathNotFoundException || t instanceof JsonPathNotFoundException) {
  493.                     this.log.debug("["+index+"] "+operazione+" failed: "+t.getMessage(),t);
  494.                 }
  495.                 else {
  496.                     notFound = false;
  497.                 }
  498.                
  499.                 if(!(t instanceof XPathNotValidException) && !(t instanceof JsonPathNotValidException)) {
  500.                     notValid = false;
  501.                 }
  502.                
  503.                 index++;
  504.             }
  505.             if(!notFound) {
  506.                 if(notValid) {
  507.                     throw new DynamicException(e.getMessage(),e);
  508.                 }
  509.                 else {
  510.                     throw new DynamicException(operazione+" failed: "+e.getMessage(),e);
  511.                 }
  512.             }
  513.         }
  514.         catch(Exception e){
  515.             throw new DynamicException(operazione+" failed: "+e.getMessage(),e);
  516.         }
  517.     }
  518.    
  519.    
  520.    
  521.     public void replaceValueByXPath(String pattern, String newValue) throws DynamicException {
  522.         if(pattern==null || StringUtils.isEmpty(pattern)) {
  523.             throw new DynamicException("XPath pattern undefined");
  524.         }
  525.         if(newValue==null) {
  526.             throw new DynamicException("New value undefined");
  527.         }
  528.         this._replaceValue(pattern, null, newValue);
  529.     }
  530.     public void replaceJsonElementValue(String name, Object newValue) throws DynamicException {
  531.         if(name==null || StringUtils.isEmpty(name)) {
  532.             throw new DynamicException("JsonElement name undefined");
  533.         }
  534.         if(newValue==null) {
  535.             throw new DynamicException("New value undefined");
  536.         }
  537.         this._replaceValue(null, name, newValue);
  538.     }
  539.     public void replaceValueByJsonPath(String pattern, String name, Object newValue) throws DynamicException {
  540.         if(pattern==null || StringUtils.isEmpty(pattern)) {
  541.             throw new DynamicException("JsonPath pattern undefined");
  542.         }
  543.         if(name==null || StringUtils.isEmpty(name)) {
  544.             throw new DynamicException("JsonElement name undefined");
  545.         }
  546.         if(newValue==null) {
  547.             throw new DynamicException("New value undefined");
  548.         }
  549.         this._replaceValue(pattern, name, newValue);
  550.     }
  551.     private void _replaceValue(String pattern, String name, Object value) throws DynamicException {
  552.         init();
  553.        
  554.         String opName = "";
  555.         if(name!=null) {
  556.             opName=" name:"+name+"";
  557.         }
  558.         String operazione = "Replace value (pattern: "+pattern+""+opName+")";
  559.        
  560.         try {
  561.             if(this.messageContent!=null) {
  562.                 this.messageContent.setUpdatable();
  563.             }
  564.            
  565.             if(this.element!=null) {
  566.                 AbstractXPathExpressionEngine xPathEngine = new org.openspcoop2.message.xml.XPathExpressionEngine(this.messageFactory);
  567.                 Node n = (Node) xPathEngine.getMatchPattern(this.element, this.dnc, pattern, XPathReturnType.NODE);
  568.                 n.setTextContent(value.toString());
  569.             }
  570.             else {
  571.                 if(this.messageContent!=null && this.messageContent.isJson() && this.messageContent.getJsonMessage()!=null) {
  572.                     OpenSPCoop2RestJsonMessage json = this.messageContent.getJsonMessage();
  573.                     if(pattern!=null) {
  574.                         json.replaceValue(pattern, name, value);
  575.                     }
  576.                     else {
  577.                         json.replaceValue(name, value);
  578.                     }
  579.                 }
  580.             }
  581.         }
  582.         catch(XPathNotFoundException e){
  583.             this.log.debug(operazione+" failed, results not found: "+e.getMessage(),e);
  584.         }
  585.         catch(XPathNotValidException e){
  586.             throw new DynamicException(e.getMessage(),e);
  587.         }
  588.         catch(Exception e){
  589.             throw new DynamicException(operazione+" failed: "+e.getMessage(),e);
  590.         }
  591.     }
  592.    
  593.    
  594.     public void replaceValuesByXPath(String pattern, String newValue) throws DynamicException {
  595.         if(pattern==null || StringUtils.isEmpty(pattern)) {
  596.             throw new DynamicException("XPath pattern undefined");
  597.         }
  598.         if(newValue==null) {
  599.             throw new DynamicException("New value undefined");
  600.         }
  601.         this._replaceValues(pattern, null, newValue);
  602.     }
  603.     private void _replaceValues(String pattern, String name, String value) throws DynamicException {
  604.         init();
  605.        
  606.         String opName = "";
  607.         if(name!=null) {
  608.             opName=" name:"+name+"";
  609.         }
  610.         String operazione = "Replace values (pattern: "+pattern+""+opName+")";
  611.        
  612.         try {
  613.             this.messageContent.setUpdatable();
  614.            
  615.             if(this.element!=null) {
  616.                 AbstractXPathExpressionEngine xPathEngine = new org.openspcoop2.message.xml.XPathExpressionEngine(this.messageFactory);
  617.                 NodeList nList = (NodeList) xPathEngine.getMatchPattern(this.element, this.dnc, pattern, XPathReturnType.NODESET);
  618.                 if(nList!=null && nList.getLength()>0) {
  619.                     for (int i = 0; i < nList.getLength(); i++) {
  620.                         Node n = nList.item(i);
  621.                         n.setTextContent(value);        
  622.                     }
  623.                 }
  624.             }
  625.             else {
  626.                 throw new Exception("Unsupported in json structure");
  627.             }
  628.         }
  629.         catch(XPathNotFoundException e){
  630.             this.log.debug(operazione+" failed, results not found: "+e.getMessage(),e);
  631.         }
  632.         catch(XPathNotValidException e){
  633.             throw new DynamicException(e.getMessage(),e);
  634.         }
  635.         catch(JsonPathNotFoundException e){
  636.             this.log.debug(operazione+" failed, results not found: "+e.getMessage(),e);
  637.         }
  638.         catch(JsonPathNotValidException e){
  639.             throw new DynamicException(e.getMessage(),e);
  640.         }
  641.         catch(org.openspcoop2.utils.UtilsMultiException e) {
  642.             int index = 0;
  643.             boolean notFound = true;
  644.             boolean notValid = true;
  645.             for (Throwable t : e.getExceptions()) {
  646.                 if(t instanceof XPathNotFoundException || t instanceof JsonPathNotFoundException) {
  647.                     this.log.debug("["+index+"] "+operazione+" failed: "+t.getMessage(),t);
  648.                 }
  649.                 else {
  650.                     notFound = false;
  651.                 }
  652.                
  653.                 if(!(t instanceof XPathNotValidException) && !(t instanceof JsonPathNotValidException)) {
  654.                     notValid = false;
  655.                 }
  656.                
  657.                 index++;
  658.             }
  659.             if(!notFound) {
  660.                 if(notValid) {
  661.                     throw new DynamicException(e.getMessage(),e);
  662.                 }
  663.                 else {
  664.                     throw new DynamicException(operazione+" failed: "+e.getMessage(),e);
  665.                 }
  666.             }
  667.         }
  668.         catch(Exception e){
  669.             throw new DynamicException(operazione+" failed: "+e.getMessage(),e);
  670.         }
  671.     }
  672. }