Skip to content

Commit

Permalink
Fixes MIME4J-225 and MIME4J-222
Browse files Browse the repository at this point in the history
* 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
ieugen committed Mar 17, 2013
1 parent 39212b8 commit f3a5ae2
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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()));
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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 :
Expand All @@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,24 +28,39 @@
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}.
*/
class EntityBuilder implements ContentHandler {

private final Entity entity;
private MessageImplFactory messageImplFactory;
private final BodyFactory bodyFactory;
private final Stack<Object> stack;

EntityBuilder(
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>();
}
Expand All @@ -62,55 +73,44 @@ 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();
expect(Entity.class);
((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);

Expand All @@ -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);

Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand All @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@ieugen
Copy link
Member Author

@ieugen ieugen commented on f3a5ae2 Mar 17, 2013 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.