You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jb...@apache.org on 2005/02/01 02:08:03 UTC

svn commit: r149345 - geronimo/trunk/specs/javamail/src/java/javax/mail/internet/MimeBodyPart.java

Author: jboynes
Date: Mon Jan 31 17:08:02 2005
New Revision: 149345

URL: http://svn.apache.org/viewcvs?view=rev&rev=149345
Log:
initial impl

Modified:
    geronimo/trunk/specs/javamail/src/java/javax/mail/internet/MimeBodyPart.java

Modified: geronimo/trunk/specs/javamail/src/java/javax/mail/internet/MimeBodyPart.java
URL: http://svn.apache.org/viewcvs/geronimo/trunk/specs/javamail/src/java/javax/mail/internet/MimeBodyPart.java?view=diff&r1=149344&r2=149345
==============================================================================
--- geronimo/trunk/specs/javamail/src/java/javax/mail/internet/MimeBodyPart.java (original)
+++ geronimo/trunk/specs/javamail/src/java/javax/mail/internet/MimeBodyPart.java Mon Jan 31 17:08:02 2005
@@ -17,6 +17,7 @@
 
 package javax.mail.internet;
 
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -30,203 +31,240 @@
  * @version $Rev$ $Date$
  */
 public class MimeBodyPart extends BodyPart implements MimePart {
+    /**
+     * The {@link DataHandler} for this Message's content.
+     */
+    protected DataHandler dh;
+    /**
+     * This message's content (unless sourced from a SharedInputStream).
+     */
     protected byte content[];
+    /**
+     * If the data for this message was supplied by a {@link SharedInputStream}
+     * then this is another such stream representing the content of this message;
+     * if this field is non-null, then {@link #content} will be null.
+     */
     protected InputStream contentStream;
-    protected DataHandler dh;
+    /**
+     * This message's headers.
+     */
     protected InternetHeaders headers;
 
     public MimeBodyPart() {
+        headers = new InternetHeaders();
     }
 
     public MimeBodyPart(InputStream in) throws MessagingException {
         this.contentStream = in;
     }
 
-    public MimeBodyPart(InternetHeaders headers, byte[] content)
-            throws MessagingException {
+    public MimeBodyPart(InternetHeaders headers, byte[] content) throws MessagingException {
         this.headers = headers;
         this.content = content;
     }
 
-    public void addHeader(String name, String value)
-            throws MessagingException {
-        headers.addHeader(name, value);
+    public int getSize() throws MessagingException {
+        if (content != null) {
+            return content.length;
+        }
+        return -1;
     }
 
-    public void addHeaderLine(String line) throws MessagingException {
-        headers.addHeaderLine(line);
+    public int getLineCount() throws MessagingException {
+        return -1;
     }
 
-    public Enumeration getAllHeaderLines() throws MessagingException {
-        return headers.getAllHeaderLines();
+    public String getContentType() throws MessagingException {
+        String value = getSingleHeader("Content-Type");
+        if (value == null) {
+            value = "text/plain";
+        }
+        return value;
     }
 
-    public Enumeration getAllHeaders() throws MessagingException {
-        return headers.getAllHeaders();
+    public boolean isMimeType(String type) throws MessagingException {
+        return new ContentType(getContentType()).match(type);
     }
 
-    public Object getContent() throws IOException, MessagingException {
-        throw new UnsupportedOperationException("Method not yet implemented");
+    public String getDisposition() throws MessagingException {
+        return getSingleHeader("Content-Disposition");
+    }
+
+    public void setDisposition(String disposition) throws MessagingException {
+        setHeader("Content-Disposition", disposition);
+    }
+
+    public String getEncoding() throws MessagingException {
+        return getSingleHeader("Content-Transfer-Encoding");
     }
 
     public String getContentID() throws MessagingException {
-        throw new UnsupportedOperationException("Method not yet implemented");
+        return getSingleHeader("Content-ID");
     }
 
-    public String[] getContentLanguage() throws MessagingException {
-        throw new UnsupportedOperationException("Method not yet implemented");
+    public void setContentID(String cid) throws MessagingException {
+        setHeader("Content-ID", cid);
     }
 
     public String getContentMD5() throws MessagingException {
-        throw new UnsupportedOperationException("Method not yet implemented");
+        return getSingleHeader("Content-MD5");
     }
 
-    protected InputStream getContentStream() throws MessagingException {
-        throw new UnsupportedOperationException("Method not yet implemented");
+    public void setContentMD5(String md5) throws MessagingException {
+        setHeader("Content-MD5", md5);
     }
 
-    public String getContentType() throws MessagingException {
-        return dh.getContentType();
+    public String[] getContentLanguage() throws MessagingException {
+        return getHeader("Content-Language");
     }
 
-    public DataHandler getDataHandler() throws MessagingException {
-        throw new UnsupportedOperationException("Method not yet implemented");
+    public void setContentLanguage(String[] languages) throws MessagingException {
+        if (languages == null || languages.length == 0) {
+            removeHeader("Content-Language");
+        } else if (languages.length == 1) {
+            setHeader("Content-Language", languages[0]);
+        } else {
+            StringBuffer buf = new StringBuffer(languages.length * 20);
+            buf.append(languages[0]);
+            for (int i = 1; i < languages.length; i++) {
+                buf.append(',').append(languages[i]);
+            }
+            setHeader("Content-Language", buf.toString());
+        }
     }
 
     public String getDescription() throws MessagingException {
-        throw new UnsupportedOperationException("Method not yet implemented");
+        return getSingleHeader("Content-Description");
     }
 
-    public String getDisposition() throws MessagingException {
-        throw new UnsupportedOperationException("Method not yet implemented");
+    public void setDescription(String description) throws MessagingException {
+        setHeader("Content-Description", description);
     }
 
-    public String getEncoding() throws MessagingException {
-        throw new UnsupportedOperationException("Method not yet implemented");
+    public void setDescription(String description, String charset) throws MessagingException {
+        // todo encoding
+        setHeader("Content-Description", description);
     }
 
     public String getFileName() throws MessagingException {
+        // TODO Implement method
         throw new UnsupportedOperationException("Method not yet implemented");
     }
 
-    public String[] getHeader(String name) throws MessagingException {
-        return headers.getHeader(name);
-    }
-
-    public String getHeader(String name, String delimiter)
-            throws MessagingException {
-        return headers.getHeader(name, delimiter);
+    public void setFileName(String name) throws MessagingException {
+        // TODO Implement method
+        throw new UnsupportedOperationException("Method not yet implemented");
     }
 
-    public InputStream getInputStream()
-            throws IOException, MessagingException {
-        throw new UnsupportedOperationException("Method not yet implemented");
+    public InputStream getInputStream() throws MessagingException, IOException {
+        return getDataHandler().getInputStream();
     }
 
-    public int getLineCount() throws MessagingException {
-        throw new UnsupportedOperationException("Method not yet implemented");
+    protected InputStream getContentStream() throws MessagingException {
+        if (content != null) {
+            return new ByteArrayInputStream(content);
+        } else {
+            throw new MessagingException("No content");
+        }
     }
 
-    public Enumeration getMatchingHeaderLines(String[] names)
-            throws MessagingException {
-        return headers.getMatchingHeaderLines(names);
+    public InputStream getRawInputStream() throws MessagingException {
+        return getContentStream();
     }
 
-    public Enumeration getMatchingHeaders(String[] name)
-            throws MessagingException {
-        return headers.getMatchingHeaders(name);
+    public synchronized DataHandler getDataHandler() throws MessagingException {
+        if (dh == null) {
+            dh = new DataHandler(new MimePartDataSource(this));
+        }
+        return dh;
     }
 
-    public Enumeration getNonMatchingHeaderLines(String[] names)
-            throws MessagingException {
-        return headers.getNonMatchingHeaderLines(names);
+    public Object getContent() throws MessagingException, IOException {
+        return getDataHandler().getContent();
     }
 
-    public Enumeration getNonMatchingHeaders(String[] name)
-            throws MessagingException {
-        return headers.getNonMatchingHeaders(name);
+    public void setDataHandler(DataHandler handler) throws MessagingException {
+        dh = handler;
     }
 
-    public InputStream getRawInputStream() throws MessagingException {
-        throw new UnsupportedOperationException("Method not yet implemented");
+    public void setContent(Object content, String type) throws MessagingException {
+        setDataHandler(new DataHandler(content, type));
     }
 
-    public int getSize() throws MessagingException {
-        throw new UnsupportedOperationException("Method not yet implemented");
+    public void setText(String text) throws MessagingException {
+        setText(text, MimeUtility.getDefaultJavaCharset());
     }
 
-    public boolean isMimeType(String type) throws MessagingException {
-        ContentType c1 = new ContentType(type);
-        ContentType c2 = new ContentType(dh.getContentType());
-        return c1.match(c2);
+    public void setText(String text, String charset) throws MessagingException {
+        setContent(text, "text/plain; charset=" + charset);
     }
 
-    public void removeHeader(String name) throws MessagingException {
-        headers.removeHeader(name);
+    public void setContent(Multipart part) throws MessagingException {
+        setDataHandler(new DataHandler(part, part.getContentType()));
+        part.setParent(this);
     }
 
-    public void setContent(Multipart content) throws MessagingException {
+    public void writeTo(OutputStream out) throws IOException, MessagingException {
         throw new UnsupportedOperationException("Method not yet implemented");
     }
 
-    public void setContent(Object content, String type)
-            throws MessagingException {
-        throw new UnsupportedOperationException("Method not yet implemented");
+    public String[] getHeader(String name) throws MessagingException {
+        return headers.getHeader(name);
     }
 
-    public void setContentID(String cid) throws MessagingException {
-        throw new UnsupportedOperationException("Method not yet implemented");
+    public String getHeader(String name, String delimiter) throws MessagingException {
+        return headers.getHeader(name, delimiter);
     }
 
-    public void setContentLanguage(String[] languages)
-            throws MessagingException {
-        throw new UnsupportedOperationException("Method not yet implemented");
+    public void setHeader(String name, String value) throws MessagingException {
+        headers.setHeader(name, value);
     }
 
-    public void setContentMD5(String md5) throws MessagingException {
-        throw new UnsupportedOperationException("Method not yet implemented");
+    public void addHeader(String name, String value) throws MessagingException {
+        headers.addHeader(name, value);
     }
 
-    public void setDataHandler(DataHandler handler) throws MessagingException {
-        throw new UnsupportedOperationException("Method not yet implemented");
+    public void removeHeader(String name) throws MessagingException {
+        headers.removeHeader(name);
     }
 
-    public void setDescription(String description) throws MessagingException {
-        throw new UnsupportedOperationException("Method not yet implemented");
+    public Enumeration getAllHeaders() throws MessagingException {
+        return headers.getAllHeaders();
     }
 
-    public void setDescription(String description, String charset)
-            throws MessagingException {
-        throw new UnsupportedOperationException("Method not yet implemented");
+    public Enumeration getMatchingHeaders(String[] name) throws MessagingException {
+        return headers.getMatchingHeaders(name);
     }
 
-    public void setDisposition(String disposition) throws MessagingException {
-        throw new UnsupportedOperationException("Method not yet implemented");
+    public Enumeration getNonMatchingHeaders(String[] name) throws MessagingException {
+        return headers.getNonMatchingHeaders(name);
     }
 
-    public void setFileName(String name) throws MessagingException {
-        throw new UnsupportedOperationException("Method not yet implemented");
+    public void addHeaderLine(String line) throws MessagingException {
+        headers.addHeaderLine(line);
     }
 
-    public void setHeader(String name, String value)
-            throws MessagingException {
-        headers.setHeader(name, value);
+    public Enumeration getAllHeaderLines() throws MessagingException {
+        return headers.getAllHeaderLines();
     }
 
-    public void setText(String text) throws MessagingException {
-        throw new UnsupportedOperationException("Method not yet implemented");
+    public Enumeration getMatchingHeaderLines(String[] names) throws MessagingException {
+        return headers.getMatchingHeaderLines(names);
     }
 
-    public void setText(String text, String charset)
-            throws MessagingException {
-        throw new UnsupportedOperationException("Method not yet implemented");
+    public Enumeration getNonMatchingHeaderLines(String[] names) throws MessagingException {
+        return headers.getNonMatchingHeaderLines(names);
     }
 
     protected void updateHeaders() throws MessagingException {
     }
 
-    public void writeTo(OutputStream out)
-            throws IOException, MessagingException {
-        throw new UnsupportedOperationException("Method not yet implemented");
+    private String getSingleHeader(String name) throws MessagingException {
+        String[] values = getHeader(name);
+        if (values == null || values.length == 0) {
+            return null;
+        } else {
+            return values[0];
+        }
     }
 }