Author: giger
Date: Mon Oct 31 11:33:14 2011
New Revision: 1195419

URL: http://svn.apache.org/viewvc?rev=1195419&view=rev
Log:
Allow to write to a XMLStreamWriter and not only to an OutputStream

Added:
    
webservices/wss4j/branches/swssf/streaming-xml-security/src/main/java/org/swssf/xmlsec/impl/XMLSecurityEventWriter.java
   (with props)
    
webservices/wss4j/branches/swssf/streaming-xml-security/src/test/java/org/swssf/xmlsec/test/XMLSecurityEventWriterTest.java
   (with props)
Modified:
    
webservices/wss4j/branches/swssf/streaming-ws-security/src/main/java/org/swssf/wss/ext/OutboundWSSec.java
    
webservices/wss4j/branches/swssf/streaming-xml-security/src/main/java/org/swssf/xmlsec/impl/processor/output/FinalOutputProcessor.java

Modified: 
webservices/wss4j/branches/swssf/streaming-ws-security/src/main/java/org/swssf/wss/ext/OutboundWSSec.java
URL: 
http://svn.apache.org/viewvc/webservices/wss4j/branches/swssf/streaming-ws-security/src/main/java/org/swssf/wss/ext/OutboundWSSec.java?rev=1195419&r1=1195418&r2=1195419&view=diff
==============================================================================
--- 
webservices/wss4j/branches/swssf/streaming-ws-security/src/main/java/org/swssf/wss/ext/OutboundWSSec.java
 (original)
+++ 
webservices/wss4j/branches/swssf/streaming-ws-security/src/main/java/org/swssf/wss/ext/OutboundWSSec.java
 Mon Oct 31 11:33:14 2011
