Search in sources :

Example 1 with XMLSecNamespace

use of org.apache.xml.security.stax.ext.stax.XMLSecNamespace in project santuario-java by apache.

the class AbstractDecryptInputProcessor method writeWrapperStartElement.

private InputStream writeWrapperStartElement(XMLSecStartElement xmlSecStartElement) throws IOException {
    // temporary writer to write the dummy wrapper element with all namespaces in the current scope
    // spec says (4.2): "The cleartext octet sequence obtained in step 3 is interpreted as UTF-8 encoded character data."
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append('<');
    stringBuilder.append(wrapperElementName.getPrefix());
    stringBuilder.append(':');
    stringBuilder.append(wrapperElementName.getLocalPart());
    stringBuilder.append(' ');
    stringBuilder.append("xmlns:");
    stringBuilder.append(wrapperElementName.getPrefix());
    stringBuilder.append("=\"");
    stringBuilder.append(wrapperElementName.getNamespaceURI());
    stringBuilder.append('\"');
    // apply all namespaces from current scope to get a valid documentfragment:
    List<XMLSecNamespace> comparableNamespacesToApply = new ArrayList<>();
    List<XMLSecNamespace> comparableNamespaceList = new ArrayList<>();
    xmlSecStartElement.getNamespacesFromCurrentScope(comparableNamespaceList);
    // reverse iteration -> From current element namespaces to parent namespaces
    for (int i = comparableNamespaceList.size() - 1; i >= 0; i--) {
        XMLSecNamespace comparableNamespace = comparableNamespaceList.get(i);
        if (!comparableNamespacesToApply.contains(comparableNamespace)) {
            comparableNamespacesToApply.add(comparableNamespace);
            stringBuilder.append(' ');
            String prefix = comparableNamespace.getPrefix();
            String uri = comparableNamespace.getNamespaceURI();
            if (prefix == null || prefix.isEmpty()) {
                stringBuilder.append("xmlns=\"");
                stringBuilder.append(uri);
                stringBuilder.append("\"");
            } else {
                stringBuilder.append("xmlns:");
                stringBuilder.append(prefix);
                stringBuilder.append("=\"");
                stringBuilder.append(uri);
                stringBuilder.append("\"");
            }
        }
    }
    stringBuilder.append('>');
    return new UnsyncByteArrayInputStream(stringBuilder.toString().getBytes(StandardCharsets.UTF_8));
}
Also used : XMLSecNamespace(org.apache.xml.security.stax.ext.stax.XMLSecNamespace) UnsyncByteArrayInputStream(org.apache.xml.security.utils.UnsyncByteArrayInputStream)

Example 2 with XMLSecNamespace

use of org.apache.xml.security.stax.ext.stax.XMLSecNamespace in project santuario-java by apache.

the class XMLSecNamespaceImpl method getInstance.

public static XMLSecNamespace getInstance(String prefix, String uri) {
    String prefixToUse = prefix;
    if (prefixToUse == null) {
        prefixToUse = "";
    }
    // sun's stax parser returns null for the default namespace
    String uriToUse = uri;
    if (uriToUse == null) {
        uriToUse = "";
    }
    Map<String, XMLSecNamespace> nsMap = xmlSecNamespaceMap.get(prefixToUse);
    if (nsMap != null) {
        XMLSecNamespace xmlSecNamespace = nsMap.get(uriToUse);
        if (xmlSecNamespace != null) {
            return xmlSecNamespace;
        } else {
            xmlSecNamespace = new XMLSecNamespaceImpl(prefixToUse, uriToUse);
            nsMap.put(uriToUse, xmlSecNamespace);
            return xmlSecNamespace;
        }
    } else {
        nsMap = new WeakHashMap<String, XMLSecNamespace>();
        XMLSecNamespace xmlSecNamespace = new XMLSecNamespaceImpl(prefixToUse, uriToUse);
        nsMap.put(uriToUse, xmlSecNamespace);
        xmlSecNamespaceMap.put(prefixToUse, nsMap);
        return xmlSecNamespace;
    }
}
Also used : XMLSecNamespace(org.apache.xml.security.stax.ext.stax.XMLSecNamespace)

Example 3 with XMLSecNamespace

use of org.apache.xml.security.stax.ext.stax.XMLSecNamespace in project santuario-java by apache.

the class AbstractOutputProcessor method createStartElementAndOutputAsEvent.

public XMLSecStartElement createStartElementAndOutputAsEvent(OutputProcessorChain outputProcessorChain, QName element, boolean outputLocalNs, List<XMLSecAttribute> attributes) throws XMLStreamException, XMLSecurityException {
    List<XMLSecNamespace> comparableNamespaces = Collections.emptyList();
    if (outputLocalNs) {
        comparableNamespaces = new ArrayList<>(2);
        comparableNamespaces.add(XMLSecEventFactory.createXMLSecNamespace(element.getPrefix(), element.getNamespaceURI()));
    }
    if (attributes != null) {
        for (int i = 0; i < attributes.size(); i++) {
            XMLSecAttribute xmlSecAttribute = attributes.get(i);
            QName attributeName = xmlSecAttribute.getName();
            String attributeNamePrefix = attributeName.getPrefix();
            if (attributeNamePrefix != null && attributeNamePrefix.isEmpty()) {
                continue;
            }
            if (!comparableNamespaces.contains(xmlSecAttribute.getAttributeNamespace())) {
                if (comparableNamespaces == Collections.<XMLSecNamespace>emptyList()) {
                    comparableNamespaces = new ArrayList<>(1);
                }
                comparableNamespaces.add(xmlSecAttribute.getAttributeNamespace());
            }
        }
    }
    XMLSecStartElement xmlSecStartElement = XMLSecEventFactory.createXmlSecStartElement(element, attributes, comparableNamespaces);
    outputAsEvent(outputProcessorChain, xmlSecStartElement);
    return xmlSecStartElement;
}
Also used : XMLSecStartElement(org.apache.xml.security.stax.ext.stax.XMLSecStartElement) XMLSecNamespace(org.apache.xml.security.stax.ext.stax.XMLSecNamespace) QName(javax.xml.namespace.QName) XMLSecAttribute(org.apache.xml.security.stax.ext.stax.XMLSecAttribute)

