?? mimemultipart.java
字號:
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License"). You * may not use this file except in compliance with the License. You can obtain * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific * language governing permissions and limitations under the License. * * When distributing the software, include this License Header Notice in each * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. * Sun designates this particular file as subject to the "Classpath" exception * as provided by Sun in the GPL Version 2 section of the License file that * accompanied this code. If applicable, add the following below the License * Header, with the fields enclosed by brackets [] replaced by your own * identifying information: "Portions Copyrighted [year] * [name of copyright owner]" * * Contributor(s): * * If you wish your version of this file to be governed by only the CDDL or * only the GPL Version 2, indicate your decision by adding "[Contributor] * elects to include this software in this distribution under the [CDDL or GPL * Version 2] license." If you don't indicate a single choice of license, a * recipient has the option to distribute your version of this file under * either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above. However, if you add GPL Version 2 code * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. *//* * @(#)MimeMultipart.java 1.48 07/05/15 */package javax.mail.internet;import javax.mail.*;import javax.activation.*;import java.util.*;import java.io.*;import com.sun.mail.util.LineOutputStream;import com.sun.mail.util.LineInputStream;import com.sun.mail.util.ASCIIUtility;/** * The MimeMultipart class is an implementation of the abstract Multipart * class that uses MIME conventions for the multipart data. <p> * * A MimeMultipart is obtained from a MimePart whose primary type * is "multipart" (by invoking the part's <code>getContent()</code> method) * or it can be created by a client as part of creating a new MimeMessage. <p> * * The default multipart subtype is "mixed". The other multipart * subtypes, such as "alternative", "related", and so on, can be * implemented as subclasses of MimeMultipart with additional methods * to implement the additional semantics of that type of multipart * content. The intent is that service providers, mail JavaBean writers * and mail clients will write many such subclasses and their Command * Beans, and will install them into the JavaBeans Activation * Framework, so that any JavaMail implementation and its clients can * transparently find and use these classes. Thus, a MIME multipart * handler is treated just like any other type handler, thereby * decoupling the process of providing multipart handlers from the * JavaMail API. Lacking these additional MimeMultipart subclasses, * all subtypes of MIME multipart data appear as MimeMultipart objects. <p> * * An application can directly construct a MIME multipart object of any * subtype by using the <code>MimeMultipart(String subtype)</code> * constructor. For example, to create a "multipart/alternative" object, * use <code>new MimeMultipart("alternative")</code>. <p> * * The <code>mail.mime.multipart.ignoremissingendboundary</code> * property may be set to <code>false</code> to cause a * <code>MessagingException</code> to be thrown if the multipart * data does not end with the required end boundary line. If this * property is set to <code>true</code> or not set, missing end * boundaries are not considered an error and the final body part * ends at the end of the data. <p> * * The <code>mail.mime.multipart.ignoremissingboundaryparameter</code> * System property may be set to <code>false</code> to cause a * <code>MessagingException</code> to be thrown if the Content-Type * of the MimeMultipart does not include a <code>boundary</code> parameter. * If this property is set to <code>true</code> or not set, the multipart * parsing code will look for a line that looks like a bounary line and * use that as the boundary separating the parts. * * @version 1.48, 07/05/15 * @author John Mani * @author Bill Shannon * @author Max Spivak */public class MimeMultipart extends Multipart { private static boolean ignoreMissingEndBoundary = true; private static boolean ignoreMissingBoundaryParameter = true; private static boolean bmparse = true; static { try { String s = System.getProperty( "mail.mime.multipart.ignoremissingendboundary"); // default to true ignoreMissingEndBoundary = s == null || !s.equalsIgnoreCase("false"); s = System.getProperty( "mail.mime.multipart.ignoremissingboundaryparameter"); // default to true ignoreMissingBoundaryParameter = s == null || !s.equalsIgnoreCase("false"); s = System.getProperty( "mail.mime.multipart.bmparse"); // default to true bmparse = s == null || !s.equalsIgnoreCase("false"); } catch (SecurityException sex) { // ignore it } } /** * The DataSource supplying our InputStream. */ protected DataSource ds = null; /** * Have we parsed the data from our InputStream yet? * Defaults to true; set to false when our constructor is * given a DataSource with an InputStream that we need to * parse. */ protected boolean parsed = true; /** * Have we seen the final bounary line? */ private boolean complete = true; /** * The MIME multipart preamble text, the text that * occurs before the first boundary line. */ private String preamble = null; /** * Default constructor. An empty MimeMultipart object * is created. Its content type is set to "multipart/mixed". * A unique boundary string is generated and this string is * setup as the "boundary" parameter for the * <code>contentType</code> field. <p> * * MimeBodyParts may be added later. */ public MimeMultipart() { this("mixed"); } /** * Construct a MimeMultipart object of the given subtype. * A unique boundary string is generated and this string is * setup as the "boundary" parameter for the * <code>contentType</code> field. <p> * * MimeBodyParts may be added later. */ public MimeMultipart(String subtype) { super(); /* * Compute a boundary string. */ String boundary = UniqueValue.getUniqueBoundaryValue(); ContentType cType = new ContentType("multipart", subtype, null); cType.setParameter("boundary", boundary); contentType = cType.toString(); } /** * Constructs a MimeMultipart object and its bodyparts from the * given DataSource. <p> * * This constructor handles as a special case the situation where the * given DataSource is a MultipartDataSource object. In this case, this * method just invokes the superclass (i.e., Multipart) constructor * that takes a MultipartDataSource object. <p> * * Otherwise, the DataSource is assumed to provide a MIME multipart * byte stream. The <code>parsed</code> flag is set to false. When * the data for the body parts are needed, the parser extracts the * "boundary" parameter from the content type of this DataSource, * skips the 'preamble' and reads bytes till the terminating * boundary and creates MimeBodyParts for each part of the stream. * * @param ds DataSource, can be a MultipartDataSource */ public MimeMultipart(DataSource ds) throws MessagingException { super(); if (ds instanceof MessageAware) { MessageContext mc = ((MessageAware)ds).getMessageContext(); setParent(mc.getPart()); } if (ds instanceof MultipartDataSource) { // ask super to do this for us. setMultipartDataSource((MultipartDataSource)ds); return; } // 'ds' was not a MultipartDataSource, we have // to parse this ourself. parsed = false; this.ds = ds; contentType = ds.getContentType(); } /** * Set the subtype. This method should be invoked only on a new * MimeMultipart object created by the client. The default subtype * of such a multipart object is "mixed". <p> * * @param subtype Subtype */ throws MessagingException { ContentType cType = new ContentType(contentType); cType.setSubType(subtype); contentType = cType.toString(); } /** * Return the number of enclosed BodyPart objects. * * @return number of parts */ public synchronized int getCount() throws MessagingException { parse(); return super.getCount(); } /** * Get the specified BodyPart. BodyParts are numbered starting at 0. * * @param index the index of the desired BodyPart * @return the Part * @exception MessagingException if no such BodyPart exists */ public synchronized BodyPart getBodyPart(int index) throws MessagingException { parse(); return super.getBodyPart(index); } /** * Get the MimeBodyPart referred to by the given ContentID (CID). * Returns null if the part is not found. * * @param CID the ContentID of the desired part * @return the Part */ public synchronized BodyPart getBodyPart(String CID) throws MessagingException { parse(); int count = getCount(); for (int i = 0; i < count; i++) { MimeBodyPart part = (MimeBodyPart)getBodyPart(i); String s = part.getContentID(); if (s != null && s.equals(CID)) return part; } return null; } /** * Remove the specified part from the multipart message. * Shifts all the parts after the removed part down one. * * @param part The part to remove * @return true if part removed, false otherwise * @exception MessagingException if no such Part exists * @exception IllegalWriteException if the underlying * implementation does not support modification * of existing values */ public boolean removeBodyPart(BodyPart part) throws MessagingException { parse(); return super.removeBodyPart(part); } /** * Remove the part at specified location (starting from 0). * Shifts all the parts after the removed part down one. * * @param index Index of the part to remove * @exception MessagingException * @exception IndexOutOfBoundsException if the given index * is out of range. * @exception IllegalWriteException if the underlying * implementation does not support modification * of existing values */ public void removeBodyPart(int index) throws MessagingException { parse(); super.removeBodyPart(index); } /** * Adds a Part to the multipart. The BodyPart is appended to * the list of existing Parts. * * @param part The Part to be appended * @exception MessagingException * @exception IllegalWriteException if the underlying * implementation does not support modification * of existing values */ public synchronized void addBodyPart(BodyPart part) throws MessagingException { parse(); super.addBodyPart(part); } /** * Adds a BodyPart at position <code>index</code>. * If <code>index</code> is not the last one in the list, * the subsequent parts are shifted up. If <code>index</code> * is larger than the number of parts present, the * BodyPart is appended to the end. * * @param part The BodyPart to be inserted * @param index Location where to insert the part * @exception MessagingException * @exception IllegalWriteException if the underlying * implementation does not support modification * of existing values */ public synchronized void addBodyPart(BodyPart part, int index) throws MessagingException { parse(); super.addBodyPart(part, index); } /** * Return true if the final boundary line for this * multipart was seen. When parsing multipart content, * this class will (by default) terminate parsing with * no error if the end of input is reached before seeing * the final multipart boundary line. In such a case, * this method will return false. (If the System property * "mail.mime.multipart.ignoremissingendboundary" is set to * false, parsing such a message will instead throw a * MessagingException.) * * @return true if the final boundary line was seen * @since JavaMail 1.4 */ public synchronized boolean isComplete() throws MessagingException { parse(); return complete; } /** * Get the preamble text, if any, that appears before the * first body part of this multipart. Some protocols, * such as IMAP, will not allow access to the preamble text. * * @return the preamble text, or null if no preamble * @since JavaMail 1.4 */ public synchronized String getPreamble() throws MessagingException {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -