001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: /*
021: * @(#)ContentType.java 1.7 02/03/27
022: */
023:
024: /*
025: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
026: *
027: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
028: *
029: * The contents of this file are subject to the terms of either the GNU
030: * General Public License Version 2 only ("GPL") or the Common Development
031: * and Distribution License("CDDL") (collectively, the "License"). You
032: * may not use this file except in compliance with the License. You can obtain
033: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
034: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
035: * language governing permissions and limitations under the License.
036: *
037: * When distributing the software, include this License Header Notice in each
038: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
039: * Sun designates this particular file as subject to the "Classpath" exception
040: * as provided by Sun in the GPL Version 2 section of the License file that
041: * accompanied this code. If applicable, add the following below the License
042: * Header, with the fields enclosed by brackets [] replaced by your own
043: * identifying information: "Portions Copyrighted [year]
044: * [name of copyright owner]"
045: *
046: * Contributor(s):
047: *
048: * If you wish your version of this file to be governed by only the CDDL or
049: * only the GPL Version 2, indicate your decision by adding "[Contributor]
050: * elects to include this software in this distribution under the [CDDL or GPL
051: * Version 2] license." If you don't indicate a single choice of license, a
052: * recipient has the option to distribute your version of this file under
053: * either the CDDL, the GPL Version 2 or to extend the choice of license to
054: * its licensees as provided above. However, if you add GPL Version 2 code
055: * and therefore, elected the GPL Version 2 license, then the option applies
056: * only if the new code is made subject to such option by the copyright
057: * holder.
058: */
059:
060: package com.sun.xml.ws.encoding;
061:
062: import javax.xml.ws.WebServiceException;
063:
064: /**
065: * This class represents a MIME ContentType value. It provides
066: * methods to parse a ContentType string into individual components
067: * and to generate a MIME style ContentType string.
068: *
069: * @version 1.7, 02/03/27
070: * @author John Mani
071: */
072: public final class ContentType {
073:
074: private String primaryType; // primary type
075: private String subType; // subtype
076: private ParameterList list; // parameter list
077:
078: /**
079: * Constructor that takes a Content-Type string. The String
080: * is parsed into its constituents: primaryType, subType
081: * and parameters. A ParseException is thrown if the parse fails.
082: *
083: * @param s the Content-Type string.
084: * @exception WebServiceException if the parse fails.
085: */
086: public ContentType(String s) throws WebServiceException {
087: HeaderTokenizer h = new HeaderTokenizer(s, HeaderTokenizer.MIME);
088: HeaderTokenizer.Token tk;
089:
090: // First "type" ..
091: tk = h.next();
092: if (tk.getType() != HeaderTokenizer.Token.ATOM)
093: throw new WebServiceException();
094: primaryType = tk.getValue();
095:
096: // The '/' separator ..
097: tk = h.next();
098: if ((char) tk.getType() != '/')
099: throw new WebServiceException();
100:
101: // Then "subType" ..
102: tk = h.next();
103: if (tk.getType() != HeaderTokenizer.Token.ATOM)
104: throw new WebServiceException();
105: subType = tk.getValue();
106:
107: // Finally parameters ..
108: String rem = h.getRemainder();
109: if (rem != null)
110: list = new ParameterList(rem);
111: }
112:
113: /**
114: * Return the primary type.
115: * @return the primary type
116: */
117: public String getPrimaryType() {
118: return primaryType;
119: }
120:
121: /**
122: * Return the subType.
123: * @return the subType
124: */
125: public String getSubType() {
126: return subType;
127: }
128:
129: /**
130: * Return the MIME type string, without the parameters.
131: * The returned value is basically the concatenation of
132: * the primaryType, the '/' character and the secondaryType.
133: *
134: * @return the type
135: */
136: public String getBaseType() {
137: return primaryType + '/' + subType;
138: }
139:
140: /**
141: * Return the specified parameter value. Returns <code>null</code>
142: * if this parameter is absent.
143: *
144: * @param name parameter name
145: * @return parameter value
146: */
147: public String getParameter(String name) {
148: if (list == null)
149: return null;
150:
151: return list.get(name);
152: }
153:
154: /**
155: * Return a ParameterList object that holds all the available
156: * parameters. Returns null if no parameters are available.
157: *
158: * @return ParameterList
159: */
160: public ParameterList getParameterList() {
161: return list;
162: }
163:
164: }
|