Author: olegk
Date: Fri Apr 1 09:49:08 2011
New Revision: 1087633
URL: http://svn.apache.org/viewvc?rev=1087633&view=rev
Log:
MIME4J-145: moved code splitting raw byte sequence into field name / field body
parts from RawField to RawFieldParser; RawField still contains logic to extract
and unfold body content, which probably should also be moved to RawFieldParser
Added:
james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java
(with props)
james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldParserTest.java
(with props)
james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldTest.java
(with props)
Modified:
james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/AbstractEntity.java
james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/DefaultFieldBuilder.java
james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawField.java
james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/BaseTestForBodyDescriptors.java
james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/DefaultFieldParser.java
james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/EntityBuilder.java
Modified:
james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/AbstractEntity.java
URL:
http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/AbstractEntity.java?rev=1087633&r1=1087632&r2=1087633&view=diff
==============================================================================
---
james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/AbstractEntity.java
(original)
+++
james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/AbstractEntity.java
Fri Apr 1 09:49:08 2011
@@ -144,7 +144,7 @@ abstract class AbstractEntity implements
if (field == null) {
continue;
}
- if (field.isObsoleteSyntax()) {
+ if (field.isUsedObsoleteSyntax()) {
monitor(Event.OBSOLETE_HEADER);
}
body.addField(field);
@@ -153,7 +153,9 @@ abstract class AbstractEntity implements
monitor(Event.INVALID_HEADER);
if (config.isMalformedHeaderStartsBody()) {
LineReaderInputStream instream = getDataStream();
- if (!instream.unread(fieldBuilder.getRaw())) {
+ ByteArrayBuffer buf = fieldBuilder.getRaw();
+ // Complain, if raw data is not available or cannot be
'unread'
+ if (buf == null || !instream.unread(buf)) {
throw new
MimeParseEventException(Event.INVALID_HEADER);
}
return false;
Modified:
james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/DefaultFieldBuilder.java
URL:
http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/DefaultFieldBuilder.java?rev=1087633&r1=1087632&r2=1087633&view=diff
==============================================================================
---
james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/DefaultFieldBuilder.java
(original)
+++
james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/DefaultFieldBuilder.java
Fri Apr 1 09:49:08 2011
@@ -59,7 +59,7 @@ public class DefaultFieldBuilder impleme
}
}
ByteArrayBuffer copy = new ByteArrayBuffer(this.buf.buffer(), len,
false);
- return new RawField(copy);
+ return RawFieldParser.DEFAULT.parseField(copy);
}
public ByteArrayBuffer getRaw() {
Modified:
james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawField.java
URL:
http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawField.java?rev=1087633&r1=1087632&r2=1087633&view=diff
==============================================================================
---
james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawField.java
(original)
+++
james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawField.java
Fri Apr 1 09:49:08 2011
@@ -19,9 +19,6 @@
package org.apache.james.mime4j.stream;
-import java.util.BitSet;
-
-import org.apache.james.mime4j.MimeException;
import org.apache.james.mime4j.util.ByteSequence;
import org.apache.james.mime4j.util.ContentUtil;
import org.apache.james.mime4j.util.MimeUtil;
@@ -29,108 +26,67 @@ import org.apache.james.mime4j.util.Mime
/**
* The basic immutable MIME field.
*/
-public class RawField {
+public final class RawField {
- private static final BitSet fieldChars = new BitSet();
+ private final ByteSequence raw;
+ private final int delimiterIdx;
+ private final boolean obsolete;
+ private final String name;
+ private final String body;
- static {
- for (int i = 0x21; i <= 0x39; i++) {
- fieldChars.set(i);
- }
- for (int i = 0x3b; i <= 0x7e; i++) {
- fieldChars.set(i);
+ RawField(ByteSequence raw, int delimiterIdx, boolean obsolete, String
name, String body) {
+ if (name == null) {
+ throw new IllegalArgumentException("Field may not be null");
}
+ this.raw = raw;
+ this.delimiterIdx = delimiterIdx;
+ this.obsolete = obsolete;
+ this.name = name.trim();
+ this.body = body;
}
-
- private ByteSequence raw;
- private int colonIdx;
- private int headerNameEndIdx;
-
- private String name;
- private String body;
- private final boolean obsoleteSyntax;
-
public RawField(String name, String body) {
- this.name = name;
- this.body = body;
- this.raw = null;
- this.obsoleteSyntax = false;
- }
-
- /**
- * @param raw bytes
- * @throws MimeException on malformed data
- */
- public RawField(ByteSequence raw) throws MimeException {
- this.raw = raw;
-
- colonIdx = -1;
- boolean obsolete = false;
- for (int i = 0; i < raw.length(); i++) {
- if (!fieldChars.get(raw.byteAt(i) & 0xff)) {
- headerNameEndIdx = i;
- for (; i < raw.length(); i++) {
- int j = raw.byteAt(i) & 0xff;
- if (j == ':') {
- colonIdx = i;
- break;
- } else if (j != 0x20 && j != 0x09) {
- throw new MimeException("Invalid header.
Unexpected char after colon: "+j);
- } else {
- obsolete = true;
- }
- }
- break;
- }
- }
- if (colonIdx == -1) throw new MimeException("Invalid header. No colon
found.");
- obsoleteSyntax = obsolete;
+ this(null, -1, false, name, body);
}
- public String getName() {
- if (name == null) {
- name = parseName();
- }
+ public ByteSequence getRaw() {
+ return raw;
+ }
+ public String getName() {
return name;
}
public String getBody() {
- if (body == null) {
- body = parseBody();
+ if (body != null) {
+ return body;
}
-
- return body;
+ if (raw != null) {
+ int len = raw.length();
+ int off = delimiterIdx + 1;
+ if (len > off + 1 && (raw.byteAt(off) & 0xff) == 0x20) off++;
+ return MimeUtil.unfold(ContentUtil.decode(raw, off, len - off));
+ }
+ return null;
}
- public ByteSequence getRaw() {
- if (raw == null) {
- raw = ContentUtil.encode(MimeUtil.fold(name+": "+body, 0));
- }
- return raw;
+ boolean isUsedObsoleteSyntax() {
+ return obsolete;
}
@Override
public String toString() {
- return getName() + ": " + getBody();
- }
-
- private String parseName() {
- // make sure we ignore ending WSP (obsolete rfc822 syntax)
- return ContentUtil.decode(raw, 0, headerNameEndIdx);
- }
-
- private String parseBody() {
- int offset = colonIdx + 1;
- // if the header body starts with a space we remove it.
- if (raw.length() > offset + 1 && (raw.byteAt(offset) & 0xff) == 0x20)
offset++;
- int length = raw.length() - offset;
- return MimeUtil.unfold(ContentUtil.decode(raw, offset, length));
+ if (raw != null) {
+ return ContentUtil.decode(raw);
+ } else {
+ StringBuilder buf = new StringBuilder();
+ buf.append(name);
+ buf.append(": ");
+ if (body != null) {
+ buf.append(body);
+ }
+ return buf.toString();
+ }
}
-
- public boolean isObsoleteSyntax() {
- return obsoleteSyntax;
- }
-
+
}
Added:
james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java
URL:
http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java?rev=1087633&view=auto
==============================================================================
---
james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java
(added)
+++
james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java
Fri Apr 1 09:49:08 2011
@@ -0,0 +1,80 @@
+/****************************************************************
+ * 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.stream;
+
+import java.util.BitSet;
+
+import org.apache.james.mime4j.MimeException;
+import org.apache.james.mime4j.util.ByteSequence;
+import org.apache.james.mime4j.util.ContentUtil;
+
+/**
+ * The basic immutable MIME field.
+ */
+public class RawFieldParser {
+
+ static final int COLON = ':';
+ static final int SPACE = 0x20;
+ static final int TAB = 0x09;
+
+ private static final BitSet FIELD_CHARS = new BitSet();
+
+ public static final RawFieldParser DEFAULT = new RawFieldParser();
+
+ static {
+ for (int i = 0x21; i <= 0x39; i++) {
+ FIELD_CHARS.set(i);
+ }
+ for (int i = 0x3b; i <= 0x7e; i++) {
+ FIELD_CHARS.set(i);
+ }
+ }
+
+ public RawField parseField(final ByteSequence raw) throws MimeException {
+ int len = raw.length();
+ int colonIdx = -1;
+ int headerNameEndIdx = -1;
+ boolean obsolete = false;
+ for (int i = 0; i < len; i++) {
+ if (!FIELD_CHARS.get(raw.byteAt(i) & 0xff)) {
+ headerNameEndIdx = i;
+ for (; i < len; i++) {
+ int j = raw.byteAt(i) & 0xff;
+ if (j == COLON) {
+ colonIdx = i;
+ break;
+ } else if (j != SPACE && j != TAB) {
+ throw new MimeException("Invalid header: unexpected
char " + j + " after colon");
+ } else {
+ obsolete = true;
+ }
+ }
+ break;
+ }
+ }
+ if (colonIdx == -1) {
+ throw new MimeException("Invalid header: no colon found");
+ }
+ // make sure we ignore ending WSP (obsolete rfc822 syntax)
+ String name = ContentUtil.decode(raw, 0, headerNameEndIdx);
+ return new RawField(raw, colonIdx, obsolete, name, null);
+ }
+
+}
Propchange:
james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange:
james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/stream/RawFieldParser.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified:
james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/BaseTestForBodyDescriptors.java
URL:
http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/BaseTestForBodyDescriptors.java?rev=1087633&r1=1087632&r2=1087633&view=diff
==============================================================================
---
james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/BaseTestForBodyDescriptors.java
(original)
+++
james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/BaseTestForBodyDescriptors.java
Fri Apr 1 09:49:08 2011
@@ -31,7 +31,7 @@ public abstract class BaseTestForBodyDes
MutableBodyDescriptor bd = null;
bd = newBodyDescriptor();
- bd.addField(new TestField("Content-Type ", "text/plain;
charset=ISO-8859-1; "
+ bd.addField(new RawField("Content-Type ", "text/plain;
charset=ISO-8859-1; "
+ "boundary=foo; param1=value1; param2=value2;
param3=value3"));
assertEquals(3, bd.getContentTypeParameters().size());
assertEquals("value1", bd.getContentTypeParameters().get("param1"));
@@ -39,7 +39,7 @@ public abstract class BaseTestForBodyDes
assertEquals("value3", bd.getContentTypeParameters().get("param3"));
bd = newBodyDescriptor();
- bd.addField(new TestField("Content-Type ", "text/plain; param1=value1;
param2=value2;"
+ bd.addField(new RawField("Content-Type ", "text/plain; param1=value1;
param2=value2;"
+ " param3=value3"));
assertEquals(3, bd.getContentTypeParameters().size());
assertEquals("value1", bd.getContentTypeParameters().get("param1"));
@@ -47,7 +47,7 @@ public abstract class BaseTestForBodyDes
assertEquals("value3", bd.getContentTypeParameters().get("param3"));
bd = newBodyDescriptor();
- bd.addField(new TestField("Content-Type ", "text/plain; "
+ bd.addField(new RawField("Content-Type ", "text/plain; "
+ "param1= \" value with\tspaces \" ; "
+ "param2=\"\\\"value4 with escaped \\\" \\\"\";"));
assertEquals(2, bd.getContentTypeParameters().size());
@@ -59,7 +59,7 @@ public abstract class BaseTestForBodyDes
* The parameter value should be \n\"
*/
bd = newBodyDescriptor();
- bd.addField(new TestField("Content-Type ", "text/plain;
param=\"\\n\\\\\\\"\""));
+ bd.addField(new RawField("Content-Type ", "text/plain;
param=\"\\n\\\\\\\"\""));
assertEquals(1, bd.getContentTypeParameters().size());
assertEquals("\\n\\\"", bd.getContentTypeParameters().get("param"));
}
@@ -71,10 +71,10 @@ public abstract class BaseTestForBodyDes
* Make sure that only the first Content-Type header added is used.
*/
bd = newBodyDescriptor();
- bd.addField(new TestField("Content-Type ", "text/plain;
charset=ISO-8859-1"));
+ bd.addField(new RawField("Content-Type ", "text/plain;
charset=ISO-8859-1"));
assertEquals("text/plain", bd.getMimeType());
assertEquals("iso-8859-1", bd.getCharset());
- bd.addField(new TestField("Content-Type ", "text/html;
charset=us-ascii"));
+ bd.addField(new RawField("Content-Type ", "text/html;
charset=us-ascii"));
assertEquals("text/plain", bd.getMimeType());
assertEquals("iso-8859-1", bd.getCharset());
}
@@ -83,32 +83,32 @@ public abstract class BaseTestForBodyDes
MutableBodyDescriptor bd = null;
bd = newBodyDescriptor();
- bd.addField(new TestField("Content-Type ", "text/PLAIN"));
+ bd.addField(new RawField("Content-Type ", "text/PLAIN"));
assertEquals("text/plain", bd.getMimeType());
bd = newBodyDescriptor();
- bd.addField(new TestField("Content-Type ", "text/PLAIN;"));
+ bd.addField(new RawField("Content-Type ", "text/PLAIN;"));
assertEquals("text/plain", bd.getMimeType());
bd = newBodyDescriptor();
- bd.addField(new TestField("content-type", " TeXt / html "));
+ bd.addField(new RawField("content-type", " TeXt / html "));
assertEquals("text/html", bd.getMimeType());
bd = newBodyDescriptor();
- bd.addField(new TestField("CONTENT-TYPE", " x-app/yada ; param =
yada"));
+ bd.addField(new RawField("CONTENT-TYPE", " x-app/yada ; param =
yada"));
assertEquals("x-app/yada", bd.getMimeType());
bd = newBodyDescriptor();
- bd.addField(new TestField("CONTENT-TYPE", " yada"));
+ bd.addField(new RawField("CONTENT-TYPE", " yada"));
assertEquals("text/plain", bd.getMimeType());
/*
* Make sure that only the first Content-Type header added is used.
*/
bd = newBodyDescriptor();
- bd.addField(new TestField("Content-Type ", "text/plain"));
+ bd.addField(new RawField("Content-Type ", "text/plain"));
assertEquals("text/plain", bd.getMimeType());
- bd.addField(new TestField("Content-Type ", "text/html"));
+ bd.addField(new RawField("Content-Type ", "text/html"));
assertEquals("text/plain", bd.getMimeType());
/*
@@ -118,19 +118,19 @@ public abstract class BaseTestForBodyDes
MutableBodyDescriptor parent = null;
parent = newBodyDescriptor();
- parent.addField(new TestField("Content-Type", "mutlipart/alternative;
boundary=foo"));
+ parent.addField(new RawField("Content-Type", "mutlipart/alternative;
boundary=foo"));
child = newBodyDescriptor(parent);
assertEquals("text/plain", child.getMimeType());
- child.addField(new TestField("Content-Type", " child/type"));
+ child.addField(new RawField("Content-Type", " child/type"));
assertEquals("child/type", child.getMimeType());
parent = newBodyDescriptor();
- parent.addField(new TestField("Content-Type", "multipart/digest;
boundary=foo"));
+ parent.addField(new RawField("Content-Type", "multipart/digest;
boundary=foo"));
child = newBodyDescriptor(parent);
assertEquals("message/rfc822", child.getMimeType());
- child.addField(new TestField("Content-Type", " child/type"));
+ child.addField(new RawField("Content-Type", " child/type"));
assertEquals("child/type", child.getMimeType());
}
@@ -143,39 +143,39 @@ public abstract class BaseTestForBodyDes
*/
bd = newBodyDescriptor();
assertEquals("us-ascii", bd.getCharset());
- bd.addField(new TestField("Content-Type ", "text/type;
charset=ISO-8859-1"));
+ bd.addField(new RawField("Content-Type ", "text/type;
charset=ISO-8859-1"));
assertEquals("iso-8859-1", bd.getCharset());
bd = newBodyDescriptor();
assertEquals("us-ascii", bd.getCharset());
- bd.addField(new TestField("Content-Type ", "text/type"));
+ bd.addField(new RawField("Content-Type ", "text/type"));
assertEquals("us-ascii", bd.getCharset());
/*
* Test boundary.
*/
bd = newBodyDescriptor();
- bd.addField(new TestField("Content-Type", "text/html; boundary=yada
yada"));
+ bd.addField(new RawField("Content-Type", "text/html; boundary=yada
yada"));
assertNull(bd.getBoundary());
bd = newBodyDescriptor();
- bd.addField(new TestField("Content-Type", "multipart/yada;
boundary=yada"));
+ bd.addField(new RawField("Content-Type", "multipart/yada;
boundary=yada"));
assertEquals("yada", bd.getBoundary());
/*
* Test some weird parameters.
*/
bd = newBodyDescriptor();
- bd.addField(new TestField("Content-Type", "multipart/yada;
boundary=yada yada"));
+ bd.addField(new RawField("Content-Type", "multipart/yada;
boundary=yada yada"));
assertEquals("yada", bd.getBoundary());
bd = newBodyDescriptor();
- bd.addField(new TestField("Content-Type", "multipart/yada; boUNdarY=
ya:*da; \tcharset\t = big5"));
+ bd.addField(new RawField("Content-Type", "multipart/yada; boUNdarY=
ya:*da; \tcharset\t = big5"));
assertEquals("ya:*da", bd.getBoundary());
assertEquals("big5", bd.getCharset());
bd = newBodyDescriptor();
- bd.addField(new TestField("Content-Type", "multipart/yada; boUNdarY=
\"ya \\\"\\\"\tda \\\"\"; "
+ bd.addField(new RawField("Content-Type", "multipart/yada; boUNdarY=
\"ya \\\"\\\"\tda \\\"\"; "
+ "\tcharset\t = \"\\\"hepp\\\" =us\t-ascii\""));
assertEquals("ya \"\"\tda \"", bd.getBoundary());
assertEquals("\"hepp\" =us\t-ascii", bd.getCharset());
@@ -188,31 +188,24 @@ public abstract class BaseTestForBodyDes
bd = newBodyDescriptor();
assertEquals(-1, bd.getContentLength());
- bd.addField(new TestField("Content-Length", "9901"));
+ bd.addField(new RawField("Content-Length", "9901"));
assertEquals(9901, bd.getContentLength());
// only the first content-length counts
- bd.addField(new TestField("Content-Length", "1239901"));
+ bd.addField(new RawField("Content-Length", "1239901"));
assertEquals(9901, bd.getContentLength());
}
public void testDoDefaultToUsAsciiWhenUntyped() throws Exception {
MutableBodyDescriptor descriptor = newBodyDescriptor();
- descriptor.addField(new TestField("To", "[email protected]"));
+ descriptor.addField(new RawField("To", "[email protected]"));
assertEquals("us-ascii", descriptor.getCharset());
}
public void testDoNotDefaultToUsAsciiForNonTextTypes() throws Exception {
MutableBodyDescriptor descriptor = newBodyDescriptor();
- descriptor.addField(new TestField("Content-Type", "image/png;
name=blob.png"));
+ descriptor.addField(new RawField("Content-Type", "image/png;
name=blob.png"));
assertNull(descriptor.getCharset());
}
- private static final class TestField extends RawField {
-
- public TestField(String name, String body){
- super(name, body);
- }
-
- }
}
Added:
james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldParserTest.java
URL:
http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldParserTest.java?rev=1087633&view=auto
==============================================================================
---
james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldParserTest.java
(added)
+++
james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldParserTest.java
Fri Apr 1 09:49:08 2011
@@ -0,0 +1,85 @@
+/****************************************************************
+ * 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.stream;
+
+import org.apache.james.mime4j.MimeException;
+import org.apache.james.mime4j.util.ByteSequence;
+import org.apache.james.mime4j.util.ContentUtil;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+public class RawFieldParserTest extends TestCase {
+
+ public void testBasicParsing() throws Exception {
+ String s = "raw: stuff;\r\n more stuff";
+ ByteSequence raw = ContentUtil.encode(s);
+
+ RawFieldParser parser = new RawFieldParser();
+
+ RawField field = parser.parseField(raw);
+ Assert.assertSame(raw, field.getRaw());
+ Assert.assertEquals("raw", field.getName());
+ Assert.assertEquals("stuff; more stuff", field.getBody());
+ Assert.assertFalse(field.isUsedObsoleteSyntax());
+ Assert.assertEquals(s, field.toString());
+ }
+
+ public void testParsingObsoleteSyntax() throws Exception {
+ String s = "raw \t : stuff;\r\n more stuff";
+ ByteSequence raw = ContentUtil.encode(s);
+
+ RawFieldParser parser = new RawFieldParser();
+
+ RawField field = parser.parseField(raw);
+ Assert.assertSame(raw, field.getRaw());
+ Assert.assertEquals("raw", field.getName());
+ Assert.assertEquals("stuff; more stuff", field.getBody());
+ Assert.assertTrue(field.isUsedObsoleteSyntax());
+ Assert.assertEquals(s, field.toString());
+ }
+
+ public void testParsingInvalidSyntax1() throws Exception {
+ String s = "raw stuff;\r\n more stuff";
+ ByteSequence raw = ContentUtil.encode(s);
+
+ RawFieldParser parser = new RawFieldParser();
+
+ try {
+ parser.parseField(raw);
+ fail("MimeException should have been thrown");
+ } catch (MimeException expected) {
+ }
+ }
+
+ public void testParsingInvalidSyntax2() throws Exception {
+ String s = "raw \t \t";
+ ByteSequence raw = ContentUtil.encode(s);
+
+ RawFieldParser parser = new RawFieldParser();
+
+ try {
+ parser.parseField(raw);
+ fail("MimeException should have been thrown");
+ } catch (MimeException expected) {
+ }
+ }
+
+}
Propchange:
james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldParserTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldParserTest.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange:
james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldParserTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldTest.java
URL:
http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldTest.java?rev=1087633&view=auto
==============================================================================
---
james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldTest.java
(added)
+++
james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldTest.java
Fri Apr 1 09:49:08 2011
@@ -0,0 +1,54 @@
+/****************************************************************
+ * 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.stream;
+
+import org.apache.james.mime4j.util.ByteSequence;
+import org.apache.james.mime4j.util.ContentUtil;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+public class RawFieldTest extends TestCase {
+
+ public void testPrivateConstructor() throws Exception {
+ String s = "raw: stuff;\r\n more stuff";
+ ByteSequence raw = ContentUtil.encode(s);
+ RawField field = new RawField(raw, 3, false, "raw", null);
+ Assert.assertSame(raw, field.getRaw());
+ Assert.assertEquals("raw", field.getName());
+ Assert.assertEquals("stuff; more stuff", field.getBody());
+ Assert.assertEquals(s, field.toString());
+ }
+
+ public void testPublicConstructor() throws Exception {
+ RawField field1 = new RawField("raw", "stuff");
+ Assert.assertNull(field1.getRaw());
+ Assert.assertEquals("raw", field1.getName());
+ Assert.assertEquals("stuff", field1.getBody());
+ Assert.assertEquals("raw: stuff", field1.toString());
+
+ RawField field2 = new RawField("raw", null);
+ Assert.assertNull(field2.getRaw());
+ Assert.assertEquals("raw", field2.getName());
+ Assert.assertEquals(null, field2.getBody());
+ Assert.assertEquals("raw: ", field2.toString());
+ }
+
+}
Propchange:
james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldTest.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange:
james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/stream/RawFieldTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified:
james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/DefaultFieldParser.java
URL:
http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/DefaultFieldParser.java?rev=1087633&r1=1087632&r2=1087633&view=diff
==============================================================================
---
james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/DefaultFieldParser.java
(original)
+++
james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/field/DefaultFieldParser.java
Fri Apr 1 09:49:08 2011
@@ -32,6 +32,7 @@ import org.apache.james.mime4j.field.Mai
import org.apache.james.mime4j.field.MailboxListFieldImpl;
import org.apache.james.mime4j.field.UnstructuredFieldImpl;
import org.apache.james.mime4j.stream.RawField;
+import org.apache.james.mime4j.stream.RawFieldParser;
import org.apache.james.mime4j.util.ByteSequence;
import org.apache.james.mime4j.util.ContentUtil;
@@ -64,11 +65,28 @@ public class DefaultFieldParser extends
public static ParsedField parse(
final ByteSequence raw,
final DecodeMonitor monitor) throws MimeException {
- RawField rawField = new RawField(raw);
+ RawField rawField = RawFieldParser.DEFAULT.parseField(raw);
return PARSER.parse(rawField.getName(), rawField.getBody(), raw,
monitor);
}
/**
+ * Parses the given <code>RawField</code> and returns an instance of the
+ * <code>Field</code> class. The type of the class returned depends on the
+ * field name; see {@link #parse(String)} for a table of field names and
+ * their corresponding classes.
+ *
+ * @param rawField the raw field to parse.
+ * @param monitor a DecodeMonitor object used while parsing/decoding.
+ * @return a <code>ParsedField</code> instance.
+ * @throws MimeException if the raw string cannot be split into field name
and body.
+ */
+ public static ParsedField parse(
+ final RawField rawField,
+ final DecodeMonitor monitor) throws MimeException {
+ return PARSER.parse(rawField.getName(), rawField.getBody(),
rawField.getRaw(), monitor);
+ }
+
+ /**
* Parses the given string and returns an instance of the
* <code>Field</code> class. The type of the class returned depends on
* the field name:
@@ -93,12 +111,14 @@ public class DefaultFieldParser extends
final String rawStr,
final DecodeMonitor monitor) throws MimeException {
ByteSequence raw = ContentUtil.encode(rawStr);
- return parse(raw, monitor);
+ RawField rawField = RawFieldParser.DEFAULT.parseField(raw);
+ // Do not retain the original raw representation as the field
+ // may require folding
+ return PARSER.parse(rawField.getName(), rawField.getBody(), null,
monitor);
}
public static ParsedField parse(final String rawStr) throws MimeException {
- ByteSequence raw = ContentUtil.encode(rawStr);
- return parse(raw, DecodeMonitor.SILENT);
+ return parse(rawStr, DecodeMonitor.SILENT);
}
public DefaultFieldParser() {
Modified:
james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/EntityBuilder.java
URL:
http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/EntityBuilder.java?rev=1087633&r1=1087632&r2=1087633&view=diff
==============================================================================
---
james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/EntityBuilder.java
(original)
+++
james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/EntityBuilder.java
Fri Apr 1 09:49:08 2011
@@ -105,7 +105,7 @@ class EntityBuilder implements ContentHa
*/
public void field(RawField field) throws MimeException {
expect(Header.class);
- Field parsedField = DefaultFieldParser.parse(field.getRaw(), monitor);
+ Field parsedField = DefaultFieldParser.parse(field, monitor);
((Header) stack.peek()).addField(parsedField);
}