You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by ve...@apache.org on 2016/05/23 22:05:01 UTC
svn commit: r1745281 - in
/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/stream:
stax/MTOMXMLStreamWriterImpl.java xop/CompletionListener.java
xop/XOPEncodingFilterHandler.java
Author: veithen
Date: Mon May 23 22:05:01 2016
New Revision: 1745281
URL: http://svn.apache.org/viewvc?rev=1745281&view=rev
Log:
Some refactoring of MTOMXMLStreamWriterImpl.
Added:
webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/stream/xop/CompletionListener.java (with props)
Modified:
webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/stream/stax/MTOMXMLStreamWriterImpl.java
webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/stream/xop/XOPEncodingFilterHandler.java
Modified: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/stream/stax/MTOMXMLStreamWriterImpl.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/stream/stax/MTOMXMLStreamWriterImpl.java?rev=1745281&r1=1745280&r2=1745281&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/stream/stax/MTOMXMLStreamWriterImpl.java (original)
+++ webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/stream/stax/MTOMXMLStreamWriterImpl.java Mon May 23 22:05:01 2016
@@ -31,15 +31,16 @@ import javax.xml.stream.XMLStreamExcepti
import javax.xml.stream.XMLStreamWriter;
import org.apache.axiom.attachments.lifecycle.DataHandlerExt;
+import org.apache.axiom.core.stream.StreamException;
import org.apache.axiom.core.stream.XmlHandler;
import org.apache.axiom.core.stream.serializer.Serializer;
import org.apache.axiom.ext.stax.datahandler.DataHandlerProvider;
import org.apache.axiom.ext.stax.datahandler.DataHandlerWriter;
-import org.apache.axiom.om.OMException;
import org.apache.axiom.om.OMOutputFormat;
import org.apache.axiom.om.OMText;
import org.apache.axiom.om.impl.MTOMXMLStreamWriter;
import org.apache.axiom.om.impl.OMMultipartWriter;
+import org.apache.axiom.om.impl.stream.xop.CompletionListener;
import org.apache.axiom.om.impl.stream.xop.XOPEncodingFilterHandler;
import org.apache.axiom.om.util.CommonUtils;
import org.apache.axiom.om.util.XMLStreamWriterFilter;
@@ -75,12 +76,10 @@ public class MTOMXMLStreamWriterImpl ext
private static final Log log = LogFactory.getLog(MTOMXMLStreamWriterImpl.class);
private XMLStreamWriter xmlWriter;
private List<Part> otherParts = new LinkedList<Part>();
- private final OMMultipartWriter multipartWriter;
private final OutputStream rootPartOutputStream;
private OMOutputFormat format;
private final OptimizationPolicy optimizationPolicy;
- private final boolean preserveAttachments;
- private final XOPEncodingFilterHandler encoder;
+ private final XmlHandler handler;
// State variables
private boolean isEndDocument = false; // has endElement been called
@@ -97,10 +96,8 @@ public class MTOMXMLStreamWriterImpl ext
}
this.format = format;
optimizationPolicy = new OptimizationPolicyImpl(format);
- preserveAttachments = true;
- multipartWriter = null;
rootPartOutputStream = null;
- encoder = null;
+ handler = null;
}
public MTOMXMLStreamWriterImpl(XMLStreamWriter xmlWriter) {
@@ -126,7 +123,7 @@ public class MTOMXMLStreamWriterImpl ext
* @throws FactoryConfigurationError
* @see OMOutputFormat#DEFAULT_CHAR_SET_ENCODING
*/
- public MTOMXMLStreamWriterImpl(OutputStream outStream, OMOutputFormat format, boolean preserveAttachments)
+ public MTOMXMLStreamWriterImpl(OutputStream outStream, OMOutputFormat format, final boolean preserveAttachments)
throws XMLStreamException, FactoryConfigurationError {
if (log.isDebugEnabled()) {
log.debug("Creating MTOMXMLStreamWriter");
@@ -138,7 +135,6 @@ public class MTOMXMLStreamWriterImpl ext
log.trace("Call Stack =" + CommonUtils.callStackToString());
}
this.format = format;
- this.preserveAttachments = preserveAttachments;
String encoding = format.getCharSetEncoding();
if (encoding == null) { //Default encoding is UTF-8
@@ -147,6 +143,7 @@ public class MTOMXMLStreamWriterImpl ext
optimizationPolicy = new OptimizationPolicyImpl(format);
+ final OMMultipartWriter multipartWriter;
if (format.isOptimized()) {
multipartWriter = new OMMultipartWriter(outStream, format);
try {
@@ -160,7 +157,6 @@ public class MTOMXMLStreamWriterImpl ext
}
Serializer serializer = new Serializer(rootPartOutputStream, encoding);
- XmlHandler handler;
if (format.isOptimized()) {
ContentIDGenerator contentIDGenerator = new ContentIDGenerator() {
@@ -168,10 +164,33 @@ public class MTOMXMLStreamWriterImpl ext
return existingContentID != null ? existingContentID : getNextContentId();
}
};
- encoder = new XOPEncodingFilterHandler(serializer, contentIDGenerator, optimizationPolicy);
- handler = encoder;
+ handler = new XOPEncodingFilterHandler(serializer, contentIDGenerator, optimizationPolicy, new CompletionListener() {
+ @Override
+ public void completed(XOPEncodingFilterHandler encoder) throws StreamException {
+ try {
+ rootPartOutputStream.close();
+ // First write the attachments added properly through the DataHandlerWriter extension
+ for (String contentID : encoder.getContentIDs()) {
+ DataHandler dataHandler = encoder.getDataHandler(contentID);
+ if (preserveAttachments || !(dataHandler instanceof DataHandlerExt)) {
+ multipartWriter.writePart(dataHandler, contentID);
+ } else {
+ OutputStream out = multipartWriter.writePart(dataHandler.getContentType(), contentID);
+ IOUtils.copy(((DataHandlerExt)dataHandler).readOnce(), out, -1);
+ out.close();
+ }
+ }
+ // Now write parts that have been added by prepareDataHandler
+ for (Part part : otherParts) {
+ multipartWriter.writePart(part.getDataHandler(), part.getContentID());
+ }
+ multipartWriter.complete();
+ } catch (IOException ex) {
+ throw new StreamException(ex);
+ }
+ }
+ });
} else {
- encoder = null;
handler = serializer;
}
@@ -248,25 +267,9 @@ public class MTOMXMLStreamWriterImpl ext
log.debug("The XML writing is completed. Now the attachments are written");
isComplete = true;
try {
- rootPartOutputStream.close();
- // First write the attachments added properly through the DataHandlerWriter extension
- for (String contentID : encoder.getContentIDs()) {
- DataHandler dataHandler = encoder.getDataHandler(contentID);
- if (preserveAttachments || !(dataHandler instanceof DataHandlerExt)) {
- multipartWriter.writePart(dataHandler, contentID);
- } else {
- OutputStream out = multipartWriter.writePart(dataHandler.getContentType(), contentID);
- IOUtils.copy(((DataHandlerExt)dataHandler).readOnce(), out, -1);
- out.close();
- }
- }
- // Now write parts that have been added by prepareDataHandler
- for (Part part : otherParts) {
- multipartWriter.writePart(part.getDataHandler(), part.getContentID());
- }
- multipartWriter.complete();
- } catch (IOException e) {
- throw new OMException(e);
+ handler.completed();
+ } catch (StreamException ex) {
+ throw new XMLStreamException(ex);
}
}
}
Added: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/stream/xop/CompletionListener.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/stream/xop/CompletionListener.java?rev=1745281&view=auto
==============================================================================
--- webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/stream/xop/CompletionListener.java (added)
+++ webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/stream/xop/CompletionListener.java Mon May 23 22:05:01 2016
@@ -0,0 +1,25 @@
+/*
+ * 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.apache.axiom.om.impl.stream.xop;
+
+import org.apache.axiom.core.stream.StreamException;
+
+public interface CompletionListener {
+ void completed(XOPEncodingFilterHandler handler) throws StreamException;
+}
Propchange: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/stream/xop/CompletionListener.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/stream/xop/XOPEncodingFilterHandler.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/stream/xop/XOPEncodingFilterHandler.java?rev=1745281&r1=1745280&r2=1745281&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/stream/xop/XOPEncodingFilterHandler.java (original)
+++ webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/stream/xop/XOPEncodingFilterHandler.java Mon May 23 22:05:01 2016
@@ -39,12 +39,14 @@ public final class XOPEncodingFilterHand
private final Map<String,Object> dataHandlerObjects = new LinkedHashMap<String,Object>();
private final ContentIDGenerator contentIDGenerator;
private final OptimizationPolicy optimizationPolicy;
+ private final CompletionListener completionListener;
public XOPEncodingFilterHandler(XmlHandler parent, ContentIDGenerator contentIDGenerator,
- OptimizationPolicy optimizationPolicy) {
+ OptimizationPolicy optimizationPolicy, CompletionListener completionListener) {
super(parent);
this.contentIDGenerator = contentIDGenerator;
this.optimizationPolicy = optimizationPolicy;
+ this.completionListener = completionListener;
}
/**
@@ -101,4 +103,10 @@ public final class XOPEncodingFilterHand
}
super.processCharacterData(data, ignorable);
}
+
+ @Override
+ public void completed() throws StreamException {
+ super.completed();
+ completionListener.completed(this);
+ }
}