Author: anil.saldhana(a)jboss.com
Date: 2010-10-19 11:27:47 -0400 (Tue, 19 Oct 2010)
New Revision: 495
Added:
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/wstrust/writers/
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wstrust/writers/WSTrustRSTWriter.java
Modified:
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wstrust/WSTrustConstants.java
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/wst/WSTrustIssueTestCase.java
Log:
PLFED-109: some xml writing
Added:
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
(rev 0)
+++
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/util/StaxUtil.java 2010-10-19
15:27:47 UTC (rev 495)
@@ -0,0 +1,191 @@
+/*
+ * 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.util;
+
+import java.io.OutputStream;
+
+import javax.xml.stream.XMLEventWriter;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.picketlink.identity.federation.core.exceptions.ProcessingException;
+
+/**
+ * Utility class that deals with StAX
+ * @author Anil.Saldhana(a)redhat.com
+ * @since Oct 19, 2010
+ */
+public class StaxUtil
+{
+ /**
+ * Flush the stream writer
+ * @param writer
+ * @throws ProcessingException
+ */
+ public static void flush( XMLStreamWriter writer ) throws ProcessingException
+ {
+ try
+ {
+ writer.flush();
+ }
+ catch (XMLStreamException e)
+ {
+ throw new ProcessingException( e );
+ }
+ }
+
+ /**
+ * Get an {@code XMLEventWriter}
+ * @param outStream
+ * @return
+ * @throws ProcessingException
+ */
+ public static XMLEventWriter getXMLEventWriter( final OutputStream outStream ) throws
ProcessingException
+ {
+ XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
+ try
+ {
+ return xmlOutputFactory.createXMLEventWriter( outStream, "UTF-8" );
+ }
+ catch (XMLStreamException e)
+ {
+ throw new ProcessingException( e );
+ }
+ }
+
+ /**
+ * Get an {@code XMLStreamWriter}
+ * @param outStream
+ * @return
+ * @throws ProcessingException
+ */
+ public static XMLStreamWriter getXMLStreamWriter( final OutputStream outStream )
throws ProcessingException
+ {
+ XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
+ try
+ {
+ return xmlOutputFactory.createXMLStreamWriter( outStream, "UTF-8" );
+ }
+ catch (XMLStreamException e)
+ {
+ throw new ProcessingException( e );
+ }
+ }
+
+ /**
+ * Write an xml attribute
+ * @param writer
+ * @param localName localpart
+ * @param value value of the attribute
+ * @throws ProcessingException
+ */
+ public static void writeAttribute( XMLStreamWriter writer, String localName, String
value ) throws ProcessingException
+ {
+ try
+ {
+ writer.writeAttribute(localName, value);
+ }
+ catch (XMLStreamException e)
+ {
+ throw new ProcessingException( e );
+ }
+ }
+
+ /**
+ * Write a string as text node
+ * @param writer
+ * @param value
+ * @throws ProcessingException
+ */
+ public static void writeCharacters( XMLStreamWriter writer, String value ) throws
ProcessingException
+ {
+ try
+ {
+ writer.writeCharacters( value);
+ }
+ catch (XMLStreamException e)
+ {
+ throw new ProcessingException( e );
+ }
+ }
+
+ /**
+ * Write a namespace
+ * @param writer
+ * @param prefix prefix
+ * @param ns Namespace URI
+ * @throws ProcessingException
+ */
+ public static void writeNameSpace( XMLStreamWriter writer, String prefix, String ns )
throws ProcessingException
+ {
+ try
+ {
+ writer.writeNamespace(prefix, ns);
+ }
+ catch (XMLStreamException e)
+ {
+ throw new ProcessingException( e );
+ }
+ }
+
+ /**
+ * Write a start element
+ * @param writer
+ * @param prefix
+ * @param localPart
+ * @param ns
+ * @throws ProcessingException
+ */
+ public static void writeStartElement( XMLStreamWriter writer, String prefix, String
localPart, String ns ) throws ProcessingException
+ {
+ try
+ {
+ writer.writeStartElement( prefix, localPart, ns);
+ }
+ catch (XMLStreamException e)
+ {
+ throw new ProcessingException( e );
+ }
+ }
+
+ /**
+ * <p>
+ * Write an end element. The stream writer keeps track of which start element
+ * needs to be closed with an end tag.
+ * </p>
+ *
+ * @param writer
+ * @throws ProcessingException
+ */
+ public static void writeEndElement( XMLStreamWriter writer ) throws
ProcessingException
+ {
+ try
+ {
+ writer.writeEndElement();
+ }
+ catch (XMLStreamException e)
+ {
+ throw new ProcessingException( e );
+ }
+ }
+}
\ No newline at end of file
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-10-18
22:48:02 UTC (rev 494)
+++
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wstrust/WSTrustConstants.java 2010-10-19
15:27:47 UTC (rev 495)
@@ -34,6 +34,7 @@
public class WSTrustConstants
{
public static final String BASE_NAMESPACE =
"http://docs.oasis-open.org/ws-sx/ws-trust/200512";
+ public static final String PREFIX = "wst";
// WS-Trust request types
public static final String BATCH_ISSUE_REQUEST = BASE_NAMESPACE +
"/BatchIssue";
Added:
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
(rev 0)
+++
federation/trunk/picketlink-fed-core/src/main/java/org/picketlink/identity/federation/core/wstrust/writers/WSTrustRSTWriter.java 2010-10-19
15:27:47 UTC (rev 495)
@@ -0,0 +1,90 @@
+/*
+ * 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.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 java.io.OutputStream;
+import java.net.URI;
+
+import javax.xml.stream.XMLStreamWriter;
+
+import org.picketlink.identity.federation.core.exceptions.ProcessingException;
+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;
+
+/**
+ * Given a {@code RequestSecurityToken}, write into an {@code OutputStream}
+ * @author Anil.Saldhana(a)redhat.com
+ * @since Oct 19, 2010
+ */
+public class WSTrustRSTWriter
+{
+ /**
+ * Write the {@code RequestSecurityToken} into the {@code OutputStream}
+ * @param requestToken
+ * @param out
+ * @throws ProcessingException
+ */
+ public void write( RequestSecurityToken requestToken, OutputStream out ) throws
ProcessingException
+ {
+ //Get the XML writer
+ XMLStreamWriter writer = StaxUtil.getXMLStreamWriter( out );
+ StaxUtil.writeStartElement( writer, PREFIX, RST, BASE_NAMESPACE);
+ StaxUtil.writeNameSpace( writer, PREFIX, BASE_NAMESPACE );
+ String context = requestToken.getContext();
+ StaxUtil.writeAttribute( writer, RST_CONTEXT, context );
+
+ URI requestType = requestToken.getRequestType();
+ if( requestType != null )
+ {
+ writeRequestType( writer, requestType );
+ }
+
+ URI tokenType = requestToken.getTokenType();
+ if( tokenType != null )
+ {
+ writeTokenType( writer, tokenType );
+ }
+
+ StaxUtil.writeEndElement( writer );
+ StaxUtil.flush( writer );
+ }
+
+ private void writeRequestType( XMLStreamWriter writer , URI uri ) throws
ProcessingException
+ {
+ StaxUtil.writeStartElement( writer, PREFIX, WSTrustConstants.REQUEST_TYPE,
BASE_NAMESPACE );
+ StaxUtil.writeCharacters(writer, uri.toASCIIString() );
+ StaxUtil.writeEndElement(writer);
+ }
+
+ private void writeTokenType( XMLStreamWriter writer , URI uri ) throws
ProcessingException
+ {
+ StaxUtil.writeStartElement( writer, PREFIX, WSTrustConstants.TOKEN_TYPE,
BASE_NAMESPACE );
+ StaxUtil.writeCharacters(writer, uri.toASCIIString() );
+ StaxUtil.writeEndElement(writer);
+ }
+}
\ No newline at end of file
Modified:
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/wst/WSTrustIssueTestCase.java
===================================================================
---
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/wst/WSTrustIssueTestCase.java 2010-10-18
22:48:02 UTC (rev 494)
+++
federation/trunk/picketlink-fed-core/src/test/java/org/picketlink/test/identity/federation/core/parser/wst/WSTrustIssueTestCase.java 2010-10-19
15:27:47 UTC (rev 495)
@@ -29,6 +29,7 @@
import org.picketlink.identity.federation.core.parsers.wst.WSTrustParser;
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;
/**
* Validate simple RST parsing
@@ -49,5 +50,12 @@
assertEquals( "testcontext", requestToken.getContext() );
assertEquals( WSTrustConstants.ISSUE_REQUEST ,
requestToken.getRequestType().toASCIIString() );
assertEquals( WSTrustConstants.SAML2_TOKEN_TYPE,
requestToken.getTokenType().toASCIIString() );
+
+ //Now for the writing part
+ WSTrustRSTWriter rstWriter = new WSTrustRSTWriter();
+ rstWriter.write(requestToken, System.out );
+
+ //TODO: use a buffer output stream. Reparse the written xml and then match the
orig object model with reparsed
+ //object model
}
-}
+}
\ No newline at end of file