You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by th...@apache.org on 2005/06/20 15:51:46 UTC

svn commit: r191467 - in /webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom: OMTextImpl.java mtom/MTOMStAXSOAPModelBuilder.java mtom/MTOMXMLStreamWriter.java

Author: thilina
Date: Mon Jun 20 06:51:44 2005
New Revision: 191467

URL: http://svn.apache.org/viewcvs?rev=191467&view=rev
Log:
commiting OM MTOM stuff 

Added:
    webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/mtom/MTOMStAXSOAPModelBuilder.java
    webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/mtom/MTOMXMLStreamWriter.java
Modified:
    webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/OMTextImpl.java

Modified: webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/OMTextImpl.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/OMTextImpl.java?rev=191467&r1=191466&r2=191467&view=diff
==============================================================================
--- webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/OMTextImpl.java (original)
+++ webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/OMTextImpl.java Mon Jun 20 06:51:44 2005
@@ -15,37 +15,89 @@
  */
 package org.apache.axis.om.impl.llom;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Date;
+import java.util.Random;
+
+import javax.activation.DataHandler;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
 
-import org.apache.axis.om.DataHandler;
+import org.apache.axis.attachments.Base64;
+import org.apache.axis.attachments.ByteArrayDataSource;
+import org.apache.axis.attachments.IOUtils;
+import org.apache.axis.om.OMAttribute;
 import org.apache.axis.om.OMConstants;
 import org.apache.axis.om.OMElement;
 import org.apache.axis.om.OMException;
+import org.apache.axis.om.OMNamespace;
 import org.apache.axis.om.OMNode;
 import org.apache.axis.om.OMText;
+import org.apache.axis.om.OMXMLParserWrapper;
+import org.apache.axis.om.impl.llom.mtom.MTOMStAXSOAPModelBuilder;
+import org.apache.axis.om.impl.llom.mtom.MTOMXMLStreamWriter;
 
 /**
  * Class OMTextImpl
  */
