aboutsummaryrefslogtreecommitdiff
path: root/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/packaging/mime/internet/MimeMultipart.java
blob: c53de35ebd46034525f0f7f9953ec5429ad0f019 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/*
 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @(#)MimeMultipart.java     1.31 03/01/29
 */



package com.sun.xml.internal.messaging.saaj.packaging.mime.internet;

import java.io.*;

import javax.activation.DataSource;

import com.sun.xml.internal.messaging.saaj.packaging.mime.*;
import com.sun.xml.internal.messaging.saaj.packaging.mime.util.*;
import com.sun.xml.internal.messaging.saaj.util.FinalArrayList;
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
import com.sun.xml.internal.messaging.saaj.util.SAAJUtil;

/**
 * The MimeMultipart class is an implementation
 * that uses MIME conventions for the multipart data. <p>
 *
 * A MimeMultipart is obtained from a MimeBodyPart 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>.
 *
 * @version 1.31, 03/01/29
 * @author  John Mani
 * @author  Bill Shannon
 * @author  Max Spivak
 */

//BM MimeMultipart can extend this
public  class MimeMultipart {

    /**
     * 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;

    /**
     * Vector of MimeBodyPart objects.
     */
    protected FinalArrayList parts = new FinalArrayList(); // Holds BodyParts

    /**
     * This field specifies the content-type of this multipart
     * object. It defaults to "multipart/mixed".
     */
    protected ContentType contentType;

    /**
     * The <code>MimeBodyPart</code> containing this <code>MimeMultipart</code>,
     * if known.
     * @since   JavaMail 1.1
     */
    protected MimeBodyPart parent;

    protected static final boolean ignoreMissingEndBoundary;
    static {
        ignoreMissingEndBoundary = SAAJUtil.getSystemBoolean("saaj.mime.multipart.ignoremissingendboundary");
    }

    /**
     * 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 = new ContentType("multipart", subtype, null);
        contentType.setParameter("boundary", boundary);
    }

    /**
     * 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.
     *
     * 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
     * @param ct
     *      This must be the same information as {@link DataSource#getContentType()}.
     *      All the callers of this method seem to have this object handy, so
     *      for performance reason this method accepts it. Can be null.
     */
    public MimeMultipart(DataSource ds, ContentType ct) throws MessagingException {
        // 'ds' was not a MultipartDataSource, we have
        // to parse this ourself.
        parsed = false;
        this.ds = ds;
        if (ct==null)
            contentType = new ContentType(ds.getContentType());
        else
            contentType = ct;
    }

    /**
     * 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
     */
    public  void setSubType(String subtype) {
        contentType.setSubType(subtype);
    }

    /**
     * Return the number of enclosed MimeBodyPart objects.
     *
     * @return          number of parts
     */
    public  int getCount() throws MessagingException {
        parse();
        if (parts == null)
            return 0;

        return parts.size();
    }

