Author: anil.saldhana(a)jboss.com
Date: 2010-11-08 13:17:09 -0500 (Mon, 08 Nov 2010)
New Revision: 543
Modified:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wstrust/WSTrustConstants.java
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wstrust/writers/WSTrustRSTWriter.java
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/wst/WSTrustBatchValidateParsingTestCase.java
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/wst/WSTrustCancelTargetSamlTestCase.java
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/wst/WSTrustIssuePublicCertificateTestCase.java
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/wst/WSTrustIssueSymmetricKeyTestCase.java
Log:
PLFED-109: wst writing
Modified:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wstrust/WSTrustConstants.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wstrust/WSTrustConstants.java 2010-11-08
17:36:26 UTC (rev 542)
+++
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wstrust/WSTrustConstants.java 2010-11-08
18:17:09 UTC (rev 543)
@@ -99,4 +99,5 @@
//Attribute Names
public static final String RST_CONTEXT = "Context";
+ public static final String TYPE = "Type";
}
\ No newline at end of file
Modified:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wstrust/writers/WSTrustRSTWriter.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wstrust/writers/WSTrustRSTWriter.java 2010-11-08
17:36:26 UTC (rev 542)
+++
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wstrust/writers/WSTrustRSTWriter.java 2010-11-08
18:17:09 UTC (rev 543)
@@ -22,22 +22,31 @@
package org.picketlink.identity.federation.core.wstrust.writers;
import static
org.picketlink.identity.federation.core.wstrust.WSTrustConstants.BASE_NAMESPACE;
-import static
org.picketlink.identity.federation.core.wstrust.WSTrustConstants.RST_CONTEXT;
import static org.picketlink.identity.federation.core.wstrust.WSTrustConstants.PREFIX;
import static org.picketlink.identity.federation.core.wstrust.WSTrustConstants.RST;
+import static
org.picketlink.identity.federation.core.wstrust.WSTrustConstants.RST_COLLECTION;
+import static
org.picketlink.identity.federation.core.wstrust.WSTrustConstants.RST_CONTEXT;
import java.io.OutputStream;
import java.net.URI;
+import java.util.List;
import javax.xml.stream.XMLStreamWriter;
import org.picketlink.identity.federation.core.exceptions.ProcessingException;
+import org.picketlink.identity.federation.core.saml.v2.writers.SAMLAssertionWriter;
import org.picketlink.identity.federation.core.util.StaxUtil;
import org.picketlink.identity.federation.core.wstrust.WSTrustConstants;
import org.picketlink.identity.federation.core.wstrust.wrappers.RequestSecurityToken;
+import
org.picketlink.identity.federation.core.wstrust.wrappers.RequestSecurityTokenCollection;
+import org.picketlink.identity.federation.saml.v2.assertion.AssertionType;
import org.picketlink.identity.federation.ws.policy.AppliesTo;
+import org.picketlink.identity.federation.ws.trust.BinarySecretType;
+import org.picketlink.identity.federation.ws.trust.CancelTargetType;
+import org.picketlink.identity.federation.ws.trust.EntropyType;
import org.picketlink.identity.federation.ws.trust.OnBehalfOfType;
import org.picketlink.identity.federation.ws.trust.UseKeyType;
+import org.picketlink.identity.federation.ws.trust.ValidateTargetType;
import org.picketlink.identity.federation.ws.wss.secext.UsernameTokenType;
import org.w3c.dom.Element;
@@ -49,6 +58,31 @@
public class WSTrustRSTWriter extends AbstractWSWriter
{
/**
+ * Write the {@code RequestSecurityTokenCollection} into the {@code OutputStream}
+ * @param requestTokenCollection
+ * @param out
+ * @throws ProcessingException
+ */
+ public void write( RequestSecurityTokenCollection requestTokenCollection, OutputStream
out ) throws ProcessingException
+ {
+ verifyWriter(out);
+ StaxUtil.writeStartElement( writer, PREFIX, RST_COLLECTION, BASE_NAMESPACE);
+ StaxUtil.writeNameSpace( writer, PREFIX, BASE_NAMESPACE );
+
+ List<RequestSecurityToken> tokenList =
requestTokenCollection.getRequestSecurityTokens();
+ if( tokenList == null )
+ throw new ProcessingException( "RST list is null" );
+
+ for( RequestSecurityToken token: tokenList )
+ {
+ write(token, out);
+ }
+
+ StaxUtil.writeEndElement( writer );
+ StaxUtil.flush( writer );
+ }
+
+ /**
* Write the {@code RequestSecurityToken} into the {@code OutputStream}
* @param requestToken
* @param out
@@ -88,35 +122,131 @@
StaxUtil.writeCharacters(writer, keyType.toString() );
StaxUtil.writeEndElement( writer );
}
+ EntropyType entropy = requestToken.getEntropy();
+ if( entropy != null )
+ {
+ writeEntropyType(entropy);
+ }
UseKeyType useKeyType = requestToken.getUseKey();
if( useKeyType != null )
{
- Object useKeyTypeValue = useKeyType.getAny();
- if( useKeyTypeValue instanceof Element )
- {
- Element domElement = (Element) useKeyTypeValue;
- StaxUtil.writeDOMElement( writer, domElement );
- }
- else
- throw new RuntimeException( " Unknown use key type:" +
useKeyTypeValue.getClass().getName() );
+ writeUseKeyType(useKeyType);
}
OnBehalfOfType onBehalfOf = requestToken.getOnBehalfOf();
if( onBehalfOf != null )
{
- StaxUtil.writeStartElement( writer, PREFIX, WSTrustConstants.On_BEHALF_OF,
BASE_NAMESPACE);
- StaxUtil.writeCharacters(writer, "" );
-
- UsernameTokenType usernameToken = (UsernameTokenType) onBehalfOf.getAny();
- WSSecurityWriter wsseWriter = new WSSecurityWriter();
- wsseWriter.write( usernameToken, out );
- StaxUtil.writeEndElement( writer );
+ writeOnBehalfOfType(onBehalfOf, out);
}
+ ValidateTargetType validateTarget = requestToken.getValidateTarget();
+ if( validateTarget != null )
+ {
+
+ writeValidateTargetType(validateTarget, out);
+ }
+
+ CancelTargetType cancelTarget = requestToken.getCancelTarget();
+ if( cancelTarget != null )
+ {
+ writeCancelTargetType(cancelTarget, out);
+ }
+
StaxUtil.writeEndElement( writer );
StaxUtil.flush( writer );
}
+
+ private void writeEntropyType(EntropyType entropy) throws ProcessingException
+ {
+ StaxUtil.writeStartElement( writer, PREFIX, WSTrustConstants.ENTROPY,
BASE_NAMESPACE);
+
+ List<Object> entropyList = entropy.getAny();
+ if( entropyList != null )
+ {
+ for( Object entropyObj: entropyList )
+ {
+ if( entropyObj instanceof BinarySecretType )
+ {
+ BinarySecretType binarySecret = (BinarySecretType) entropyObj;
+ writeBinarySecretType( writer, binarySecret );
+ }
+ }
+ }
+ StaxUtil.writeEndElement( writer );
+ }
+
+ private void writeUseKeyType(UseKeyType useKeyType) throws ProcessingException
+ {
+ Object useKeyTypeValue = useKeyType.getAny();
+ if( useKeyTypeValue instanceof Element )
+ {
+ Element domElement = (Element) useKeyTypeValue;
+ StaxUtil.writeDOMElement( writer, domElement );
+ }
+ else
+ throw new RuntimeException( " Unknown use key type:" +
useKeyTypeValue.getClass().getName() );
+ }
+
+ private void writeOnBehalfOfType(OnBehalfOfType onBehalfOf, OutputStream out) throws
ProcessingException
+ {
+ StaxUtil.writeStartElement( writer, PREFIX, WSTrustConstants.On_BEHALF_OF,
BASE_NAMESPACE);
+ StaxUtil.writeCharacters(writer, "" );
+
+ UsernameTokenType usernameToken = (UsernameTokenType) onBehalfOf.getAny();
+ WSSecurityWriter wsseWriter = new WSSecurityWriter();
+ wsseWriter.write( usernameToken, out );
+ StaxUtil.writeEndElement( writer );
+ }
+
+ private void writeValidateTargetType(ValidateTargetType validateTarget, OutputStream
out) throws ProcessingException
+ {
+ StaxUtil.writeStartElement( writer, PREFIX, WSTrustConstants.VALIDATE_TARGET,
BASE_NAMESPACE);
+ StaxUtil.writeCharacters(writer, "" );
+
+ Object validateTargetObj = validateTarget.getAny();
+ if( validateTargetObj instanceof AssertionType )
+ {
+ AssertionType assertion = (AssertionType) validateTargetObj;
+ SAMLAssertionWriter samlAssertionWriter = new SAMLAssertionWriter();
+ samlAssertionWriter.write(assertion, out);
+ }
+ else throw new ProcessingException( "Unknown validate target type=" +
validateTargetObj.getClass().getName() );
+
+ StaxUtil.writeEndElement( writer );
+ }
+
+ private void writeCancelTargetType(CancelTargetType cancelTarget, OutputStream out)
throws ProcessingException
+ {
+ StaxUtil.writeStartElement( writer, PREFIX, WSTrustConstants.CANCEL_TARGET,
BASE_NAMESPACE);
+ StaxUtil.writeCharacters(writer, "" );
+
+ Object cancelTargetObj = cancelTarget.getAny();
+ if( cancelTargetObj instanceof AssertionType )
+ {
+ AssertionType assertion = (AssertionType) cancelTargetObj;
+ SAMLAssertionWriter samlAssertionWriter = new SAMLAssertionWriter();
+ samlAssertionWriter.write(assertion, out);
+ }
+ else throw new ProcessingException( "Unknown cancel target type=" +
cancelTargetObj.getClass().getName() );
+
+ StaxUtil.writeEndElement( writer );
+ }
+
+ /**
+ * Write a {@code BinarySecretType} to stream
+ * @param writer
+ * @param binarySecret
+ * @throws ProcessingException
+ */
+ private void writeBinarySecretType( XMLStreamWriter writer, BinarySecretType
binarySecret ) throws ProcessingException
+ {
+ StaxUtil.writeStartElement( writer, PREFIX, WSTrustConstants.BINARY_SECRET,
BASE_NAMESPACE );
+ String type = binarySecret.getType();
+ StaxUtil.writeAttribute(writer, WSTrustConstants.TYPE, type );
+ StaxUtil.writeCharacters(writer, new String( binarySecret.getValue() ) );
+ StaxUtil.writeEndElement(writer);
+ }
private void writeRequestType( XMLStreamWriter writer , URI uri ) throws
ProcessingException
{
Modified:
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/wst/WSTrustBatchValidateParsingTestCase.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/wst/WSTrustBatchValidateParsingTestCase.java 2010-11-08
17:36:26 UTC (rev 542)
+++
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/wst/WSTrustBatchValidateParsingTestCase.java 2010-11-08
18:17:09 UTC (rev 543)
@@ -24,14 +24,18 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;
import org.junit.Test;
import org.picketlink.identity.federation.core.parsers.wst.WSTrustParser;
+import org.picketlink.identity.federation.core.saml.v2.util.DocumentUtil;
import org.picketlink.identity.federation.core.wstrust.WSTrustConstants;
import org.picketlink.identity.federation.core.wstrust.wrappers.RequestSecurityToken;
import
org.picketlink.identity.federation.core.wstrust.wrappers.RequestSecurityTokenCollection;
+import org.picketlink.identity.federation.core.wstrust.writers.WSTrustRSTWriter;
/**
* Validate the parsing of wst-batch-validate.xml
@@ -62,6 +66,16 @@
assertEquals( "validatecontext2", rst2.getContext() );
assertEquals( WSTrustConstants.BATCH_VALIDATE_REQUEST ,
rst2.getRequestType().toASCIIString() );
assertEquals( WSTrustConstants.RSTR_STATUS_TOKEN_TYPE ,
rst2.getTokenType().toASCIIString() );
+
+ //Now for the writing part
+ WSTrustRSTWriter rstWriter = new WSTrustRSTWriter();
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+ rstWriter.write( requestCollection, baos );
+
+ System.out.println( new String( baos.toByteArray() ));
+ DocumentUtil.getDocument( new ByteArrayInputStream( baos.toByteArray() ));
+ baos.close();
}
}
Modified:
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/wst/WSTrustCancelTargetSamlTestCase.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/wst/WSTrustCancelTargetSamlTestCase.java 2010-11-08
17:36:26 UTC (rev 542)
+++
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/wst/WSTrustCancelTargetSamlTestCase.java 2010-11-08
18:17:09 UTC (rev 543)
@@ -24,6 +24,8 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;
@@ -33,8 +35,10 @@
import org.junit.Test;
import org.picketlink.identity.federation.core.parsers.wst.WSTrustParser;
import org.picketlink.identity.federation.core.saml.v2.constants.JBossSAMLURIConstants;
+import org.picketlink.identity.federation.core.saml.v2.util.DocumentUtil;
import org.picketlink.identity.federation.core.wstrust.WSTrustConstants;
import org.picketlink.identity.federation.core.wstrust.wrappers.RequestSecurityToken;
+import org.picketlink.identity.federation.core.wstrust.writers.WSTrustRSTWriter;
import org.picketlink.identity.federation.saml.v2.assertion.AssertionType;
import org.picketlink.identity.federation.saml.v2.assertion.ConditionsType;
import org.picketlink.identity.federation.saml.v2.assertion.NameIDType;
@@ -56,14 +60,23 @@
InputStream configStream = tcl.getResourceAsStream(
"parser/wst/wst-cancel-saml.xml" );
WSTrustParser parser = new WSTrustParser();
- RequestSecurityToken rst1 = (RequestSecurityToken) parser.parse( configStream );
- assertEquals( "cancelcontext", rst1.getContext() );
- assertEquals( WSTrustConstants.CANCEL_REQUEST,
rst1.getRequestType().toASCIIString() );
+ RequestSecurityToken requestToken = (RequestSecurityToken) parser.parse(
configStream );
+ assertEquals( "cancelcontext", requestToken.getContext() );
+ assertEquals( WSTrustConstants.CANCEL_REQUEST,
requestToken.getRequestType().toASCIIString() );
- CancelTargetType cancelTarget = rst1.getCancelTarget();
+ CancelTargetType cancelTarget = requestToken.getCancelTarget();
AssertionType assertion = (AssertionType) cancelTarget.getAny();
validateAssertion( assertion );
+
+ //Now for the writing part
+ WSTrustRSTWriter rstWriter = new WSTrustRSTWriter();
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+ rstWriter.write(requestToken, baos );
+
+ System.out.println( new String( baos.toByteArray() ));
+ DocumentUtil.getDocument( new ByteArrayInputStream( baos.toByteArray() ));
}
private void validateAssertion( AssertionType assertion ) throws Exception
Modified:
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/wst/WSTrustIssuePublicCertificateTestCase.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/wst/WSTrustIssuePublicCertificateTestCase.java 2010-11-08
17:36:26 UTC (rev 542)
+++
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/wst/WSTrustIssuePublicCertificateTestCase.java 2010-11-08
18:17:09 UTC (rev 543)
@@ -23,6 +23,8 @@
import static org.junit.Assert.assertEquals;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import javax.xml.bind.JAXBElement;
@@ -30,8 +32,10 @@
import org.junit.Test;
import
org.picketlink.identity.federation.core.parsers.wst.WSTRequestSecurityTokenParser;
import org.picketlink.identity.federation.core.parsers.wst.WSTrustParser;
+import org.picketlink.identity.federation.core.saml.v2.util.DocumentUtil;
import org.picketlink.identity.federation.core.wstrust.WSTrustConstants;
import org.picketlink.identity.federation.core.wstrust.wrappers.RequestSecurityToken;
+import org.picketlink.identity.federation.core.wstrust.writers.WSTrustRSTWriter;
import org.picketlink.identity.federation.ws.addressing.EndpointReferenceType;
import org.picketlink.identity.federation.ws.policy.AppliesTo;
import org.picketlink.identity.federation.ws.trust.UseKeyType;
@@ -70,5 +74,14 @@
Element certEl = (Element) useKeyType.getAny();
assertEquals( "ds:" + WSTRequestSecurityTokenParser.X509CERTIFICATE,
certEl.getTagName() );
+
+ //Now for the writing part
+ WSTrustRSTWriter rstWriter = new WSTrustRSTWriter();
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+ rstWriter.write(requestToken, baos );
+
+ System.out.println( new String( baos.toByteArray() ));
+ DocumentUtil.getDocument( new ByteArrayInputStream( baos.toByteArray() ));
}
}
\ No newline at end of file
Modified:
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/wst/WSTrustIssueSymmetricKeyTestCase.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/wst/WSTrustIssueSymmetricKeyTestCase.java 2010-11-08
17:36:26 UTC (rev 542)
+++
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/wst/WSTrustIssueSymmetricKeyTestCase.java 2010-11-08
18:17:09 UTC (rev 543)
@@ -23,14 +23,18 @@
import static org.junit.Assert.assertEquals;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import javax.xml.bind.JAXBElement;
import org.junit.Test;
import org.picketlink.identity.federation.core.parsers.wst.WSTrustParser;
+import org.picketlink.identity.federation.core.saml.v2.util.DocumentUtil;
import org.picketlink.identity.federation.core.wstrust.WSTrustConstants;
import org.picketlink.identity.federation.core.wstrust.wrappers.RequestSecurityToken;
+import org.picketlink.identity.federation.core.wstrust.writers.WSTrustRSTWriter;
import org.picketlink.identity.federation.ws.addressing.EndpointReferenceType;
import org.picketlink.identity.federation.ws.policy.AppliesTo;
import org.picketlink.identity.federation.ws.trust.BinarySecretType;
@@ -69,5 +73,14 @@
assertEquals( WSTrustConstants.BS_TYPE_NONCE, binarySecret.getType() );
assertEquals( "M0/7qLpV49c=" , new String( binarySecret.getValue() ));
+
+ //Now for the writing part
+ WSTrustRSTWriter rstWriter = new WSTrustRSTWriter();
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+ rstWriter.write(requestToken, baos );
+
+ System.out.println( new String( baos.toByteArray() ));
+ DocumentUtil.getDocument( new ByteArrayInputStream( baos.toByteArray() ));
}
}
\ No newline at end of file