You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@santuario.apache.org by gi...@apache.org on 2015/11/29 18:24:01 UTC
svn commit: r1717097 - in /santuario/xml-security-java/trunk/src:
main/java/org/apache/xml/security/stax/impl/XMLSecurityStreamWriter.java
test/java/org/apache/xml/security/test/stax/XMLSecurityStreamWriterTest.java
Author: giger
Date: Sun Nov 29 17:24:00 2015
New Revision: 1717097
URL: http://svn.apache.org/viewvc?rev=1717097&view=rev
Log:
SANTUARIO-433: catch null prefix and remove some duplicate code.
Modified:
santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/XMLSecurityStreamWriter.java
santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/stax/XMLSecurityStreamWriterTest.java
Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/XMLSecurityStreamWriter.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/XMLSecurityStreamWriter.java?rev=1717097&r1=1717096&r2=1717097&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/XMLSecurityStreamWriter.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/XMLSecurityStreamWriter.java Sun Nov 29 17:24:00 2015
@@ -32,8 +32,10 @@ import javax.xml.namespace.NamespaceCont
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
-
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
/**
* Custom XMLStreamWriter to map XMLStreamWriter method calls into XMLEvent's
@@ -93,69 +95,22 @@ public class XMLSecurityStreamWriter imp
}
}
- @Override
- public void writeStartElement(String localName) throws XMLStreamException {
- outputOpenStartElement();
-
- Element element;
+ private String getNamespacePrefix(String namespaceURI) {
if (elementStack == null) {
- element = new Element(elementStack, namespaceContext,
- XMLConstants.NULL_NS_URI, localName, XMLConstants.DEFAULT_NS_PREFIX);
- if (signEntireRequestPart != null) {
- signEntireRequestPart.setName(new QName("", localName));
- outputProcessorChain.getSecurityContext().putAsMap(
- XMLSecurityConstants.SIGNATURE_PARTS,
- signEntireRequestPart.getName(),
- signEntireRequestPart
- );
- }
- if (encryptEntireRequestPart != null) {
- encryptEntireRequestPart.setName(new QName("", localName));
- outputProcessorChain.getSecurityContext().putAsMap(
- XMLSecurityConstants.ENCRYPTION_PARTS,
- encryptEntireRequestPart.getName(),
- encryptEntireRequestPart
- );
- }
+ return namespaceContext.getPrefix(namespaceURI);
} else {
- element = new Element(elementStack, XMLConstants.NULL_NS_URI, localName, XMLConstants.DEFAULT_NS_PREFIX);
+ return elementStack.getNamespaceContext().getPrefix(namespaceURI);
}
+ }
- elementStack = element;
- openStartElement = element;
+ @Override
+ public void writeStartElement(String localName) throws XMLStreamException {
+ writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, localName, XMLConstants.NULL_NS_URI);
}
@Override
public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException {
- outputOpenStartElement();
-
- Element element;
- if (elementStack == null) {
- element = new Element(elementStack, namespaceContext,
- namespaceURI, localName, namespaceContext.getPrefix(namespaceURI));
- if (signEntireRequestPart != null) {
- signEntireRequestPart.setName(new QName(namespaceURI, localName));
- outputProcessorChain.getSecurityContext().putAsMap(
- XMLSecurityConstants.SIGNATURE_PARTS,
- signEntireRequestPart.getName(),
- signEntireRequestPart
- );
- }
- if (encryptEntireRequestPart != null) {
- encryptEntireRequestPart.setName(new QName(namespaceURI, localName));
- outputProcessorChain.getSecurityContext().putAsMap(
- XMLSecurityConstants.ENCRYPTION_PARTS,
- encryptEntireRequestPart.getName(),
- encryptEntireRequestPart
- );
- }
- } else {
- element = new Element(elementStack,
- namespaceURI, localName, elementStack.getNamespaceContext().getPrefix(namespaceURI));
- }
-
- elementStack = element;
- openStartElement = element;
+ writeStartElement(getNamespacePrefix(namespaceURI), localName, namespaceURI);
}
@Override
@@ -168,17 +123,17 @@ public class XMLSecurityStreamWriter imp
if (signEntireRequestPart != null) {
signEntireRequestPart.setName(new QName(namespaceURI, localName, prefix));
outputProcessorChain.getSecurityContext().putAsMap(
- XMLSecurityConstants.SIGNATURE_PARTS,
- signEntireRequestPart.getName(),
- signEntireRequestPart
+ XMLSecurityConstants.SIGNATURE_PARTS,
+ signEntireRequestPart.getName(),
+ signEntireRequestPart
);
}
if (encryptEntireRequestPart != null) {
encryptEntireRequestPart.setName(new QName(namespaceURI, localName, prefix));
outputProcessorChain.getSecurityContext().putAsMap(
- XMLSecurityConstants.ENCRYPTION_PARTS,
- encryptEntireRequestPart.getName(),
- encryptEntireRequestPart
+ XMLSecurityConstants.ENCRYPTION_PARTS,
+ encryptEntireRequestPart.getName(),
+ encryptEntireRequestPart
);
}
} else {
@@ -190,22 +145,18 @@ public class XMLSecurityStreamWriter imp
}
@Override
- public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException {
- writeStartElement(namespaceURI, localName);
- openStartElement.setEmptyElement(true);
- haveToWriteEndElement = true;
+ public void writeEmptyElement(String localName) throws XMLStreamException {
+ writeEmptyElement(XMLConstants.DEFAULT_NS_PREFIX, localName, XMLConstants.NULL_NS_URI);
}
@Override
- public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
- writeStartElement(prefix, localName, namespaceURI);
- openStartElement.setEmptyElement(true);
- haveToWriteEndElement = true;
+ public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException {
+ writeEmptyElement(getNamespacePrefix(namespaceURI), localName, namespaceURI);
}
@Override
- public void writeEmptyElement(String localName) throws XMLStreamException {
- writeStartElement(localName);
+ public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
+ writeStartElement(prefix, localName, namespaceURI);
openStartElement.setEmptyElement(true);
haveToWriteEndElement = true;
}
@@ -250,33 +201,23 @@ public class XMLSecurityStreamWriter imp
@Override
public void writeAttribute(String localName, String value) throws XMLStreamException {
- if (openStartElement == null) {
- throw new XMLStreamException("No open start element.");
- }
- openStartElement.addAttribute(
- XMLSecEventFactory.createXMLSecAttribute(
- new QName(localName), value));
+ writeAttribute(XMLConstants.DEFAULT_NS_PREFIX, XMLConstants.NULL_NS_URI, localName, value);
}
@Override
- public void writeAttribute(String prefix, String namespaceURI, String localName, String value)
- throws XMLStreamException {
- if (openStartElement == null) {
- throw new XMLStreamException("No open start element.");
- }
- openStartElement.addAttribute(
- XMLSecEventFactory.createXMLSecAttribute(
- new QName(namespaceURI, localName, prefix), value));
+ public void writeAttribute(String namespaceURI, String localName, String value) throws XMLStreamException {
+ writeAttribute(getNamespacePrefix(namespaceURI), namespaceURI, localName, value);
}
@Override
- public void writeAttribute(String namespaceURI, String localName, String value) throws XMLStreamException {
+ public void writeAttribute(String prefix, String namespaceURI, String localName, String value)
+ throws XMLStreamException {
if (openStartElement == null) {
throw new XMLStreamException("No open start element.");
}
openStartElement.addAttribute(
XMLSecEventFactory.createXMLSecAttribute(
- new QName(namespaceURI, localName, getNamespaceContext().getPrefix(namespaceURI)), value));
+ new QName(namespaceURI, localName, prefix), value));
}
@Override
@@ -292,12 +233,13 @@ public class XMLSecurityStreamWriter imp
if (openStartElement == null) {
throw new XMLStreamException("No open start element.");
}
- //workaround for sun's stax parser:
+ //workaround for sun's stax parser
if (this.openStartElement.getElementPrefix().equals(XMLConstants.DEFAULT_NS_PREFIX)) {
this.openStartElement.setElementNamespace(namespaceURI);
this.openStartElement.setElementPrefix(XMLConstants.DEFAULT_NS_PREFIX);
}
- this.openStartElement.addNamespace(XMLSecEventFactory.createXMLSecNamespace(null, namespaceURI));
+ this.openStartElement.addNamespace(
+ XMLSecEventFactory.createXMLSecNamespace(XMLConstants.DEFAULT_NS_PREFIX, namespaceURI));
}
@Override
@@ -308,8 +250,7 @@ public class XMLSecurityStreamWriter imp
@Override
public void writeProcessingInstruction(String target) throws XMLStreamException {
- outputOpenStartElement();
- chainProcessEvent(XMLSecEventFactory.createXMLSecProcessingInstruction(target, XMLConstants.DEFAULT_NS_PREFIX));
+ writeProcessingInstruction(target, XMLConstants.DEFAULT_NS_PREFIX);
}
@Override
@@ -345,12 +286,12 @@ public class XMLSecurityStreamWriter imp
@Override
public void writeStartDocument() throws XMLStreamException {
- chainProcessEvent(XMLSecEventFactory.createXmlSecStartDocument(null, null, null, null));
+ writeStartDocument(null, null);
}
@Override
public void writeStartDocument(String version) throws XMLStreamException {
- chainProcessEvent(XMLSecEventFactory.createXmlSecStartDocument(null, null, null, version));
+ writeStartDocument(null, version);
}
@Override
@@ -372,11 +313,7 @@ public class XMLSecurityStreamWriter imp
@Override
public String getPrefix(String uri) throws XMLStreamException {
- if (elementStack == null) {
- return this.namespaceContext.getPrefix(uri);
- } else {
- return this.elementStack.getNamespaceContext().getPrefix(uri);
- }
+ return getNamespacePrefix(uri);
}
@Override
@@ -457,9 +394,9 @@ public class XMLSecurityStreamWriter imp
String elementNamespace, String elementName, String elementPrefix) {
this.parentElement = parentElement;
this.namespaceContext = namespaceContext;
- this.elementNamespace = elementNamespace;
this.elementName = elementName;
- this.elementPrefix = elementPrefix;
+ setElementNamespace(elementNamespace);
+ setElementPrefix(elementPrefix);
}
private Element getParentElement() {
@@ -479,7 +416,11 @@ public class XMLSecurityStreamWriter imp
}
private void setElementNamespace(String elementNamespace) {
- this.elementNamespace = elementNamespace;
+ if (elementNamespace == null) {
+ this.elementNamespace = XMLConstants.NULL_NS_URI;
+ } else {
+ this.elementNamespace = elementNamespace;
+ }
this.qName = null;
}
@@ -488,7 +429,11 @@ public class XMLSecurityStreamWriter imp
}
private void setElementPrefix(String elementPrefix) {
- this.elementPrefix = elementPrefix;
+ if (elementPrefix == null) {
+ this.elementPrefix = XMLConstants.DEFAULT_NS_PREFIX;
+ } else {
+ this.elementPrefix = elementPrefix;
+ }
this.qName = null;
}
Modified: santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/stax/XMLSecurityStreamWriterTest.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/stax/XMLSecurityStreamWriterTest.java?rev=1717097&r1=1717096&r2=1717097&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/stax/XMLSecurityStreamWriterTest.java (original)
+++ santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/stax/XMLSecurityStreamWriterTest.java Sun Nov 29 17:24:00 2015
@@ -184,6 +184,20 @@ public class XMLSecurityStreamWriterTest
XMLAssert.assertXMLEqual(stdStringWriter.toString(), securityStringWriter.toString());
}
+ // @see https://issues.apache.org/jira/browse/SANTUARIO-433
+ @Test
+ public void testNullPrefix() throws Exception {
+ StringWriter securityStringWriter = new StringWriter();
+ OutboundSecurityContextImpl securityContext = new OutboundSecurityContextImpl();
+ OutputProcessorChainImpl outputProcessorChain = new OutputProcessorChainImpl(securityContext);
+ outputProcessorChain.addProcessor(new EventWriterProcessor(securityStringWriter));
+ XMLSecurityStreamWriter xmlSecurityStreamWriter = new XMLSecurityStreamWriter(outputProcessorChain);
+
+ xmlSecurityStreamWriter.writeStartElement(null, "element", "http://element.ns");
+ xmlSecurityStreamWriter.writeDefaultNamespace("http://element.ns");
+ xmlSecurityStreamWriter.writeStartElement("childElement");
+ }
+
class EventWriterProcessor implements OutputProcessor {
private XMLEventWriter xmlEventWriter;