Author: mwiederkehr
Date: Mon Jan 12 14:33:18 2009
New Revision: 733941
URL: http://svn.apache.org/viewvc?rev=733941&view=rev
Log:
Initial import of a high-level factory for Field instances (MIME4J-100).
Added:
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Fields.java
james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/FieldsTest.java
Added:
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Fields.java
URL:
http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Fields.java?rev=733941&view=auto
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Fields.java
(added)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/field/Fields.java
Mon Jan 12 14:33:18 2009
@@ -0,0 +1,75 @@
+/****************************************************************
+ * 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.field;
+
+import java.util.Map;
+
+import org.apache.james.mime4j.util.MimeUtil;
+
+public class Fields {
+ private Fields() {
+ }
+
+ public static ContentTypeField contentType(String contentType) {
+ String body = MimeUtil.fold(contentType,
+ Field.CONTENT_TYPE.length() + 2);
+
+ return (ContentTypeField) Field.parse(Field.CONTENT_TYPE, body);
+ }
+
+ public static ContentTypeField contentType(String mimeType,
+ Map<String, String> parameters) {
+ if (parameters == null || parameters.isEmpty()) {
+ return (ContentTypeField) Field.parse(Field.CONTENT_TYPE,
mimeType);
+ } else {
+ StringBuilder sb = new StringBuilder(mimeType);
+ for (Map.Entry<String, String> entry : parameters.entrySet()) {
+ sb.append("; ");
+ sb.append(entry.getKey());
+ sb.append('=');
+ sb.append(quote(entry.getValue()));
+ }
+ String contentType = sb.toString();
+ return contentType(contentType);
+ }
+ }
+
+ public static ContentTransferEncodingField contentTransferEncoding(
+ String contentTransferEncoding) {
+ return (ContentTransferEncodingField) Field.parse(
+ Field.CONTENT_TRANSFER_ENCODING, contentTransferEncoding);
+ }
+
+ private static String quote(String value) {
+ for (int idx = 0; idx < value.length(); idx++) {
+ if (isSpecial(value.charAt(idx))) {
+ value = value.replaceAll("[\\\"]", "\\\\$0");
+ return "\"" + value + "\"";
+ }
+ }
+
+ return value;
+ }
+
+ private static boolean isSpecial(char ch) {
+ final String tspecials = "()<>@,;:\\\"/[]?="; // rfc 2045
+ return ch < 32 || ch >= 127 || tspecials.indexOf(ch) != -1;
+ }
+}
Added:
james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/FieldsTest.java
URL:
http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/FieldsTest.java?rev=733941&view=auto
==============================================================================
---
james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/FieldsTest.java
(added)
+++
james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/FieldsTest.java
Mon Jan 12 14:33:18 2009
@@ -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.field;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+public class FieldsTest extends TestCase {
+
+ public void testContentTypeString() throws Exception {
+ ContentTypeField field = Fields.contentType("multipart/mixed; "
+ + "boundary=\"-=Part.0.37877968dd4f6595.11eccf0271c"
+ + ".2dce5678cbc933d5=-\"");
+ assertTrue(field.isValidField());
+
+ String expectedRaw = "Content-Type: multipart/mixed;\r\n "
+ + "boundary=\"-=Part.0.37877968dd4f6595.11eccf0271c"
+ + ".2dce5678cbc933d5=-\"";
+ assertEquals(expectedRaw, field.getRaw());
+ }
+
+ public void testContentTypeStringParameters() throws Exception {
+ Map<String, String> parameters = new HashMap<String, String>();
+ parameters.put("boundary",
+ "-=Part.0.37877968dd4f6595.11eccf0271c.2dce5678cbc933d5=-");
+ ContentTypeField field = Fields.contentType("multipart/mixed",
+ parameters);
+ assertTrue(field.isValidField());
+
+ String expectedRaw = "Content-Type: multipart/mixed;\r\n "
+ + "boundary=\"-=Part.0.37877968dd4f6595.11eccf0271c"
+ + ".2dce5678cbc933d5=-\"";
+ assertEquals(expectedRaw, field.getRaw());
+ }
+
+ public void testContentTypeStringNullParameters() throws Exception {
+ ContentTypeField field = Fields.contentType("text/plain", null);
+ assertTrue(field.isValidField());
+
+ String expectedRaw = "Content-Type: text/plain";
+ assertEquals(expectedRaw, field.getRaw());
+ }
+
+ public void testInvalidContentType() throws Exception {
+ ContentTypeField field = Fields.contentType("multipart/mixed; "
+ + "boundary=-=Part.0.37877968dd4f6595.11eccf0271c"
+ + ".2dce5678cbc933d5=-");
+ assertFalse(field.isValidField());
+
+ assertEquals("multipart/mixed", field.getMimeType());
+ }
+
+ public void testContentTransferEncoding() throws Exception {
+ ContentTransferEncodingField field = Fields
+ .contentTransferEncoding("base64");
+ assertTrue(field.isValidField());
+
+ assertEquals("Content-Transfer-Encoding: base64", field.getRaw());
+ }
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]