XMLSecEnvelopeIdResolver.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.security.message.signature;

  21. import java.util.Iterator;

  22. import javax.xml.soap.AttachmentPart;

  23. import org.apache.xml.security.signature.XMLSignatureInput;
  24. import org.apache.xml.security.utils.resolver.ResourceResolverContext;
  25. import org.apache.xml.security.utils.resolver.ResourceResolverException;
  26. import org.apache.xml.security.utils.resolver.ResourceResolverSpi;
  27. import org.apache.xml.utils.URI;
  28. import org.openspcoop2.message.OpenSPCoop2SoapMessage;
  29. import org.openspcoop2.security.message.constants.SecurityConstants;
  30. import org.openspcoop2.utils.LoggerWrapperFactory;
  31. import org.slf4j.Logger;
  32. import org.w3c.dom.Attr;
  33. import org.w3c.dom.Document;
  34. import org.w3c.dom.Element;


  35. /**
  36.  * XMLSecEnvelopeIdResolver
  37.  *
  38.  * @author Andrea Poli (apoli@link.it)
  39.  * @author $Author$
  40.  * @version $Rev$, $Date$
  41.  */
  42. public class XMLSecEnvelopeIdResolver extends ResourceResolverSpi {


  43.     private static final Logger logger = LoggerWrapperFactory.getLogger(XMLSecEnvelopeIdResolver.class);
  44.  
  45.     public static ResourceResolverSpi getInstance(OpenSPCoop2SoapMessage message) {
  46.         return new XMLSecEnvelopeIdResolver(message);
  47.     }

  48. //    private AbstractXMLUtils xmlUtils = null;
  49.     private OpenSPCoop2SoapMessage message = null;
  50.     private XMLSecEnvelopeIdResolver(OpenSPCoop2SoapMessage message) {
  51.         this.message = message;
  52. //      this.xmlUtils = XMLUtils.getInstance();
  53.     }

  54.     @SuppressWarnings("deprecation")
  55.     public XMLSignatureInput engineResolve(Attr uri, String BaseURI) throws ResourceResolverException {

  56.         //System.out.println("@ engineResolve DEPRECATED @");
  57.        
  58.         String wsuId = null;
  59.         boolean attach = false;
  60.         if(uri.getNodeValue().startsWith("#")){
  61.             wsuId = uri.getNodeValue().substring(1);
  62.         }else if(uri.getNodeValue().startsWith("cid:")){
  63.             wsuId = uri.getNodeValue().substring(4);
  64.             attach = true;
  65.         }else{
  66.             throw new ResourceResolverException("Cannot resoulve uri "+uri.getNodeValue(),uri.getLocalName(),BaseURI);
  67.         }
  68.         Document doc = uri.getOwnerDocument();

  69.         if (XMLSecEnvelopeIdResolver.logger.isDebugEnabled()) {
  70.             XMLSecEnvelopeIdResolver.logger.debug("Attempting to resolve : #" + wsuId);
  71.         }

  72.         if(attach){
  73.             Iterator<?> it = null;
  74.             try{
  75.                 it = this.message.getAttachments();
  76.             }catch(Exception e){
  77.                 throw new ResourceResolverException(e,"Cannot resoulve uri "+uri.getNodeValue(),uri.getLocalName(),BaseURI);
  78.             }
  79.             while (it.hasNext()) {
  80.                 AttachmentPart ap = (AttachmentPart) it.next();
  81.                 String contentId = ap.getContentId();
  82.                 if(contentId.startsWith("<"))
  83.                     contentId = contentId.substring(1);
  84.                 if(contentId.endsWith(">"))
  85.                     contentId = contentId.substring(0,contentId.length()-1);
  86.                 if(wsuId.equals(contentId)){
  87.                     try{
  88.                         byte[]raw = ap.getRawContentBytes();
  89.                         XMLSignatureInput result = new XMLSignatureInput(raw);
  90.                         result.setMIMEType(ap.getContentType());
  91.                         return result;
  92.                     }catch(Exception e){
  93.                         throw new ResourceResolverException(e.getMessage(),e,uri.getLocalName(),BaseURI);
  94.                     }
  95.                 }
  96.             }
  97.            
  98.             throw new ResourceResolverException("Cannot resoulve attachment uri "+uri.getNodeValue(),uri.getLocalName(),BaseURI);
  99.         }
  100.         else{
  101.             Element refElem = null;
  102.             try{
  103.                 refElem = EnvelopeIdResolverUtilities.findElementById(doc, wsuId, SecurityConstants.WSS_HEADER_UTILITY_NAMESPACE);
  104.                 if (refElem == null) {
  105.                     refElem = EnvelopeIdResolverUtilities.findElementById(doc, wsuId, "");
  106.                 }
  107.             }catch(Exception e){
  108.                 throw new ResourceResolverException("Cannot resoulve uri "+uri.getNodeValue()+" : "+e.getMessage(),e,uri.getLocalName(),BaseURI);
  109.             }
  110.    
  111.             XMLSignatureInput result = new XMLSignatureInput(refElem);
  112.             result.setMIMEType("text/xml");
  113.             try {
  114.                 URI uriNew = new URI(new URI(BaseURI), uri.getNodeValue());
  115.                 result.setSourceURI(uriNew.toString());
  116.             } catch (URI.MalformedURIException ex) {
  117.                 result.setSourceURI(BaseURI);
  118.             }
  119.    
  120.             if (XMLSecEnvelopeIdResolver.logger.isDebugEnabled()) {
  121.                 XMLSecEnvelopeIdResolver.logger.debug("Result: " + result);
  122.             }
  123.             return result;
  124.         }
  125.        
  126.     }

  127.     @Override
  128.     public XMLSignatureInput engineResolveURI(ResourceResolverContext context)
  129.             throws ResourceResolverException {
  130.         //System.out.println("@ engineResolveURI @");
  131.         return this.engineResolve(context.attr, context.baseUri);
  132.     }
  133.    
  134.    
  135.     /**
  136.      * This method helps the ResourceResolver to decide whether a
  137.      * ResourceResolverSpi is able to perform the requested action.
  138.      *
  139.      * @param uri
  140.      * @param BaseURI
  141.      * @return true if this attribute can be resolved
  142.      */
  143.     public boolean engineCanResolve(Attr uri, String BaseURI) {
  144.         //System.out.println("@ engineCanResolve DEPRECATED @");
  145.         if (uri == null) {
  146.             return false;
  147.         }
  148.         String uriNodeValue = uri.getNodeValue();
  149.         return uriNodeValue.startsWith("#") || uriNodeValue.startsWith("cid:");
  150.     }
  151.    
  152.     @Override
  153.     public boolean engineCanResolveURI(ResourceResolverContext context) {
  154.         //System.out.println("@ engineCanResolveURI @");
  155.         return this.engineCanResolve(context.attr, context.baseUri);
  156.     }
  157. }