+/**
+ * @author Thilina
+ * @date Jun 20, 2005
+ */
+/**
+ * @author Thilina
+ * @date Jun 20, 2005
+ */
 public class OMTextImpl extends OMNodeImpl implements OMText, OMConstants {
-
-    protected String value;
+    
+    protected String value = null;
+    
     protected short textType = TEXT_NODE;
+    
     protected String mimeType;
-
-    public OMTextImpl(String s, String mimeType) {
-        this(s);
-        this.mimeType = mimeType;
-    }
-    public OMTextImpl(OMElement parent, String s, String mimeType) {
-        this(parent, s);
-        this.mimeType = mimeType;
+    
+    protected boolean isBinary = false;
+    
+    /**
+     * Field contentID for the mime part used when serialising Binary stuff as
+     * MTOM optimised
+     */
+    private String contentID = null;
+    
+    /**
+     * Field dataHandler
+     */
+    private DataHandler dataHandler = null;
+    
+    /**
+     * Field nameSpace used when serialising Binary stuff as MTOM optimised
+     */
+    protected OMNamespace ns = new OMNamespaceImpl(
+            "http://www.w3.org/2004/08/xop/Include", "xop");
+    
+    /**
+     * Field localName used when serialising Binary stuff as MTOM optimised
+     */
+    protected String localName = "Include";
+    
+    /**
+     * Field attributes used when serialising Binary stuff as MTOM optimised
+     */
+    protected OMAttribute attribute;
+    
+    /**
+     * Constructor OMTextImpl
+     * 
+     * @param s
+     */
+    public OMTextImpl(String s) {
+        this.value = s;
     }
-
+    
     /**
      * Constructor OMTextImpl
-     *
+     * 
      * @param parent
      * @param text
      */
@@ -54,31 +106,85 @@
         this.value = text;
         done = true;
     }
-
+    
     /**
-     * Constructor OMTextImpl
-     *
-     * @param s
+     * @param s -
+     *            base64 encoded String representation of Binary
+     * @param mimeType
+     *            of the Binary
      */
-    public OMTextImpl(String s) {
-        this.value = s;
+    public OMTextImpl(String s, String mimeType) {
+        this(s);
+        this.mimeType = mimeType;
     }
-
+    
     /**
-     * @return
-     * @throws org.apache.axis.om.OMException
-     * @throws OMException
+     * @param parent
+     * @param s -
+     *            base64 encoded String representation of Binary
+     * @param mimeType
+     *            of the Binary
+     */
+    public OMTextImpl(OMElement parent, String s, String mimeType) {
+        this(parent, s);
+        this.mimeType = mimeType;
+    }
+    
+    /**
+     * @param dataHandler
+     *            To send binary optimised content Created programatically
+     */
+    public OMTextImpl(DataHandler dataHandler) {
+        this.dataHandler = dataHandler;
+        if (this.contentID == null) {
+            // We can use a UUID, taken using Apache commons id project.
+            // TODO change to UUID
+            this.contentID = String.valueOf(new Random(new Date().getTime())
+                    .nextLong());
+            this.isBinary = true;
+        }
+    }
+    
+    /**
+     * @param contentID
+     * @param parent
+     * @param builder
+     *            Used when the builder is encountered with a XOP:Include tag
+     *            Stores a reference to the builder and the content-id. Supports
+     *            deffered parsing of MIME messages
+     */
+    public OMTextImpl(String contentID, OMElement parent,OMXMLParserWrapper builder) {
+        super(parent);
+        this.contentID = contentID;
+        this.isBinary = true;
+        this.builder = builder;
+    }
+    
+    /**
+     * We use the OMText class to hold comments, text, characterData, CData,
+     * etc., The codes are found in OMNode class
+     * 
+     * @param type
      */
+    public void setTextType(short type) {
+        if ((type == TEXT_NODE) || (type == COMMENT_NODE)
+                || (type == CDATA_SECTION_NODE)) {
+            this.textType = type;
+        } else {
+            throw new UnsupportedOperationException("Attempt to set wrong type");
+        }
+    }
+    
     public int getType() throws OMException {
         return textType;
     }
-
+    
     /**
      * @param writer
      * @throws XMLStreamException
      */
     public void serializeWithCache(XMLStreamWriter writer)
-        throws XMLStreamException {
+    throws XMLStreamException {
         if (textType == TEXT_NODE) {
             writer.writeCharacters(this.value);
         } else if (textType == COMMENT_NODE) {
@@ -91,54 +197,249 @@
             nextSibling.serializeWithCache(writer);
         }
     }
-
-    public void serialize(XMLStreamWriter writer) throws XMLStreamException {
-        this.serializeWithCache(writer);
+    
+    /**
+     * Returns the value
+     * 
+     * @return
+     */
+    public String getText() throws OMException {
+        if (!isBinary) {
+            return this.value;
+        } else {
+            try {
+                InputStream inStream;
+                inStream = this.getInputStream();
+                byte[] data;
+                data = new byte[inStream.available()];
+                
+                IOUtils.readFully(inStream, data);
+                return Base64.encode(data);
+            } catch (Exception e) {
+                throw new OMException(
+                        "Cannot read from Stream taken form the Data Handler"
+                        + e);
+            }
+        }
+        
     }
-
+    
+    public boolean isOptimised() {
+        return isBinary;
+    }
+    
     /**
-     * Slightly different implementation of the discard method 
+     * @return
+     * @throws org.apache.axis.om.OMException
      * @throws OMException
      */
-    public void discard() throws OMException {
-        if (done) {
-            this.detach();
+    public DataHandler getDataHandler() {
+        
+        /*
+         * this should return a DataHandler containing the binary data
+         * reperesented by the Base64 strings stored in OMText
+         */
+        if (isBinary) {
+            if (dataHandler == null) {
+                dataHandler = ((MTOMStAXSOAPModelBuilder) builder)
+                .getDataHandler(contentID);
+            }
+            return dataHandler;
+        }
+        if (value != null) {
+            ByteArrayDataSource dataSource;
+            byte[] data = Base64.decode(value);
+            if (mimeType != null) {
+                dataSource = new ByteArrayDataSource(data, mimeType);
+            } else {
+                // Assumes type as application/octet-stream
+                dataSource = new ByteArrayDataSource(data);
+            }
+            DataHandler dataHandler = new DataHandler(dataSource);
+            return dataHandler;
+        }
+        return null;
+        
+    }
+    
+   
+    public String getLocalName() {
+        return localName;
+    }
+    
+    public java.io.InputStream getInputStream() throws OMException {
+        if (dataHandler == null) {
+            getDataHandler();
+        }
+        InputStream inStream;
+        try {
+            inStream = dataHandler.getDataSource().getInputStream();
+        } catch (IOException e) {
+            throw new OMException("Cannot get InoutStream from DataHandler."
+                    + e);
+        }
+        return inStream;
+    }
+    
+    public String getContentID() {
+        return this.contentID;
+    }
+    
+    public boolean isComplete() {
+        return true;
+    }
+    
+    public void serialize(XMLStreamWriter writer) throws XMLStreamException {
+        boolean firstElement = false;
+        
+        if (!this.isBinary) {
+            serializeWithCache(writer);
         } else {
-            builder.discard(this.parent);
+            if (writer instanceof MTOMXMLStreamWriter) {
+                // send binary as MTOM optimised
+                MTOMXMLStreamWriter mtomWriter = (MTOMXMLStreamWriter) writer;
+                this.attribute = new OMAttributeImpl("href",
+                        new OMNamespaceImpl("", ""), "cid:"
+                        + this.contentID.trim());
+                
+                this.serializeStartpart(mtomWriter);
+                mtomWriter.writeOptimised(this);
+                mtomWriter.writeEndElement();
+            } else {
+                
+                writer.writeCharacters(this.getText());
+            }
+            
+            OMNode nextSibling = this.getNextSibling();
+            if (nextSibling != null) {
+                // serilize next sibling
+                nextSibling.serialize(writer);
+            } else {
+                // TODO : See whether following part is really needed
+                if (parent == null) {
+                    return;
+                } else if (parent.isComplete()) {
+                    return;
+                } else {
+                    // do the special serialization
+                    // Only the push serializer is left now
+                    builder.next();
+                }
+                
+            }
         }
+        
     }
-
+    
+    /*
+     * Methods to copy from OMSerialize utils
+     */
+    private void serializeStartpart(XMLStreamWriter writer)
+    throws XMLStreamException {
+        String nameSpaceName = null;
+        String writer_prefix = null;
+        String prefix = null;
+        if (this.ns != null) {
+            nameSpaceName = this.ns.getName();
+            writer_prefix = writer.getPrefix(nameSpaceName);
+            prefix = this.ns.getPrefix();
+            if (nameSpaceName != null) {
+                if (writer_prefix != null) {
+                    writer
+                    .writeStartElement(nameSpaceName, this
+                            .getLocalName());
+                } else {
+                    if (prefix != null) {
+                        writer.writeStartElement(prefix, this.getLocalName(),
+                                nameSpaceName);
+                        writer.writeNamespace(prefix, nameSpaceName);
+                        writer.setPrefix(prefix, nameSpaceName);
+                    } else {
+                        writer.writeStartElement(nameSpaceName, this
+                                .getLocalName());
+                        writer.writeDefaultNamespace(nameSpaceName);
+                        writer.setDefaultNamespace(nameSpaceName);
+                    }
+                }
+            } else {
+                writer.writeStartElement(this.getLocalName());
+                
+            }
+        } else {
+            writer.writeStartElement(this.getLocalName());
+            
+        }
+        
+        // add the elements attribute "href"
+        serializeAttribute(this.attribute, writer);
+        
+        // add the namespace
+        serializeNamespace(this.ns, writer);
+        
+    }
+    
     /**
-     * Returns the value
-     * @return
+     * Method serializeAttribute
+     * 
+     * @param attr
+     * @param writer
+     * @throws XMLStreamException
      */
-    public String getText() {
-        return this.value;
+    static void serializeAttribute(OMAttribute attr, XMLStreamWriter writer)
+    throws XMLStreamException {
+        
+        // first check whether the attribute is associated with a namespace
+        OMNamespace ns = attr.getNamespace();
+        String prefix = null;
+        String namespaceName = null;
+        if (ns != null) {
+            
+            // add the prefix if it's availble
+            prefix = ns.getPrefix();
+            namespaceName = ns.getName();
+            if (prefix != null) {
+                writer.writeAttribute(prefix, namespaceName, attr
+                        .getLocalName(), attr.getValue());
+            } else {
+                writer.writeAttribute(namespaceName, attr.getLocalName(), attr
+                        .getValue());
+            }
+        } else {
+            writer.writeAttribute(attr.getLocalName(), attr.getValue());
+        }
     }
-
-    public boolean isOptimized() {
-        return false; //Todo
+    
+    /**
+     * Method serializeNamespace
+     * 
+     * @param namespace
+     * @param writer
+     * @throws XMLStreamException
+     */
+    static void serializeNamespace(OMNamespace namespace, XMLStreamWriter writer)
+    throws XMLStreamException {
+        if (namespace != null) {
+            String uri = namespace.getName();
+            String prefix = writer.getPrefix(uri);
+            String ns_prefix = namespace.getPrefix();
+            if (prefix == null) {
+                writer.writeNamespace(ns_prefix, namespace.getName());
+                writer.setPrefix(ns_prefix, uri);
+            }
+        }
     }
-
+    
     /**
-     * @return
-     * @throws org.apache.axis.om.OMException
+     * Slightly different implementation of the discard method
+     * 
      * @throws OMException
      */
-    public DataHandler getDataHandler() throws OMException {
-        try {
-            if ((value = getText()) != null) {
-                DataHandler dh =
-                    (DataHandler) (Class
-                        .forName("org.apache.axis.om.impl.llom.mtom.DataHandlerImpl"))
-                        .newInstance();
-                dh.init(value, mimeType);
-            }
-        } catch (Exception e) {
-            throw new OMException(e);
+    public void discard() throws OMException {
+        if (done) {
+            this.detach();
+        } else {
+            builder.discard(this.parent);
         }
-        return null;
-
     }
-
-}
+    
+}
\ No newline at end of file

Added: webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/mtom/MTOMStAXSOAPModelBuilder.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/mtom/MTOMStAXSOAPModelBuilder.java?rev=191467&view=auto
==============================================================================
--- webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/mtom/MTOMStAXSOAPModelBuilder.java (added)
+++ webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/mtom/MTOMStAXSOAPModelBuilder.java Mon Jun 20 06:51:44 2005
@@ -0,0 +1,238 @@
+/**  
+ * Copyright 2001-2004 The Apache Software Foundation.
+ * <p/>
+ * Licensed 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.
+ * <p/>
+ */
+package org.apache.axis.om.impl.llom.mtom;
+
+import java.util.LinkedList;
+import java.util.ListIterator;
+
+import javax.activation.DataHandler;
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeBodyPart;
+import javax.mail.internet.MimeMessage;
+import javax.mail.internet.MimeMultipart;
+import javax.mail.internet.MimePartDataSource;
+import javax.xml.stream.XMLStreamReader;
+
+import org.apache.axis.attachments.MIMEHelper;
+import org.apache.axis.om.OMAttribute;
+import org.apache.axis.om.OMElement;
+import org.apache.axis.om.OMException;
+import org.apache.axis.om.OMNode;
+import org.apache.axis.om.OMText;
+import org.apache.axis.om.impl.llom.OMTextImpl;
+import org.apache.axis.soap.SOAPFactory;
+import org.apache.axis.soap.impl.llom.builder.StAXSOAPModelBuilder;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * @author Thilina Gunarathne thilina@opensource.lk
+ */
+public class MTOMStAXSOAPModelBuilder extends StAXSOAPModelBuilder {
+	private Log log = LogFactory.getLog(getClass());
+
+	/**
+	 * <code>mimeHelper</code> handles deffered parsing of incoming MIME
+	 * Messages
+	 */
+	MIMEHelper mimeHelper;
+
+	LinkedList mimeBodyPartsList;
+
+	MimeMessage mimeMessage;
+
+	int partIndex = 0;
+
+	
+	public MTOMStAXSOAPModelBuilder(XMLStreamReader parser,SOAPFactory factory, MIMEHelper mimeHelper)
+	{
+		super(parser,factory);
+		this.mimeHelper=  mimeHelper;
+	}
+	
+
+	/**
+	 * @param reader
+	 * @param mimeHelper2
+	 */
+	public MTOMStAXSOAPModelBuilder(XMLStreamReader reader, MIMEHelper mimeHelper) {
+		super(reader);
+		this.mimeHelper = mimeHelper;
+	}
+
+	/*
+	 * private XMLStreamReader getParserFromMime() throws OMException {
+	 * mimeBodyPartsList = new LinkedList(); Properties props = new
+	 * Properties(); javax.mail.Session session =
+	 * javax.mail.Session.getInstance(props, null); try { mimeMessage = new
+	 * MimeMessage(session, inStream);
+	 * 
+	 * MimeBodyPart rootMimeBodyPart;
+	 * 
+	 * rootMimeBodyPart = getRootMimeBodyPart(); return
+	 * XMLInputFactory.newInstance().createXMLStreamReader(
+	 * rootMimeBodyPart.getInputStream());
+	 *  } catch (IOException e1) { throw new OMException(e1.toString());
+	 *  } catch (MessagingException e) { throw new OMException( "Message
+	 * identified as MTOM optimised doesn't contain a valid MIME Stream" +
+	 * e.toString()); } catch (XMLStreamException e) { throw new OMException(
+	 * "SOAP Message contained within Mime root is invalid " + e.toString()); }
+	 * catch (FactoryConfigurationError e) { throw new OMException( "SOAP
+	 * Message contained within Mime root is invalid" + e.toString()); }
+	 *  }
+	 */
+	protected OMNode createOMElement() throws OMException {
+
+		String elementName = parser.getLocalName();
+
+		String namespaceURI = parser.getNamespaceURI();
+
+		// create an OMBlob if the element is an <xop:Include>
+		if (elementName.equalsIgnoreCase("Include")
+				& namespaceURI
+						.equalsIgnoreCase("http://www.w3.org/2004/08/xop/include")) {
+
+			OMText node;
+			String contentID = null;
+			String contentIDName = null;
+			OMAttribute Attr;
+			if (lastNode == null) {
+				// Decide whether to ckeck the level >3 or not
+				throw new OMException(
+						"XOP:Include element is not supported here");
+			}
+			if (parser.getAttributeCount() > 0) {
+				contentID = parser.getAttributeValue(0);
+				contentID = contentID.trim();
+				contentIDName = parser.getAttributeLocalName(0);
+				if (contentIDName.equalsIgnoreCase("href")
+						& contentID.substring(0, 3).equalsIgnoreCase("cid")) {
+					contentID = contentID.substring(4);
+				} else {
+					throw new OMException(
+							"contentID not Found in XOP:Include element");
+				}
+			} else {
+				throw new OMException(
+						"Href attribute not found in XOP:Include element");
+			}
+
+			if (lastNode.isComplete()) {
+				node = new OMTextImpl(contentID, lastNode.getParent(),this);
+				lastNode.setNextSibling(node);
+				node.setPreviousSibling(lastNode);
+			} else {
+				OMElement e = (OMElement) lastNode;
+				node = new OMTextImpl(contentID, (OMElement) lastNode, this);
+				e.setFirstChild(node);
+			}
+			return node;
+
+		} else {
+			OMElement node;
+			if (lastNode == null) {
+				node = constructNode(null, elementName, true);
+			} else if (lastNode.isComplete()) {
+				node = constructNode(lastNode.getParent(), elementName, false);
+				lastNode.setNextSibling(node);
+				node.setPreviousSibling(lastNode);
+			} else {
+				OMElement e = (OMElement) lastNode;
+				node = constructNode((OMElement) lastNode, elementName, false);
+				e.setFirstChild(node);
+			}
+
+			// fill in the attributes
+			processAttributes(node);
+			log.info("Build the OMElelment {" + node.getLocalName() + '}'
+					+ node.getLocalName() + "By the StaxSOAPModelBuilder");
+			return node;
+		}
+	}
+
+	public DataHandler getDataHandler(String blobContentID) throws OMException {
+		/*
+		 * First checks whether the part is already parsed by checking the parts
+		 * linked list. If it is not parsed yet then call the getnextPart() till
+		 * we find the required part.
+		 */
+		MimeBodyPart mimeBodyPart;
+
+		boolean attachmentFound = false;
+		ListIterator partsIterator = mimeBodyPartsList.listIterator();
+		try {
+			while (partsIterator.hasNext()) {
+				mimeBodyPart = (MimeBodyPart) partsIterator.next();
+				if (blobContentID.equals(mimeBodyPart.getContentID())) {
+					attachmentFound = true;
+					DataHandler dh = mimeBodyPart.getDataHandler();
+					return dh;
+				}
+			}
+			while (!attachmentFound) {
+				mimeBodyPart = this.getNextMimeBodyPart();
+
+				if (mimeBodyPart == null) {
+					break;
+				}
+				String partContentID = mimeBodyPart.getContentID();
+				String delimitedBlobContentID = "<" + blobContentID + ">";
+				if (delimitedBlobContentID.equals(partContentID)) {
+					attachmentFound = true;
+					DataHandler dh = mimeBodyPart.getDataHandler();
+					return dh;
+				}
+			}
+			return null;
+		} catch (MessagingException e) {
+			throw new OMException("Invalid Mime Message " + e.toString());
+		}
+	}
+
+	private MimeBodyPart getMimeBodyPart() throws MessagingException {
+		MimeBodyPart mimeBodyPart = null;
+
+		DataHandler dh = mimeMessage.getDataHandler();
+		MimeMultipart multiPart = new MimeMultipart((MimePartDataSource) dh
+				.getDataSource());
+		mimeBodyPart = (MimeBodyPart) multiPart.getBodyPart(partIndex);
+
+		partIndex++;
+		return mimeBodyPart;
+	}
+
+	private MimeBodyPart getRootMimeBodyPart() throws MessagingException {
+		MimeBodyPart rootMimeBodyPart;
+		if (mimeBodyPartsList.isEmpty()) {
+			rootMimeBodyPart = getMimeBodyPart();
+			mimeBodyPartsList.add(rootMimeBodyPart);
+		} else {
+			rootMimeBodyPart = (MimeBodyPart) mimeBodyPartsList.getFirst();
+		}
+		return rootMimeBodyPart;
+	}
+
+	private MimeBodyPart getNextMimeBodyPart() throws MessagingException {
+		MimeBodyPart nextMimeBodyPart;
+		nextMimeBodyPart = getMimeBodyPart();
+		if (nextMimeBodyPart != null) {
+			mimeBodyPartsList.add(nextMimeBodyPart);
+			return nextMimeBodyPart;
+		} else
+			return null;
+	}
+}
\ No newline at end of file

Added: webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/mtom/MTOMXMLStreamWriter.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/mtom/MTOMXMLStreamWriter.java?rev=191467&view=auto
==============================================================================
--- webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/mtom/MTOMXMLStreamWriter.java (added)
+++ webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/mtom/MTOMXMLStreamWriter.java Mon Jun 20 06:51:44 2005
@@ -0,0 +1,439 @@
+/**  
+ * Copyright 2001-2004 The Apache Software Foundation.
+ * <p/>
+ * Licensed 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.
+ * <p/>
+ */
+package org.apache.axis.om.impl.llom.mtom;
+
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStream;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.Properties;
+import java.util.Random;
+
+import javax.activation.DataHandler;
+import javax.mail.internet.MimeBodyPart;
+import javax.mail.internet.MimeMultipart;
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.stream.FactoryConfigurationError;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.axis.om.OMText;
+
+
+/**
+ * @author Thilina Gunarathne thilina@opensource.lk
+ */
+public class MTOMXMLStreamWriter implements XMLStreamWriter {
+
+    OutputStream outStream;
+
+    XMLStreamWriter writer;
+
+    Random rnd;
+
+    LinkedList binaryNodeList;
+
+    ByteArrayOutputStream bufferedSoapOutStream;
+
+    public static String[] filter = new String[] { "Message-ID" }; // to filter
+                                                                   // the
+                                                                   // message ID
+                                                                   // header
+
+    public MTOMXMLStreamWriter(OutputStream outStream)
+            throws XMLStreamException, FactoryConfigurationError {
+        super();
+        this.outStream = outStream;
+
+        bufferedSoapOutStream = new ByteArrayOutputStream();
+
+        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(
+                bufferedSoapOutStream);
+        binaryNodeList = new LinkedList();
+        rnd = new Random();
+
+    }
+
+    public void writeOptimised(OMText node) {
+        binaryNodeList.add(node);
+    }
+
+    private MimeBodyPart createMimeBodyPart(OMText node)
+            throws Exception {
+        MimeBodyPart mimeBodyPart = new MimeBodyPart();
+        mimeBodyPart.setDataHandler(node.getDataHandler());
+        mimeBodyPart.addHeader("Content-Transfer-Encoding", "binary");
+        mimeBodyPart.addHeader("Content-ID", "<" + node.getContentID()+ ">");
+        return mimeBodyPart;
+
+    }
+
+    public void complete() throws Exception {
+        DataHandler dh = new DataHandler(bufferedSoapOutStream.toString(),
+                "text/xml");
+        MimeBodyPart mimeBodyPart = new MimeBodyPart();
+        mimeBodyPart.setDataHandler(dh);
+        mimeBodyPart.addHeader("Content-Type", "application/xop+xml");
+        mimeBodyPart.addHeader("Content-Transfer-Encoding", "8bit");
+        String contentID = "<http://example.org/my.hsh>";
+        mimeBodyPart.addHeader("Content-ID", contentID);
+
+        Properties props = new Properties();
+        javax.mail.Session session = javax.mail.Session
+                .getInstance(props, null);
+        javax.mail.internet.MimeMessage mimeMessage = new javax.mail.internet.MimeMessage(
+                session);
+        MimeMultipart multipartMessage = new MimeMultipart("Related");
+        multipartMessage.addBodyPart(mimeBodyPart);
+
+        Iterator binaryNodeIterator = binaryNodeList.iterator();
+        while (binaryNodeIterator.hasNext()) {
+            OMText binaryNode = (OMText) binaryNodeIterator.next();
+            multipartMessage
+                    .addBodyPart(createMimeBodyPart(binaryNode));
+        }
+        mimeMessage.setContent(multipartMessage);
+        mimeMessage.writeTo(outStream, filter);
+    }
+
+    public void writeStartElement(String localName) throws XMLStreamException {
+        writer.writeStartElement(localName);
+    }
+
+    public void writeStartElement(String namespaceURI, String localName)
+            throws XMLStreamException {
+        writer.writeStartElement(namespaceURI, localName);
+
+    }
+
+    public void writeStartElement(String prefix, String localName,
+            String namespaceURI) throws XMLStreamException {
+        writer.writeStartElement(prefix, localName, namespaceURI);
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#writeEmptyElement(java.lang.String,
+     *      java.lang.String)
+     */
+    public void writeEmptyElement(String namespaceURI, String localName)
+            throws XMLStreamException {
+        writer.writeEmptyElement(namespaceURI, localName);
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#writeEmptyElement(java.lang.String,
+     *      java.lang.String, java.lang.String)
+     */
+    public void writeEmptyElement(String prefix, String localName,
+            String namespaceURI) throws XMLStreamException {
+        writer.writeEmptyElement(prefix, localName, namespaceURI);
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#writeEmptyElement(java.lang.String)
+     */
+    public void writeEmptyElement(String localName) throws XMLStreamException {
+        writer.writeEmptyElement(localName);
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#writeEndElement()
+     */
+    public void writeEndElement() throws XMLStreamException {
+        writer.writeEndElement();
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#writeEndDocument()
+     */
+    public void writeEndDocument() throws XMLStreamException {
+        writer.writeEndDocument();
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#close()
+     */
+    public void close() throws XMLStreamException {
+        writer.close();
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#flush()
+     */
+    public void flush() throws XMLStreamException {
+        writer.flush();
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#writeAttribute(java.lang.String,
+     *      java.lang.String)
+     */
+    public void writeAttribute(String localName, String value)
+            throws XMLStreamException {
+        writer.writeAttribute(localName, value);
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#writeAttribute(java.lang.String,
+     *      java.lang.String, java.lang.String, java.lang.String)
+     */
+    public void writeAttribute(String prefix, String namespaceURI,
+            String localName, String value) throws XMLStreamException {
+        writer.writeAttribute(prefix, namespaceURI, localName, value);
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#writeAttribute(java.lang.String,
+     *      java.lang.String, java.lang.String)
+     */
+    public void writeAttribute(String namespaceURI, String localName,
+            String value) throws XMLStreamException {
+        writer.writeAttribute(namespaceURI, localName, value);
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#writeNamespace(java.lang.String,
+     *      java.lang.String)
+     */
+    public void writeNamespace(String prefix, String namespaceURI)
+            throws XMLStreamException {
+        writer.writeNamespace(prefix, namespaceURI);
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#writeDefaultNamespace(java.lang.String)
+     */
+    public void writeDefaultNamespace(String namespaceURI)
+            throws XMLStreamException {
+        writer.writeDefaultNamespace(namespaceURI);
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#writeComment(java.lang.String)
+     */
+    public void writeComment(String data) throws XMLStreamException {
+        writer.writeComment(data);
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#writeProcessingInstruction(java.lang.String)
+     */
+    public void writeProcessingInstruction(String target)
+            throws XMLStreamException {
+        writer.writeProcessingInstruction(target);
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#writeProcessingInstruction(java.lang.String,
+     *      java.lang.String)
+     */
+    public void writeProcessingInstruction(String target, String data)
+            throws XMLStreamException {
+        writer.writeProcessingInstruction(target, data);
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#writeCData(java.lang.String)
+     */
+    public void writeCData(String data) throws XMLStreamException {
+        writer.writeCData(data);
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#writeDTD(java.lang.String)
+     */
+    public void writeDTD(String dtd) throws XMLStreamException {
+        writer.writeDTD(dtd);
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#writeEntityRef(java.lang.String)
+     */
+    public void writeEntityRef(String name) throws XMLStreamException {
+        writer.writeEntityRef(name);
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#writeStartDocument()
+     */
+    public void writeStartDocument() throws XMLStreamException {
+        writer.writeStartDocument();
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#writeStartDocument(java.lang.String)
+     */
+    public void writeStartDocument(String version) throws XMLStreamException {
+        writer.writeStartDocument(version);
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#writeStartDocument(java.lang.String,
+     *      java.lang.String)
+     */
+    public void writeStartDocument(String encoding, String version)
+            throws XMLStreamException {
+        writer.writeStartDocument(encoding, version);
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#writeCharacters(java.lang.String)
+     */
+    public void writeCharacters(String text) throws XMLStreamException {
+        writer.writeCharacters(text);
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#writeCharacters(char[], int, int)
+     */
+    public void writeCharacters(char[] text, int start, int len)
+            throws XMLStreamException {
+        writer.writeCharacters(text, start, len);
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#getPrefix(java.lang.String)
+     */
+    public String getPrefix(String uri) throws XMLStreamException {
+
+        return writer.getPrefix(uri);
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#setPrefix(java.lang.String,
+     *      java.lang.String)
+     */
+    public void setPrefix(String prefix, String uri) throws XMLStreamException {
+        writer.setPrefix(prefix, uri);
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#setDefaultNamespace(java.lang.String)
+     */
+    public void setDefaultNamespace(String uri) throws XMLStreamException {
+        writer.setDefaultNamespace(uri);
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#setNamespaceContext(javax.xml.namespace.NamespaceContext)
+     */
+    public void setNamespaceContext(NamespaceContext context)
+            throws XMLStreamException {
+        writer.setNamespaceContext(context);
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#getNamespaceContext()
+     */
+    public NamespaceContext getNamespaceContext() {
+
+        return writer.getNamespaceContext();
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see javax.xml.stream.XMLStreamWriter#getProperty(java.lang.String)
+     */
+    public Object getProperty(String name) throws IllegalArgumentException {
+
+        return writer.getProperty(name);
+    }
+}
\ No newline at end of file