ws-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From veit...@apache.org
Subject svn commit: r1748352 - in /webservices/axiom/trunk/axiom-api: pom.xml src/main/java/org/apache/axiom/attachments/MIMEMessageAdapter.java src/main/java/org/apache/axiom/mime/MIMEMessage.java
Date Tue, 14 Jun 2016 06:54:55 GMT
Author: veithen
Date: Tue Jun 14 06:54:55 2016
New Revision: 1748352

URL: http://svn.apache.org/viewvc?rev=1748352&view=rev
Log:
Add a fluent builder API for MIMEMessage.

Modified:
    webservices/axiom/trunk/axiom-api/pom.xml
    webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/MIMEMessageAdapter.java
    webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/mime/MIMEMessage.java

Modified: webservices/axiom/trunk/axiom-api/pom.xml
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/axiom-api/pom.xml?rev=1748352&r1=1748351&r2=1748352&view=diff
==============================================================================
--- webservices/axiom/trunk/axiom-api/pom.xml (original)
+++ webservices/axiom/trunk/axiom-api/pom.xml Tue Jun 14 06:54:55 2016
@@ -299,6 +299,7 @@
                                 org.apache.axiom.attachments.lifecycle.LifecycleManager ->
org.apache.axiom.attachments.lifecycle.impl.FileAccessor,
                                 <!-- TODO -->
                                 org.apache.axiom.mime.MIMEMessage -> org.apache.axiom.om.OMException,
+                                org.apache.axiom.mime.MIMEMessage$Builder -> org.apache.axiom.om.OMException,
                                 org.apache.axiom.mime.PartImpl -> org.apache.axiom.om.OMException,
                                 org.apache.axiom.om.impl.OMMultipartWriter -> org.apache.axiom.attachments.ConfigurableDataHandler,
                                 org.apache.axiom.om.ds.jaxb.AttachmentMarshallerImpl ->
org.apache.axiom.attachments.ByteArrayDataSource,

Modified: webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/MIMEMessageAdapter.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/MIMEMessageAdapter.java?rev=1748352&r1=1748351&r2=1748352&view=diff
==============================================================================
--- webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/MIMEMessageAdapter.java
(original)
+++ webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/attachments/MIMEMessageAdapter.java
Tue Jun 14 06:54:55 2016
@@ -70,12 +70,18 @@ final class MIMEMessageAdapter extends A
             filterIS = null;
         }
 
-        this.message = new MIMEMessage(inStream, contentTypeString, attachmentBlobFactory,
new DataHandlerFactory() {
-            @Override
-            public DataHandler createDataHandler(Part part) {
-                return new LegacyPartDataHandler(part);
-            }
-        }, this);
+        this.message = MIMEMessage.newBuilder()
+                .setInputStream(inStream)
+                .setContentType(contentTypeString)
+                .setAttachmentBlobFactory(attachmentBlobFactory)
+                .setDataHandlerFactory(new DataHandlerFactory() {
+                        @Override
+                        public DataHandler createDataHandler(Part part) {
+                            return new LegacyPartDataHandler(part);
+                        }
+                    })
+                .setPartCreationListener(this)
+                .build();
 
         rootPart = message.getRootPart();
         String rootPartContentID = rootPart.getContentID();

Modified: webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/mime/MIMEMessage.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/mime/MIMEMessage.java?rev=1748352&r1=1748351&r2=1748352&view=diff
==============================================================================
--- webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/mime/MIMEMessage.java
(original)
+++ webservices/axiom/trunk/axiom-api/src/main/java/org/apache/axiom/mime/MIMEMessage.java
Tue Jun 14 06:54:55 2016
@@ -49,6 +49,65 @@ public final class MIMEMessage implement
         void partCreated(Part part);
     }
 
+    public final static class Builder {
+        private InputStream inputStream;
+        private ContentType contentType;
+        private WritableBlobFactory<?> attachmentBlobFactory;
+        private DataHandlerFactory dataHandlerFactory;
+        private PartCreationListener partCreationListener;
+        
+        Builder() {}
+
+        public Builder setInputStream(InputStream inputStream) {
+            this.inputStream = inputStream;
+            return this;
+        }
+
+        public Builder setContentType(ContentType contentType) {
+            this.contentType = contentType;
+            return this;
+        }
+
+        public Builder setContentType(String contentType) {
+            try {
+                this.contentType = new ContentType(contentType);
+            } catch (ParseException ex) {
+                throw new OMException(ex);
+            }
+            return this;
+        }
+
+        public Builder setAttachmentBlobFactory(WritableBlobFactory<?> attachmentBlobFactory)
{
+            this.attachmentBlobFactory = attachmentBlobFactory;
+            return this;
+        }
+
+        public Builder setDataHandlerFactory(DataHandlerFactory dataHandlerFactory) {
+            this.dataHandlerFactory = dataHandlerFactory;
+            return this;
+        }
+
+        public Builder setPartCreationListener(PartCreationListener partCreationListener)
{
+            this.partCreationListener = partCreationListener;
+            return this;
+        }
+
+        public MIMEMessage build() {
+            if (inputStream == null) {
+                throw new IllegalArgumentException("inputStream is mandatory");
+            }
+            if (contentType == null) {
+                throw new IllegalArgumentException("contentType is mandatory");
+            }
+            return new MIMEMessage(
+                    inputStream,
+                    contentType,
+                    attachmentBlobFactory == null ? MemoryBlob.FACTORY : attachmentBlobFactory,
+                    dataHandlerFactory == null ? DataHandlerFactory.DEFAULT : dataHandlerFactory,
+                    partCreationListener);
+        }
+    }
+
     private static final Log log = LogFactory.getLog(MIMEMessage.class);
     
     /** <code>ContentType</code> of the MIME message */
@@ -73,20 +132,14 @@ public final class MIMEMessage implement
     private final DataHandlerFactory dataHandlerFactory;
     private final PartCreationListener partCreationListener;
     
-    public MIMEMessage(InputStream inStream, String contentTypeString,
+    MIMEMessage(InputStream inStream, ContentType contentType,
             WritableBlobFactory<?> attachmentBlobFactory,
             DataHandlerFactory dataHandlerFactory,
             PartCreationListener partCreationListener) throws OMException {
         this.attachmentBlobFactory = attachmentBlobFactory;
         this.dataHandlerFactory = dataHandlerFactory;
         this.partCreationListener = partCreationListener;
-        try {
-            contentType = new ContentType(contentTypeString);
-        } catch (ParseException e) {
-            throw new OMException(
-                    "Invalid Content Type Field in the Mime Message"
-                    , e);
-        }
+        this.contentType = contentType;
 
         String start = contentType.getParameter("start");
         rootPartContentID = start == null ? null : normalizeContentID(start);
@@ -95,7 +148,7 @@ public final class MIMEMessage implement
         config.setStrictParsing(true);
         parser = new MimeTokenStream(config);
         parser.setRecursionMode(RecursionMode.M_NO_RECURSE);
-        parser.parseHeadless(inStream, contentTypeString);
+        parser.parseHeadless(inStream, contentType.toString());
         
         // Move the parser to the beginning of the first part
         while (parser.getState() != EntityState.T_START_BODYPART) {
@@ -109,6 +162,10 @@ public final class MIMEMessage implement
         }
     }
 
+    public static Builder newBuilder() {
+        return new Builder();
+    }
+
     private static String normalizeContentID(String contentID) {
         contentID = contentID.trim();
         if (contentID.length() >= 2 && contentID.charAt(0) == '<'



Mime
View raw message