本文整理匯總了Java中org.apache.xml.security.transforms.params.XPathContainer.setXPathNamespaceContext方法的典型用法代碼示例。如果您正苦於以下問題:Java XPathContainer.setXPathNamespaceContext方法的具體用法?Java XPathContainer.setXPathNamespaceContext怎麽用?Java XPathContainer.setXPathNamespaceContext使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.xml.security.transforms.params.XPathContainer
的用法示例。
在下文中一共展示了XPathContainer.setXPathNamespaceContext方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: marshalParameters
import org.apache.xml.security.transforms.params.XPathContainer; //導入方法依賴的package包/類
@Override
public List<Node> marshalParameters(XPathTransform alg, Document doc)
{
XPathContainer xpathContainer = new XPathContainer(doc);
xpathContainer.setXPath(alg.getXPath());
for(Map.Entry<String, String> ns : alg.getNamespaces().entrySet())
{
try
{
xpathContainer.setXPathNamespaceContext(ns.getKey(), ns.getValue());
}catch (XMLSecurityException ex)
{
throw new IllegalArgumentException("Invalid namespaces for XPath query", ex);
}
}
return Collections.singletonList((Node)xpathContainer.getElement());
}
示例2: checkXmlSignatureSoftwareStack
import org.apache.xml.security.transforms.params.XPathContainer; //導入方法依賴的package包/類
private void checkXmlSignatureSoftwareStack(boolean cert) throws Exception {
Init.init();
DocumentBuilder documentBuilder = XMLUtils.createDocumentBuilder(false);
Document testDocument = documentBuilder.newDocument();
Element rootElement =
testDocument.createElementNS("urn:namespace", "tns:document");
rootElement.setAttributeNS
(Constants.NamespaceSpecNS, "xmlns:tns", "urn:namespace");
testDocument.appendChild(rootElement);
Element childElement =
testDocument.createElementNS("urn:childnamespace", "t:child");
childElement.setAttributeNS
(Constants.NamespaceSpecNS, "xmlns:t", "urn:childnamespace");
childElement.appendChild(testDocument.createTextNode("hello world"));
rootElement.appendChild(childElement);
PrivateKey privateKey = null;
PublicKey publicKey = null;
X509Certificate signingCert = null;
if (cert) {
// get key & self-signed certificate from keystore
String fs = System.getProperty("file.separator");
FileInputStream fis =
new FileInputStream(BASEDIR + fs + "src/test/resources" + fs + "test.jks");
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(fis, "changeit".toCharArray());
signingCert = (X509Certificate) ks.getCertificate("mullan");
publicKey = signingCert.getPublicKey();
privateKey = (PrivateKey) ks.getKey("mullan", "changeit".toCharArray());
} else {
KeyPair keyPair = KeyPairGenerator.getInstance("DSA").generateKeyPair();
publicKey = keyPair.getPublic();
privateKey = keyPair.getPrivate();
}
XMLSignature signature =
new XMLSignature(
testDocument, "", XMLSignature.ALGO_ID_SIGNATURE_DSA,
Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS
);
Element signatureElement = signature.getElement();
rootElement.appendChild(signatureElement);
Transforms transforms = new Transforms(testDocument);
XPathContainer xpath = new XPathContainer(testDocument);
xpath.setXPathNamespaceContext("ds", Constants.SignatureSpecNS);
xpath.setXPath("not(ancestor-or-self::ds:Signature)");
transforms.addTransform(Transforms.TRANSFORM_XPATH, xpath.getElementPlusReturns());
transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
signature.addDocument("", transforms, MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA1);
if (cert) {
signature.addKeyInfo(signingCert);
} else {
signature.addKeyInfo(publicKey);
}
Element nsElement = testDocument.createElementNS(null, "nsElement");
nsElement.setAttributeNS(
Constants.NamespaceSpecNS, "xmlns:ds", Constants.SignatureSpecNS
);
signature.sign(privateKey);
XPathFactory xpf = XPathFactory.newInstance();
XPath xPath = xpf.newXPath();
xPath.setNamespaceContext(new DSNamespaceContext());
String expression = "//ds:Signature[1]";
Element sigElement =
(Element) xPath.evaluate(expression, testDocument, XPathConstants.NODE);
XMLSignature signatureToVerify = new XMLSignature(sigElement, "");
boolean signResult = signatureToVerify.checkSignatureValue(publicKey);
assertTrue(signResult);
}