Author: anil.saldhana(a)jboss.com
Date: 2011-07-22 14:46:26 -0400 (Fri, 22 Jul 2011)
New Revision: 1110
Modified:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/saml/v2/util/DocumentUtil.java
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/util/StaxUtil.java
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/util/TransformerUtil.java
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wstrust/plugins/saml/SAML20TokenProvider.java
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wstrust/plugins/saml/SAMLUtil.java
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/saml/SAMLResponseParserTestCase.java
Log:
PLFED-212: TransformerUtil/StaxUtil changes with xmlns
Modified:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/saml/v2/util/DocumentUtil.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/saml/v2/util/DocumentUtil.java 2011-07-22
17:17:21 UTC (rev 1109)
+++
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/saml/v2/util/DocumentUtil.java 2011-07-22
18:46:26 UTC (rev 1110)
@@ -50,6 +50,7 @@
import org.picketlink.identity.federation.core.exceptions.ProcessingException;
import org.picketlink.identity.federation.core.util.TransformerUtil;
import org.w3c.dom.DOMConfiguration;
+import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
@@ -109,6 +110,31 @@
}
/**
+ * Create a document with the root element of the form <someElement
xmlns="customNamespace"
+ * @param baseNamespace
+ * @return
+ * @throws ProcessingException
+ */
+ public static Document createDocumentWithBaseNamespace(String baseNamespace, String
localPart)
+ throws ProcessingException
+ {
+ try
+ {
+ DocumentBuilderFactory factory = getDocumentBuilderFactory();
+ DocumentBuilder builder = factory.newDocumentBuilder();
+ return builder.getDOMImplementation().createDocument(baseNamespace, localPart,
null);
+ }
+ catch (DOMException e)
+ {
+ throw new ProcessingException(e);
+ }
+ catch (ParserConfigurationException e)
+ {
+ throw new ProcessingException(e);
+ }
+ }
+
+ /**
* Parse a document from the string
* @param docString
* @return
Modified:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/util/StaxUtil.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/util/StaxUtil.java 2011-07-22
17:17:21 UTC (rev 1109)
+++
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/util/StaxUtil.java 2011-07-22
18:46:26 UTC (rev 1110)
@@ -373,6 +373,10 @@
// writeNameSpace(writer, domElementPrefix, domElementNS );
registeredNSStack.get().push(domElementNS);
}
+ else if (domElementPrefix == "" && domElementNS != null)
+ {
+ writeNameSpace(writer, "xmlns", domElementNS);
+ }
// Deal with Attributes
NamedNodeMap attrs = domElement.getAttributes();
@@ -385,12 +389,8 @@
if (attributePrefix == null || attributePrefix.length() == 0)
{
- if ("xmlns".equals(attribLocalName))
+ if (!("xmlns".equals(attribLocalName)))
{
- writeDefaultNameSpace(writer, attribValue);
- }
- else
- {
writeAttribute(writer, attribLocalName, attribValue);
}
}
Modified:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/util/TransformerUtil.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/util/TransformerUtil.java 2011-07-22
17:17:21 UTC (rev 1109)
+++
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/util/TransformerUtil.java 2011-07-22
18:46:26 UTC (rev 1110)
@@ -54,7 +54,9 @@
import org.apache.log4j.Logger;
import org.picketlink.identity.federation.core.exceptions.ConfigurationException;
import org.picketlink.identity.federation.core.exceptions.ParsingException;
+import org.picketlink.identity.federation.core.exceptions.ProcessingException;
import org.picketlink.identity.federation.core.parsers.util.StaxParserUtil;
+import org.picketlink.identity.federation.core.saml.v2.util.DocumentUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
@@ -226,7 +228,7 @@
}
}
}
- catch (ParsingException e)
+ catch (Exception e)
{
throw new TransformerException(e);
}
@@ -293,7 +295,7 @@
}
private Element handleStartElement(XMLEventReader xmlEventReader, StartElement
startElement, CustomHolder holder)
- throws ParsingException
+ throws ParsingException, ProcessingException
{
Document doc = holder.doc;
@@ -303,8 +305,15 @@
String localPart = elementName.getLocalPart();
String qual = prefix != null && prefix != "" ? prefix +
":" + localPart : localPart;
+
Element el = doc.createElementNS(ns, qual);
+ String containsBaseNamespace = containsBaseNamespace(startElement);
+ if (StringUtil.isNotNull(containsBaseNamespace))
+ {
+ el = DocumentUtil.createDocumentWithBaseNamespace(containsBaseNamespace,
localPart).getDocumentElement();
+ el = (Element) doc.importNode(el, true);
+ }
if (StringUtil.isNotNull(prefix))
{
el.setPrefix(prefix);
@@ -342,9 +351,12 @@
if (prefix != null && prefix != "")
qual = (localPart != null && localPart != "") ? prefix +
":" + localPart : prefix;
+ if (qual.equals("xmlns"))
+ continue;
if (trace)
{
- log.trace("Set Attribute Namespace=" + name.getNamespaceURI() +
":" + qual);
+ log.trace("Set Attribute Namespace=" + name.getNamespaceURI() +
"::Qual=:" + qual + "::Value="
+ + namespace.getNamespaceURI());
}
el.setAttributeNS(name.getNamespaceURI(), qual,
namespace.getNamespaceURI());
}
@@ -383,6 +395,27 @@
return el;
}
+ @SuppressWarnings("unchecked")
+ private String containsBaseNamespace(StartElement startElement)
+ {
+ String localPart, prefix, qual = null;
+
+ Iterator<Namespace> namespaces = startElement.getNamespaces();
+ while (namespaces != null && namespaces.hasNext())
+ {
+ Namespace namespace = namespaces.next();
+ QName name = namespace.getName();
+ localPart = name.getLocalPart();
+ prefix = name.getPrefix();
+ if (prefix != null && prefix != "")
+ qual = (localPart != null && localPart != "") ? prefix +
":" + localPart : prefix;
+
+ if (qual.equals("xmlns"))
+ return namespace.getNamespaceURI();
+ }
+ return null;
+ }
+
private class CustomHolder
{
public Document doc;
Modified:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wstrust/plugins/saml/SAML20TokenProvider.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wstrust/plugins/saml/SAML20TokenProvider.java 2011-07-22
17:17:21 UTC (rev 1109)
+++
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wstrust/plugins/saml/SAML20TokenProvider.java 2011-07-22
18:46:26 UTC (rev 1110)
@@ -33,6 +33,7 @@
import org.picketlink.identity.federation.core.saml.v2.constants.JBossSAMLConstants;
import org.picketlink.identity.federation.core.saml.v2.factories.SAMLAssertionFactory;
import org.picketlink.identity.federation.core.saml.v2.util.AssertionUtil;
+import org.picketlink.identity.federation.core.saml.v2.util.DocumentUtil;
import org.picketlink.identity.federation.core.saml.v2.util.StatementUtil;
import org.picketlink.identity.federation.core.sts.AbstractSecurityTokenProvider;
import org.picketlink.identity.federation.core.wstrust.SecurityToken;
@@ -67,6 +68,8 @@
{
protected static Logger logger = Logger.getLogger(SAML20TokenProvider.class);
+ protected boolean trace = logger.isTraceEnabled();
+
private SAML20TokenAttributeProvider attributeProvider;
/*
@@ -340,6 +343,10 @@
{
try
{
+ if (trace)
+ {
+ logger.trace("Assertion Element=" +
DocumentUtil.getNodeAsString(assertionElement));
+ }
assertion = SAMLUtil.fromElement(assertionElement);
}
catch (Exception e)
Modified:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wstrust/plugins/saml/SAMLUtil.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wstrust/plugins/saml/SAMLUtil.java 2011-07-22
17:17:21 UTC (rev 1109)
+++
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wstrust/plugins/saml/SAMLUtil.java 2011-07-22
18:46:26 UTC (rev 1110)
@@ -25,6 +25,7 @@
import java.io.ByteArrayOutputStream;
import java.security.GeneralSecurityException;
+import org.apache.log4j.Logger;
import org.picketlink.identity.federation.core.exceptions.ConfigurationException;
import org.picketlink.identity.federation.core.exceptions.ParsingException;
import org.picketlink.identity.federation.core.exceptions.ProcessingException;
@@ -48,7 +49,10 @@
*/
public class SAMLUtil
{
+ protected static Logger log = Logger.getLogger(SAMLUtil.class);
+ protected static boolean trace = log.isTraceEnabled();
+
public static final String SAML11_BEARER_URI =
"urn:oasis:names:tc:SAML:1.0:cm:bearer";
public static final String SAML11_HOLDER_OF_KEY_URI =
"urn:oasis:names:tc:SAML:1.0:cm:holder-of-key";
@@ -89,6 +93,11 @@
ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
Document document = DocumentUtil.getDocument(bis);
+ if (trace)
+ {
+ log.trace("Written Assertion=" + DocumentUtil.asString(document));
+ }
+
return document.getDocumentElement();
}
Modified:
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/saml/SAMLResponseParserTestCase.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/saml/SAMLResponseParserTestCase.java 2011-07-22
17:17:21 UTC (rev 1109)
+++
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/saml/SAMLResponseParserTestCase.java 2011-07-22
18:46:26 UTC (rev 1110)
@@ -292,5 +292,13 @@
else
throw new RuntimeException("Unknown statement type:" + statement);
}
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ //Lets do the writing
+ SAMLResponseWriter writer = new
SAMLResponseWriter(StaxUtil.getXMLStreamWriter(baos));
+ writer.write(response);
+ String writtenString = new String(baos.toByteArray());
+ System.out.println(writtenString);
+ validateSchema(writtenString);
}
}
\ No newline at end of file