You are viewing a plain text version of this content. The canonical link for it is here.
Posted to mime4j-dev@james.apache.org by ba...@apache.org on 2010/03/27 22:17:12 UTC
svn commit: r928281 - in /james/mime4j/trunk/dom/src:
main/java/org/apache/james/mime4j/dom/
main/java/org/apache/james/mime4j/message/
test/java/org/apache/james/mime4j/dom/
Author: bago
Date: Sat Mar 27 21:17:08 2010
New Revision: 928281
URL: http://svn.apache.org/viewvc?rev=928281&view=rev
Log:
Make sure the MessageWriter for the DOM does not introduce a preamble when it's been built from a message with no preamble. Multipart preamble and epilogue are now nullable so to be able to distinguish between empty preamble/epilogue and no preamble/epilogue (MIME4J-176)
Modified:
james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/Multipart.java
james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MessageWriter.java
james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MultipartImpl.java
james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/MessageParserTest.java
james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/MessageWriteToTest.java
james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/MultipartFormTest.java
Modified: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/Multipart.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/Multipart.java?rev=928281&r1=928280&r2=928281&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/Multipart.java (original)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/Multipart.java Sat Mar 27 21:17:08 2010
@@ -194,14 +194,14 @@ public abstract class Multipart implemen
}
/**
- * Gets the preamble.
+ * Gets the preamble or null if the message has no preamble.
*
* @return the preamble.
*/
public abstract String getPreamble();
/**
- * Sets the preamble.
+ * Sets the preamble with a value or null to remove the preamble.
*
* @param preamble
* the preamble.
@@ -209,14 +209,14 @@ public abstract class Multipart implemen
public abstract void setPreamble(String preamble);
/**
- * Gets the epilogue.
+ * Gets the epilogue or null if the message has no epilogue
*
* @return the epilogue.
*/
public abstract String getEpilogue();
/**
- * Sets the epilogue.
+ * Sets the epilogue value, or remove it if the value passed is null.
*
* @param epilogue
* the epilogue.
Modified: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MessageWriter.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MessageWriter.java?rev=928281&r1=928280&r2=928281&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MessageWriter.java (original)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MessageWriter.java Sat Mar 27 21:17:08 2010
@@ -141,11 +141,13 @@ public class MessageWriter {
preamble = ((MultipartImpl) multipart).getPreambleRaw();
epilogue = ((MultipartImpl) multipart).getEpilogueRaw();
} else {
- preamble = ContentUtil.encode(multipart.getPreamble());
- epilogue = ContentUtil.encode(multipart.getEpilogue());
+ preamble = multipart.getPreamble() != null ? ContentUtil.encode(multipart.getPreamble()) : null;
+ epilogue = multipart.getEpilogue() != null ? ContentUtil.encode(multipart.getEpilogue()) : null;
+ }
+ if (preamble != null) {
+ writeBytes(preamble, out);
+ out.write(CRLF);
}
- writeBytes(preamble, out);
- out.write(CRLF);
for (Entity bodyPart : multipart.getBodyParts()) {
out.write(DASHES);
@@ -159,9 +161,10 @@ public class MessageWriter {
out.write(DASHES);
writeBytes(boundary, out);
out.write(DASHES);
- out.write(CRLF);
-
- writeBytes(epilogue, out);
+ if (epilogue != null) {
+ out.write(CRLF);
+ writeBytes(epilogue, out);
+ }
}
/**
Modified: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MultipartImpl.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MultipartImpl.java?rev=928281&r1=928280&r2=928281&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MultipartImpl.java (original)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/MultipartImpl.java Sat Mar 27 21:17:08 2010
@@ -37,18 +37,22 @@ public class MultipartImpl extends Multi
private ByteSequence preamble;
private transient String preambleStrCache;
+ private transient boolean preambleComputed = false;
private ByteSequence epilogue;
private transient String epilogueStrCache;
+ private transient boolean epilogueComputed = false;
/**
* Creates a new empty <code>Multipart</code> instance.
*/
public MultipartImpl(String subType) {
super(subType);
- preamble = ByteSequence.EMPTY;
- preambleStrCache = "";
- epilogue = ByteSequence.EMPTY;
- epilogueStrCache = "";
+ preamble = null;
+ preambleStrCache = null;
+ preambleComputed = true;
+ epilogue = null;
+ epilogueStrCache = null;
+ epilogueComputed = true;
}
/**
@@ -80,8 +84,10 @@ public class MultipartImpl extends Multi
if (other instanceof MultipartImpl) {
preamble = ((MultipartImpl) other).preamble;
epilogue = ((MultipartImpl) other).epilogue;
- preambleStrCache = ((MultipartImpl) other).preambleStrCache;
- epilogueStrCache = ((MultipartImpl) other).epilogueStrCache;
+ preambleStrCache = ((MultipartImpl) other).preambleStrCache;
+ epilogueStrCache = ((MultipartImpl) other).epilogueStrCache;
+ preambleComputed = ((MultipartImpl) other).preambleComputed;
+ epilogueComputed = ((MultipartImpl) other).epilogueComputed;
} else {
setPreamble(other.getPreamble());
setEpilogue(other.getEpilogue());
@@ -96,6 +102,7 @@ public class MultipartImpl extends Multi
public void setPreambleRaw(ByteSequence preamble) {
this.preamble = preamble;
this.preambleStrCache = null;
+ this.preambleComputed = false;
}
/**
@@ -104,8 +111,9 @@ public class MultipartImpl extends Multi
* @return the preamble.
*/
public String getPreamble() {
- if (preambleStrCache == null) {
- preambleStrCache = ContentUtil.decode(preamble);
+ if (!preambleComputed) {
+ preambleStrCache = preamble != null ? ContentUtil.decode(preamble) : null;
+ preambleComputed = true;
}
return preambleStrCache;
}
@@ -117,8 +125,9 @@ public class MultipartImpl extends Multi
* the preamble.
*/
public void setPreamble(String preamble) {
- this.preamble = ContentUtil.encode(preamble);
+ this.preamble = preamble != null ? ContentUtil.encode(preamble) : null;
this.preambleStrCache = preamble;
+ this.preambleComputed = true;
}
// package private for now; might become public someday
@@ -129,6 +138,7 @@ public class MultipartImpl extends Multi
public void setEpilogueRaw(ByteSequence epilogue) {
this.epilogue = epilogue;
this.epilogueStrCache = null;
+ this.epilogueComputed = false;
}
/**
@@ -137,8 +147,9 @@ public class MultipartImpl extends Multi
* @return the epilogue.
*/
public String getEpilogue() {
- if (epilogueStrCache == null) {
- epilogueStrCache = ContentUtil.decode(epilogue);
+ if (!epilogueComputed) {
+ epilogueStrCache = epilogue != null ? ContentUtil.decode(epilogue) : null;
+ epilogueComputed = true;
}
return epilogueStrCache;
}
@@ -150,8 +161,9 @@ public class MultipartImpl extends Multi
* the epilogue.
*/
public void setEpilogue(String epilogue) {
- this.epilogue = ContentUtil.encode(epilogue);
+ this.epilogue = epilogue != null ? ContentUtil.encode(epilogue) : null;
this.epilogueStrCache = epilogue;
+ this.epilogueComputed = true;
}
}
Modified: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/MessageParserTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/MessageParserTest.java?rev=928281&r1=928280&r2=928281&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/MessageParserTest.java (original)
+++ james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/MessageParserTest.java Sat Mar 27 21:17:08 2010
@@ -143,18 +143,22 @@ public class MessageParserTest extends T
Multipart multipart =(Multipart) e.getBody();
List<Entity> parts = multipart.getBodyParts();
- sb.append("<preamble>\r\n");
- sb.append(escape(multipart.getPreamble()));
- sb.append("</preamble>\r\n");
+ if (multipart.getPreamble() != null) {
+ sb.append("<preamble>\r\n");
+ sb.append(escape(multipart.getPreamble()));
+ sb.append("</preamble>\r\n");
+ }
int i = 1;
for (Entity bodyPart : parts) {
sb.append(getStructure(bodyPart, prefix, id + "_" + (i++)));
}
- sb.append("<epilogue>\r\n");
- sb.append(escape(multipart.getEpilogue()));
- sb.append("</epilogue>\r\n");
+ if (multipart.getEpilogue() != null) {
+ sb.append("<epilogue>\r\n");
+ sb.append(escape(multipart.getEpilogue()));
+ sb.append("</epilogue>\r\n");
+ }
sb.append("</multipart>\r\n");
Modified: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/MessageWriteToTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/MessageWriteToTest.java?rev=928281&r1=928280&r2=928281&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/MessageWriteToTest.java (original)
+++ james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/MessageWriteToTest.java Sat Mar 27 21:17:08 2010
@@ -49,6 +49,7 @@ public class MessageWriteToTest extends
private void assertEquals(byte[] expected, byte[] actual) {
StringBuilder buffer = new StringBuilder(expected.length);
+ assertEquals(new String(expected), new String(actual));
assertEquals(expected.length, actual.length);
for (int i = 0; i < actual.length; i++) {
buffer.append((char)actual[i]);
@@ -64,6 +65,22 @@ public class MessageWriteToTest extends
assertEquals(ExampleMail.MULTIPART_WITH_BINARY_ATTACHMENTS_BYTES, out.toByteArray());
}
+ public void testBinaryAttachmentNoPreamble() throws Exception {
+ MessageImpl message = createMessage(ExampleMail.MULTIPART_WITH_BINARY_ATTACHMENTS_NOPREAMBLE_BYTES);
+ assertTrue("Is multipart", message.isMultipart());
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ message.writeTo(out);
+ assertEquals(ExampleMail.MULTIPART_WITH_BINARY_ATTACHMENTS_NOPREAMBLE_BYTES, out.toByteArray());
+ }
+
+ public void testBinaryAttachmentPreambleEpilogue() throws Exception {
+ MessageImpl message = createMessage(ExampleMail.MULTIPART_WITH_BINARY_ATTACHMENTS_PREAMBLE_EPILOGUE_BYTES);
+ assertTrue("Is multipart", message.isMultipart());
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ message.writeTo(out);
+ assertEquals(ExampleMail.MULTIPART_WITH_BINARY_ATTACHMENTS_PREAMBLE_EPILOGUE_BYTES, out.toByteArray());
+ }
+
private MessageImpl createMessage(byte[] octets) throws Exception {
ByteArrayInputStream in = new ByteArrayInputStream(octets);
MessageImpl message = new MessageImpl(in);
Modified: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/MultipartFormTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/MultipartFormTest.java?rev=928281&r1=928280&r2=928281&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/MultipartFormTest.java (original)
+++ james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/MultipartFormTest.java Sat Mar 27 21:17:08 2010
@@ -69,8 +69,7 @@ public class MultipartFormTest extends T
MessageWriter.DEFAULT.writeMultipart(multipart, out);
out.close();
- String expected = "\r\n" +
- "--foo\r\n" +
+ String expected = "--foo\r\n" +
"Content-Type: text/plain\r\n" +
"\r\n" +
"this stuff\r\n" +
@@ -82,7 +81,7 @@ public class MultipartFormTest extends T
"Content-Type: text/plain\r\n" +
"\r\n" +
"all kind of stuff\r\n" +
- "--foo--\r\n";
+ "--foo--";
String s = out.toString("US-ASCII");
assertEquals(expected, s);
}