KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > om > OMOutput


1 /*
2  * Copyright 2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis2.om;
17
18 import javax.xml.stream.FactoryConfigurationError;
19 import javax.xml.stream.XMLOutputFactory;
20 import javax.xml.stream.XMLStreamException;
21 import javax.xml.stream.XMLStreamWriter;
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.util.LinkedList JavaDoc;
25
26 /**
27  * @author <a HREF="mailto:thilina@opensource.lk">Thilina Gunarathne </a> For
28  * the moment this assumes that transport takes the decision of whether
29  * to optimise or not by looking at whether the MTOM optimise is enabled &
30  * also looking at the OM tree whether it has any optimisable content
31  */

32
33 public class OMOutput {
34     private XMLStreamWriter xmlWriter;
35
36     private boolean doOptimise;
37
38     private OutputStream JavaDoc outStream;
39
40     private XMLStreamWriter writer;
41
42     private LinkedList JavaDoc binaryNodeList;
43
44     private ByteArrayOutputStream JavaDoc bufferedSoapOutStream;
45
46     private static String JavaDoc mimeBoundary = "----=_AxIs2_Def_boundary_=42214532";
47
48     //private String contentType = null;
49

50     /**
51      * @param xmlWriter
52      * if it is guaranteed for not using attachments one can use this
53      */

54     public OMOutput(XMLStreamWriter xmlWriter) {
55         this.xmlWriter = xmlWriter;
56     }
57
58     /**
59      * @throws FactoryConfigurationError
60      * @throws XMLStreamException
61      *
62      */

63     public OMOutput(OutputStream JavaDoc outStream, boolean doOptimise)
64             throws XMLStreamException, FactoryConfigurationError {
65         this.doOptimise = doOptimise;
66         this.outStream = outStream;
67         if (doOptimise) {
68             bufferedSoapOutStream = new ByteArrayOutputStream JavaDoc();
69             xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(
70                     bufferedSoapOutStream);
71             binaryNodeList = new LinkedList JavaDoc();
72         } else {
73             xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(
74                     outStream);
75
76         }
77
78     }
79
80     public XMLStreamWriter getXmlStreamWriter() {
81         return xmlWriter;
82     }
83
84     public void flush() throws XMLStreamException {
85         // if (doOptimise) {
86
// try {
87
// this.complete();
88
// } catch (IOException e) {
89
// //TODO this is just a hack to avoid passing IOException. Must find a
90
// better way to handle this
91
// throw new XMLStreamException("Error creating mime parts. Problem with
92
// Streams");
93
// } catch (MessagingException e) {
94
// throw new XMLStreamException("Error creating mime Body parts");
95
// }
96
// } else {
97
xmlWriter.flush();
98         // }
99

100     }
101
102     public boolean doOptimise() {
103         return doOptimise;
104     }
105
106     public static String JavaDoc getContentType(boolean doOptimize) {
107         if (doOptimize) {
108             return MIMEOutputUtils.getContentTypeForMime(mimeBoundary);
109         }
110         //TODO have to check whether SOAP1.1 & SOAP 1.2
111
return null;
112     }
113
114     public void writeOptimised(OMText node) {
115         binaryNodeList.add(node);
116     }
117
118     public void complete() throws XMLStreamException {
119         if (doOptimise) {
120             xmlWriter.flush();
121             MIMEOutputUtils.complete(outStream, bufferedSoapOutStream,
122                     binaryNodeList, mimeBoundary);
123         }
124     }
125
126     /*private String getMimeBoundary() {
127         //TODO have to dynamically generate.
128         if (mimeBoundary == null) {
129             mimeBoundary = "----=_AxIs2_Def_boundary_=42214532";
130         }
131         return mimeBoundary;
132     }*/

133
134 }
135
Popular Tags