示例#1
0
 /**
  * Returns the prefix associated with the specified namespace URI
  *
  * @param context contains the namespace map
  * @param nsURI the namespace URI
  * @return the prefix associated with the specified namespace URI, or null if not set
  */
 public static String getNSPrefix(XMLCryptoContext context, String nsURI) {
   if (context != null) {
     return context.getNamespacePrefix(nsURI, context.getDefaultNamespacePrefix());
   } else {
     return null;
   }
 }
  private Data dereference(XMLCryptoContext context) throws XMLSignatureException {
    Data data = null;

    // use user-specified URIDereferencer if specified; otherwise use deflt
    URIDereferencer deref = context.getURIDereferencer();
    if (deref == null) {
      deref = DOMURIDereferencer.INSTANCE;
    }
    try {
      data = deref.dereference(this, context);
      if (log.isLoggable(Level.FINE)) {
        log.log(Level.FINE, "URIDereferencer class name: " + deref.getClass().getName());
        log.log(Level.FINE, "Data class name: " + data.getClass().getName());
      }
    } catch (URIReferenceException ure) {
      throw new XMLSignatureException(ure);
    }

    return data;
  }
  private byte[] transform(Data dereferencedData, XMLCryptoContext context)
      throws XMLSignatureException {

    if (md == null) {
      try {
        md =
            MessageDigest.getInstance(((DOMDigestMethod) digestMethod).getMessageDigestAlgorithm());
      } catch (NoSuchAlgorithmException nsae) {
        throw new XMLSignatureException(nsae);
      }
    }
    md.reset();
    DigesterOutputStream dos;
    Boolean cache = (Boolean) context.getProperty("javax.xml.crypto.dsig.cacheReference");
    if (cache != null && cache.booleanValue() == true) {
      this.derefData = copyDerefData(dereferencedData);
      dos = new DigesterOutputStream(md, true);
    } else {
      dos = new DigesterOutputStream(md);
    }
    OutputStream os = new UnsyncBufferedOutputStream(dos);
    Data data = dereferencedData;
    for (int i = 0, size = transforms.size(); i < size; i++) {
      DOMTransform transform = (DOMTransform) transforms.get(i);
      try {
        if (i < size - 1) {
          data = transform.transform(data, context);
        } else {
          data = transform.transform(data, context, os);
        }
      } catch (TransformException te) {
        throw new XMLSignatureException(te);
      }
    }

    try {
      if (data != null) {
        XMLSignatureInput xi;
        if (data instanceof ApacheData) {
          xi = ((ApacheData) data).getXMLSignatureInput();
        } else if (data instanceof OctetStreamData) {
          xi = new XMLSignatureInput(((OctetStreamData) data).getOctetStream());
        } else if (data instanceof NodeSetData) {
          TransformService spi =
              TransformService.getInstance(CanonicalizationMethod.INCLUSIVE, "DOM");
          data = spi.transform(data, context);
          xi = new XMLSignatureInput(((OctetStreamData) data).getOctetStream());
        } else {
          throw new XMLSignatureException("unrecognized Data type");
        }
        xi.updateOutputStream(os);
      }
      os.flush();
      if (cache != null && cache.booleanValue() == true) {
        this.dis = dos.getInputStream();
      }
      return dos.getDigestValue();
    } catch (Exception e) {
      throw new XMLSignatureException(e);
    }
  }