com.upokecenter.mail.ContentDisposition

# com.upokecenter.mail.ContentDisposition

public class ContentDisposition extends Object

Specifies how a message body should be displayed or handled by a mail user agent. This type is immutable; its contents can't be changed after it's created. To create a changeable disposition object, use the ContentDisposition.Builder class.

About the "filename" parameter

The "filename" parameter of a content disposition suggests a name to use when saving data to a file. For the "filename" parameter, the GetParameter method and Parameters property( getParameters) method in Java) do not adapt that parameter's value using the ContentDisposition.MakeFilename method. Thus, for example, the "filename" parameter, if any, returned by this method could have an arbitrary length, be encoded using RFC 2047 encoded words (which some email and HTTP implementations still like to write out in headers, even though that RFC says encoded words "MUST NOT appear within a 'quoted-string'"; see ContentDisposition.MakeFilename), or not be usable as is as a file name.

Example: An example of RFC 2047 encoded words is:

=?UTF-8?Q?test?=

Content-Disposition header fields like the following have appeared in practice:

Content-Disposition: attachment; filename==?UTF-8?Q?example?=

Content-Disposition: attachment; filename==?UTF-8?Q?test.png?=

Content-Disposition: attachment; filename="=?UTF-8?Q?test.png?="

In this implementation, the first and second of these are syntactically invalid, so they trigger parse errors, while the third of these is syntactically valid, but the "filename" parameter is treated as "=?UTF-8?Q?test.png?=", not "test.png" or something else -- RFC 2047 encoded words are not decoded at the moment a content disposition is parsed (using the Parse method).

Nested Classes

Fields

Methods

Field Details

Attachment

public static final ContentDisposition Attachment

The content disposition value “attachment” .

Inline

public static final ContentDisposition Inline

The content disposition value “inline” .

Method Details

getDispositionType

public final String getDispositionType()

Gets a string containing this object’s disposition type, such as “inline” or “attachment”. Note that under RFC 6266 sec. 4.2 and RFC 2183 sec. 2.8, unrecognized disposition types should be treated as “attachment”. (There is no default content disposition in a message has no Content-Disposition header field.) The resulting string will be in lowercase; that is, with its basic uppercase letters (“A” to “Z”) converted to basic lowercase letters (“a” to “z”).

Returns:

equals

public boolean equals(Object obj)

Determines whether this object and another object are equal.

Overrides:

Parameters:

Returns:

hashCode

public int hashCode()

Calculates the hash code of this object. The exact algorithm used by this method may change between versions of this library, and no application or process IDs are used in the hash code calculation.

Overrides:

Returns:

isInline

public final boolean isInline()

Gets a value indicating whether the disposition type is inline.

Returns:

isAttachment

public final boolean isAttachment()

Gets a value indicating whether the disposition type is attachment.

Returns:

getParameters

public final Map<String,String> getParameters()

Gets a list of parameter names associated with this object and their values. Each parameter name will be in lowercase; that is, with its basic uppercase letters ("A" to "Z") converted to basic lowercase letters ("a" to "z").

For the "filename" parameter, the value of that parameter is not adapted with the ContentDisposition.MakeFilename method; see the documentation for the ContentDisposition class.

Returns:

toString

public String toString()

Converts this content disposition to a text string form suitable for inserting in email headers. Notably, the string contains the value of a Content-Disposition header field (without the text necessarily starting with “Content-Disposition” followed by a space), and consists of one or more lines.

Overrides:

Returns:

ToSingleLineString

public String ToSingleLineString()

Converts this content disposition to a text string form suitable for inserting in HTTP headers. Notably, the string contains the value of a Content-Disposition header field (without the text necessarily starting with “Content-Disposition” followed by a space), and consists of a single line.

Returns:

MakeFilename

public static String MakeFilename(String str)