    /**
     * Get the specified MimeBodyPart.  BodyParts are numbered starting at 0.
     *
     * @param index     the index of the desired MimeBodyPart
     * @return          the MimeBodyPart
     * @exception       MessagingException if no such MimeBodyPart exists
     */
    public  MimeBodyPart getBodyPart(int index)
                        throws MessagingException {
        parse();
        if (parts == null)
            throw new IndexOutOfBoundsException("No such BodyPart");

        return (MimeBodyPart)parts.get(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 MimeBodyPart
     */
    public  MimeBodyPart getBodyPart(String CID)
                        throws MessagingException {
        parse();

        int count = getCount();
        for (int i = 0; i < count; i++) {
           MimeBodyPart part = getBodyPart(i);
           String s = part.getContentID();
           // Old versions of AXIS2 put angle brackets around the content
           // id but not the start param
           String sNoAngle = (s!= null) ? s.replaceFirst("^<", "").replaceFirst(">$", "")
                   :null;
           if (s != null && (s.equals(CID) || CID.equals(sNoAngle)))
                return part;
        }
        return null;
    }

    /**
     * Update headers. The default implementation here just
     * calls the <code>updateHeaders</code> method on each of its
     * children BodyParts. <p>
     *
     * Note that the boundary parameter is already set up when
     * a new and empty MimeMultipart object is created. <p>
     *
     * This method is called when the <code>saveChanges</code>
     * method is invoked on the Message object containing this
     * MimeMultipart. This is typically done as part of the Message
     * send process, however note that a client is free to call
     * it any number of times. So if the header updating process is
     * expensive for a specific MimeMultipart subclass, then it
     * might itself want to track whether its internal state actually
     * did change, and do the header updating only if necessary.
     */
    protected void updateHeaders() throws MessagingException {
        for (int i = 0; i < parts.size(); i++)
            ((MimeBodyPart)parts.get(i)).updateHeaders();
    }

    /**
     * Iterates through all the parts and outputs each Mime part
     * separated by a boundary.
     */
    public void writeTo(OutputStream os)
            throws IOException, MessagingException {
        parse();

        String boundary = "--" + contentType.getParameter("boundary");

        for (int i = 0; i < parts.size(); i++) {
            OutputUtil.writeln(boundary, os); // put out boundary
            getBodyPart(i).writeTo(os);
            OutputUtil.writeln(os); // put out empty line
        }

        // put out last boundary
        OutputUtil.writeAsAscii(boundary, os);
        OutputUtil.writeAsAscii("--", os);
        os.flush();
    }

    /**
     * Parse the InputStream from our DataSource, constructing the
     * appropriate MimeBodyParts.  The <code>parsed</code> flag is
     * set to true, and if true on entry nothing is done.  This
     * method is called by all other methods that need data for
     * the body parts, to make sure the data has been parsed.
     *
     * @since   JavaMail 1.2
     */
    protected  void parse() throws MessagingException {
        if (parsed)
            return;

        InputStream in;
        SharedInputStream sin = null;
        long start = 0, end = 0;
        boolean foundClosingBoundary = false;

        try {
            in = ds.getInputStream();
            if (!(in instanceof ByteArrayInputStream) &&
                !(in instanceof BufferedInputStream) &&
                !(in instanceof SharedInputStream))
                in = new BufferedInputStream(in);
        } catch (Exception ex) {
            throw new MessagingException("No inputstream from datasource");
        }
        if (in instanceof SharedInputStream)
            sin = (SharedInputStream)in;

        String boundary = "--" + contentType.getParameter("boundary");
        byte[] bndbytes = ASCIIUtility.getBytes(boundary);
        int bl = bndbytes.length;

        try {
            // Skip the preamble
            LineInputStream lin = new LineInputStream(in);
            String line;
            while ((line = lin.readLine()) != null) {
                /*
                 * Strip trailing whitespace.  Can't use trim method
                 * because it's too aggressive.  Some bogus MIME
                 * messages will include control characters in the
                 * boundary string.
                 */
                int i;
                for (i = line.length() - 1; i >= 0; i--) {
                    char c = line.charAt(i);
                    if (!(c == ' ' || c == '\t'))
                        break;
                }
                line = line.substring(0, i + 1);
                if (line.equals(boundary))
                    break;
            }
            if (line == null)
                throw new MessagingException("Missing start boundary");

            /*
             * Read and process body parts until we see the
             * terminating boundary line (or EOF).
             */
            boolean done = false;
        getparts:
            while (!done) {
                InternetHeaders headers = null;
                if (sin != null) {
                    start = sin.getPosition();
                    // skip headers
                    while ((line = lin.readLine()) != null && line.length() > 0)
                        ;
                    if (line == null) {
                        if (!ignoreMissingEndBoundary) {
                           throw new MessagingException("Missing End Boundary for Mime Package : EOF while skipping headers");
                        }
                        // assume there's just a missing end boundary
                        break getparts;
                    }
                } else {
                    // collect the headers for this body part
                    headers = createInternetHeaders(in);
                }

                if (!in.markSupported())
                    throw new MessagingException("Stream doesn't support mark");

                ByteOutputStream buf = null;
                // if we don't have a shared input stream, we copy the data
                if (sin == null)
                    buf = new ByteOutputStream();
                int b;
                boolean bol = true;    // beginning of line flag
                // the two possible end of line characters
                int eol1 = -1, eol2 = -1;

                /*
                 * Read and save the content bytes in buf.
                 */
                for (;;) {
                    if (bol) {
                        /*
                         * At the beginning of a line, check whether the
                         * next line is a boundary.
                         */
                        int i;
                        in.mark(bl + 4 + 1000); // bnd + "--\r\n" + lots of LWSP
                        // read bytes, matching against the boundary
                        for (i = 0; i < bl; i++)
                            if (in.read() != bndbytes[i])
                                break;
                        if (i == bl) {
                            // matched the boundary, check for last boundary
                            int b2 = in.read();
                            if (b2 == '-') {
                                if (in.read() == '-') {
                                    done = true;
                                    foundClosingBoundary = true;
                                    break;      // ignore trailing text
                                }
                            }
                            // skip linear whitespace
                            while (b2 == ' ' || b2 == '\t')
                                b2 = in.read();
                            // check for end of line
                            if (b2 == '\n')
                                break;  // got it!  break out of the loop
                            if (b2 == '\r') {
                                in.mark(1);
                                if (in.read() != '\n')
                                    in.reset();
                                break;  // got it!  break out of the loop
                            }
                        }
                        // failed to match, reset and proceed normally
                        in.reset();

                        // if this is not the first line, write out the
                        // end of line characters from the previous line
                        if (buf != null && eol1 != -1) {
                            buf.write(eol1);
                            if (eol2 != -1)
                                buf.write(eol2);
                            eol1 = eol2 = -1;
                        }
                    }

                    // read the next byte
                    if ((b = in.read()) < 0) {
                        done = true;
                        break;
                    }

                    /*
                     * If we're at the end of the line, save the eol characters
                     * to be written out before the beginning of the next line.
                     */
                    if (b == '\r' || b == '\n') {
                        bol = true;
                        if (sin != null)
                            end = sin.getPosition() - 1;
                        eol1 = b;
                        if (b == '\r') {
                            in.mark(1);
                            if ((b = in.read()) == '\n')
                                eol2 = b;
                            else
                                in.reset();
                        }
                    } else {
                        bol = false;
                        if (buf != null)
                            buf.write(b);
                    }
                }

                /*
                 * Create a MimeBody element to represent this body part.
                 */
                MimeBodyPart part;
                if (sin != null)
                    part = createMimeBodyPart(sin.newStream(start, end));
                else
                    part = createMimeBodyPart(headers, buf.getBytes(), buf.getCount());
                addBodyPart(part);
            }
        } catch (IOException ioex) {
            throw new MessagingException("IO Error", ioex);
        }

        if (!ignoreMissingEndBoundary && !foundClosingBoundary && sin== null) {
            throw new MessagingException("Missing End Boundary for Mime Package : EOF while skipping headers");
        }
        parsed = true;
    }

    /**
     * Create and return an InternetHeaders object that loads the
     * headers from the given InputStream.  Subclasses can override
     * this method to return a subclass of InternetHeaders, if
     * necessary.  This implementation simply constructs and returns
     * an InternetHeaders object.
     *
     * @param   is      the InputStream to read the headers from
     * @exception       MessagingException
     * @since           JavaMail 1.2
     */
    protected InternetHeaders createInternetHeaders(InputStream is)
                                throws MessagingException {
        return new InternetHeaders(is);
    }

    /**
     * Create and return a MimeBodyPart object to represent a
     * body part parsed from the InputStream.  Subclasses can override
     * this method to return a subclass of MimeBodyPart, if
     * necessary.  This implementation simply constructs and returns
     * a MimeBodyPart object.
     *
     * @param   headers         the headers for the body part
     * @param   content         the content of the body part
     * @since                   JavaMail 1.2
     */
    protected MimeBodyPart createMimeBodyPart(InternetHeaders headers, byte[] content, int len) {
            return new MimeBodyPart(headers, content,len);
    }

    /**
     * Create and return a MimeBodyPart object to represent a
     * body part parsed from the InputStream.  Subclasses can override
     * this method to return a subclass of MimeBodyPart, if
     * necessary.  This implementation simply constructs and returns
     * a MimeBodyPart object.
     *
     * @param   is              InputStream containing the body part
     * @exception               MessagingException
     * @since                   JavaMail 1.2
     */
    protected MimeBodyPart createMimeBodyPart(InputStream is) throws MessagingException {
            return new MimeBodyPart(is);
    }

    /**
     * Setup this MimeMultipart object from the given MultipartDataSource. <p>
     *
     * The method adds the MultipartDataSource's MimeBodyPart
     * objects into this MimeMultipart. This MimeMultipart's contentType is
     * set to that of the MultipartDataSource. <p>
     *
     * This method is typically used in those cases where one
     * has a multipart data source that has already been pre-parsed into
     * the individual body parts (for example, an IMAP datasource), but
     * needs to create an appropriate MimeMultipart subclass that represents
     * a specific multipart subtype.
     *
     * @param   mp      MimeMultipart datasource
     */

    protected void setMultipartDataSource(MultipartDataSource mp)
                        throws MessagingException {
        contentType = new ContentType(mp.getContentType());

        int count = mp.getCount();
        for (int i = 0; i < count; i++)
            addBodyPart(mp.getBodyPart(i));
    }

    /**
     * Return the content-type of this MimeMultipart. <p>
     *
     * This implementation just returns the value of the
     * <code>contentType</code> field.
     *
     * @return  content-type
     * @see     #contentType
     */
    public ContentType getContentType() {
            return contentType;
    }

    /**
     * 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 MimeBodyPart exists
     */
    public boolean removeBodyPart(MimeBodyPart part) throws MessagingException {
        if (parts == null)
            throw new MessagingException("No such body part");

        boolean ret = parts.remove(part);
        part.setParent(null);
        return ret;
    }

    /**
     * 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       IndexOutOfBoundsException if the given index
     *                  is out of range.
     */
    public void removeBodyPart(int index) {
        if (parts == null)
            throw new IndexOutOfBoundsException("No such BodyPart");

        MimeBodyPart part = (MimeBodyPart)parts.get(index);
        parts.remove(index);
        part.setParent(null);
    }

    /**
     * Adds a MimeBodyPart to the multipart.  The MimeBodyPart is appended to
     * the list of existing Parts.
     *
     * @param  part  The MimeBodyPart to be appended
     */
    public synchronized void addBodyPart(MimeBodyPart part) {
        if (parts == null)
            parts = new FinalArrayList();

        parts.add(part);
        part.setParent(this);
    }

    /**
     * Adds a MimeBodyPart 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
     * MimeBodyPart is appended to the end.
     *
     * @param  part  The MimeBodyPart to be inserted
     * @param  index Location where to insert the part
     */
    public synchronized void addBodyPart(MimeBodyPart part, int index) {
        if (parts == null)
            parts = new FinalArrayList();

        parts.add(index,part);
        part.setParent(this);
    }

    /**
     * Return the <code>MimeBodyPart</code> that contains this <code>MimeMultipart</code>
     * object, or <code>null</code> if not known.
     * @since   JavaMail 1.1
     */
    MimeBodyPart getParent() {
        return parent;
    }

    /**
     * Set the parent of this <code>MimeMultipart</code> to be the specified
     * <code>MimeBodyPart</code>.  Normally called by the <code>Message</code>
     * or <code>MimeBodyPart</code> <code>setContent(MimeMultipart)</code> method.
     * <code>parent</code> may be <code>null</code> if the
     * <code>MimeMultipart</code> is being removed from its containing
     * <code>MimeBodyPart</code>.
     * @since   JavaMail 1.1
     */
    void setParent(MimeBodyPart parent) {
        this.parent = parent;
    }
}