aoqi@0: /*
aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0: *
aoqi@0: * This code is free software; you can redistribute it and/or modify it
aoqi@0: * under the terms of the GNU General Public License version 2 only, as
aoqi@0: * published by the Free Software Foundation. Oracle designates this
aoqi@0: * particular file as subject to the "Classpath" exception as provided
aoqi@0: * by Oracle in the LICENSE file that accompanied this code.
aoqi@0: *
aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0: * accompanied this code).
aoqi@0: *
aoqi@0: * You should have received a copy of the GNU General Public License version
aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0: *
aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0: * or visit www.oracle.com if you need additional information or have any
aoqi@0: * questions.
aoqi@0: */
aoqi@0:
aoqi@0: /*
aoqi@0: * @(#)ContentType.java 1.7 02/03/27
aoqi@0: */
aoqi@0:
aoqi@0:
aoqi@0:
aoqi@0: package com.sun.xml.internal.messaging.saaj.packaging.mime.internet;
aoqi@0:
aoqi@0:
aoqi@0: /**
aoqi@0: * This class represents a MIME ContentType value. It provides
aoqi@0: * methods to parse a ContentType string into individual components
aoqi@0: * and to generate a MIME style ContentType string.
aoqi@0: *
aoqi@0: * @version 1.7, 02/03/27
aoqi@0: * @author John Mani
aoqi@0: */
aoqi@0: public final class ContentType {
aoqi@0:
aoqi@0: private String primaryType; // primary type
aoqi@0: private String subType; // subtype
aoqi@0: private ParameterList list; // parameter list
aoqi@0:
aoqi@0: /**
aoqi@0: * No-arg Constructor.
aoqi@0: */
aoqi@0: public ContentType() { }
aoqi@0:
aoqi@0: /**
aoqi@0: * Constructor.
aoqi@0: *
aoqi@0: * @param primaryType primary type
aoqi@0: * @param subType subType
aoqi@0: * @param list ParameterList
aoqi@0: */
aoqi@0: public ContentType(String primaryType, String subType,
aoqi@0: ParameterList list) {
aoqi@0: this.primaryType = primaryType;
aoqi@0: this.subType = subType;
aoqi@0: if (list == null)
aoqi@0: list = new ParameterList();
aoqi@0: this.list = list;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Constructor that takes a Content-Type string. The String
aoqi@0: * is parsed into its constituents: primaryType, subType
aoqi@0: * and parameters. A ParseException is thrown if the parse fails.
aoqi@0: *
aoqi@0: * @param s the Content-Type string.
aoqi@0: * @exception ParseException if the parse fails.
aoqi@0: */
aoqi@0: public ContentType(String s) throws ParseException {
aoqi@0: HeaderTokenizer h = new HeaderTokenizer(s, HeaderTokenizer.MIME);
aoqi@0: HeaderTokenizer.Token tk;
aoqi@0:
aoqi@0: // First "type" ..
aoqi@0: tk = h.next();
aoqi@0: if (tk.getType() != HeaderTokenizer.Token.ATOM)
aoqi@0: throw new ParseException();
aoqi@0: primaryType = tk.getValue();
aoqi@0:
aoqi@0: // The '/' separator ..
aoqi@0: tk = h.next();
aoqi@0: if ((char)tk.getType() != '/')
aoqi@0: throw new ParseException();
aoqi@0:
aoqi@0: // Then "subType" ..
aoqi@0: tk = h.next();
aoqi@0: if (tk.getType() != HeaderTokenizer.Token.ATOM)
aoqi@0: throw new ParseException();
aoqi@0: subType = tk.getValue();
aoqi@0:
aoqi@0: // Finally parameters ..
aoqi@0: String rem = h.getRemainder();
aoqi@0: if (rem != null)
aoqi@0: list = new ParameterList(rem);
aoqi@0: }
aoqi@0:
aoqi@0: public ContentType copy() {
aoqi@0: return new ContentType(primaryType,subType,list.copy());
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Return the primary type.
aoqi@0: * @return the primary type
aoqi@0: */
aoqi@0: public String getPrimaryType() {
aoqi@0: return primaryType;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Return the subType.
aoqi@0: * @return the subType
aoqi@0: */
aoqi@0: public String getSubType() {
aoqi@0: return subType;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Return the MIME type string, without the parameters.
aoqi@0: * The returned value is basically the concatenation of
aoqi@0: * the primaryType, the '/' character and the secondaryType.
aoqi@0: *
aoqi@0: * @return the type
aoqi@0: */
aoqi@0: public String getBaseType() {
aoqi@0: return primaryType + '/' + subType;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Return the specified parameter value. Returns null
aoqi@0: * if this parameter is absent.
aoqi@0: * @return parameter value
aoqi@0: */
aoqi@0: public String getParameter(String name) {
aoqi@0: if (list == null)
aoqi@0: return null;
aoqi@0:
aoqi@0: return list.get(name);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Return a ParameterList object that holds all the available
aoqi@0: * parameters. Returns null if no parameters are available.
aoqi@0: *
aoqi@0: * @return ParameterList
aoqi@0: */
aoqi@0: public ParameterList getParameterList() {
aoqi@0: return list;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Set the primary type. Overrides existing primary type.
aoqi@0: * @param primaryType primary type
aoqi@0: */
aoqi@0: public void setPrimaryType(String primaryType) {
aoqi@0: this.primaryType = primaryType;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Set the subType. Overrides existing subType
aoqi@0: * @param subType subType
aoqi@0: */
aoqi@0: public void setSubType(String subType) {
aoqi@0: this.subType = subType;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Set the specified parameter. If this parameter already exists,
aoqi@0: * it is replaced by this new value.
aoqi@0: *
aoqi@0: * @param name parameter name
aoqi@0: * @param value parameter value
aoqi@0: */
aoqi@0: public void setParameter(String name, String value) {
aoqi@0: if (list == null)
aoqi@0: list = new ParameterList();
aoqi@0:
aoqi@0: list.set(name, value);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Set a new ParameterList.
aoqi@0: * @param list ParameterList
aoqi@0: */
aoqi@0: public void setParameterList(ParameterList list) {
aoqi@0: this.list = list;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Retrieve a RFC2045 style string representation of
aoqi@0: * this Content-Type. Returns null
if
aoqi@0: * the conversion failed.
aoqi@0: *
aoqi@0: * @return RFC2045 style string
aoqi@0: */
aoqi@0: public String toString() {
aoqi@0: if (primaryType == null || subType == null) // need both
aoqi@0: return null;
aoqi@0:
aoqi@0: StringBuffer sb = new StringBuffer();
aoqi@0: sb.append(primaryType).append('/').append(subType);
aoqi@0: if (list != null)
aoqi@0: // Http Binding section of the "SOAP with attachments" specification says,
aoqi@0: // "SOAP message senders should send Content-Type headers on a single long line."
aoqi@0: // (http://www.w3.org/TR/SOAP-attachments#HTTPBinding)
aoqi@0: sb.append(list.toString());
aoqi@0:
aoqi@0: return sb.toString();
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Match with the specified ContentType object. This method
aoqi@0: * compares only the primaryType
and
aoqi@0: * subType
. The parameters of both operands
aoqi@0: * are ignored.
aoqi@0: *
aoqi@0: * For example, this method will return true
when
aoqi@0: * comparing the ContentTypes for "text/plain"
aoqi@0: * and "text/plain; charset=foobar".
aoqi@0: *
aoqi@0: * If the subType
of either operand is the special
aoqi@0: * character '*', then the subtype is ignored during the match.
aoqi@0: * For example, this method will return true
when
aoqi@0: * comparing the ContentTypes for "text/plain"
aoqi@0: * and "text/*"
aoqi@0: *
aoqi@0: * @param cType to compare this against
aoqi@0: */
aoqi@0: public boolean match(ContentType cType) {
aoqi@0: // Match primaryType
aoqi@0: if (!primaryType.equalsIgnoreCase(cType.getPrimaryType()))
aoqi@0: return false;
aoqi@0:
aoqi@0: String sType = cType.getSubType();
aoqi@0:
aoqi@0: // If either one of the subTypes is wildcarded, return true
aoqi@0: if ((subType.charAt(0) == '*') || (sType.charAt(0) == '*'))
aoqi@0: return true;
aoqi@0:
aoqi@0: // Match subType
aoqi@0: if (!subType.equalsIgnoreCase(sType))
aoqi@0: return false;
aoqi@0:
aoqi@0: return true;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Match with the specified content-type string. This method
aoqi@0: * compares only the primaryType
and
aoqi@0: * subType
.
aoqi@0: * The parameters of both operands are ignored.
aoqi@0: *
aoqi@0: * For example, this method will return true
when
aoqi@0: * comparing the ContentType for "text/plain"
aoqi@0: * with "text/plain; charset=foobar".
aoqi@0: *
aoqi@0: * If the subType
of either operand is the special
aoqi@0: * character '*', then the subtype is ignored during the match.
aoqi@0: * For example, this method will return true
when
aoqi@0: * comparing the ContentType for "text/plain"
aoqi@0: * with "text/*"
aoqi@0: */
aoqi@0: public boolean match(String s) {
aoqi@0: try {
aoqi@0: return match(new ContentType(s));
aoqi@0: } catch (ParseException pex) {
aoqi@0: return false;
aoqi@0: }
aoqi@0: }
aoqi@0: }