Author: anil.saldhana(a)jboss.com
Date: 2010-11-02 23:29:36 -0400 (Tue, 02 Nov 2010)
New Revision: 518
Added:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/saml/v2/writers/
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/saml/v2/writers/SAMLRequestWriter.java
Modified:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/saml/v2/constants/JBossSAMLConstants.java
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/util/StaxUtil.java
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/saml/SAMLAuthnRequestParserTestCase.java
Log:
PLFED-109: PLFED-110: add write of saml request
Modified:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/saml/v2/constants/JBossSAMLConstants.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/saml/v2/constants/JBossSAMLConstants.java 2010-11-02
23:41:04 UTC (rev 517)
+++
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/saml/v2/constants/JBossSAMLConstants.java 2010-11-03
03:29:36 UTC (rev 518)
@@ -28,7 +28,9 @@
*/
public enum JBossSAMLConstants
{
+ ALLOW_CREATE( "AllowCreate" ),
ASSERTION( "Assertion" ),
+ ASSERTION_CONSUMER_SERVICE_URL( "AssertionConsumerServiceURL" ),
AUDIENCE( "Audience" ),
AUDIENCE_RESTRICTION( "AudienceRestriction" ),
AUTHN_CONTEXT( "AuthnContext" ),
@@ -36,6 +38,9 @@
AUTHN_REQUEST( "AuthnRequest" ),
AUTHN_STATEMENT( "AuthnStatement" ),
CONDITIONS( "Conditions" ),
+ CONSENT( "Consent" ),
+ DESTINATION( "Destination" ),
+ FORMAT( "Format" ),
ID( "ID" ),
ISSUE_INSTANT( "IssueInstant" ),
ISSUER( "Issuer" ),
@@ -48,6 +53,8 @@
NOT_BEFORE( "NotBefore" ),
NOT_ON_OR_AFTER( "NotOnOrAfter" ),
RESPONSE( "Response" ),
+ SP_PROVIDED_ID( "SPProvidedID" ),
+ SP_NAME_QUALIFIER( "SPNameQualifier" ),
SIGNATURE( "Signature" ),
SIGNATURE_SHA1_WITH_DSA("http://www.w3.org/2000/09/xmldsig#dsa-sha1&...,
SIGNATURE_SHA1_WITH_RSA("http://www.w3.org/2000/09/xmldsig#rsa-sha1&...,
Added:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/saml/v2/writers/SAMLRequestWriter.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/saml/v2/writers/SAMLRequestWriter.java
(rev 0)
+++
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/saml/v2/writers/SAMLRequestWriter.java 2010-11-03
03:29:36 UTC (rev 518)
@@ -0,0 +1,178 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.picketlink.identity.federation.core.saml.v2.writers;
+
+import static
org.picketlink.identity.federation.core.saml.v2.constants.JBossSAMLURIConstants.ASSERTION_NSURI;
+import static
org.picketlink.identity.federation.core.saml.v2.constants.JBossSAMLURIConstants.PROTOCOL_NSURI;
+
+import java.io.OutputStream;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.picketlink.identity.federation.core.exceptions.ProcessingException;
+import org.picketlink.identity.federation.core.saml.v2.constants.JBossSAMLConstants;
+import org.picketlink.identity.federation.core.util.StaxUtil;
+import org.picketlink.identity.federation.core.util.StringUtil;
+import org.picketlink.identity.federation.saml.v2.assertion.NameIDType;
+import org.picketlink.identity.federation.saml.v2.protocol.AuthnRequestType;
+import org.picketlink.identity.federation.saml.v2.protocol.NameIDPolicyType;
+
+/**
+ * Writes a SAML2 Request Type to Stream
+ * @author Anil.Saldhana(a)redhat.com
+ * @since Nov 2, 2010
+ */
+public class SAMLRequestWriter
+{
+ private static String PROTOCOL_PREFIX = "samlp";
+
+ private XMLStreamWriter writer = null;
+
+ /**
+ * Write a {@code AuthnRequestType } to stream
+ * @param request
+ * @param out
+ * @throws ProcessingException
+ */
+ public void write( AuthnRequestType request, OutputStream out ) throws
ProcessingException
+ {
+ //Get the XML writer
+ if( writer == null )
+ writer = StaxUtil.getXMLStreamWriter( out );
+
+ StaxUtil.writeStartElement( writer, PROTOCOL_PREFIX,
JBossSAMLConstants.AUTHN_REQUEST.get() , PROTOCOL_NSURI.get() );
+
+ StaxUtil.writeNameSpace( writer, PROTOCOL_PREFIX, PROTOCOL_NSURI.get() );
+ StaxUtil.WriteDefaultNameSpace( writer, ASSERTION_NSURI.get() );
+
+ //Attributes
+ StaxUtil.writeAttribute( writer, JBossSAMLConstants.ID.get(), request.getID() );
+ StaxUtil.writeAttribute( writer, JBossSAMLConstants.VERSION.get(),
request.getVersion() );
+ StaxUtil.writeAttribute( writer, JBossSAMLConstants.ISSUE_INSTANT.get(),
request.getIssueInstant().toString() );
+
+ String destination = request.getDestination();
+ if( StringUtil.isNotNull( destination ))
+ StaxUtil.writeAttribute( writer, JBossSAMLConstants.DESTINATION.get(),
destination );
+
+ String consent = request.getConsent();
+ if( StringUtil.isNotNull( consent ))
+ StaxUtil.writeAttribute( writer, JBossSAMLConstants.CONSENT.get(), consent );
+
+ String assertionURL = request.getAssertionConsumerServiceURL();
+ if( StringUtil.isNotNull( assertionURL ) )
+ StaxUtil.writeAttribute( writer,
JBossSAMLConstants.ASSERTION_CONSUMER_SERVICE_URL.get(), assertionURL );
+
+ NameIDType issuer = request.getIssuer();
+ write( issuer, new QName( ASSERTION_NSURI.get(), JBossSAMLConstants.ISSUER.get() ),
out );
+
+ NameIDPolicyType nameIDPolicy = request.getNameIDPolicy();
+ if( nameIDPolicy != null )
+ write( nameIDPolicy, out );
+
+ StaxUtil.writeEndElement( writer);
+ StaxUtil.flush( writer );
+ }
+
+ /**
+ * Write {@code NameIDType} to stream
+ * @param nameIDType
+ * @param tag
+ * @param out
+ * @throws ProcessingException
+ */
+ public void write( NameIDType nameIDType, QName tag, OutputStream out ) throws
ProcessingException
+ {
+ if( writer == null )
+ writer = StaxUtil.getXMLStreamWriter( out );
+
+ StaxUtil.writeStartElement( writer, tag.getPrefix(), tag.getLocalPart() ,
tag.getNamespaceURI() );
+
+ String format = nameIDType.getFormat();
+ if( StringUtil.isNotNull( format ))
+ {
+ StaxUtil.writeAttribute( writer, JBossSAMLConstants.FORMAT.get(), format );
+ }
+
+ String spProvidedID = nameIDType.getSPProvidedID();
+ if( StringUtil.isNotNull( spProvidedID ))
+ {
+ StaxUtil.writeAttribute( writer, JBossSAMLConstants.SP_PROVIDED_ID.get(),
spProvidedID );
+ }
+
+ String spNameQualifier = nameIDType.getSPNameQualifier();
+ if( StringUtil.isNotNull( spNameQualifier ))
+ {
+ StaxUtil.writeAttribute( writer, JBossSAMLConstants.SP_NAME_QUALIFIER.get(),
spNameQualifier );
+ }
+
+ String nameQualifier = nameIDType.getNameQualifier();
+ if( StringUtil.isNotNull( nameQualifier ))
+ {
+ StaxUtil.writeAttribute( writer, JBossSAMLConstants.NAME_QUALIFIER.get(),
nameQualifier );
+ }
+
+ String value = nameIDType.getValue();
+ if( StringUtil.isNotNull( value ))
+ {
+ StaxUtil.writeCharacters( writer, value );
+ }
+
+ StaxUtil.writeEndElement( writer);
+ StaxUtil.flush( writer );
+ }
+
+ /**
+ * Write a {@code NameIDPolicyType} to stream
+ * @param nameIDPolicy
+ * @param out
+ * @throws ProcessingException
+ */
+ public void write( NameIDPolicyType nameIDPolicy, OutputStream out ) throws
ProcessingException
+ {
+ if( writer == null )
+ writer = StaxUtil.getXMLStreamWriter( out );
+
+ StaxUtil.writeStartElement( writer, PROTOCOL_PREFIX,
JBossSAMLConstants.NAMEID_POLICY.get(), PROTOCOL_NSURI.get() );
+
+ String format = nameIDPolicy.getFormat();
+ if( StringUtil.isNotNull( format ))
+ {
+ StaxUtil.writeAttribute( writer, JBossSAMLConstants.FORMAT.get(), format );
+ }
+
+ String spNameQualifier = nameIDPolicy.getSPNameQualifier();
+ if( StringUtil.isNotNull( spNameQualifier ))
+ {
+ StaxUtil.writeAttribute( writer, JBossSAMLConstants.SP_NAME_QUALIFIER.get(),
spNameQualifier );
+ }
+
+ Boolean allowCreate = nameIDPolicy.isAllowCreate();
+ if( allowCreate != null )
+ {
+ StaxUtil.writeAttribute( writer, JBossSAMLConstants.ALLOW_CREATE.get(),
allowCreate.toString() );
+ }
+
+ StaxUtil.writeEndElement( writer);
+ StaxUtil.flush( writer );
+ }
+}
\ No newline at end of file
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 2010-11-02
23:41:04 UTC (rev 517)
+++
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/util/StaxUtil.java 2010-11-03
03:29:36 UTC (rev 518)
@@ -130,6 +130,24 @@
}
/**
+ * Write the default namespace
+ * @param writer
+ * @param ns
+ * @throws ProcessingException
+ */
+ public static void WriteDefaultNameSpace( XMLStreamWriter writer, String ns ) throws
ProcessingException
+ {
+ try
+ {
+ writer.writeDefaultNamespace( ns );
+ }
+ catch (XMLStreamException e)
+ {
+ throw new ProcessingException( e );
+ }
+ }
+
+ /**
* Write a namespace
* @param writer
* @param prefix prefix
Modified:
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/saml/SAMLAuthnRequestParserTestCase.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/saml/SAMLAuthnRequestParserTestCase.java 2010-11-02
23:41:04 UTC (rev 517)
+++
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/saml/SAMLAuthnRequestParserTestCase.java 2010-11-03
03:29:36 UTC (rev 518)
@@ -29,6 +29,7 @@
import org.junit.Test;
import org.picketlink.identity.federation.core.parsers.saml.SAMLParser;
import org.picketlink.identity.federation.core.saml.v2.util.XMLTimeUtil;
+import org.picketlink.identity.federation.core.saml.v2.writers.SAMLRequestWriter;
import org.picketlink.identity.federation.saml.v2.protocol.AuthnRequestType;
import org.picketlink.identity.federation.saml.v2.protocol.NameIDPolicyType;
@@ -64,5 +65,9 @@
NameIDPolicyType nameIDPolicy = authnRequest.getNameIDPolicy();
assertEquals( "urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified",
nameIDPolicy.getFormat() );
assertEquals( Boolean.TRUE , nameIDPolicy.isAllowCreate() );
+
+ //Try out writing
+ SAMLRequestWriter writer = new SAMLRequestWriter();
+ writer.write(authnRequest, System.out );
}
}
\ No newline at end of file