This is an automated email from the ASF dual-hosted git repository. veithen pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/ws-axiom.git
The following commit(s) were added to refs/heads/master by this push: new c452fd909 Make use of ContentType in some places where String is used c452fd909 is described below commit c452fd909665cdb7d901fd84c4daf61c829021a2 Author: Andreas Veithen <andreas.veit...@gmail.com> AuthorDate: Fri Nov 4 08:52:40 2022 +0000 Make use of ContentType in some places where String is used --- axiom-api/pom.xml | 2 + .../org/apache/axiom/mime/MultipartBodyWriter.java | 22 +- .../java/org/apache/axiom/om/OMOutputFormat.java | 4 +- .../apache/axiom/om/impl/OMMultipartWriter.java | 55 ++-- .../org/apache/axiom/soap/SOAP11Constants.java | 4 +- .../org/apache/axiom/soap/SOAP12Constants.java | 4 +- .../apache/axiom/mime/MultipartBodyWriterTest.java | 2 +- .../org/apache/axiom/om/OMOutputFormatTest.java | 14 +- .../org/apache/axiom/om/impl/MIMEOutputUtils.java | 359 --------------------- .../apache/axiom/attachments/AttachmentsTest.java | 67 ---- .../axiom/om/impl/mixin/AxiomContainerMixin.java | 11 +- 11 files changed, 77 insertions(+), 467 deletions(-) diff --git a/axiom-api/pom.xml b/axiom-api/pom.xml index b457ff6dd..ddae29a5a 100644 --- a/axiom-api/pom.xml +++ b/axiom-api/pom.xml @@ -264,6 +264,8 @@ org.apache.axiom.om.OMXMLBuilderFactory -> org.apache.axiom.soap.SOAPModelBuilder, org.apache.axiom.om.OMXMLBuilderFactory -> org.apache.axiom.soap.SOAPProcessingException, org.apache.axiom.om.OMXMLBuilderFactory -> org.apache.axiom.soap.SOAPVersion, + org.apache.axiom.om.OMOutputFormat -> org.apache.axiom.soap.SOAP11Constants, + org.apache.axiom.om.OMOutputFormat -> org.apache.axiom.soap.SOAP12Constants, <!-- The public API shouldn't depend on classes in o.a.a.om.util --> org.apache.axiom.om.OMMetaFactorySPI -> org.apache.axiom.om.util.StAXParserConfiguration, org.apache.axiom.om.OMXMLBuilderFactory -> org.apache.axiom.om.util.StAXParserConfiguration, diff --git a/axiom-api/src/main/java/org/apache/axiom/mime/MultipartBodyWriter.java b/axiom-api/src/main/java/org/apache/axiom/mime/MultipartBodyWriter.java index 73ed4b746..726e0336f 100644 --- a/axiom-api/src/main/java/org/apache/axiom/mime/MultipartBodyWriter.java +++ b/axiom-api/src/main/java/org/apache/axiom/mime/MultipartBodyWriter.java @@ -20,6 +20,7 @@ package org.apache.axiom.mime; import java.io.IOException; import java.io.OutputStream; +import java.text.ParseException; import java.util.List; import javax.activation.DataHandler; @@ -28,7 +29,7 @@ import org.apache.axiom.util.UIDGenerator; /** * Writes a MIME multipart body as used by XOP/MTOM and SOAP with Attachments. MIME parts are - * written using {@link #writePart(String, ContentTransferEncoding, String, List)} or + * written using {@link #writePart(ContentType, ContentTransferEncoding, String, List)} or * {@link #writePart(DataHandler, ContentTransferEncoding, String, List)}. Calls to both methods can be mixed, i.e. * it is not required to use the same method for all MIME parts. Instead, the caller should choose * the most convenient method for each part (depending on the form in which the content is @@ -118,7 +119,7 @@ public final class MultipartBodyWriter { * {@link OutputStream#close()} must be called to complete the writing of the MIME part. * * @param contentType - * the value of the {@code Content-Type} header of the MIME part; may be {@code null} + * the content type of the MIME part; may be {@code null} * @param contentTransferEncoding * the content transfer encoding to be used (see above); must not be * <code>null</code> @@ -131,7 +132,7 @@ public final class MultipartBodyWriter { * @throws IOException * if an I/O error occurs when writing to the underlying stream */ - public OutputStream writePart(String contentType, ContentTransferEncoding contentTransferEncoding, + public OutputStream writePart(ContentType contentType, ContentTransferEncoding contentTransferEncoding, String contentID, List<Header> extraHeaders) throws IOException { writeAscii("--"); writeAscii(boundary); @@ -139,7 +140,7 @@ public final class MultipartBodyWriter { // text/plain; charset=us-ascii). if (contentType != null) { writeAscii("\r\nContent-Type: "); - writeAscii(contentType); + writeAscii(contentType.toString()); } writeAscii("\r\nContent-Transfer-Encoding: "); writeAscii(contentTransferEncoding.toString()); @@ -178,7 +179,18 @@ public final class MultipartBodyWriter { */ public void writePart(DataHandler dataHandler, ContentTransferEncoding contentTransferEncoding, String contentID, List<Header> extraHeaders) throws IOException { - OutputStream partOutputStream = writePart(dataHandler.getContentType(), contentTransferEncoding, contentID, extraHeaders); + ContentType contentType; + String contentTypeString = dataHandler.getContentType(); + if (contentTypeString == null) { + contentType = null; + } else { + try { + contentType = new ContentType(contentTypeString); + } catch (ParseException ex) { + throw new RuntimeException(ex); + } + } + OutputStream partOutputStream = writePart(contentType, contentTransferEncoding, contentID, extraHeaders); dataHandler.writeTo(partOutputStream); partOutputStream.close(); } diff --git a/axiom-api/src/main/java/org/apache/axiom/om/OMOutputFormat.java b/axiom-api/src/main/java/org/apache/axiom/om/OMOutputFormat.java index 62e8b9370..b300ebe1f 100644 --- a/axiom-api/src/main/java/org/apache/axiom/om/OMOutputFormat.java +++ b/axiom-api/src/main/java/org/apache/axiom/om/OMOutputFormat.java @@ -189,9 +189,9 @@ public class OMOutputFormat { } if (contentType == null) { if (isSoap11) { - contentType = SOAP11Constants.SOAP_11_CONTENT_TYPE; + contentType = SOAP11Constants.SOAP_11_CONTENT_TYPE.toString(); } else { - contentType = SOAP12Constants.SOAP_12_CONTENT_TYPE; + contentType = SOAP12Constants.SOAP_12_CONTENT_TYPE.toString(); } } // If MTOM or SWA, the returned content-type is an diff --git a/axiom-api/src/main/java/org/apache/axiom/om/impl/OMMultipartWriter.java b/axiom-api/src/main/java/org/apache/axiom/om/impl/OMMultipartWriter.java index f214de7a4..d06817892 100644 --- a/axiom-api/src/main/java/org/apache/axiom/om/impl/OMMultipartWriter.java +++ b/axiom-api/src/main/java/org/apache/axiom/om/impl/OMMultipartWriter.java @@ -30,6 +30,7 @@ import org.apache.axiom.attachments.ConfigurableDataHandler; import org.apache.axiom.mime.ContentTransferEncoding; import org.apache.axiom.mime.ContentType; import org.apache.axiom.mime.Header; +import org.apache.axiom.mime.MediaType; import org.apache.axiom.mime.MultipartBodyWriter; import org.apache.axiom.om.OMOutputFormat; import org.apache.axiom.soap.SOAP11Constants; @@ -44,7 +45,7 @@ public class OMMultipartWriter { private final OMOutputFormat format; private final MultipartBodyWriter writer; private final boolean useCTEBase64; - private final String rootPartContentType; + private final ContentType rootPartContentType; public OMMultipartWriter(OutputStream out, OMOutputFormat format) { this.format = format; @@ -54,30 +55,28 @@ public class OMMultipartWriter { useCTEBase64 = format != null && Boolean.TRUE.equals( format.getProperty(OMOutputFormat.USE_CTE_BASE64_FOR_NON_TEXTUAL_ATTACHMENTS)); - String soapContentType; + MediaType soapContentType; if (format.isSOAP11()) { soapContentType = SOAP11Constants.SOAP_11_CONTENT_TYPE; } else { soapContentType = SOAP12Constants.SOAP_12_CONTENT_TYPE; } if (format.isOptimized()) { - rootPartContentType = "application/xop+xml; charset=" + format.getCharSetEncoding() - + "; type=\"" + soapContentType + "\""; + rootPartContentType = ContentType.builder() + .setMediaType(MediaType.APPLICATION_XOP_XML) + .setParameter("charset", format.getCharSetEncoding()) + .setParameter("type", soapContentType.toString()) + .build(); } else { - rootPartContentType = soapContentType + "; charset=" + format.getCharSetEncoding(); + rootPartContentType = ContentType.builder() + .setMediaType(soapContentType) + .setParameter("charset", format.getCharSetEncoding()) + .build(); } } - private static boolean isTextual(String contentType) { - try { - return new ContentType(contentType).isTextual(); - } catch (ParseException ex) { - return false; - } - } - - private ContentTransferEncoding getContentTransferEncoding(String contentType) { - if (useCTEBase64 && !isTextual(contentType)) { + private ContentTransferEncoding getContentTransferEncoding(ContentType contentType) { + if (useCTEBase64 && !contentType.isTextual()) { return ContentTransferEncoding.BASE64; } else { return ContentTransferEncoding.BINARY; @@ -90,13 +89,13 @@ public class OMMultipartWriter { * * @return the content type of the root part */ - public String getRootPartContentType() { + public ContentType getRootPartContentType() { return rootPartContentType; } /** * Start writing the root part of the MIME package. This method delegates to - * {@link MultipartBodyWriter#writePart(String, ContentTransferEncoding, String, List)}, but computes the content type, + * {@link MultipartBodyWriter#writePart(ContentType, ContentTransferEncoding, String, List)}, but computes the content type, * content transfer encoding and content ID from the {@link OMOutputFormat}. * * @return an output stream to write the content of the MIME part @@ -109,7 +108,7 @@ public class OMMultipartWriter { /** * Start writing an attachment part of the MIME package. This method delegates to - * {@link MultipartBodyWriter#writePart(String, ContentTransferEncoding, String, List)}, but computes the content transfer + * {@link MultipartBodyWriter#writePart(ContentType, ContentTransferEncoding, String, List)}, but computes the content transfer * encoding based on the content type and the {@link OMOutputFormat}. * * @param contentType @@ -120,13 +119,13 @@ public class OMMultipartWriter { * @throws IOException * if an I/O error occurs when writing to the underlying stream */ - public OutputStream writePart(String contentType, String contentID) throws IOException { + public OutputStream writePart(ContentType contentType, String contentID) throws IOException { return writer.writePart(contentType, getContentTransferEncoding(contentType), contentID, null); } /** * Start writing an attachment part of the MIME package. This method delegates to - * {@link MultipartBodyWriter#writePart(String, ContentTransferEncoding, String, List)}, but computes the content + * {@link MultipartBodyWriter#writePart(ContentType, ContentTransferEncoding, String, List)}, but computes the content * transfer encoding based on the content type and the {@link OMOutputFormat}. * * @param contentType @@ -139,7 +138,7 @@ public class OMMultipartWriter { * @throws IOException * if an I/O error occurs when writing to the underlying stream */ - public OutputStream writePart(String contentType, String contentID, List<Header> extraHeaders) throws IOException { + public OutputStream writePart(ContentType contentType, String contentID, List<Header> extraHeaders) throws IOException { return writer.writePart(contentType, getContentTransferEncoding(contentType), contentID, extraHeaders); } @@ -172,7 +171,19 @@ public class OMMultipartWriter { } } if (contentTransferEncoding == null) { - contentTransferEncoding = getContentTransferEncoding(dataHandler.getContentType()); + String contentTypeString = dataHandler.getContentType(); + if (contentTypeString != null) { + ContentType contentType; + try { + contentType = new ContentType(contentTypeString); + } catch (ParseException ex) { + throw new RuntimeException(ex); + } + contentTransferEncoding = getContentTransferEncoding(contentType); + } + } + if (contentTransferEncoding == null) { + contentTransferEncoding = ContentTransferEncoding.BINARY; } writer.writePart(dataHandler, contentTransferEncoding, contentID, extraHeaders); } diff --git a/axiom-api/src/main/java/org/apache/axiom/soap/SOAP11Constants.java b/axiom-api/src/main/java/org/apache/axiom/soap/SOAP11Constants.java index 5a7707438..1b421d793 100644 --- a/axiom-api/src/main/java/org/apache/axiom/soap/SOAP11Constants.java +++ b/axiom-api/src/main/java/org/apache/axiom/soap/SOAP11Constants.java @@ -21,6 +21,8 @@ package org.apache.axiom.soap; import javax.xml.namespace.QName; +import org.apache.axiom.mime.MediaType; + public interface SOAP11Constants extends SOAPConstants { static final String SOAP_ENVELOPE_NAMESPACE_URI = @@ -41,7 +43,7 @@ public interface SOAP11Constants extends SOAPConstants { static final String SOAP_FAULT_DETAIL_LOCAL_NAME = "detail"; //SOAP 1.2 Content Type - static final String SOAP_11_CONTENT_TYPE = "text/xml"; + static final MediaType SOAP_11_CONTENT_TYPE = MediaType.TEXT_XML; // -------- SOAP Fault Codes ------------------------------ static final String FAULT_CODE_SENDER = "Client"; diff --git a/axiom-api/src/main/java/org/apache/axiom/soap/SOAP12Constants.java b/axiom-api/src/main/java/org/apache/axiom/soap/SOAP12Constants.java index eaee64a57..8ac541ea6 100644 --- a/axiom-api/src/main/java/org/apache/axiom/soap/SOAP12Constants.java +++ b/axiom-api/src/main/java/org/apache/axiom/soap/SOAP12Constants.java @@ -21,6 +21,8 @@ package org.apache.axiom.soap; import javax.xml.namespace.QName; +import org.apache.axiom.mime.MediaType; + public interface SOAP12Constants extends SOAPConstants { public String SOAP_ENVELOPE_NAMESPACE_URI = @@ -61,7 +63,7 @@ public interface SOAP12Constants extends SOAPConstants { public static final String SOAP_FAULT_ROLE_LOCAL_NAME = "Role"; //SOAP 1.2 Content Type - public static final String SOAP_12_CONTENT_TYPE = "application/soap+xml"; + public static final MediaType SOAP_12_CONTENT_TYPE = MediaType.APPLICATION_SOAP_XML; // -------- SOAP Fault Codes ------------------------------ public static final String FAULT_CODE_SENDER = "Sender"; diff --git a/axiom-api/src/test/java/org/apache/axiom/mime/MultipartBodyWriterTest.java b/axiom-api/src/test/java/org/apache/axiom/mime/MultipartBodyWriterTest.java index 7096df03d..22102131d 100644 --- a/axiom-api/src/test/java/org/apache/axiom/mime/MultipartBodyWriterTest.java +++ b/axiom-api/src/test/java/org/apache/axiom/mime/MultipartBodyWriterTest.java @@ -38,7 +38,7 @@ public class MultipartBodyWriterTest extends TestCase { MultipartBodyWriter mpw = new MultipartBodyWriter(baos, UIDGenerator.generateMimeBoundary()); byte[] content = new byte[8192]; random.nextBytes(content); - OutputStream partOutputStream = mpw.writePart("application/octet-stream", contentTransferEncoding, UIDGenerator.generateContentId(), null); + OutputStream partOutputStream = mpw.writePart(new ContentType(MediaType.APPLICATION_OCTET_STREAM), contentTransferEncoding, UIDGenerator.generateContentId(), null); partOutputStream.write(content); partOutputStream.close(); mpw.complete(); diff --git a/axiom-api/src/test/java/org/apache/axiom/om/OMOutputFormatTest.java b/axiom-api/src/test/java/org/apache/axiom/om/OMOutputFormatTest.java index 4096aafe6..395a07d91 100644 --- a/axiom-api/src/test/java/org/apache/axiom/om/OMOutputFormatTest.java +++ b/axiom-api/src/test/java/org/apache/axiom/om/OMOutputFormatTest.java @@ -54,14 +54,14 @@ public class OMOutputFormatTest extends TestCase { public void testGetContentTypeDefault() { OMOutputFormat format = new OMOutputFormat(); String contentType = format.getContentType(); - assertTrue(contentType.equals(SOAP11Constants.SOAP_11_CONTENT_TYPE)); + assertTrue(contentType.equals(SOAP11Constants.SOAP_11_CONTENT_TYPE.toString())); } public void testGetContentTypeSOAP12() { OMOutputFormat format = new OMOutputFormat(); format.setSOAP11(false); String contentType = format.getContentType(); - assertTrue(contentType.equals(SOAP12Constants.SOAP_12_CONTENT_TYPE)); + assertTrue(contentType.equals(SOAP12Constants.SOAP_12_CONTENT_TYPE.toString())); } public void testGetContentTypeSOAP11MTOM() { @@ -71,7 +71,7 @@ public class OMOutputFormatTest extends TestCase { // This is rudimentary. We can add a more complete test that checks // sub items in the future. - assertTrue(contentType.indexOf(SOAP11Constants.SOAP_11_CONTENT_TYPE)!=-1); + assertTrue(contentType.indexOf(SOAP11Constants.SOAP_11_CONTENT_TYPE.toString())!=-1); assertTrue(contentType.indexOf(MTOMConstants.MTOM_TYPE)!=-1); // Test for a double quoted boundary value. @@ -89,7 +89,7 @@ public class OMOutputFormatTest extends TestCase { // This is rudimentary. We can add a more complete test that checks // sub items in the future. - assertTrue(contentType.indexOf(SOAP11Constants.SOAP_11_CONTENT_TYPE)>=0); + assertTrue(contentType.indexOf(SOAP11Constants.SOAP_11_CONTENT_TYPE.toString())>=0); assertTrue(contentType.indexOf("multipart/related")>=0); assertTrue(contentType.indexOf(MTOMConstants.MTOM_TYPE) < 0); @@ -104,7 +104,7 @@ public class OMOutputFormatTest extends TestCase { // This is rudimentary. We can add a more complete test that checks // sub items in the future. - assertTrue(contentType.indexOf(SOAP11Constants.SOAP_11_CONTENT_TYPE)>=0); + assertTrue(contentType.indexOf(SOAP11Constants.SOAP_11_CONTENT_TYPE.toString())>=0); assertTrue(contentType.indexOf("multipart/related")>=0); assertTrue(contentType.indexOf(MTOMConstants.MTOM_TYPE) < 0); @@ -123,7 +123,7 @@ public class OMOutputFormatTest extends TestCase { // This is rudimentary. We can add a more complete test that checks // sub items in the future. - assertTrue(contentType.indexOf(SOAP12Constants.SOAP_12_CONTENT_TYPE)!=-1); + assertTrue(contentType.indexOf(SOAP12Constants.SOAP_12_CONTENT_TYPE.toString())!=-1); assertTrue(contentType.indexOf(MTOMConstants.MTOM_TYPE)!=-1); } @@ -136,7 +136,7 @@ public class OMOutputFormatTest extends TestCase { // This is rudimentary. We can add a more complete test that checks // sub items in the future. - assertTrue(contentType.indexOf(SOAP12Constants.SOAP_12_CONTENT_TYPE)!=-1); + assertTrue(contentType.indexOf(SOAP12Constants.SOAP_12_CONTENT_TYPE.toString())!=-1); assertTrue(contentType.indexOf(MTOMConstants.MTOM_TYPE)!=-1); assertTrue(contentType.indexOf("action=\\\"testSoapAction\\\"")!=-1); } diff --git a/axiom-compat/src/main/java/org/apache/axiom/om/impl/MIMEOutputUtils.java b/axiom-compat/src/main/java/org/apache/axiom/om/impl/MIMEOutputUtils.java deleted file mode 100644 index 97098f80a..000000000 --- a/axiom-compat/src/main/java/org/apache/axiom/om/impl/MIMEOutputUtils.java +++ /dev/null @@ -1,359 +0,0 @@ -/* - * 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; - -import java.io.IOException; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.StringWriter; -import java.io.Writer; -import java.util.Arrays; -import java.util.Collection; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.Map; - -import javax.activation.DataHandler; -import javax.mail.MessagingException; -import javax.mail.internet.MimeBodyPart; - -import org.apache.axiom.attachments.Attachments; -import org.apache.axiom.attachments.ConfigurableDataHandler; -import org.apache.axiom.attachments.ByteArrayDataSource; -import org.apache.axiom.om.OMException; -import org.apache.axiom.om.OMOutputFormat; -import org.apache.axiom.om.OMText; -import org.apache.axiom.om.util.CommonUtils; -import org.apache.axiom.util.activation.DataHandlerWrapper; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** - * @deprecated The features of this class are now implemented by {@link OMMultipartWriter}, which - * has as cleaner API and supports streaming of individual MIME parts, in particular the - * SOAP part. - */ -@SuppressWarnings("rawtypes") -public class MIMEOutputUtils { - - private static final Log log = LogFactory.getLog(MIMEOutputUtils.class); - - private static byte[] CRLF = { 13, 10 }; - - /** - * @deprecated Use {@link OMMultipartWriter} instead. - */ - public static void complete(OutputStream outStream, - byte[] xmlData, - LinkedList binaryNodeList, - String boundary, - String contentId, - String charSetEncoding, - String SOAPContentType) { - complete(outStream, xmlData, binaryNodeList, boundary, - contentId, charSetEncoding, SOAPContentType, null); - } - - /** - * @deprecated Use {@link OMMultipartWriter} instead. - */ - public static void complete(OutputStream outStream, - byte[] xmlData, - LinkedList binaryNodeList, - String boundary, - String contentId, - String charSetEncoding, - String SOAPContentType, - OMOutputFormat omOutputFormat) { - try { - log.debug("Start: write the SOAPPart and the attachments"); - - // Write out the mime boundary - startWritingMime(outStream, boundary); - - javax.activation.DataHandler dh = - new javax.activation.DataHandler(new ByteArrayDataSource(xmlData, - "text/xml; charset=" + charSetEncoding)); - MimeBodyPart rootMimeBodyPart = new MimeBodyPart(); - rootMimeBodyPart.setDataHandler(dh); - - rootMimeBodyPart.addHeader("Content-Type", - "application/xop+xml; charset=" + charSetEncoding + - "; type=\"" + SOAPContentType + "\""); - rootMimeBodyPart.addHeader("Content-Transfer-Encoding", "binary"); - rootMimeBodyPart.addHeader("Content-ID", "<" + contentId + ">"); - - // Write out the SOAPPart - writeBodyPart(outStream, rootMimeBodyPart, boundary); - - // Now write out the Attachment parts (which are represented by the - // text nodes int the binary node list) - Iterator binaryNodeIterator = binaryNodeList.iterator(); - while (binaryNodeIterator.hasNext()) { - OMText binaryNode = (OMText) binaryNodeIterator.next(); - writeBodyPart(outStream, createMimeBodyPart(binaryNode.getContentID(), - binaryNode.getDataHandler(), omOutputFormat), boundary); - } - finishWritingMime(outStream); - outStream.flush(); - log.debug("End: write the SOAPPart and the attachments"); - } catch (IOException e) { - throw new OMException("Error while writing to the OutputStream.", e); - } catch (MessagingException e) { - throw new OMException("Problem writing Mime Parts.", e); - } - } - - /** - * @deprecated This method is only useful in conjunction with - * {@link #writeBodyPart(OutputStream, MimeBodyPart, String)}, which is deprecated. - */ - public static MimeBodyPart createMimeBodyPart(String contentID, - DataHandler dataHandler) - throws MessagingException { - return createMimeBodyPart(contentID, dataHandler, null); - } - - /** - * @deprecated This method is only useful in conjunction with - * {@link #writeBodyPart(OutputStream, MimeBodyPart, String)}, which is deprecated. - */ - public static MimeBodyPart createMimeBodyPart(String contentID, - DataHandler dataHandler, - OMOutputFormat omOutputFormat) - throws MessagingException { - String contentType = dataHandler.getContentType(); - - // Get the content-transfer-encoding - String contentTransferEncoding = "binary"; - if (dataHandler instanceof ConfigurableDataHandler) { - ConfigurableDataHandler configurableDataHandler = (ConfigurableDataHandler) dataHandler; - contentTransferEncoding = configurableDataHandler.getTransferEncoding(); - } - - if (log.isDebugEnabled()) { - log.debug("Create MimeBodyPart"); - log.debug(" Content-ID = " + contentID); - log.debug(" Content-Type = " + contentType); - log.debug(" Content-Transfer-Encoding = " + contentTransferEncoding); - } - - boolean useCTEBase64 = omOutputFormat != null && - Boolean.TRUE.equals( - omOutputFormat.getProperty( - OMOutputFormat.USE_CTE_BASE64_FOR_NON_TEXTUAL_ATTACHMENTS)); - if (useCTEBase64) { - if (!CommonUtils.isTextualPart(contentType) && - "binary".equals(contentTransferEncoding)) { - if (log.isDebugEnabled()) { - log.debug(" changing Content-Transfer-Encoding from " + - contentTransferEncoding + " to base-64"); - } - contentTransferEncoding = "base64"; - } - - } - - // Now create the mimeBodyPart for the datahandler and add the appropriate content headers - MimeBodyPart mimeBodyPart = new MimeBodyPart(); - mimeBodyPart.setDataHandler(dataHandler); - mimeBodyPart.addHeader("Content-ID", "<" + contentID + ">"); - mimeBodyPart.addHeader("Content-Type", contentType); - mimeBodyPart.addHeader("Content-Transfer-Encoding", contentTransferEncoding); - return mimeBodyPart; - } - - /** - * @deprecated Use {@link OMMultipartWriter} instead. - */ - public static void writeMimeBoundary(OutputStream outStream, - String boundary) throws IOException { - // REVIEW: This conversion is hard-coded to UTF-8. - // The complete solution is to respect the charset setting of the message. - // However this may cause problems in BoundaryDelimittedStream and other - // lower level classes. - outStream.write(new byte[] { 45, 45 }); - outStream.write(boundary.getBytes("UTF-8")); - } - - /** - * @deprecated Use {@link OMMultipartWriter} instead. - */ - public static void startWritingMime(OutputStream outStream, - String boundary) - throws IOException { - writeMimeBoundary(outStream, boundary); - //outStream.write(CRLF); - } - - /** - * @deprecated Use {@link OMMultipartWriter} instead. - */ - public static void writeBodyPart(OutputStream outStream, - MimeBodyPart part, - String boundary) throws IOException, - MessagingException { - if (log.isDebugEnabled()) { - log.debug("Start writeMimeBodyPart for " + part.getContentID()); - } - outStream.write(CRLF); - part.writeTo(outStream); - outStream.write(CRLF); - writeMimeBoundary(outStream, boundary); - outStream.flush(); - log.debug("End writeMimeBodyPart"); - } - - /** - * @deprecated Use {@link OMMultipartWriter} instead. - */ - public static void finishWritingMime(OutputStream outStream) - throws IOException { - log.debug("Write --, which indicates the end of the last boundary"); - outStream.write(new byte[] { 45, 45 }); - } - - /** - * @deprecated Use {@link OMMultipartWriter} instead. - */ - public static void writeSOAPWithAttachmentsMessage(StringWriter writer, - OutputStream outputStream, - Attachments attachments, - OMOutputFormat format) { - try { - OMMultipartWriter mpw = new OMMultipartWriter(outputStream, format); - - Writer rootPartWriter = new OutputStreamWriter(mpw.writeRootPart(), format.getCharSetEncoding()); - rootPartWriter.write(writer.toString()); - rootPartWriter.close(); - - // Get the collection of ids associated with the attachments - Collection ids = Arrays.asList(attachments.getAllContentIDs()); - - for (Iterator it = ids.iterator(); it.hasNext(); ) { - String id = (String)it.next(); - mpw.writePart(attachments.getDataHandler(id), id); - } - - mpw.complete(); - } catch (IOException ex) { - throw new OMException("Error writing SwA message", ex); - } - } - - /** - * @deprecated Use {@link OMMultipartWriter} instead. - */ - public static void writeDataHandlerWithAttachmentsMessage(DataHandler rootDataHandler, - String contentType, - OutputStream outputStream, - Map attachments, - OMOutputFormat format) { - writeDataHandlerWithAttachmentsMessage(rootDataHandler, - contentType, - outputStream, - attachments, - format, - null); - - } - - /** - * @deprecated Use {@link OMMultipartWriter} instead. - */ - public static void writeDataHandlerWithAttachmentsMessage(DataHandler rootDataHandler, - final String contentType, - OutputStream outputStream, - Map attachments, - OMOutputFormat format, - Collection ids) { - try { - if (!rootDataHandler.getContentType().equals(contentType)) { - rootDataHandler = new DataHandlerWrapper(rootDataHandler) { - @Override - public String getContentType() { - return contentType; - } - }; - } - - OMMultipartWriter mpw = new OMMultipartWriter(outputStream, format); - - mpw.writePart(rootDataHandler, format.getRootContentId()); - - Iterator idIterator = null; - if (ids == null) { - // If ids are not provided, use the attachment map - // to get the keys - idIterator = attachments.keySet().iterator(); - } else { - // if ids are provided (normal case), iterate - // over the ids so that the attachments are - // written in the same order as the id keys. - idIterator = ids.iterator(); - } - - while (idIterator.hasNext()) { - String key = (String) idIterator.next(); - mpw.writePart((DataHandler) attachments.get(key), key); - } - mpw.complete(); - outputStream.flush(); - } catch (IOException e) { - throw new OMException("Error while writing to the OutputStream.", e); - } - } - - /** - * @deprecated Axiom only supports standard SwA messages. However, {@link OMMultipartWriter} - * provides a flexible way to build MIME packages for non standard formats such as - * MM7. - */ - public static void writeMM7Message(StringWriter writer, - OutputStream outputStream, Attachments attachments, - OMOutputFormat format, String innerPartCID, - String innerBoundary) { - try { - OMMultipartWriter mpw = new OMMultipartWriter(outputStream, format); - - Writer rootPartWriter = new OutputStreamWriter(mpw.writeRootPart(), format.getCharSetEncoding()); - rootPartWriter.write(writer.toString()); - rootPartWriter.close(); - - if (attachments.getContentIDSet().size() != 0) { - OMOutputFormat innerFormat = new OMOutputFormat(format); - innerFormat.setMimeBoundary(innerBoundary); - OutputStream innerOutputStream = mpw.writePart("multipart/related; boundary=\"" + innerBoundary + "\"", innerPartCID); - OMMultipartWriter innerMpw = new OMMultipartWriter(innerOutputStream, innerFormat); - Collection ids = Arrays.asList(attachments.getAllContentIDs()); - for (Iterator it = ids.iterator(); it.hasNext(); ) { - String id = (String)it.next(); - innerMpw.writePart(attachments.getDataHandler(id), id); - } - innerMpw.complete(); - innerOutputStream.close(); - } - - mpw.complete(); - } catch (IOException e) { - throw new OMException("Error while writing to the OutputStream.", e); - } - } -} diff --git a/axiom-compat/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java b/axiom-compat/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java deleted file mode 100644 index 0fc5765f4..000000000 --- a/axiom-compat/src/test/java/org/apache/axiom/attachments/AttachmentsTest.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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.attachments; - -import org.apache.axiom.om.OMElement; -import org.apache.axiom.om.OMOutputFormat; -import org.apache.axiom.om.OMXMLBuilderFactory; -import org.apache.axiom.om.impl.MIMEOutputUtils; -import org.apache.axiom.soap.SOAPModelBuilder; -import org.apache.axiom.ts.soap.SwASample; - -import java.io.ByteArrayOutputStream; -import java.io.InputStream; -import java.io.StringWriter; - -import junit.framework.TestCase; - -@SuppressWarnings("deprecation") -public class AttachmentsTest extends TestCase { - public void testSWAWriteWithIncomingOrder() throws Exception { - - // Read the stream that has soap xml followed by BAttachment then AAttachment - InputStream inStream = SwASample.SAMPLE1.getInputStream(); - Attachments attachments = new Attachments(inStream, SwASample.SAMPLE1.getContentType()); - - // Get the contentIDs to force the reading - attachments.getAllContentIDs(); - - // Get the root - SOAPModelBuilder builder = OMXMLBuilderFactory.createSOAPModelBuilder(attachments.getRootPartInputStream(), "UTF-8"); - OMElement root = builder.getDocumentElement(); - StringWriter xmlWriter = new StringWriter(); - root.serialize(xmlWriter); - - // Serialize the message using the legacy behavior (order by content id) - OMOutputFormat format = new OMOutputFormat(); - format.setCharSetEncoding("utf-8"); - format.setDoingSWA(true); - - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - - MIMEOutputUtils.writeSOAPWithAttachmentsMessage(xmlWriter, baos, attachments, format); - - String text = baos.toString(); - // Assert that AAttachment occurs before BAttachment since - // that is the natural ordering of the content ids. - assertTrue(text.indexOf("BAttachment") < text.indexOf("AAttachment")); - - } -} diff --git a/mixins/om-mixins/src/main/java/org/apache/axiom/om/impl/mixin/AxiomContainerMixin.java b/mixins/om-mixins/src/main/java/org/apache/axiom/om/impl/mixin/AxiomContainerMixin.java index bb8c8a23d..49e86387b 100644 --- a/mixins/om-mixins/src/main/java/org/apache/axiom/om/impl/mixin/AxiomContainerMixin.java +++ b/mixins/om-mixins/src/main/java/org/apache/axiom/om/impl/mixin/AxiomContainerMixin.java @@ -21,6 +21,7 @@ package org.apache.axiom.om.impl.mixin; import java.io.IOException; import java.io.OutputStream; import java.io.Writer; +import java.text.ParseException; import java.util.Iterator; import javax.activation.DataHandler; @@ -48,6 +49,7 @@ import org.apache.axiom.core.stream.sax.input.XmlHandlerContentHandler; import org.apache.axiom.core.stream.serializer.Serializer; import org.apache.axiom.core.stream.stax.pull.output.StAXPivot; import org.apache.axiom.core.stream.stax.push.input.XMLStreamWriterNamespaceContextProvider; +import org.apache.axiom.mime.ContentType; import org.apache.axiom.mime.PartDataHandler; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMException; @@ -387,8 +389,13 @@ public abstract class AxiomContainerMixin implements AxiomContainer { if (cache || !(dataHandler instanceof PartDataHandler)) { multipartWriter.writePart(dataHandler, contentID); } else { - OutputStream part = - multipartWriter.writePart(dataHandler.getContentType(), contentID); + ContentType contentType; + try { + contentType = new ContentType(dataHandler.getContentType()); + } catch (ParseException ex) { + throw new OMException(ex); + } + OutputStream part = multipartWriter.writePart(contentType, contentID); IOUtils.copy( ((PartDataHandler) dataHandler).getPart().getInputStream(false), part,