Skip to content

Package: MSMultipartDataSource

MSMultipartDataSource

nameinstructionbranchcomplexitylinemethod
MSMultipartDataSource(MimePart, byte[])
M: 76 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 18 C: 0
0%
M: 1 C: 0
0%
getBodyPart(int)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getCount()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
startsWith(byte[], int, String)
M: 52 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Distribution License v. 1.0, which is available at
6: * http://www.eclipse.org/org/documents/edl-v10.php.
7: *
8: * SPDX-License-Identifier: BSD-3-Clause
9: */
10:
11: import java.util.ArrayList;
12: import java.util.List;
13:
14: import jakarta.mail.BodyPart;
15: import jakarta.mail.MessagingException;
16: import jakarta.mail.MultipartDataSource;
17: import jakarta.mail.internet.MimePart;
18: import jakarta.mail.internet.MimePartDataSource;
19: import jakarta.mail.util.StreamProvider.EncoderTypes;
20:
21: /**
22: * A special MultipartDataSource used with MSMessage.
23: *
24: * @author John Mani
25: * @author Bill Shannon
26: */
27: public class MSMultipartDataSource extends MimePartDataSource
28:                                 implements MultipartDataSource {
29: //private List<MSBodyPart> parts;
30: private List parts;
31:
32: public MSMultipartDataSource(MimePart part, byte[] content)
33:                                 throws MessagingException {
34:         super(part);
35:         //parts = new ArrayList<MSBodyPart>();
36:         parts = new ArrayList();
37:
38:         /*
39:          * Parse the text of the message to find the attachments.
40:          *
41:          * Currently we just look for the lines that mark the
42:          * begin and end of uuencoded data, but this can be
43:          * fooled by similar text in the message body. Instead,
44:          * we could use the Encoding header, which indicates how
45:          * many lines are in each body part. For example:
46:          *
47:          * Encoding: 41 TEXT, 38 UUENCODE, 3155 UUENCODE, 1096 UUENCODE
48:          *
49:          * Similarly, we could get the filenames of the attachments
50:          * from the X-MS-Attachment headers. For example:
51:          *
52:          * X-MS-Attachment: ATT00000.htx 0 00-00-1980 00:00
53:          * X-MS-Attachment: Serengeti 2GG.mpp 0 00-00-1980 00:00
54:          * X-MS-Attachment: project team update 031298.doc 0 00-00-1980 00:00
55:          *
56:          * (Note that there might be unquoted spaces in the filename.)
57:          */
58:         int pos = startsWith(content, 0, "begin");
59:•        if (pos == -1)
60:          throw new MessagingException("invalid multipart");
61:         
62:•        if (pos > 0)        // we have an unencoded main body part
63:          parts.add(new MSBodyPart(content, 0, pos, "inline", EncoderTypes.BIT7_ENCODER.getEncoder()));
64:         else                // no main body part
65:          pos = 0;
66:
67:         // now collect all the uuencoded individual body parts
68:         int start;
69:         for (;;) {
70:          start = startsWith(content, pos, "begin");
71:•         if (start == -1)
72:                 break;
73:          pos = startsWith(content, start, "end");
74:•         if (pos == -1)
75:                 break;
76:          pos += 3;        // skip to the end of "end"
77:          parts.add(new MSBodyPart(content, start, pos,
78:                                         "attachment", EncoderTypes.UU_ENCODER.getEncoder()));
79:         }
80: }
81:
82: public int getCount() {
83:         return parts.size();
84: }
85:
86: public BodyPart getBodyPart(int index) throws MessagingException {
87:         return (BodyPart)parts.get(index);
88: }
89:
90: /**
91: * This method scans the given byte[], beginning at "start", for
92: * lines that begin with the sequence "seq". If found, the start
93: * position of the sequence within the byte[] is returned.
94: */
95: private int startsWith(byte[] content, int start, String seq) {
96:         int slen = seq.length();
97:         boolean bol = true;
98:•        for (int i = start; i < content.length; i++) {
99:•         if (bol) {
100:•                if ((i + slen) < content.length) {
101:                  String s = MSMessage.toString(content, i, i + slen);
102:•                 if (s.equalsIgnoreCase(seq))
103:                         return i;
104:                 }
105:          }
106:          int b = content[i] & 0xff;
107:•         bol = b == '\r' || b == '\n';
108:         }
109:         return -1;
110: }
111: }