FileDocCategorySizeDatePackage
ContentType.javaAPI DocGlassfish v2 API8690Mon May 14 15:28:48 BST 2007javax.mail.internet

ContentType

public class ContentType extends Object
This class represents a MIME ContentType value. It provides methods to parse a ContentType string into individual components and to generate a MIME style ContentType string.
version
1.10, 07/05/04
author
John Mani

Fields Summary
private String
primaryType
private String
subType
private ParameterList
list
Constructors Summary
public ContentType()
No-arg Constructor.

 
public ContentType(String primaryType, String subType, ParameterList list)
Constructor.

param
primaryType primary type
param
subType subType
param
list ParameterList

	this.primaryType = primaryType;
	this.subType = subType;
	this.list = list;
    
public ContentType(String s)
Constructor that takes a Content-Type string. The String is parsed into its constituents: primaryType, subType and parameters. A ParseException is thrown if the parse fails.

param
s the Content-Type string.
exception
ParseException if the parse fails.

	HeaderTokenizer h = new HeaderTokenizer(s, HeaderTokenizer.MIME);
	HeaderTokenizer.Token tk;

	// First "type" ..
	tk = h.next();
	if (tk.getType() != HeaderTokenizer.Token.ATOM)
	    throw new ParseException();
	primaryType = tk.getValue();

	// The '/' separator ..
	tk = h.next();
	if ((char)tk.getType() != '/")
	    throw new ParseException();

	// Then "subType" ..
	tk = h.next();
	if (tk.getType() != HeaderTokenizer.Token.ATOM)
	    throw new ParseException();
	subType = tk.getValue();

	// Finally parameters ..
	String rem = h.getRemainder();
	if (rem != null)
	    list = new ParameterList(rem);
    
Methods Summary
public java.lang.StringgetBaseType()
Return the MIME type string, without the parameters. The returned value is basically the concatenation of the primaryType, the '/' character and the secondaryType.

return
the type

	return primaryType + '/" + subType;
    
public java.lang.StringgetParameter(java.lang.String name)
Return the specified parameter value. Returns null if this parameter is absent.

return
parameter value

	if (list == null)
	    return null;

	return list.get(name);
    
public javax.mail.internet.ParameterListgetParameterList()
Return a ParameterList object that holds all the available parameters. Returns null if no parameters are available.

return
ParameterList

	return list;
    
public java.lang.StringgetPrimaryType()
Return the primary type.

return
the primary type

	return primaryType;
    
public java.lang.StringgetSubType()
Return the subType.

return
the subType

	return subType;
    
public booleanmatch(javax.mail.internet.ContentType cType)
Match with the specified ContentType object. This method compares only the primaryType and subType . The parameters of both operands are ignored.

For example, this method will return true when comparing the ContentTypes for "text/plain" and "text/plain; charset=foobar". If the subType of either operand is the special character '*', then the subtype is ignored during the match. For example, this method will return true when comparing the ContentTypes for "text/plain" and "text/*"

param
cType ContentType to compare this against

	// Match primaryType
	if (!primaryType.equalsIgnoreCase(cType.getPrimaryType()))
	    return false;
	
	String sType = cType.getSubType();

	// If either one of the subTypes is wildcarded, return true
	if ((subType.charAt(0) == '*") || (sType.charAt(0) == '*"))
	    return true;
	
	// Match subType
	if (!subType.equalsIgnoreCase(sType))
	    return false;

	return true;
    
public booleanmatch(java.lang.String s)
Match with the specified content-type string. This method compares only the primaryType and subType . The parameters of both operands are ignored.

For example, this method will return true when comparing the ContentType for "text/plain" with "text/plain; charset=foobar". If the subType of either operand is the special character '*', then the subtype is ignored during the match. For example, this method will return true when comparing the ContentType for "text/plain" with "text/*"

	try {
	    return match(new ContentType(s));
	} catch (ParseException pex) {
	    return false;
	}
    
public voidsetParameter(java.lang.String name, java.lang.String value)
Set the specified parameter. If this parameter already exists, it is replaced by this new value.

param
name parameter name
param
value parameter value

	if (list == null)
	    list = new ParameterList();

	list.set(name, value);
    
public voidsetParameterList(javax.mail.internet.ParameterList list)
Set a new ParameterList.

param
list ParameterList

	this.list = list;
    
public voidsetPrimaryType(java.lang.String primaryType)
Set the primary type. Overrides existing primary type.

param
primaryType primary type

	this.primaryType = primaryType;
    
public voidsetSubType(java.lang.String subType)
Set the subType. Replaces the existing subType.

param
subType the subType

	this.subType = subType;
    
public java.lang.StringtoString()
Retrieve a RFC2045 style string representation of this Content-Type. Returns null if the conversion failed.

return
RFC2045 style string

	if (primaryType == null || subType == null) // need both
	    return null;

	StringBuffer sb = new StringBuffer();
	sb.append(primaryType).append('/").append(subType);
	if (list != null)
            // append the parameter list 
            // use the length of the string buffer + the length of
            // the header name formatted as follows "Content-Type: "
	    sb.append(list.toString(sb.length() + 14));
	
	return sb.toString();