Author: anil.saldhana(a)jboss.com
Date: 2010-11-04 12:48:13 -0400 (Thu, 04 Nov 2010)
New Revision: 525
Modified:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/saml/v2/writers/SAMLAssertionWriter.java
Log:
write subject
Modified:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/saml/v2/writers/SAMLAssertionWriter.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/saml/v2/writers/SAMLAssertionWriter.java 2010-11-04
16:47:13 UTC (rev 524)
+++
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/saml/v2/writers/SAMLAssertionWriter.java 2010-11-04
16:48:13 UTC (rev 525)
@@ -32,12 +32,20 @@
import org.picketlink.identity.federation.core.exceptions.ProcessingException;
import org.picketlink.identity.federation.core.saml.v2.constants.JBossSAMLConstants;
+import org.picketlink.identity.federation.core.saml.v2.constants.JBossSAMLURIConstants;
import org.picketlink.identity.federation.core.util.StaxUtil;
+import org.picketlink.identity.federation.core.util.StringUtil;
import org.picketlink.identity.federation.saml.v2.assertion.AssertionType;
+import org.picketlink.identity.federation.saml.v2.assertion.AttributeStatementType;
+import org.picketlink.identity.federation.saml.v2.assertion.AttributeType;
import org.picketlink.identity.federation.saml.v2.assertion.AuthnContextType;
import org.picketlink.identity.federation.saml.v2.assertion.AuthnStatementType;
+import org.picketlink.identity.federation.saml.v2.assertion.BaseIDAbstractType;
import org.picketlink.identity.federation.saml.v2.assertion.NameIDType;
import org.picketlink.identity.federation.saml.v2.assertion.StatementAbstractType;
+import org.picketlink.identity.federation.saml.v2.assertion.SubjectConfirmationDataType;
+import org.picketlink.identity.federation.saml.v2.assertion.SubjectConfirmationType;
+import org.picketlink.identity.federation.saml.v2.assertion.SubjectType;
/**
* Write the SAML Assertion to stream
@@ -68,6 +76,14 @@
NameIDType issuer = assertion.getIssuer();
write( issuer, new QName( ASSERTION_NSURI.get(), JBossSAMLConstants.ISSUER.get() ),
out );
+ SubjectType subject = assertion.getSubject();
+ if( subject != null )
+ {
+ write(subject, out);
+ }
+
+ //TODO: conditions and advice
+
List<StatementAbstractType> statements =
assertion.getStatementOrAuthnStatementOrAuthzDecisionStatement();
if( statements != null )
{
@@ -77,10 +93,16 @@
{
write( ( AuthnStatementType )statement, out );
}
- else write( statement, out );
+ else if( statement instanceof AttributeStatementType )
+ {
+ write( ( AttributeStatementType )statement, out );
+ }
+ else
+ throw new RuntimeException( "unknown statement type=" +
statement.getClass().getName() );
}
}
- StaxUtil.writeEndElement( writer);
+
+ StaxUtil.writeEndElement( writer);
StaxUtil.flush( writer );
}
@@ -96,6 +118,29 @@
//TODO: handle this section
}
+ public void write( AttributeStatementType statement, OutputStream out ) throws
ProcessingException
+ {
+ verifyWriter( out );
+ StaxUtil.writeStartElement( writer, ASSERTION_PREFIX,
JBossSAMLConstants.ATTRIBUTE_STATEMENT.get() , ASSERTION_NSURI.get() );
+
+ List<Object> attributes = statement.getAttributeOrEncryptedAttribute();
+ if( attributes != null )
+ {
+ for( Object attr : attributes )
+ {
+ if( attr instanceof AttributeType )
+ {
+ AttributeType attributeType = (AttributeType) attr;
+ write( attributeType, out );
+ }
+ }
+ }
+
+ StaxUtil.writeEndElement( writer);
+ StaxUtil.flush( writer );
+ }
+
+
/**
* Write an {@code AuthnStatementType} to stream
* @param authnStatement
@@ -150,5 +195,158 @@
throw new RuntimeException( "Unsupported :" + elName );
}
}
+
+ StaxUtil.writeEndElement( writer);
+ StaxUtil.flush( writer );
}
+
+ /**
+ * Write an {@code AttributeType} to stream
+ * @param attributeType
+ * @param out
+ * @throws ProcessingException
+ */
+ public void write( AttributeType attributeType, OutputStream out ) throws
ProcessingException
+ {
+ verifyWriter( out );
+ StaxUtil.writeStartElement( writer, ASSERTION_PREFIX,
JBossSAMLConstants.ATTRIBUTE.get() , ASSERTION_NSURI.get() );
+
+ StaxUtil.writeAttribute( writer, JBossSAMLConstants.NAME.get(),
attributeType.getName() );
+
+ String friendlyName = attributeType.getFriendlyName();
+ if( StringUtil.isNotNull( friendlyName ))
+ {
+ StaxUtil.writeAttribute( writer, JBossSAMLConstants.FRIENDLY_NAME.get(),
friendlyName );
+ }
+
+ String nameFormat = attributeType.getNameFormat();
+ if( StringUtil.isNotNull( nameFormat ))
+ {
+ StaxUtil.writeAttribute( writer, JBossSAMLConstants.NAME_FORMAT.get(),
friendlyName );
+ }
+
+ List<Object> attributeValues = attributeType.getAttributeValue();
+ if( attributeValues != null )
+ {
+ for( Object attributeValue : attributeValues )
+ {
+ if( attributeValue instanceof String )
+ {
+ StaxUtil.writeStartElement( writer, ASSERTION_PREFIX,
JBossSAMLConstants.ATTRIBUTE_VALUE.get() , ASSERTION_NSURI.get() );
+
+ StaxUtil.writeNameSpace( writer, "xsi",
JBossSAMLURIConstants.XSI_NSURI.get() );
+ StaxUtil.writeNameSpace( writer, "xs",
JBossSAMLURIConstants.XMLSCHEMA_NSURI.get() );
+ StaxUtil.writeAttribute( writer, JBossSAMLURIConstants.XSI_NSURI.get(),
"type", "xs:string");
+ StaxUtil.writeCharacters(writer, (String) attributeValue );
+
+ StaxUtil.writeEndElement( writer);
+ }
+ else
+ throw new RuntimeException( "Unsupported attribute value:" +
attributeValue.getClass().getName() );
+ }
+ }
+ StaxUtil.writeEndElement( writer);
+ StaxUtil.flush( writer );
+ }
+
+ /**
+ * write an {@code SubjectType} to stream
+ * @param subject
+ * @param out
+ * @throws ProcessingException
+ */
+ public void write( SubjectType subject, OutputStream out ) throws ProcessingException
+ {
+ verifyWriter( out );
+ StaxUtil.writeStartElement( writer, ASSERTION_PREFIX,
JBossSAMLConstants.SUBJECT.get() , ASSERTION_NSURI.get() );
+ List<JAXBElement<?>> contentList = subject.getContent();
+ if( contentList != null )
+ {
+ for( JAXBElement<?> jaxbEl: contentList )
+ {
+ Class<?> declaredType = jaxbEl.getDeclaredType();
+ if( declaredType.equals( SubjectConfirmationType.class) )
+ {
+ SubjectConfirmationType subjectConfirmationType =
(SubjectConfirmationType) jaxbEl.getValue();
+ StaxUtil.writeStartElement( writer, ASSERTION_PREFIX,
JBossSAMLConstants.SUBJECT_CONFIRMATION.get(), ASSERTION_NSURI.get() );
+
+ StaxUtil.writeAttribute(writer, JBossSAMLConstants.METHOD.get(),
subjectConfirmationType.getMethod() );
+
+ BaseIDAbstractType baseID = subjectConfirmationType.getBaseID();
+ if( baseID != null )
+ {
+ write( baseID, out );
+ }
+ NameIDType nameIDType = subjectConfirmationType.getNameID();
+ if( nameIDType != null )
+ {
+ write( nameIDType, new QName( ASSERTION_NSURI.get(),
JBossSAMLConstants.NAMEID.get(), ASSERTION_PREFIX), out );
+ }
+ SubjectConfirmationDataType subjectConfirmationData =
subjectConfirmationType.getSubjectConfirmationData();
+ if( subjectConfirmationData != null )
+ {
+ write( subjectConfirmationData, out );
+ }
+
+
+ StaxUtil.writeEndElement( writer);
+ }
+ else if( declaredType.equals( NameIDType.class ))
+ {
+ NameIDType nameIDType = (NameIDType) jaxbEl.getValue();
+ write( nameIDType, new QName( ASSERTION_NSURI.get(),
JBossSAMLConstants.NAMEID.get(), ASSERTION_PREFIX), out );
+ }
+ else
+ throw new RuntimeException( "SAMLAssertionWriter: NYI: declared
Type:" + declaredType.getName() );
+ }
+ }
+
+ StaxUtil.writeEndElement( writer);
+ StaxUtil.flush( writer );
+ }
+
+ private void write( BaseIDAbstractType baseId, OutputStream out ) throws
ProcessingException
+ {
+ throw new RuntimeException( "NYI");
+ }
+
+ private void write( SubjectConfirmationDataType subjectConfirmationData, OutputStream
out ) throws ProcessingException
+ {
+ verifyWriter(out);
+ StaxUtil.writeStartElement( writer, ASSERTION_PREFIX,
JBossSAMLConstants.SUBJECT_CONFIRMATION_DATA.get(), ASSERTION_NSURI.get() );
+
+ //Let us look at attributes
+ String inResponseTo = subjectConfirmationData.getInResponseTo();
+ if( StringUtil.isNotNull( inResponseTo ))
+ {
+ StaxUtil.writeAttribute(writer, JBossSAMLConstants.IN_RESPONSE_TO.get(),
inResponseTo );
+ }
+
+ XMLGregorianCalendar notBefore = subjectConfirmationData.getNotBefore();
+ if( notBefore != null )
+ {
+ StaxUtil.writeAttribute(writer,
JBossSAMLConstants.NOT_BEFORE.get(),notBefore.toString() );
+ }
+
+ XMLGregorianCalendar notOnOrAfter = subjectConfirmationData.getNotOnOrAfter();
+ if( notOnOrAfter != null )
+ {
+ StaxUtil.writeAttribute(writer,
JBossSAMLConstants.NOT_ON_OR_AFTER.get(),notOnOrAfter.toString() );
+ }
+
+ String recipient = subjectConfirmationData.getRecipient();
+ if( StringUtil.isNotNull( recipient ))
+ {
+ StaxUtil.writeAttribute(writer, JBossSAMLConstants.RECIPIENT.get(), recipient
);
+ }
+
+ String address = subjectConfirmationData.getAddress();
+ if( StringUtil.isNotNull( address ))
+ {
+ StaxUtil.writeAttribute(writer, JBossSAMLConstants.ADDRESS.get(), address );
+ }
+
+ StaxUtil.writeEndElement( writer);
+ StaxUtil.flush( writer );
+ }
}
\ No newline at end of file