Save This Page
Home » apache-tomcat-6.0.16-src » javax » mail » internet » [javadoc | source]
    1   /**
    2    *
    3    * Copyright 2003-2004 The Apache Software Foundation
    4    *
    5    *  Licensed under the Apache License, Version 2.0 (the "License");
    6    *  you may not use this file except in compliance with the License.
    7    *  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   
   18   package javax.mail.internet;
   19   
   20   import java.io.ByteArrayInputStream;
   21   import java.io.IOException;
   22   import java.io.InputStream;
   23   import java.io.OutputStream;
   24   import java.util.Enumeration;
   25   import javax.activation.DataHandler;
   26   import javax.mail.BodyPart;
   27   import javax.mail.MessagingException;
   28   import javax.mail.Multipart;
   29   
   30   /**
   31    * @version $Rev: 154541 $ $Date: 2005-02-20 10:01:49 -0800 (Sun, 20 Feb 2005) $
   32    */
   33   public class MimeBodyPart extends BodyPart implements MimePart {
   34       /**
   35        * The {@link DataHandler} for this Message's content.
   36        */
   37       protected DataHandler dh;
   38       /**
   39        * This message's content (unless sourced from a SharedInputStream).
   40        */
   41       protected byte content[];
   42       /**
   43        * If the data for this message was supplied by a {@link SharedInputStream}
   44        * then this is another such stream representing the content of this message;
   45        * if this field is non-null, then {@link #content} will be null.
   46        */
   47       protected InputStream contentStream;
   48       /**
   49        * This message's headers.
   50        */
   51       protected InternetHeaders headers;
   52   
   53       public MimeBodyPart() {
   54           headers = new InternetHeaders();
   55       }
   56   
   57       public MimeBodyPart(InputStream in) throws MessagingException {
   58           this.contentStream = in;
   59       }
   60   
   61       public MimeBodyPart(InternetHeaders headers, byte[] content) throws MessagingException {
   62           this.headers = headers;
   63           this.content = content;
   64       }
   65   
   66       public int getSize() throws MessagingException {
   67           if (content != null) {
   68               return content.length;
   69           }
   70           return -1;
   71       }
   72   
   73       public int getLineCount() throws MessagingException {
   74           return -1;
   75       }
   76   
   77       public String getContentType() throws MessagingException {
   78           String value = getSingleHeader("Content-Type");
   79           if (value == null) {
   80               value = "text/plain";
   81           }
   82           return value;
   83       }
   84   
   85       public boolean isMimeType(String type) throws MessagingException {
   86           return new ContentType(getContentType()).match(type);
   87       }
   88   
   89       public String getDisposition() throws MessagingException {
   90           return getSingleHeader("Content-Disposition");
   91       }
   92   
   93       public void setDisposition(String disposition) throws MessagingException {
   94           setHeader("Content-Disposition", disposition);
   95       }
   96   
   97       public String getEncoding() throws MessagingException {
   98           return getSingleHeader("Content-Transfer-Encoding");
   99       }
  100   
  101       public String getContentID() throws MessagingException {
  102           return getSingleHeader("Content-ID");
  103       }
  104   
  105       public void setContentID(String cid) throws MessagingException {
  106           setHeader("Content-ID", cid);
  107       }
  108   
  109       public String getContentMD5() throws MessagingException {
  110           return getSingleHeader("Content-MD5");
  111       }
  112   
  113       public void setContentMD5(String md5) throws MessagingException {
  114           setHeader("Content-MD5", md5);
  115       }
  116   
  117       public String[] getContentLanguage() throws MessagingException {
  118           return getHeader("Content-Language");
  119       }
  120   
  121       public void setContentLanguage(String[] languages) throws MessagingException {
  122           if (languages == null || languages.length == 0) {
  123               removeHeader("Content-Language");
  124           } else if (languages.length == 1) {
  125               setHeader("Content-Language", languages[0]);
  126           } else {
  127               StringBuffer buf = new StringBuffer(languages.length * 20);
  128               buf.append(languages[0]);
  129               for (int i = 1; i < languages.length; i++) {
  130                   buf.append(',').append(languages[i]);
  131               }
  132               setHeader("Content-Language", buf.toString());
  133           }
  134       }
  135   
  136       public String getDescription() throws MessagingException {
  137           return getSingleHeader("Content-Description");
  138       }
  139   
  140       public void setDescription(String description) throws MessagingException {
  141           setHeader("Content-Description", description);
  142       }
  143   
  144       public void setDescription(String description, String charset) throws MessagingException {
  145           // todo encoding
  146           setHeader("Content-Description", description);
  147       }
  148   
  149       public String getFileName() throws MessagingException {
  150           // TODO Implement method
  151           throw new UnsupportedOperationException("Method not yet implemented");
  152       }
  153   
  154       public void setFileName(String name) throws MessagingException {
  155           // TODO Implement method
  156           throw new UnsupportedOperationException("Method not yet implemented");
  157       }
  158   
  159       public InputStream getInputStream() throws MessagingException, IOException {
  160           return getDataHandler().getInputStream();
  161       }
  162   
  163       protected InputStream getContentStream() throws MessagingException {
  164           if (content != null) {
  165               return new ByteArrayInputStream(content);
  166           } else {
  167               throw new MessagingException("No content");
  168           }
  169       }
  170   
  171       public InputStream getRawInputStream() throws MessagingException {
  172           return getContentStream();
  173       }
  174   
  175       public synchronized DataHandler getDataHandler() throws MessagingException {
  176           if (dh == null) {
  177               dh = new DataHandler(new MimePartDataSource(this));
  178           }
  179           return dh;
  180       }
  181   
  182       public Object getContent() throws MessagingException, IOException {
  183           return getDataHandler().getContent();
  184       }
  185   
  186       public void setDataHandler(DataHandler handler) throws MessagingException {
  187           dh = handler;
  188       }
  189   
  190       public void setContent(Object content, String type) throws MessagingException {
  191           setDataHandler(new DataHandler(content, type));
  192       }
  193   
  194       public void setText(String text) throws MessagingException {
  195           setText(text, MimeUtility.getDefaultJavaCharset());
  196       }
  197   
  198       public void setText(String text, String charset) throws MessagingException {
  199           setContent(text, "text/plain; charset=" + charset);
  200       }
  201   
  202       public void setContent(Multipart part) throws MessagingException {
  203           setDataHandler(new DataHandler(part, part.getContentType()));
  204           part.setParent(this);
  205       }
  206   
  207       public void writeTo(OutputStream out) throws IOException, MessagingException {
  208           headers.writeTo(out, null);
  209           out.write(13);
  210           out.write(10);
  211           getDataHandler().writeTo(out);
  212       }
  213   
  214       public String[] getHeader(String name) throws MessagingException {
  215           return headers.getHeader(name);
  216       }
  217   
  218       public String getHeader(String name, String delimiter) throws MessagingException {
  219           return headers.getHeader(name, delimiter);
  220       }
  221   
  222       public void setHeader(String name, String value) throws MessagingException {
  223           headers.setHeader(name, value);
  224       }
  225   
  226       public void addHeader(String name, String value) throws MessagingException {
  227           headers.addHeader(name, value);
  228       }
  229   
  230       public void removeHeader(String name) throws MessagingException {
  231           headers.removeHeader(name);
  232       }
  233   
  234       public Enumeration getAllHeaders() throws MessagingException {
  235           return headers.getAllHeaders();
  236       }
  237   
  238       public Enumeration getMatchingHeaders(String[] name) throws MessagingException {
  239           return headers.getMatchingHeaders(name);
  240       }
  241   
  242       public Enumeration getNonMatchingHeaders(String[] name) throws MessagingException {
  243           return headers.getNonMatchingHeaders(name);
  244       }
  245   
  246       public void addHeaderLine(String line) throws MessagingException {
  247           headers.addHeaderLine(line);
  248       }
  249   
  250       public Enumeration getAllHeaderLines() throws MessagingException {
  251           return headers.getAllHeaderLines();
  252       }
  253   
  254       public Enumeration getMatchingHeaderLines(String[] names) throws MessagingException {
  255           return headers.getMatchingHeaderLines(names);
  256       }
  257   
  258       public Enumeration getNonMatchingHeaderLines(String[] names) throws MessagingException {
  259           return headers.getNonMatchingHeaderLines(names);
  260       }
  261   
  262       protected void updateHeaders() throws MessagingException {
  263       }
  264   
  265       private String getSingleHeader(String name) throws MessagingException {
  266           String[] values = getHeader(name);
  267           if (values == null || values.length == 0) {
  268               return null;
  269           } else {
  270               return values[0];
  271           }
  272       }
  273   }

Save This Page
Home » apache-tomcat-6.0.16-src » javax » mail » internet » [javadoc | source]