use of org.apache.cxf.helpers.MapNamespaceContext in project cxf by apache.
the class AbstractBindingBuilder method getElements.
/**
* Identifies the portions of the message to be signed/encrypted.
*
* @param encryptionModifier
* indicates the scope of the crypto operation over matched
* elements. Either "Content" or "Element".
* @param xpaths
* any XPath expressions to sign/encrypt matches
* @param found
* a list of elements that have previously been tagged for
* signing/encryption. Populated with additional matches found by
* this method and used to prevent including the same element
* twice under the same operation.
* @param forceId
* force adding a wsu:Id onto the elements. Recommended for signatures.
* @return a configured list of {@code WSEncryptionPart}s suitable for
* processing by WSS4J
* @throws SOAPException
* if there is an error extracting SOAP content from the SAAJ
* model
*/
protected List<WSEncryptionPart> getElements(String encryptionModifier, List<org.apache.wss4j.policy.model.XPath> xpaths, List<Element> found, boolean forceId) throws SOAPException {
List<WSEncryptionPart> result = new ArrayList<>();
if (xpaths != null && !xpaths.isEmpty()) {
boolean useSTRTransform = MessageUtils.getContextualBoolean(message, SecurityConstants.USE_STR_TRANSFORM, true);
XPathFactory factory = XPathFactory.newInstance();
for (org.apache.wss4j.policy.model.XPath xPath : xpaths) {
XPath xpath = factory.newXPath();
if (xPath.getPrefixNamespaceMap() != null) {
xpath.setNamespaceContext(new MapNamespaceContext(xPath.getPrefixNamespaceMap()));
}
NodeList list = null;
try {
Element envelope = saaj.getSOAPPart().getEnvelope();
envelope = (Element) DOMUtils.getDomElement(envelope);
list = (NodeList) xpath.evaluate(xPath.getXPath(), envelope, XPathConstants.NODESET);
} catch (XPathExpressionException e) {
LOG.log(Level.WARNING, "Failure in evaluating an XPath expression", e);
}
if (list != null) {
for (int x = 0; x < list.getLength(); x++) {
Element el = (Element) list.item(x);
if (!found.contains(el)) {
found.add(el);
WSEncryptionPart part = null;
boolean saml1 = WSS4JConstants.SAML_NS.equals(el.getNamespaceURI()) && "Assertion".equals(el.getLocalName());
boolean saml2 = WSS4JConstants.SAML2_NS.equals(el.getNamespaceURI()) && "Assertion".equals(el.getLocalName());
if (useSTRTransform && (saml1 || saml2)) {
String id = saml2 ? el.getAttributeNS(null, "ID") : el.getAttributeNS(null, "AssertionID");
SecurityTokenReference secRef = createSTRForSamlAssertion(el.getOwnerDocument(), id, saml1, false);
Element clone = cloneElement(secRef.getElement());
addSupportingElement(clone);
part = new WSEncryptionPart("STRTransform", null, "Element");
part.setId(secRef.getID());
part.setElement(clone);
} else {
String id = setIdOnElement(el, forceId);
part = new WSEncryptionPart(id, encryptionModifier);
part.setElement(el);
}
part.setXpath(xPath.getXPath());
result.add(part);
}
}
}
}
}
return result;
}
use of org.apache.cxf.helpers.MapNamespaceContext in project cxf by apache.
the class AbstractSupportingTokenPolicyValidator method validateSignedEncryptedElements.
/**
* Validate SignedElements or EncryptedElements policies
*/
private boolean validateSignedEncryptedElements(RequiredElements elements, boolean content, List<WSSecurityEngineResult> protResults, List<WSSecurityEngineResult> tokenResults, Message message) {
if (elements == null) {
return true;
}
List<org.apache.wss4j.policy.model.XPath> xpaths = elements.getXPaths();
if (xpaths != null && !xpaths.isEmpty()) {
SOAPMessage soapMessage = message.getContent(SOAPMessage.class);
Element soapEnvelope = soapMessage.getSOAPPart().getDocumentElement();
// XPathFactory and XPath are not thread-safe so we must recreate them
// each request.
final XPathFactory factory = XPathFactory.newInstance();
final XPath xpath = factory.newXPath();
MapNamespaceContext namespaceContext = new MapNamespaceContext();
for (org.apache.wss4j.policy.model.XPath xPath : xpaths) {
Map<String, String> namespaceMap = xPath.getPrefixNamespaceMap();
if (namespaceMap != null) {
namespaceContext.addNamespaces(namespaceMap);
}
}
xpath.setNamespaceContext(namespaceContext);
for (org.apache.wss4j.policy.model.XPath xPath : xpaths) {
if (!checkXPathResult(soapEnvelope, xpath, xPath.getXPath(), protResults, tokenResults)) {
return false;
}
}
}
return true;
}
use of org.apache.cxf.helpers.MapNamespaceContext in project cxf by apache.
the class AbstractAegisTest method writeObjectToElement.
protected Element writeObjectToElement(AegisType type, Object bean, Context context) {
Element element = createElement("urn:Bean", "root", "b");
ElementWriter writer = getElementWriter(element, new MapNamespaceContext());
type.writeObject(bean, writer, getContext());
writer.close();
return element;
}
Aggregations