| 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.io.*; |
| 12 |
import jakarta.activation.*; |
| 13 |
import jakarta.mail.*; |
| 14 |
import jakarta.mail.internet.*; |
| 15 |
|
| 16 |
/** |
| 17 |
* A special MimeBodyPart used with MSMessage. |
| 18 |
* |
| 19 |
* @author John Mani |
| 20 |
* @author Bill Shannon |
| 21 |
*/ |
| 22 |
public class MSBodyPart extends MimeBodyPart { |
| 23 |
private int start; |
| 24 |
private int end; |
| 25 |
private String type = UNKNOWN; |
| 26 |
private String disposition; |
| 27 |
private String encoding; |
| 28 |
private String filename = UNKNOWN; |
| 29 |
|
| 30 |
private static final String UNKNOWN = "UNKNOWN"; |
| 31 |
|
| 32 |
public MSBodyPart(byte[] content, int start, int end, |
| 33 |
String disposition, String encoding) { |
| 34 |
this.content = content; |
| 35 |
this.start = start; |
| 36 |
this.end = end; |
| 37 |
this.disposition = disposition; |
| 38 |
this.encoding = encoding; |
| 39 |
} |
| 40 |
|
| 41 |
public String getContentType() throws MessagingException { |
| 42 |
// try to figure this out from the filename extension |
| 43 |
if (type == UNKNOWN) |
| 44 |
processBegin(); |
| 45 |
return type; |
| 46 |
} |
| 47 |
|
| 48 |
public String getEncoding() throws MessagingException { |
| 49 |
return encoding; |
| 50 |
} |
| 51 |
|
| 52 |
public String getDisposition() throws MessagingException { |
| 53 |
return disposition; |
| 54 |
} |
| 55 |
|
| 56 |
public String getFileName() throws MessagingException { |
| 57 |
// get filename from the "begin" line |
| 58 |
if (filename == UNKNOWN) |
| 59 |
processBegin(); |
| 60 |
return filename; |
| 61 |
} |
| 62 |
|
| 63 |
protected InputStream getContentStream() { |
| 64 |
return new ByteArrayInputStream(content, start, end - start); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Process the "begin" line to extract the filename, |
| 69 |
* and from it determine the Content-Type. |
| 70 |
*/ |
| 71 |
private void processBegin() { |
| 72 |
InputStream in = getContentStream(); |
| 73 |
try { |
| 74 |
BufferedReader r = new BufferedReader(new InputStreamReader(in)); |
| 75 |
String begin = r.readLine(); |
| 76 |
// format is "begin 666 filename.txt" |
| 77 |
if (begin != null && begin.regionMatches(true, 0, "begin ", 0, 6)) { |
| 78 |
int i = begin.indexOf(' ', 6); |
| 79 |
if (i > 0) { |
| 80 |
filename = begin.substring(i + 1); |
| 81 |
FileTypeMap map = FileTypeMap.getDefaultFileTypeMap(); |
| 82 |
type = map.getContentType(filename); |
| 83 |
if (type == null) |
| 84 |
type = "application/octet-stream"; |
| 85 |
} |
| 86 |
} |
| 87 |
} catch (IOException ex) { |
| 88 |
// ignore |
| 89 |
} finally { |
| 90 |
try { |
| 91 |
in.close(); |
| 92 |
} catch (IOException ex) { |
| 93 |
// ignore it |
| 94 |
} |
| 95 |
if (filename == UNKNOWN) |
| 96 |
filename = null; |
| 97 |
if (type == UNKNOWN || type == null) |
| 98 |
type = "text/plain"; |
| 99 |
} |
| 100 |
} |
| 101 |
} |