Index: src/java/org/apache/axis/om/impl/llom/builder/MTOMStAXSOAPModelBuilder.java =================================================================== --- src/java/org/apache/axis/om/impl/llom/builder/MTOMStAXSOAPModelBuilder.java (revision 169595) +++ src/java/org/apache/axis/om/impl/llom/builder/MTOMStAXSOAPModelBuilder.java (working copy) @@ -221,4 +221,4 @@ } else return null; } -} +} \ No newline at end of file Index: src/java/org/apache/axis/om/impl/llom/mtom/MTOMHelper.java =================================================================== Index: src/java/org/apache/axis/om/impl/llom/OMBlobImpl.java =================================================================== --- src/java/org/apache/axis/om/impl/llom/OMBlobImpl.java (revision 169595) +++ src/java/org/apache/axis/om/impl/llom/OMBlobImpl.java (working copy) @@ -25,6 +25,8 @@ import javax.mail.MessagingException; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; +import java.util.Date; +import java.util.Random; import org.apache.axis.encoding.Base64; import org.apache.axis.om.OMAttribute; @@ -37,7 +39,7 @@ import org.apache.axis.om.impl.llom.OMAttributeImpl; import org.apache.axis.om.impl.llom.OMNamespaceImpl; import org.apache.axis.om.impl.llom.OMNodeImpl; -import org.apache.axis.om.impl.llom.mtom.MTOMHelper; +import org.apache.axis.om.impl.llom.builder.MTOMStAXSOAPModelBuilder; import org.apache.axis.om.impl.llom.mtom.MTOMXMLStreamWriter; /** @@ -45,29 +47,55 @@ */ public class OMBlobImpl extends OMNodeImpl implements OMBlob { - + /** + * Field contentID for the mime part + */ private String contentID = null; - - private MTOMHelper mtomHelper; - + /** + * Field builder + */ private OMXMLParserWrapper builder; - + /** + * Field dataHandler + */ private DataHandler dataHandler = null; + /** + * Field nameSpace + */ + protected OMNamespace ns = new OMNamespaceImpl("http://www.w3.org/2004/08/xop/Include", "xop"); + /** + * Field localName + */ + protected String localName= "Include"; + /** + * Field attributes + */ + protected OMAttribute attribute; + public OMBlobImpl(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()); + } } - - public OMBlobImpl(String contentID, OMElement parent, MTOMHelper mtomHelper, + + public OMBlobImpl(String contentID, OMElement parent, OMXMLParserWrapper builder) { super(parent); this.builder = builder; this.contentID = contentID; - this.mtomHelper = mtomHelper; } - + + public String getLocalName() { + return localName; + } + public java.io.OutputStream getOutputStream() throws IOException, - MessagingException { + MessagingException { if (dataHandler == null) { getDataHandler(); } @@ -81,78 +109,61 @@ } return outStream; } - + public String getValue() throws OMException { throw new OMException( - "Blob contains Binary data. Returns Stream or Datahandler only"); + "Blob contains Binary data. Returns Stream or Datahandler only"); } - + public DataHandler getDataHandler() throws MessagingException { if (dataHandler == null) { - dataHandler = mtomHelper.getDataHandler(contentID); + dataHandler = ((MTOMStAXSOAPModelBuilder) builder) + .getDataHandler(contentID); } return dataHandler; } - + public short getType() throws OMException { return OMNode.BLOB_NODE; } - + + public String getContentID() { + return this.contentID; + } + public boolean isComplete() { return true; } - + public void serialize(XMLStreamWriter writer, boolean cache) - throws XMLStreamException { + throws XMLStreamException { boolean firstElement = false; - - // Special case for the pull type building with cache off - // The getPullParser method returns the current elements itself. - - if (isComplete()) { - - // serialize own normally + if (!cache) { + //No caching if (writer instanceof MTOMXMLStreamWriter) { + // send as optimised MTOMXMLStreamWriter mtomWriter = (MTOMXMLStreamWriter) writer; - // write the XOP:Include namespace - 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) { - mtomWriter.writeStartElement(prefix, "Include", - nameSpaceName); - //if (!prefixList.contains(prefix)) { - mtomWriter.writeNamespace(prefix, nameSpaceName); - // prefixList.add(prefix); - //} - } - } - - //get the Cid from the MTOMwriter - String contentID; - contentID = mtomWriter.writeAttachment(this); - OMAttribute href = new OMAttributeImpl("href", - new OMNamespaceImpl("", ""), "cid:" + contentID.trim()); - serializeAttribute(href, writer); + this.attribute= new OMAttributeImpl("href",new OMNamespaceImpl("", ""), "cid:" + this.contentID.trim()); + + this.serializeStartpart(mtomWriter); + mtomWriter.writeOptimised(this); mtomWriter.writeEndElement(); } else { + // send as non optimised ByteArrayOutputStream byteStream; - - try { - byteStream = (ByteArrayOutputStream) this.getOutputStream(); + try { + byteStream = (ByteArrayOutputStream) this.getOutputStream(); + writer.writeCharacters(Base64.encode(byteStream .toByteArray())); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (MessagingException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (MessagingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } if (nextSibling != null) { // serilize next sibling @@ -171,28 +182,102 @@ } } } - - protected void serializeAttribute(OMAttribute attr, XMLStreamWriter writer) - throws XMLStreamException { - - //first check whether the attribute is associated with a namespace + + /* + * 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()); + // throw new OMException( + // "Non namespace qualified elements are not allowed"); + } + } else { + writer.writeStartElement(this.getLocalName()); + // throw new OMException( + // "Non namespace qualified elements are not allowed"); + } + + // add the elements attribute "href" + serializeAttribute(this.attribute, writer); + + // add the namespace + serializeNamespace(this.ns, writer); + + } + + /** + * Method serializeAttribute + * + * @param attr + * @param writer + * @throws XMLStreamException + */ + 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 + + // add the prefix if it's availble prefix = ns.getPrefix(); namespaceName = ns.getName(); - - if (prefix != null) + if (prefix != null) { writer.writeAttribute(prefix, namespaceName, attr .getLocalName(), attr.getValue()); - else + } else { writer.writeAttribute(namespaceName, attr.getLocalName(), attr .getValue()); + } } else { writer.writeAttribute(attr.getLocalName(), attr.getValue()); } - } + + /** + * 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); + } + } + } + } \ No newline at end of file