You are viewing a plain text version of this content. The canonical link for it is here.
Posted to axis-cvs@ws.apache.org by ch...@apache.org on 2005/03/24 11:17:32 UTC
svn commit: r158906 [2/2] - in
webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2: ./ lib/ src/
src/java/ src/java/org/ src/java/org/apache/ src/java/org/apache/axis/
src/java/org/apache/axis/encoding/ src/java/org/apache/axis/om/
src/java/org/apache/axis/om/impl/ src/java/org/apache/axis/om/impl/llom/
src/java/org/apache/axis/om/impl/llom/builder/
src/java/org/apache/axis/om/impl/llom/mtom/
src/java/org/apache/axis/om/impl/llom/serialize/ src/test-resources/
src/test/ src/test/org/ src/test/org/apache/ src/test/org/apache/axis/
src/test/org/apache/axis/om/ src/test/org/apache/axis/om/impl/
src/test/org/apache/axis/om/impl/llom/
src/test/org/apache/axis/om/impl/llom/mtom/
Added: webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2/src/java/org/apache/axis/om/impl/llom/mtom/MTOMHelper.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2/src/java/org/apache/axis/om/impl/llom/mtom/MTOMHelper.java?view=auto&rev=158906
==============================================================================
--- webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2/src/java/org/apache/axis/om/impl/llom/mtom/MTOMHelper.java (added)
+++ webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2/src/java/org/apache/axis/om/impl/llom/mtom/MTOMHelper.java Thu Mar 24 02:17:28 2005
@@ -0,0 +1,172 @@
+package org.apache.axis.om.impl.llom.mtom;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.LinkedList;
+import java.util.ListIterator;
+import java.util.Properties;
+
+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.FactoryConfigurationError;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+import org.apache.axis.om.OMException;
+
+/**
+ * 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/>
+ */
+
+/**
+ * @author Thilina Gunarathne thilina@opensource.lk
+ */
+public class MTOMHelper {
+
+ InputStream inStream;
+
+ boolean mime;
+
+ LinkedList mimeBodyPartsList;
+
+ MimeMessage mimeMessage;
+
+ int partIndex = 0;
+
+ public MTOMHelper(InputStream inStream) throws OMException {
+ this.inStream = inStream;
+
+ mimeBodyPartsList = new LinkedList();
+
+ Properties props = new Properties();
+ javax.mail.Session session = javax.mail.Session
+ .getInstance(props, null);
+ try {
+ mimeMessage = new MimeMessage(session, inStream);
+ mime = true;
+ } catch (MessagingException e) {
+ mime = false;
+ throw new OMException(
+ "MTOM identified message doesn't contain valid MIME Stream");
+ }
+ }
+
+ 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 SOAP Message in Root Mime part "
+ + 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 getNextMimeBodyPart() throws MessagingException {
+ MimeBodyPart nextMimeBodyPart;
+ nextMimeBodyPart = getMimeBodyPart();
+ if (nextMimeBodyPart != null) {
+ mimeBodyPartsList.add(nextMimeBodyPart);
+ System.out.println("Next part parsed. NO of parts parsed :"
+ + mimeBodyPartsList.size());
+ return nextMimeBodyPart;
+ } else
+ return null;
+ }
+
+ public XMLStreamReader getParser() throws OMException, XMLStreamException, FactoryConfigurationError {
+
+ if (mime) {
+ MimeBodyPart rootMimeBodyPart;
+ try {
+ rootMimeBodyPart = getRootMimeBodyPart();
+ return XMLInputFactory.newInstance().createXMLStreamReader(
+ rootMimeBodyPart.getInputStream());
+
+ } catch (IOException e1) {
+ throw new OMException(e1.toString());
+
+ } catch (MessagingException e) {
+ throw new OMException("Unable to extract root MIME part "
+ + e.toString());
+ }
+
+ } else {
+ throw new OMException(
+ "MTOM identified message doesn't contain valid MIME Stream");
+ }
+ }
+
+ private MimeBodyPart getRootMimeBodyPart() throws MessagingException {
+ MimeBodyPart rootMimeBodyPart;
+ if (mimeBodyPartsList.isEmpty()) {
+ rootMimeBodyPart = getMimeBodyPart();
+ mimeBodyPartsList.add(rootMimeBodyPart);
+ } else {
+ rootMimeBodyPart = (MimeBodyPart) mimeBodyPartsList.getFirst();
+ }
+ System.out.println("MIME root parsed. NO of parts parsed :"
+ + mimeBodyPartsList.size());
+ return rootMimeBodyPart;
+ }
+
+}
\ No newline at end of file
Added: webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2/src/java/org/apache/axis/om/impl/llom/mtom/MTOMXMLStreamWriter.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2/src/java/org/apache/axis/om/impl/llom/mtom/MTOMXMLStreamWriter.java?view=auto&rev=158906
==============================================================================
--- webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2/src/java/org/apache/axis/om/impl/llom/mtom/MTOMXMLStreamWriter.java (added)
+++ webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2/src/java/org/apache/axis/om/impl/llom/mtom/MTOMXMLStreamWriter.java Thu Mar 24 02:17:28 2005
@@ -0,0 +1,448 @@
+package org.apache.axis.om.impl.llom.mtom;
+
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStream;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Properties;
+import java.util.Random;
+import java.util.Set;
+
+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.impl.llom.OMBlobImpl;
+
+/**
+ * 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/>
+ */
+
+/**
+ * @author Thilina Gunarathne thilina@opensource.lk
+ */
+public class MTOMXMLStreamWriter implements XMLStreamWriter {
+
+ OutputStream outStream;
+
+ XMLStreamWriter writer;
+
+ Random rnd;
+
+ HashMap attachmentsMap;
+
+ 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);
+ attachmentsMap = new HashMap();
+ rnd = new Random();
+
+ }
+
+ public String writeAttachment(OMBlobImpl blob) {
+
+ String contentID = "http://example.org/my.hsh"
+ + (System.currentTimeMillis());
+
+ attachmentsMap.put(contentID, blob);
+
+ return contentID;
+ }
+
+ public MimeBodyPart createMimeBodyPart(String contentID, OMBlobImpl blob)
+ throws Exception {
+ MimeBodyPart mimeBodyPart = new MimeBodyPart();
+ mimeBodyPart.setDataHandler(blob.getDataHandler());
+ mimeBodyPart.addHeader("Content-Transfer-Encoding", "binary");
+ mimeBodyPart.addHeader("Content-ID", "<" + contentID + ">");
+ 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);
+ Set attachmentKeys = attachmentsMap.keySet();
+
+ Iterator keyIterator = attachmentKeys.iterator();
+ while (keyIterator.hasNext()) {
+ String partContentID = (String) keyIterator.next();
+ OMBlobImpl blob = (OMBlobImpl) attachmentsMap.get(partContentID);
+ multipartMessage
+ .addBodyPart(createMimeBodyPart(partContentID, blob));
+ }
+ 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
Added: webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2/src/java/org/apache/axis/om/impl/llom/serialize/SimpleOMSerializer.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2/src/java/org/apache/axis/om/impl/llom/serialize/SimpleOMSerializer.java?view=auto&rev=158906
==============================================================================
--- webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2/src/java/org/apache/axis/om/impl/llom/serialize/SimpleOMSerializer.java (added)
+++ webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2/src/java/org/apache/axis/om/impl/llom/serialize/SimpleOMSerializer.java Thu Mar 24 02:17:28 2005
@@ -0,0 +1,232 @@
+package org.apache.axis.om.impl.llom.serialize;
+
+import java.io.ByteArrayOutputStream;
+import java.util.Iterator;
+import java.util.Vector;
+
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.axis.encoding.Base64;
+import org.apache.axis.om.OMAttribute;
+import org.apache.axis.om.OMElement;
+import org.apache.axis.om.OMNamespace;
+import org.apache.axis.om.OMNode;
+import org.apache.axis.om.OMText;
+import org.apache.axis.om.impl.llom.OMAttributeImpl;
+import org.apache.axis.om.impl.llom.OMBlobImpl;
+import org.apache.axis.om.impl.llom.OMNamespaceImpl;
+import org.apache.axis.om.impl.llom.mtom.MTOMXMLStreamWriter;
+
+/**
+ * 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.
+ */
+public class SimpleOMSerializer {
+
+ private Vector prefixList = new Vector();
+
+ public void serialize(Object omNode, XMLStreamWriter writer)
+ throws XMLStreamException {
+
+ if (!(omNode instanceof OMNode)) {
+ throw new UnsupportedOperationException(
+ "Unsupported input object. Must be of the the type OMNode");
+ }
+
+ OMNode node = (OMNode) omNode;
+ serializeNode(node, writer);
+ }
+
+ protected void serializeNode(OMNode node, XMLStreamWriter writer)
+ throws XMLStreamException {
+ short nodeType = node.getType();
+ if (nodeType == OMNode.ELEMENT_NODE) {
+ serializeElement((OMElement) node, writer);
+ } else if (nodeType == OMNode.ATTRIBUTE_NODE) {
+ serializeAttribute((OMAttribute) node, writer);
+ } else if (nodeType == OMNode.TEXT_NODE) {
+ serializeText((OMText) node, writer);
+ } else if (nodeType == OMNode.COMMENT_NODE) {
+ serializeComment((OMText) node, writer);
+ } else if (nodeType == OMNode.CDATA_SECTION_NODE) {
+ serializeCData((OMText) node, writer);
+ // added by Thilina
+ } else if (nodeType == OMNode.BLOB_NODE) {
+ serializeBlob((OMBlobImpl) node, writer);
+ }
+ writer.flush();
+ }
+
+ /**
+ * @param element
+ */
+ protected void serializeElement(OMElement element, XMLStreamWriter writer)
+ throws XMLStreamException {
+
+ OMNamespace ns = element.getNamespace();
+ String prefix = null;
+ String nameSpaceName = null;
+ if (ns != null) {
+ prefix = ns.getPrefix();
+ nameSpaceName = ns.getName();
+ if (prefix != null) {
+ writer.writeStartElement(prefix, element.getLocalName(),
+ nameSpaceName);
+ if (!prefixList.contains(prefix)) {
+ writer.writeNamespace(prefix, nameSpaceName);
+ prefixList.add(prefix);
+ }
+ } else {
+ writer.writeStartElement(nameSpaceName, element.getLocalName());
+ //add the own namespace
+ writer.writeDefaultNamespace(nameSpaceName);
+
+ }
+ }
+
+ //add the elements attributes
+ Iterator attributes = element.getAttributes();
+ if (attributes != null) {
+ while (attributes.hasNext()) {
+ serializeAttribute((OMAttribute) attributes.next(), writer);
+ }
+ }
+
+ //add the namespaces
+ Iterator namespaces = element.getAllDeclaredNamespaces();
+ if (namespaces != null) {
+ while (namespaces.hasNext()) {
+ serializeNamespace((OMNamespace) namespaces.next(), writer);
+ }
+ }
+
+ //add the children
+ Iterator children = element.getChildren();
+
+ while (children.hasNext()) {
+ Object node = children.next();
+ if (node != null) {
+ serializeNode((OMNode) node, writer);
+ }
+ }
+
+ writer.writeEndElement();
+
+ }
+
+ /*
+ * Edited by Thilina Gunarathne
+ */
+ protected void serializeBlob(OMBlobImpl blob, XMLStreamWriter xmlWriter)
+ throws XMLStreamException {
+
+ if (xmlWriter instanceof MTOMXMLStreamWriter) {
+ MTOMXMLStreamWriter writer = (MTOMXMLStreamWriter) xmlWriter;
+ OMNamespace ns = new OMNamespaceImpl(
+ "http://www.w3.org/2004/08/xop/Include", "xop");
+ String prefix = null;
+ String nameSpaceName = null;
+ if (ns != null) {
+ prefix = ns.getPrefix();
+ nameSpaceName = ns.getName();
+ if (prefix != null) {
+ writer.writeStartElement(prefix, "Include", nameSpaceName);
+ //if (!prefixList.contains(prefix)) {
+ writer.writeNamespace(prefix, nameSpaceName);
+ // prefixList.add(prefix);
+ //}
+ }
+ }
+
+ //get the Cid from the MTOMwriter
+ String cid;
+ try {
+ cid = writer.writeAttachment(blob);
+ OMAttribute href = new OMAttributeImpl("href",
+ new OMNamespaceImpl("", ""), "cid:" + cid.trim());
+ serializeAttribute(href, writer);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ writer.writeEndElement();
+ } else {
+ ByteArrayOutputStream byteStream;
+ try {
+ byteStream = (ByteArrayOutputStream) blob.getOutputStream();
+ xmlWriter.writeCharacters(Base64.encode(byteStream
+ .toByteArray()));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ }
+
+ /*
+ * Edit over
+ */
+
+ protected void serializeText(OMText text, XMLStreamWriter writer)
+ throws XMLStreamException {
+ writer.writeCharacters(text.getValue());
+ }
+
+ protected void serializeCData(OMText text, XMLStreamWriter writer)
+ throws XMLStreamException {
+ writer.writeCData(text.getValue());
+ }
+
+ protected void serializeComment(OMText text, XMLStreamWriter writer)
+ throws XMLStreamException {
+ writer.writeComment(text.getValue());
+ }
+
+ /**
+ * @param attr
+ * @param writer
+ * @throws XMLStreamException
+ */
+
+ protected 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());
+ }
+
+ }
+
+ protected void serializeNamespace(OMNamespace namespace,
+ XMLStreamWriter writer) throws XMLStreamException {
+ if (namespace != null) {
+ String prefix = namespace.getPrefix();
+ if (!prefixList.contains(prefix))
+ writer.writeNamespace(prefix, namespace.getName());
+
+ }
+ }
+
+}
\ No newline at end of file
Added: webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2/src/test-resources/OMSerializeBase64Out.xml
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2/src/test-resources/OMSerializeBase64Out.xml?view=auto&rev=158906
==============================================================================
--- webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2/src/test-resources/OMSerializeBase64Out.xml (added)
+++ webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2/src/test-resources/OMSerializeBase64Out.xml Thu Mar 24 02:17:28 2005
@@ -0,0 +1 @@
+<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><m:data xmlns:m="http://www.example.org/stuff"><m:name m:contentType="text/plain">rO0ABXQAE1Byb2dyYW1taW5nIFByb2plY3Q=</m:name></m:data></soap:Body></soap:Envelope>
\ No newline at end of file
Added: webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2/src/test-resources/OMSerializeMTOMOut.txt
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2/src/test-resources/OMSerializeMTOMOut.txt?view=auto&rev=158906
==============================================================================
--- webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2/src/test-resources/OMSerializeMTOMOut.txt (added)
+++ webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2/src/test-resources/OMSerializeMTOMOut.txt Thu Mar 24 02:17:28 2005
@@ -0,0 +1,17 @@
+MIME-Version: 1.0
+Content-Type: multipart/Related;
+ boundary="----=_Part_0_5230193.1111339759393"
+
+------=_Part_0_5230193.1111339759393
+Content-Type: application/xop+xml
+Content-Transfer-Encoding: 8bit
+Content-ID: <http://example.org/my.hsh>
+
+<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><m:data xmlns:m="http://www.example.org/stuff"><m:name m:contentType="text/plain"><xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/Include" href="cid:http://example.org/my.hsh1111339759302"></xop:Include></m:name></m:data></soap:Body></soap:Envelope>
+------=_Part_0_5230193.1111339759393
+Content-Type: text/plain; charset=Cp1252
+Content-Transfer-Encoding: binary
+Content-ID: <http://example.org/my.hsh1111339759302>
+
+Programming Project
+------=_Part_0_5230193.1111339759393--
Added: webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2/src/test/org/apache/axis/om/impl/llom/OMTextImplTest.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2/src/test/org/apache/axis/om/impl/llom/OMTextImplTest.java?view=auto&rev=158906
==============================================================================
--- webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2/src/test/org/apache/axis/om/impl/llom/OMTextImplTest.java (added)
+++ webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2/src/test/org/apache/axis/om/impl/llom/OMTextImplTest.java Thu Mar 24 02:17:28 2005
@@ -0,0 +1,107 @@
+/*
+ * Created on Mar 14, 2005
+ *
+ * TODO To change the template for this generated file go to
+ * Window - Preferences - Java - Code Style - Code Templates
+ */
+package org.apache.axis.om.impl.llom;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
+import javax.activation.DataHandler;
+
+import junit.framework.TestCase;
+
+import org.apache.axis.encoding.Base64;
+import org.apache.axis.om.impl.llom.mtom.ByteArrayDataSource;
+
+/**
+ * @author TGunarathne
+ *
+ * TODO To change the template for this generated type comment go to Window -
+ * Preferences - Java - Code Style - Code Templates
+ */
+public class OMTextImplTest extends TestCase {
+ Object expectedObject;
+
+ OMTextImpl textNode;
+
+ String expectedBase64;
+
+ public static void main(String[] args) {
+ junit.swingui.TestRunner.run(OMTextImplTest.class);
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ expectedObject = new String("Lanka Software Foundation");
+ ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
+ ObjectOutputStream objectOutStream = new ObjectOutputStream(byteStream);
+ objectOutStream.writeObject(expectedObject);
+ expectedBase64 = Base64.encode(byteStream.toByteArray());
+ textNode = new OMTextImpl(expectedBase64, "text/plain");
+ }
+
+ /*
+ * @see TestCase#tearDown()
+ */
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ /**
+ * Constructor for OMTextImplTest.
+ *
+ * @param name
+ */
+ public OMTextImplTest(String name) {
+ super(name);
+ }
+
+ public void testBase64Encode() {
+ Object actualObject1;
+ try {
+ byte[] tempa = Base64.decode(expectedBase64);
+ ObjectInputStream objectInStream = new ObjectInputStream(
+ new ByteArrayInputStream(tempa));
+ actualObject1 = objectInStream.readObject();
+ assertEquals("Base64 Encoding Check", expectedObject, actualObject1);
+ } catch (IOException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ } catch (ClassNotFoundException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ }
+ }
+
+ public void testGetDataHandler() {
+ String actualObject;
+
+ DataHandler DH = textNode.getDataHandler();
+
+ ByteArrayDataSource ds = (ByteArrayDataSource) DH.getDataSource();
+ try {
+ InputStream inStream = ds.getInputStream();
+ ObjectInputStream obj = new ObjectInputStream(inStream);
+
+ actualObject = (String) obj.readObject();
+
+ assertEquals("OMTextImpl GetDataHandler Check", expectedObject,
+ actualObject);
+ } catch (ClassNotFoundException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+}
\ No newline at end of file
Added: webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2/src/test/org/apache/axis/om/impl/llom/mtom/MTOMTest.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2/src/test/org/apache/axis/om/impl/llom/mtom/MTOMTest.java?view=auto&rev=158906
==============================================================================
--- webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2/src/test/org/apache/axis/om/impl/llom/mtom/MTOMTest.java (added)
+++ webservices/axis/trunk/archive/java/scratch/Thilina/MTOM2/src/test/org/apache/axis/om/impl/llom/mtom/MTOMTest.java Thu Mar 24 02:17:28 2005
@@ -0,0 +1,172 @@
+/*
+ * Created on Mar 9, 2005
+ *
+ * TODO To change the template for this generated file go to
+ * Window - Preferences - Java - Code Style - Code Templates
+ */
+package org.apache.axis.om.impl.llom.mtom;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.util.Iterator;
+
+import javax.activation.DataHandler;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamWriter;
+
+import junit.framework.TestCase;
+
+import org.apache.axis.om.OMAttribute;
+import org.apache.axis.om.OMElement;
+import org.apache.axis.om.impl.llom.OMAttributeImpl;
+import org.apache.axis.om.impl.llom.OMBlobImpl;
+import org.apache.axis.om.impl.llom.OMElementImpl;
+import org.apache.axis.om.impl.llom.OMNamespaceImpl;
+import org.apache.axis.om.impl.llom.builder.MTOMStAXSOAPModelBuilder;
+
+/**
+ * @author TGunarathne
+ *
+ * TODO To change the template for this generated type comment go to Window -
+ * Preferences - Java - Code Style - Code Templates
+ */
+public class MTOMTest extends TestCase {
+ String expectedObject;
+
+ String outFileName;
+
+ String outBase64FileName;
+
+ MTOMStAXSOAPModelBuilder builder;
+
+ DataHandler expectedDH;
+
+ public static void main(String[] args) {
+ junit.swingui.TestRunner.run(MTOMTest.class);
+ }
+
+ /*
+ * @see TestCase#setUp()
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+ outFileName = "mtom/src/test-resources/OMSerializeMTOMOut.txt";
+ outBase64FileName = "mtom/src/test-resources/OMSerializeBase64Out.xml";
+ serializeSetUp();
+ builder = new MTOMStAXSOAPModelBuilder(new FileInputStream(outFileName));
+
+ }
+
+ protected void serializeSetUp() {
+ File outMTOMFile;
+ File outBase64File;
+
+ try {
+
+ outMTOMFile = new File(outFileName);
+ outBase64File = new File(outBase64FileName);
+ MTOMXMLStreamWriter mtomWriter = new MTOMXMLStreamWriter(
+ new FileOutputStream(outMTOMFile));
+ XMLStreamWriter baseWriter = XMLOutputFactory.newInstance()
+ .createXMLStreamWriter(new FileOutputStream(outBase64File));
+
+ //SimpleOMSerializer mtomSimpleOMSerialiser = new
+ // SimpleOMSerializer();
+
+ OMNamespaceImpl soap = new OMNamespaceImpl(
+ "http://schemas.xmlsoap.org/soap/envelope/", "soap");
+ OMElement envelope = new OMElementImpl("Envelope", soap);
+ OMElement body = new OMElementImpl("Body", soap);
+
+ OMNamespaceImpl dataName = new OMNamespaceImpl(
+ "http://www.example.org/stuff", "m");
+ OMElement data = new OMElementImpl("data", dataName);
+
+ OMNamespaceImpl mime = new OMNamespaceImpl(
+ "http://www.w3.org/2003/06/xmlmime", "m");
+
+ OMElement text = new OMElementImpl("name", dataName);
+ OMAttribute cType1 = new OMAttributeImpl("contentType", mime,
+ "text/plain");
+ text.insertAttribute(cType1);
+ expectedObject = new String("Programming Project");
+ expectedDH = new DataHandler(expectedObject, "text/plain");
+ OMBlobImpl textData = new OMBlobImpl(expectedDH);
+
+ envelope.addChild(body);
+ body.addChild(data);
+ data.addChild(text);
+ text.addChild(textData);
+
+ envelope.serialize(baseWriter, false);
+ baseWriter.flush();
+
+ envelope.serialize(mtomWriter, false);
+ //MTOMser.serialize(Envelope, MTOMWriter);
+ mtomWriter.flush();
+ mtomWriter.complete();
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ /*
+ * @see TestCase#tearDown()
+ */
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ /**
+ * Constructor for OMBlobTest.
+ *
+ * @param name
+ */
+ public MTOMTest(String name) {
+ super(name);
+ }
+
+ public void testGetDataHandler() {
+ try {
+ OMElement root = (OMElement) builder.getDocumentElement();
+ System.out.println(root.getLocalName() + " : "
+ + root.getNamespaceName());
+ OMElement body = (OMElement) root.getFirstChild();
+ System.out.println(body.getLocalName() + " : "
+ + body.getNamespaceName());
+ OMElement data = (OMElement) body.getFirstChild();
+ System.out.println(data.getLocalName() + " : "
+ + data.getNamespaceName());
+ Iterator childIt = data.getChildren();
+ //while (childIt.hasNext()) {
+ OMElement child = (OMElement) childIt.next();
+ OMBlobImpl blob = (OMBlobImpl) child.getFirstChild();
+ /*
+ * Following is the procedure the user has to follow to read objects
+ * in OBBlob User has to know the object type & whether it is
+ * serializable. If it is not he has to use a Custom Defined
+ * DataSource to get the Object.
+ */
+
+ DataHandler actualDH;
+ actualDH = blob.getDataHandler();
+ //assertEquals("DataHandler
+ // check",expectedDH.getContent(),(actualDH =
+ // blob.getDataHandler()).getContent());
+ Object actualObject = actualDH.getContent(); //This returns a
+ // String cause string
+ // is serializable
+ assertEquals("Object check", expectedObject, (String) actualObject);
+
+ System.out.println(child.getLocalName() + ":-\t" + actualObject);
+
+ //}
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+}
\ No newline at end of file