Example 4 with XMLSecNamespace

use of org.apache.xml.security.stax.ext.stax.XMLSecNamespace in project santuario-java by apache.

the class AbstractOutputProcessor method addAttributes.

public XMLSecStartElement addAttributes(XMLSecStartElement xmlSecStartElement, List<XMLSecAttribute> attributeList) throws XMLStreamException {
    List<XMLSecNamespace> declaredNamespaces = xmlSecStartElement.getOnElementDeclaredNamespaces();
    for (int i = 0; i < attributeList.size(); i++) {
        XMLSecAttribute xmlSecAttribute = attributeList.get(i);
        xmlSecStartElement.addAttribute(xmlSecAttribute);
        final QName attributeName = xmlSecAttribute.getName();
        if (attributeName.getNamespaceURI() != null && !"".equals(attributeName.getNamespaceURI()) && !declaredNamespaces.contains(xmlSecAttribute.getAttributeNamespace())) {
            xmlSecStartElement.addNamespace(xmlSecAttribute.getAttributeNamespace());
        }
    }
    return xmlSecStartElement;
}
Also used : XMLSecNamespace(org.apache.xml.security.stax.ext.stax.XMLSecNamespace) QName(javax.xml.namespace.QName) XMLSecAttribute(org.apache.xml.security.stax.ext.stax.XMLSecAttribute)

Example 5 with XMLSecNamespace

use of org.apache.xml.security.stax.ext.stax.XMLSecNamespace in project santuario-java by apache.

the class AbstractOutputProcessor method outputDOMElement.

protected void outputDOMElement(Element element, OutputProcessorChain outputProcessorChain) throws XMLStreamException, XMLSecurityException {
    NamedNodeMap namedNodeMap = element.getAttributes();
    List<XMLSecAttribute> attributes = new ArrayList<>(namedNodeMap.getLength());
    List<XMLSecNamespace> namespaces = new ArrayList<>(namedNodeMap.getLength());
    for (int i = 0; i < namedNodeMap.getLength(); i++) {
        Attr attribute = (Attr) namedNodeMap.item(i);
        if (attribute.getPrefix() == null) {
            attributes.add(createAttribute(new QName(attribute.getNamespaceURI(), attribute.getLocalName()), attribute.getValue()));
        } else if ("xmlns".equals(attribute.getPrefix()) || "xmlns".equals(attribute.getLocalName())) {
            namespaces.add(createNamespace(attribute.getLocalName(), attribute.getValue()));
        } else {
            attributes.add(createAttribute(new QName(attribute.getNamespaceURI(), attribute.getLocalName(), attribute.getPrefix()), attribute.getValue()));
        }
    }
    QName elementName = new QName(element.getNamespaceURI(), element.getLocalName(), element.getPrefix());
    createStartElementAndOutputAsEvent(outputProcessorChain, elementName, namespaces, attributes);
    Node childNode = element.getFirstChild();
    while (childNode != null) {
        switch(childNode.getNodeType()) {
            case Node.ELEMENT_NODE:
                outputDOMElement((Element) childNode, outputProcessorChain);
                break;
            case Node.TEXT_NODE:
                createCharactersAndOutputAsEvent(outputProcessorChain, ((Text) childNode).getData());
                break;
        }
        childNode = childNode.getNextSibling();
    }
    createEndElementAndOutputAsEvent(outputProcessorChain, elementName);
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) XMLSecNamespace(org.apache.xml.security.stax.ext.stax.XMLSecNamespace) QName(javax.xml.namespace.QName) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) XMLSecAttribute(org.apache.xml.security.stax.ext.stax.XMLSecAttribute) Attr(org.w3c.dom.Attr)

Aggregations

XMLSecNamespace (org.apache.xml.security.stax.ext.stax.XMLSecNamespace)8 XMLSecAttribute (org.apache.xml.security.stax.ext.stax.XMLSecAttribute)4 QName (javax.xml.namespace.QName)3 Namespace (javax.xml.stream.events.Namespace)2 XMLSecStartElement (org.apache.xml.security.stax.ext.stax.XMLSecStartElement)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 NamespaceContext (javax.xml.namespace.NamespaceContext)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 Attribute (javax.xml.stream.events.Attribute)1 UnsyncByteArrayInputStream (org.apache.xml.security.utils.UnsyncByteArrayInputStream)1 Attr (org.w3c.dom.Attr)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1 Node (org.w3c.dom.Node)1