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