hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r709485 - in /httpcomponents/httpclient/trunk/module-httpmime/src: main/java/org/apache/http/entity/mime/content/ test/java/org/apache/http/entity/mime/
Date Fri, 31 Oct 2008 17:16:08 GMT
Author: olegk
Date: Fri Oct 31 10:16:08 2008
New Revision: 709485

URL: http://svn.apache.org/viewvc?rev=709485&view=rev
Log:
HTTPCLIENT-807: Added means to set mime type on default ContentBody implementations

Added:
    httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/entity/mime/content/AbstractContentBody.java
    httpcomponents/httpclient/trunk/module-httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartContentBody.java
Modified:
    httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/entity/mime/content/FileBody.java
    httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/entity/mime/content/InputStreamBody.java
    httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/entity/mime/content/StringBody.java

Added: httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/entity/mime/content/AbstractContentBody.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/entity/mime/content/AbstractContentBody.java?rev=709485&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/entity/mime/content/AbstractContentBody.java
(added)
+++ httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/entity/mime/content/AbstractContentBody.java
Fri Oct 31 10:16:08 2008
@@ -0,0 +1,77 @@
+/*
+ * $HeadURL:$
+ * $Revision:$
+ * $Date:$
+ *
+ * ====================================================================
+ * 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.entity.mime.content;
+
+import java.util.Collections;
+import java.util.Map;
+
+import org.apache.james.mime4j.message.AbstractBody;
+
+public abstract class AbstractContentBody extends AbstractBody implements ContentBody {
+
+    private final String mimeType;
+    private final String mediaType;
+    private final String subType;
+    
+    public AbstractContentBody(final String mimeType) {
+        super();
+        if (mimeType == null) {
+            throw new IllegalArgumentException("MIME type may not be null");
+        }
+        this.mimeType = mimeType;
+        int i = mimeType.indexOf('/');
+        if (i != -1) {
+            this.mediaType = mimeType.substring(0, i);
+            this.subType = mimeType.substring(i + 1);
+        } else {
+            this.mediaType = mimeType;
+            this.subType = null;
+        }
+    }
+    
+    public String getMimeType() {
+        return this.mimeType;
+    }
+    
+    public String getMediaType() {
+        return this.mediaType;
+    }
+
+    public String getSubType() {
+        return this.subType;
+    }
+
+    public Map<?, ?> getContentTypeParameters() {
+        return Collections.EMPTY_MAP;
+    }
+
+}

Modified: httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/entity/mime/content/FileBody.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/entity/mime/content/FileBody.java?rev=709485&r1=709484&r2=709485&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/entity/mime/content/FileBody.java
(original)
+++ httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/entity/mime/content/FileBody.java
Fri Oct 31 10:16:08 2008
@@ -36,25 +36,26 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.util.Collections;
-import java.util.Map;
 
 import org.apache.http.entity.mime.MIME;
-import org.apache.james.mime4j.message.AbstractBody;
 import org.apache.james.mime4j.message.BinaryBody;
 
-public class FileBody extends AbstractBody implements BinaryBody, ContentBody {
+public class FileBody extends AbstractContentBody implements BinaryBody {
 
     private final File file;
     
-    public FileBody(final File file) {
-        super();
+    public FileBody(final File file, final String mimeType) {
+        super(mimeType);
         if (file == null) {
             throw new IllegalArgumentException("File may not be null");
         }
         this.file = file;
     }
     
+    public FileBody(final File file) {
+        this(file, "application/octet-stream");
+    }
+    
     public InputStream getInputStream() throws IOException {
         return new FileInputStream(this.file);
     }
@@ -84,22 +85,6 @@
         return null;
     }
 
-    public String getMimeType() {
-        return "application/octet-stream";
-    }
-    
-    public Map<?, ?> getContentTypeParameters() {
-        return Collections.EMPTY_MAP;
-    }
-
-    public String getMediaType() {
-        return "application";
-    }
-
-    public String getSubType() {
-        return "octet-stream";
-    }
-
     public long getContentLength() {
         return this.file.length();
     }

Modified: httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/entity/mime/content/InputStreamBody.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/entity/mime/content/InputStreamBody.java?rev=709485&r1=709484&r2=709485&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/entity/mime/content/InputStreamBody.java
(original)
+++ httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/entity/mime/content/InputStreamBody.java
Fri Oct 31 10:16:08 2008
@@ -34,20 +34,17 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.util.Collections;
-import java.util.Map;
 
 import org.apache.http.entity.mime.MIME;
-import org.apache.james.mime4j.message.AbstractBody;
 import org.apache.james.mime4j.message.BinaryBody;
 
-public class InputStreamBody extends AbstractBody implements BinaryBody, ContentBody {
+public class InputStreamBody extends AbstractContentBody implements BinaryBody {
 
     private final InputStream in;
     private final String filename;
     
-    public InputStreamBody(final InputStream in, final String filename) {
-        super();
+    public InputStreamBody(final InputStream in, final String mimeType, final String filename)
{
+        super(mimeType);
         if (in == null) {
             throw new IllegalArgumentException("Input stream may not be null");
         }
@@ -55,6 +52,10 @@
         this.filename = filename;
     }
     
+    public InputStreamBody(final InputStream in, final String filename) {
+        this(in, "application/octet-stream", filename);
+    }
+    
     public InputStream getInputStream() throws IOException {
         return this.in;
     }
@@ -83,22 +84,6 @@
         return null;
     }
 
-    public String getMimeType() {
-        return "application/octet-stream";
-    }
-    
-    public Map<?, ?> getContentTypeParameters() {
-        return Collections.EMPTY_MAP;
-    }
-
-    public String getMediaType() {
-        return "application";
-    }
-
-    public String getSubType() {
-        return "octet-stream";
-    }
-
     public long getContentLength() {
         return -1;
     }

Modified: httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/entity/mime/content/StringBody.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/entity/mime/content/StringBody.java?rev=709485&r1=709484&r2=709485&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/entity/mime/content/StringBody.java
(original)
+++ httpcomponents/httpclient/trunk/module-httpmime/src/main/java/org/apache/http/entity/mime/content/StringBody.java
Fri Oct 31 10:16:08 2008
@@ -43,16 +43,18 @@
 import java.util.Map;
 
 import org.apache.http.entity.mime.MIME;
-import org.apache.james.mime4j.message.AbstractBody;
 import org.apache.james.mime4j.message.TextBody;
 
-public class StringBody extends AbstractBody implements TextBody, ContentBody {
+public class StringBody extends AbstractContentBody implements TextBody {
 
     private final byte[] content;
     private final Charset charset;
     
-    public StringBody(final String text, Charset charset) throws UnsupportedEncodingException
{
-        super();
+    public StringBody(
+            final String text, 
+            final String mimeType, 
+            Charset charset) throws UnsupportedEncodingException {
+        super(mimeType);
         if (text == null) {
             throw new IllegalArgumentException("Text may not be null");
         }
@@ -63,8 +65,12 @@
         this.charset = charset;
     }
     
+    public StringBody(final String text, Charset charset) throws UnsupportedEncodingException
{
+        this(text, "text/plain", charset);
+    }
+    
     public StringBody(final String text) throws UnsupportedEncodingException {
-        this(text, null);
+        this(text, "text/plain", null);
     }
     
     public Reader getReader() throws IOException {
@@ -94,18 +100,7 @@
         return this.charset.name();
     }
 
-    public String getMimeType() {
-        return "text/plain";
-    }
-    
-    public String getMediaType() {
-        return "text";
-    }
-
-    public String getSubType() {
-        return "plain";
-    }
-
+    @Override
     public Map<?, ?> getContentTypeParameters() {
         Map<Object, Object> map = new HashMap<Object, Object>();
         map.put("charset", this.charset.name());

Added: httpcomponents/httpclient/trunk/module-httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartContentBody.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartContentBody.java?rev=709485&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/module-httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartContentBody.java
(added)
+++ httpcomponents/httpclient/trunk/module-httpmime/src/test/java/org/apache/http/entity/mime/TestMultipartContentBody.java
Fri Oct 31 10:16:08 2008
@@ -0,0 +1,117 @@
+/*
+ * $HeadURL:$
+ * $Revision:$
+ * $Date:$
+ *
+ * ====================================================================
+ * 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.entity.mime;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.nio.charset.Charset;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.http.entity.mime.content.InputStreamBody;
+import org.apache.http.entity.mime.content.StringBody;
+
+public class TestMultipartContentBody extends TestCase {
+
+    // ------------------------------------------------------------ Constructor
+    public TestMultipartContentBody(final String testName) throws IOException {
+        super(testName);
+    }
+
+    // ------------------------------------------------------------------- Main
+    public static void main(String args[]) {
+        String[] testCaseName = { TestMultipartContentBody.class.getName() };
+        junit.textui.TestRunner.main(testCaseName);
+    }
+
+    // ------------------------------------------------------- TestCase Methods
+
+    public static Test suite() {
+        return new TestSuite(TestMultipartContentBody.class);
+    }
+
+    public void testStringBody() throws Exception {
+        StringBody b1 = new StringBody("text");
+        assertEquals(4, b1.getContentLength());
+        
+        Charset defCharset = Charset.defaultCharset();
+        
+        assertEquals(defCharset.name(), b1.getCharset());
+        assertEquals(defCharset.name(), b1.getContentTypeParameters().get("charset"));
+        
+        assertNull(b1.getFilename());
+        assertEquals("text/plain", b1.getMimeType());
+        assertEquals("text", b1.getMediaType());
+        assertEquals("plain", b1.getSubType());
+
+        assertEquals(MIME.ENC_8BIT, b1.getTransferEncoding());
+
+        StringBody b2 = new StringBody("more text", "text/other", MIME.DEFAULT_CHARSET);
+        assertEquals(9, b2.getContentLength());
+        assertEquals(MIME.DEFAULT_CHARSET.name(), b2.getCharset());
+        assertEquals(MIME.DEFAULT_CHARSET.name(), b2.getContentTypeParameters().get("charset"));
+        
+        assertNull(b2.getFilename());
+        assertEquals("text/other", b2.getMimeType());
+        assertEquals("text", b2.getMediaType());
+        assertEquals("other", b2.getSubType());
+
+        assertEquals(MIME.ENC_8BIT, b2.getTransferEncoding());
+    }
+
+    public void testInputStreamBody() throws Exception {
+        byte[] stuff = "Stuff".getBytes("US-ASCII");
+        InputStreamBody b1 = new InputStreamBody(new ByteArrayInputStream(stuff), "stuff");
+        assertEquals(-1, b1.getContentLength());
+        
+        assertNull(b1.getCharset());
+        assertEquals("stuff", b1.getFilename());
+        assertEquals("application/octet-stream", b1.getMimeType());
+        assertEquals("application", b1.getMediaType());
+        assertEquals("octet-stream", b1.getSubType());
+
+        assertEquals(MIME.ENC_BINARY, b1.getTransferEncoding());
+
+        InputStreamBody b2 = new InputStreamBody(
+                new ByteArrayInputStream(stuff), "some/stuff", "stuff");
+        assertEquals(-1, b2.getContentLength());
+        assertNull(b2.getCharset());
+        assertEquals("stuff", b2.getFilename());
+        assertEquals("some/stuff", b2.getMimeType());
+        assertEquals("some", b2.getMediaType());
+        assertEquals("stuff", b2.getSubType());
+
+        assertEquals(MIME.ENC_BINARY, b2.getTransferEncoding());
+    }
+}



Mime
View raw message