Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixes MIME4J-225 and MIME4J-222

* thanks Rustam Gupta for the MIME4J-222 patch
* changed version format to 0.8.0

git-svn-id: https://svn.apache.org/repos/asf/james/mime4j/trunk@1457434 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
commit f3a5ae285d1c60db9b7ad0ec06e477c7a51e8c70 1 parent 39212b8
@ieugen ieugen authored
View
16 dom/src/main/java/org/apache/james/mime4j/message/DefaultMessageBuilder.java
@@ -49,6 +49,7 @@
public class DefaultMessageBuilder implements MessageBuilder {
private FieldParser<? extends ParsedField> fieldParser = null;
+ private MessageImplFactory messageImplFactory = null;
private BodyFactory bodyFactory = null;
private MimeConfig config = null;
private BodyDescriptorBuilder bodyDescBuilder = null;
@@ -64,6 +65,10 @@ public void setFieldParser(final FieldParser<? extends ParsedField> fieldParser)
this.fieldParser = fieldParser;
}
+ public void setMessageImplFactory(final MessageImplFactory messageImplFactory) {
+ this.messageImplFactory = messageImplFactory;
+ }
+
public void setBodyFactory(final BodyFactory bodyFactory) {
this.bodyFactory = bodyFactory;
}
@@ -220,7 +225,7 @@ public Body copy(Body body) {
* {@link SingleBody}.
*/
public Message copy(Message other) {
- MessageImpl copy = new MessageImpl();
+ MessageImpl copy = newMessageImpl();
if (other.getHeader() != null) {
copy.setHeader(copy(other.getHeader()));
}
@@ -280,7 +285,7 @@ public void field(Field field) throws MimeException {
}
public Message newMessage() {
- return new MessageImpl();
+ return newMessageImpl();
}
public Message newMessage(final Message source) {
@@ -289,7 +294,7 @@ public Message newMessage(final Message source) {
public Message parseMessage(final InputStream is) throws IOException, MimeIOException {
try {
- MessageImpl message = new MessageImpl();
+ MessageImpl message = newMessageImpl();
MimeConfig cfg = config != null ? config : new MimeConfig();
boolean strict = cfg.isStrictParsing();
DecodeMonitor mon = monitor != null ? monitor :
@@ -315,4 +320,9 @@ public Message parseMessage(final InputStream is) throws IOException, MimeIOExce
}
}
+ private MessageImpl newMessageImpl() {
+ MessageImplFactory mif = messageImplFactory != null ? messageImplFactory : new DefaultMessageImplFactory();
+ return mif.messageImpl();
+ }
+
}
View
31 dom/src/main/java/org/apache/james/mime4j/message/DefaultMessageImplFactory.java
@@ -0,0 +1,31 @@
+/****************************************************************
+ * 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.james.mime4j.message;
+
+/**
+ * A default MessageImplFactory that returns an unmodified MessageImpl using the default constructor.
+ */
+public class DefaultMessageImplFactory implements MessageImplFactory {
+
+ @Override
+ public MessageImpl messageImpl() {
+ return new MessageImpl();
+ }
+}
View
75 dom/src/main/java/org/apache/james/mime4j/message/EntityBuilder.java
@@ -19,10 +19,6 @@
package org.apache.james.mime4j.message;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Stack;
-
import org.apache.james.mime4j.MimeException;
import org.apache.james.mime4j.dom.Body;
import org.apache.james.mime4j.dom.Entity;
@@ -32,10 +28,13 @@
import org.apache.james.mime4j.parser.ContentHandler;
import org.apache.james.mime4j.stream.BodyDescriptor;
import org.apache.james.mime4j.stream.Field;
-import org.apache.james.mime4j.stream.RawField;
import org.apache.james.mime4j.util.ByteArrayBuffer;
import org.apache.james.mime4j.util.ByteSequence;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Stack;
+
/**
* A <code>ContentHandler</code> for building an <code>Entity</code> to be
* used in conjunction with a {@link org.apache.james.mime4j.parser.MimeStreamParser}.
@@ -43,6 +42,7 @@
class EntityBuilder implements ContentHandler {
private final Entity entity;
+ private MessageImplFactory messageImplFactory;
private final BodyFactory bodyFactory;
private final Stack<Object> stack;
@@ -50,6 +50,17 @@
final Entity entity,
final BodyFactory bodyFactory) {
this.entity = entity;
+ this.messageImplFactory = new DefaultMessageImplFactory();
+ this.bodyFactory = bodyFactory;
+ this.stack = new Stack<Object>();
+ }
+
+ EntityBuilder(
+ final Entity entity,
+ final MessageImplFactory messageImplFactory,
+ final BodyFactory bodyFactory) {
+ this.entity = entity;
+ this.messageImplFactory = messageImplFactory;
this.bodyFactory = bodyFactory;
this.stack = new Stack<Object>();
}
@@ -62,45 +73,36 @@ private void expect(Class<?> c) {
}
}
- /**
- * @see org.apache.james.mime4j.parser.ContentHandler#startMessage()
- */
+ @Override
public void startMessage() throws MimeException {
if (stack.isEmpty()) {
stack.push(this.entity);
} else {
expect(Entity.class);
- Message m = new MessageImpl();
+ Message m = messageImplFactory.messageImpl();
((Entity) stack.peek()).setBody(m);
stack.push(m);
}
}
- /**
- * @see org.apache.james.mime4j.parser.ContentHandler#endMessage()
- */
+ @Override
public void endMessage() throws MimeException {
expect(Message.class);
stack.pop();
}
- /**
- * @see org.apache.james.mime4j.parser.ContentHandler#startHeader()
- */
+ @Override
public void startHeader() throws MimeException {
stack.push(new HeaderImpl());
}
- /**
- */
+ @Override
public void field(Field field) throws MimeException {
expect(Header.class);
((Header) stack.peek()).addField(field);
}
- /**
- * @see org.apache.james.mime4j.parser.ContentHandler#endHeader()
- */
+ @Override
public void endHeader() throws MimeException {
expect(Header.class);
Header h = (Header) stack.pop();
@@ -108,9 +110,7 @@ public void endHeader() throws MimeException {
((Entity) stack.peek()).setHeader(h);
}
- /**
- * @see org.apache.james.mime4j.parser.ContentHandler#startMultipart(org.apache.james.mime4j.stream.BodyDescriptor)
- */
+ @Override
public void startMultipart(final BodyDescriptor bd) throws MimeException {
expect(Entity.class);
@@ -121,9 +121,7 @@ public void startMultipart(final BodyDescriptor bd) throws MimeException {
stack.push(multiPart);
}
- /**
- * @see org.apache.james.mime4j.parser.ContentHandler#body(org.apache.james.mime4j.stream.BodyDescriptor, java.io.InputStream)
- */
+ @Override
public void body(BodyDescriptor bd, final InputStream is) throws MimeException, IOException {
expect(Entity.class);
@@ -156,16 +154,12 @@ public void body(BodyDescriptor bd, final InputStream is) throws MimeException,
entity.setBody(body);
}
- /**
- * @see org.apache.james.mime4j.parser.ContentHandler#endMultipart()
- */
+ @Override
public void endMultipart() throws MimeException {
stack.pop();
}
- /**
- * @see org.apache.james.mime4j.parser.ContentHandler#startBodyPart()
- */
+ @Override
public void startBodyPart() throws MimeException {
expect(Multipart.class);
@@ -174,26 +168,20 @@ public void startBodyPart() throws MimeException {
stack.push(bodyPart);
}
- /**
- * @see org.apache.james.mime4j.parser.ContentHandler#endBodyPart()
- */
+ @Override
public void endBodyPart() throws MimeException {
expect(BodyPart.class);
stack.pop();
}
- /**
- * @see org.apache.james.mime4j.parser.ContentHandler#epilogue(java.io.InputStream)
- */
+ @Override
public void epilogue(InputStream is) throws MimeException, IOException {
expect(MultipartImpl.class);
ByteSequence bytes = loadStream(is);
((MultipartImpl) stack.peek()).setEpilogueRaw(bytes);
}
- /**
- * @see org.apache.james.mime4j.parser.ContentHandler#preamble(java.io.InputStream)
- */
+ @Override
public void preamble(InputStream is) throws MimeException, IOException {
expect(MultipartImpl.class);
ByteSequence bytes = loadStream(is);
@@ -202,8 +190,11 @@ public void preamble(InputStream is) throws MimeException, IOException {
/**
* Unsupported.
- * @see org.apache.james.mime4j.parser.ContentHandler#raw(java.io.InputStream)
+ *
+ * @param is the raw contents of the entity.
+ * @throws UnsupportedOperationException
*/
+ @Override
public void raw(InputStream is) throws MimeException, IOException {
throw new UnsupportedOperationException("Not supported");
}
View
29 dom/src/main/java/org/apache/james/mime4j/message/MessageImplFactory.java
@@ -0,0 +1,29 @@
+/****************************************************************
+ * 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.james.mime4j.message;
+
+/**
+ * Factory for creating message implementations.
+ */
+public interface MessageImplFactory {
+
+ MessageImpl messageImpl();
+
+}

2 comments on commit f3a5ae2

@rocketraman

@ieugen FYI - Its "Raman Gupta", not "Rustam Gupta" :) Thanks for the attribution though.

@ieugen
Please sign in to comment.
Something went wrong with that request. Please try again.