AbstractXMLDigestReader.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.utils.digest;

  21. import org.slf4j.Logger;
  22. import org.openspcoop2.utils.LoggerWrapperFactory;
  23. import org.openspcoop2.utils.UtilsException;
  24. import org.openspcoop2.utils.xml.AbstractXMLUtils;
  25. import org.openspcoop2.utils.xml.AbstractXPathExpressionEngine;
  26. import org.openspcoop2.utils.xml.DynamicNamespaceContext;
  27. import org.openspcoop2.utils.xml.XPathNotFoundException;
  28. import org.openspcoop2.utils.xml.XPathReturnType;
  29. import org.w3c.dom.Element;
  30. import org.w3c.dom.Node;

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

  39.     private Logger log;
  40.     private AbstractXPathExpressionEngine xpathEngine;
  41.     private AbstractXMLUtils xmlUtils;

  42.     public AbstractXMLDigestReader(AbstractXMLUtils xmlUtils,AbstractXPathExpressionEngine xpathEngine) {
  43.         this(LoggerWrapperFactory.getLogger(AbstractXMLDigestReader.class),xmlUtils,xpathEngine);
  44.     }
  45.     public AbstractXMLDigestReader(Logger log,AbstractXMLUtils xmlUtils,AbstractXPathExpressionEngine xpathEngine) {
  46.         this.log = log;
  47.         this.xmlUtils = xmlUtils;
  48.         this.xpathEngine = xpathEngine;
  49.     }

  50.     @Override
  51.     public String getDigest(Element element,String referenceId,DynamicNamespaceContext dnc) throws UtilsException{

  52.         try {

  53.             // Search ReferenceElement
  54.             String xPathReferenceElement =
  55.                     "//{"+Constants.DS_NAMESPACE+"}"+
  56.                             Constants.DS_REFERENCE_ELEMENT+
  57.                             "[@"+Constants.DS_REFERENCE_ATTRIBUTE_URI+"='"+referenceId+"']";
  58.             Node n = null;
  59.             try{
  60.                 this.log.debug("Search referenceId con xpath ["+xPathReferenceElement+"] ...");
  61.                 n = (Node) this.xpathEngine.getMatchPattern(element, dnc, xPathReferenceElement , XPathReturnType.NODE);
  62.             }catch(XPathNotFoundException notFound){
  63.                 xPathReferenceElement =
  64.                         "//"+Constants.DS_REFERENCE_ELEMENT+
  65.                         "[@"+Constants.DS_REFERENCE_ATTRIBUTE_URI+"='"+referenceId+"']";
  66.                 this.log.debug("Search(2) referenceId con xpath ["+xPathReferenceElement+"] ...");
  67.                 try{
  68.                     n = (Node) this.xpathEngine.getMatchPattern(element, dnc, xPathReferenceElement , XPathReturnType.NODE);
  69.                 }catch(XPathNotFoundException notFoundInternal){
  70.                     this.log.debug("Reference id ["+referenceId+"] non troavata");
  71.                     return null;
  72.                 }
  73.             }
  74.             byte [] nBytes= this.xmlUtils.toByteArray(n);
  75.             this.log.debug("Found Reference Element ["+this.xmlUtils.toString(n)+"]");

  76.             // Search DigestValue
  77.             String xPathDigestValue = "//{"+Constants.DS_NAMESPACE+"}"+Constants.DS_REFERENCE_DIGEST_VALUE_ELEMENT+"/text()";
  78.             String digest = null;
  79.             try{
  80.                 this.log.debug("Search digestValue con xpath ["+xPathDigestValue+"] ...");
  81.                 digest = (String) this.xpathEngine.getMatchPattern(this.xmlUtils.newDocument(nBytes), dnc, xPathDigestValue , XPathReturnType.STRING);
  82.             }catch(XPathNotFoundException notFound){
  83.                 xPathDigestValue = "//"+Constants.DS_REFERENCE_DIGEST_VALUE_ELEMENT+"/text()";
  84.                 this.log.debug("Search(2) digestValue con xpath ["+xPathDigestValue+"] ...");
  85.                 digest = (String) this.xpathEngine.getMatchPattern(element, dnc, xPathReferenceElement , XPathReturnType.STRING);
  86.             }
  87.             this.log.debug("Found DigestValue ["+digest+"]");

  88.             return digest;
  89.         } catch (Exception e) {
  90.             this.log.error("Errore durante la getDigest", e);
  91.             throw new UtilsException(e.getMessage(),e);
  92.         }
  93.    

  94.     }

  95. }