@@ -50,7 +50,7 @@ public class OutboundWSSec {
 
     /**
      * This method is the entry point for the incoming security-engine.
-     * Hand over the original XMLStreamReader and use the returned one for 
further processing
+     * Hand over a outputStream and use the returned XMLStreamWriter for 
further processing
      *
      * @param outputStream The original outputStream
      * @return A new XMLStreamWriter which does transparently the security 
processing.
@@ -62,14 +62,41 @@ public class OutboundWSSec {
 
     /**
      * This method is the entry point for the incoming security-engine.
-     * Hand over the original XMLStreamReader and use the returned one for 
further processing
+     * Hand over the original XMLStreamWriter and use the returned one for 
further processing
+     *
+     * @param outputStream The original outputStream
+     * @return A new XMLStreamWriter which does transparently the security 
processing.
+     * @throws WSSecurityException thrown when a Security failure occurs
+     */
+    public XMLStreamWriter processOutMessage(XMLStreamWriter xmlStreamWriter, 
String encoding, List<SecurityEvent> requestSecurityEvents) throws 
WSSecurityException {
+        return processOutMessage(xmlStreamWriter, encoding, 
requestSecurityEvents, null);
+    }
+
+    /**
+     * This method is the entry point for the incoming security-engine.
+     * Hand over a outputstream and use the returned XMLStreamWriter for 
further processing
      *
      * @param outputStream The original outputStream
      * @return A new XMLStreamWriter which does transparently the security 
processing.
      * @throws WSSecurityException thrown when a Security failure occurs
      */
     public XMLStreamWriter processOutMessage(OutputStream outputStream, String 
encoding, List<SecurityEvent> requestSecurityEvents, SecurityEventListener 
securityEventListener) throws WSSecurityException {
+        return processOutMessage((Object) outputStream, encoding, 
requestSecurityEvents, securityEventListener);
+    }
 
+    /**
+     * This method is the entry point for the incoming security-engine.
+     * Hand over the original XMLStreamWriter and use the returned one for 
further processing
+     *
+     * @param outputStream The original outputStream
+     * @return A new XMLStreamWriter which does transparently the security 
processing.
+     * @throws WSSecurityException thrown when a Security failure occurs
+     */
+    public XMLStreamWriter processOutMessage(XMLStreamWriter xmlStreamWriter, 
String encoding, List<SecurityEvent> requestSecurityEvents, 
SecurityEventListener securityEventListener) throws WSSecurityException {
+        return processOutMessage((Object) xmlStreamWriter, encoding, 
requestSecurityEvents, securityEventListener);
+    }
+
+    private XMLStreamWriter processOutMessage(Object output, String encoding, 
List<SecurityEvent> requestSecurityEvents, SecurityEventListener 
securityEventListener) throws WSSecurityException {
         final WSSecurityContextImpl securityContextImpl = new 
WSSecurityContextImpl();
         securityContextImpl.putList(SecurityEvent.class, 
requestSecurityEvents);
         securityContextImpl.setSecurityEventListener(securityEventListener);
@@ -149,8 +176,13 @@ public class OutboundWSSec {
                     processorChain.addProcessor(new 
SAMLTokenOutputProcessor(securityProperties, action));
                 }
             }
-
-            processorChain.addProcessor(new FinalOutputProcessor(outputStream, 
encoding, securityProperties, null));
+            if (output instanceof OutputStream) {
+                processorChain.addProcessor(new 
FinalOutputProcessor((OutputStream) output, encoding, securityProperties, 
null));
+            } else if (output instanceof XMLStreamWriter) {
+                processorChain.addProcessor(new 
FinalOutputProcessor((XMLStreamWriter) output, securityProperties, null));
+            } else {
+                throw new IllegalArgumentException(output + " is not supported 
as output");
+            }
         } catch (XMLSecurityException e) {
             throw new WSSecurityException(e.getMessage(), e);
         }

Added: 
webservices/wss4j/branches/swssf/streaming-xml-security/src/main/java/org/swssf/xmlsec/impl/XMLSecurityEventWriter.java
URL: 
http://svn.apache.org/viewvc/webservices/wss4j/branches/swssf/streaming-xml-security/src/main/java/org/swssf/xmlsec/impl/XMLSecurityEventWriter.java?rev=1195419&view=auto
==============================================================================
--- 
webservices/wss4j/branches/swssf/streaming-xml-security/src/main/java/org/swssf/xmlsec/impl/XMLSecurityEventWriter.java
 (added)
+++ 
webservices/wss4j/branches/swssf/streaming-xml-security/src/main/java/org/swssf/xmlsec/impl/XMLSecurityEventWriter.java
 Mon Oct 31 11:33:14 2011
@@ -0,0 +1,159 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.swssf.xmlsec.impl;
+
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+import javax.xml.stream.*;
+import javax.xml.stream.events.*;
+import java.util.Iterator;
+
+/**
+ * @author $Author$
+ * @version $Revision$ $Date$
+ */
+public class XMLSecurityEventWriter implements XMLEventWriter {
+
+    private XMLStreamWriter xmlStreamWriter;
+
+    public XMLSecurityEventWriter(XMLStreamWriter xmlStreamWriter) {
+        this.xmlStreamWriter = xmlStreamWriter;
+    }
+
+    public void add(XMLEvent event) throws XMLStreamException {
+        switch (event.getEventType()) {
+            case XMLStreamConstants.START_ELEMENT:
+                StartElement startElement = event.asStartElement();
+                QName n = startElement.getName();
+                this.xmlStreamWriter.writeStartElement(n.getPrefix(), 
n.getLocalPart(), n.getNamespaceURI());
+
+                @SuppressWarnings("unchecked")
+                Iterator<Namespace> namespaceIterator = 
startElement.getNamespaces();
+                while (namespaceIterator.hasNext()) {
+                    add(namespaceIterator.next());
+                }
+
+                @SuppressWarnings("unchecked")
+                Iterator<Attribute> attributeIterator = 
startElement.getAttributes();
+                while (attributeIterator.hasNext()) {
+                    add(attributeIterator.next());
+                }
+                break;
+
+            case XMLStreamConstants.END_ELEMENT:
+                this.xmlStreamWriter.writeEndElement();
+                break;
+
+            case XMLStreamConstants.PROCESSING_INSTRUCTION:
+                ProcessingInstruction pi = (ProcessingInstruction) event;
+                
this.xmlStreamWriter.writeProcessingInstruction(pi.getTarget(), pi.getData());
+                break;
+
+            case XMLStreamConstants.CHARACTERS:
+                Characters characters = event.asCharacters();
+                String text = characters.getData();
+                if (characters.isCData()) {
+                    this.xmlStreamWriter.writeCData(text);
+                } else {
+                    this.xmlStreamWriter.writeCharacters(text);
+                }
+                break;
+
+            case XMLStreamConstants.COMMENT:
+                this.xmlStreamWriter.writeComment(((Comment) event).getText());
+                break;
+
+            case XMLStreamConstants.START_DOCUMENT:
+                StartDocument startDocument = (StartDocument) event;
+                if (!startDocument.encodingSet()) {
+                    
this.xmlStreamWriter.writeStartDocument(startDocument.getVersion());
+                } else {
+                    
this.xmlStreamWriter.writeStartDocument(startDocument.getCharacterEncodingScheme(),
 startDocument.getVersion());
+                }
+                break;
+
+            case XMLStreamConstants.END_DOCUMENT:
+                this.xmlStreamWriter.writeEndDocument();
+                break;
+
+            case XMLStreamConstants.ENTITY_REFERENCE:
+                this.xmlStreamWriter.writeEntityRef(((EntityReference) 
event).getName());
+                break;
+
+            case XMLStreamConstants.ATTRIBUTE:
+                Attribute attribute = (Attribute) event;
+                QName name = attribute.getName();
+                this.xmlStreamWriter.writeAttribute(name.getPrefix(), 
name.getNamespaceURI(), name.getLocalPart(), attribute.getValue());
+                break;
+
+            case XMLStreamConstants.DTD:
+                this.xmlStreamWriter.writeDTD(((DTD) 
event).getDocumentTypeDeclaration());
+                break;
+
+            case XMLStreamConstants.CDATA:
+                
this.xmlStreamWriter.writeCData(event.asCharacters().getData());
+                break;
+
+            case XMLStreamConstants.NAMESPACE:
+                Namespace ns = (Namespace) event;
+                this.xmlStreamWriter.writeNamespace(ns.getPrefix(), 
ns.getNamespaceURI());
+                break;
+
+            case XMLStreamConstants.SPACE:
+            case XMLStreamConstants.NOTATION_DECLARATION:
+            case XMLStreamConstants.ENTITY_DECLARATION:
+            default:
+                throw new XMLStreamException("Illegal event");
+        }
+    }
+
+    public void add(XMLEventReader reader) throws XMLStreamException {
+        while (reader.hasNext()) {
+            add(reader.nextEvent());
+        }
+    }
+
+    public void close() throws XMLStreamException {
+        this.xmlStreamWriter.close();
+    }
+
+    public void flush() throws XMLStreamException {
+        this.xmlStreamWriter.flush();
+    }
+
+    public NamespaceContext getNamespaceContext() {
+        return this.xmlStreamWriter.getNamespaceContext();
+    }
+
+    public String getPrefix(String uri) throws XMLStreamException {
+        return this.xmlStreamWriter.getPrefix(uri);
+    }
+
+    public void setDefaultNamespace(String uri) throws XMLStreamException {
+        this.xmlStreamWriter.setDefaultNamespace(uri);
+    }
+
+    public void setNamespaceContext(NamespaceContext namespaceContext) throws 
XMLStreamException {
+        this.xmlStreamWriter.setNamespaceContext(namespaceContext);
+    }
+
+    public void setPrefix(String prefix, String uri) throws XMLStreamException 
{
+        this.xmlStreamWriter.setPrefix(prefix, uri);
+    }
+}

Propchange: 
webservices/wss4j/branches/swssf/streaming-xml-security/src/main/java/org/swssf/xmlsec/impl/XMLSecurityEventWriter.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: 
webservices/wss4j/branches/swssf/streaming-xml-security/src/main/java/org/swssf/xmlsec/impl/processor/output/FinalOutputProcessor.java
URL: 
http://svn.apache.org/viewvc/webservices/wss4j/branches/swssf/streaming-xml-security/src/main/java/org/swssf/xmlsec/impl/processor/output/FinalOutputProcessor.java?rev=1195419&r1=1195418&r2=1195419&view=diff
==============================================================================
--- 
webservices/wss4j/branches/swssf/streaming-xml-security/src/main/java/org/swssf/xmlsec/impl/processor/output/FinalOutputProcessor.java
 (original)
+++ 
webservices/wss4j/branches/swssf/streaming-xml-security/src/main/java/org/swssf/xmlsec/impl/processor/output/FinalOutputProcessor.java
 Mon Oct 31 11:33:14 2011
@@ -19,10 +19,12 @@
 package org.swssf.xmlsec.impl.processor.output;
 
 import org.swssf.xmlsec.ext.*;
+import org.swssf.xmlsec.impl.XMLSecurityEventWriter;
 
 import javax.xml.stream.XMLEventWriter;
 import javax.xml.stream.XMLOutputFactory;
 import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
 import javax.xml.stream.events.XMLEvent;
 import java.io.OutputStream;
 
@@ -42,7 +44,9 @@ public class FinalOutputProcessor extend
         xmlOutputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, 
false);
     }
 