Converts a file name from the Content-Disposition header field (or another string representing a title and an optional file extension) to a suitable name for saving data to a file. This method is idempotent; that is, calling the method again on the result doesn't change that result. The method avoids characters and combinations of characters that are problematic to use in certain file systems, and leaves the vast majority of file names seen in practice untouched.

Examples of how this method works follows:

"=?utf-8?q?hello=2Etxt?=" -&gt;"hello.txt" (RFC 2047 encoding).

"=?utf-8?q?long_filename?=" -&gt;"long filename" (RFC 2047 encoding).

"utf-8'en'hello%2Etxt" -&gt;"hello.txt" (RFC 2231 encoding).

"nul.txt" -&gt;"_nul.txt" (Reserved name).

"dir1/dir2/file" -&gt;"dir1_dir2_file" (Directory separators).

Remarks:

.

Parameters:

Returns:

GetFilename

public String GetFilename()

Gets an adapted version of the “filename” parameter in this content disposition object by using the “MakeFilename” method.

Returns:

GetCreationDate

public int[] GetCreationDate()

Gets the date and time extracted from this content disposition’s “creation-date” parameter, which specifies the date of creation of a file (RFC 2183 sec. 2.4). The parameter is parsed as though by MailDateTime.ParseDateString with obsolete time zones (including “GMT”) allowed. See that method’s documentation for information on the format of this method’s return value.

Returns:

GetModificationDate

public int[] GetModificationDate()

Gets the date and time extracted from this content disposition’s “modification-date” parameter, which specifies the date of last modification of a file (RFC 2183 sec. 2.5). The parameter is parsed as though by MailDateTime.ParseDateString with obsolete time zones (including “GMT”) allowed. See that method’s documentation for information on the format of this method’s return value.

Returns:

GetReadDate

public int[] GetReadDate()

Gets the date and time extracted from this content disposition’s “read-date” parameter, which specifies the date at which a file was last read (RFC 2183 sec. 2.6). The parameter is parsed as though by MailDateTime.ParseDateString with obsolete time zones (including “GMT”) allowed. See that method’s documentation for information on the format of this method’s return value.

Returns:

GetParameter

public String GetParameter(String name)

Gets a parameter from this disposition object. For the “filename” parameter, the value of that parameter is not adapted with the ContentDisposition.MakeFilename method; see the documentation for the ContentDisposition class.

Parameters:

Returns:

Throws:

Parse

public static ContentDisposition Parse(String dispoValue)

Creates a new content disposition object from the value of a Content-Disposition header field.

Parameters:

Returns:

Throws:

Parse

public static ContentDisposition Parse(String dispositionValue, ContentDisposition defaultValue)

Parses a content disposition string and returns a content disposition object, or the default value if the string is invalid. This method checks the syntactic validity of the string, but not whether it has all parameters it's required to have or whether the parameters themselves are set to valid values for the parameter.

This method assumes the specified content disposition string was directly extracted from the Content-Disposition header field (as defined for email messages) and follows the syntax given in RFC 2183. Accordingly, among other things, the content disposition string can contain comments (delimited by parentheses).

RFC 2231 extensions allow each content disposition parameter to be associated with a character encoding and/or language, and support parameter values that span two or more key-value pairs. Parameters making use of RFC 2231 extensions have names with an asterisk ("*"). Such a parameter will be ignored if it is ill-formed because of RFC 2231's rules (except for illegal percent-decoding or undecodable sequences for the specified character encoding). Examples of RFC 2231 extensions follow (both examples encode the same "filename" parameter):

inline; filename*=utf-8'en'filename.txt

inline; filename*0*=utf-8'en'file; filename*1*=name%2Etxt

This implementation ignores keys (in parameter key-value pairs) that appear more than once in the content disposition. Nothing in RFCs 2045, 2183, 2231, 6266, or 7231 explicitly disallows such keys, or otherwise specifies error-handling behavior for such keys.

Parameters:

Returns:

Throws:

Back to MailLib start page.