-    public FinalOutputProcessor(OutputStream outputStream, String encoding, 
XMLSecurityProperties securityProperties, XMLSecurityConstants.Action action) 
throws XMLSecurityException {
+    public FinalOutputProcessor(OutputStream outputStream, String encoding,
+                                XMLSecurityProperties securityProperties,
+                                XMLSecurityConstants.Action action) throws 
XMLSecurityException {
         super(securityProperties, action);
         setPhase(XMLSecurityConstants.Phase.POSTPROCESSING);
         try {
@@ -52,6 +56,14 @@ public class FinalOutputProcessor extend
         }
     }
 
+    public FinalOutputProcessor(XMLStreamWriter xmlStreamWriter,
+                                XMLSecurityProperties securityProperties,
+                                XMLSecurityConstants.Action action) throws 
XMLSecurityException {
+        super(securityProperties, action);
+        setPhase(XMLSecurityConstants.Phase.POSTPROCESSING);
+        this.xmlEventWriter = new XMLSecurityEventWriter(xmlStreamWriter);
+    }
+
     @Override
     public void processEvent(XMLEvent xmlEvent, OutputProcessorChain 
outputProcessorChain) throws XMLStreamException, XMLSecurityException {
         xmlEventWriter.add(xmlEvent);

Added: 
webservices/wss4j/branches/swssf/streaming-xml-security/src/test/java/org/swssf/xmlsec/test/XMLSecurityEventWriterTest.java
URL: 
http://svn.apache.org/viewvc/webservices/wss4j/branches/swssf/streaming-xml-security/src/test/java/org/swssf/xmlsec/test/XMLSecurityEventWriterTest.java?rev=1195419&view=auto
==============================================================================
--- 
webservices/wss4j/branches/swssf/streaming-xml-security/src/test/java/org/swssf/xmlsec/test/XMLSecurityEventWriterTest.java
 (added)
+++ 
webservices/wss4j/branches/swssf/streaming-xml-security/src/test/java/org/swssf/xmlsec/test/XMLSecurityEventWriterTest.java
 Mon Oct 31 11:33:14 2011
@@ -0,0 +1,58 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.swssf.xmlsec.test;
+
+import org.custommonkey.xmlunit.XMLAssert;
+import org.swssf.xmlsec.impl.XMLSecurityEventWriter;
+import org.testng.annotations.Test;
+
+import javax.xml.stream.*;
+import javax.xml.stream.events.XMLEvent;
+import java.io.StringWriter;
+
+/**
+ * @author $Author$
+ * @version $Revision$ $Date$
+ */
+public class XMLSecurityEventWriterTest {
+
+    @Test
+    public void testConformness() throws Exception {
+        XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
+        StringWriter secStringWriter = new StringWriter();
+        XMLStreamWriter secXmlStreamWriter = 
xmlOutputFactory.createXMLStreamWriter(secStringWriter);
+        XMLSecurityEventWriter xmlSecurityEventWriter = new 
XMLSecurityEventWriter(secXmlStreamWriter);
+
+        StringWriter stdStringWriter = new StringWriter();
+        XMLEventWriter stdXmlEventWriter = 
xmlOutputFactory.createXMLEventWriter(stdStringWriter);
+
+        XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
+        XMLEventReader xmlEventReader = 
xmlInputFactory.createXMLEventReader(this.getClass().getClassLoader().getResourceAsStream("testdata/plain-soap-1.1.xml"));
+
+        while (xmlEventReader.hasNext()) {
+            XMLEvent xmlEvent = xmlEventReader.nextEvent();
+            xmlSecurityEventWriter.add(xmlEvent);
+            stdXmlEventWriter.add(xmlEvent);
+        }
+
+        xmlSecurityEventWriter.close();
+        stdXmlEventWriter.close();
+        XMLAssert.assertXMLEqual(stdStringWriter.toString(), 
secStringWriter.toString());
+    }
+}

Propchange: 
webservices/wss4j/branches/swssf/streaming-xml-security/src/test/java/org/swssf/xmlsec/test/XMLSecurityEventWriterTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision


Reply via email to