View Javadoc
1   /*
2    * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  /*
27   * @(#)ContentType.java       1.7 02/03/27
28   */
29  
30  
31  
32  package com.sun.xml.internal.ws.encoding;
33  
34  import javax.xml.ws.WebServiceException;
35  
36  /**
37   * This class represents a MIME ContentType value. It provides
38   * methods to parse a ContentType string into individual components
39   * and to generate a MIME style ContentType string.
40   *
41   * @version 1.7, 02/03/27
42   * @author  John Mani
43   */
44  public final class ContentType {
45  
46      private String primaryType; // primary type
47      private String subType;     // subtype
48      private ParameterList list; // parameter list
49  
50      /**
51       * Constructor that takes a Content-Type string. The String
52       * is parsed into its constituents: primaryType, subType
53       * and parameters. A ParseException is thrown if the parse fails.
54       *
55       * @param   s       the Content-Type string.
56       * @exception WebServiceException if the parse fails.
57       */
58      public ContentType(String s) throws WebServiceException {
59          HeaderTokenizer h = new HeaderTokenizer(s, HeaderTokenizer.MIME);
60          HeaderTokenizer.Token tk;
61  
62          // First "type" ..
63          tk = h.next();
64          if (tk.getType() != HeaderTokenizer.Token.ATOM)
65              throw new WebServiceException();
66          primaryType = tk.getValue();
67  
68          // The '/' separator ..
69          tk = h.next();
70          if ((char)tk.getType() != '/')
71              throw new WebServiceException();
72  
73          // Then "subType" ..
74          tk = h.next();
75          if (tk.getType() != HeaderTokenizer.Token.ATOM)
76              throw new WebServiceException();
77          subType = tk.getValue();
78  
79          // Finally parameters ..
80          String rem = h.getRemainder();
81          if (rem != null)
82              list = new ParameterList(rem);
83      }
84  
85  
86      /**
87       * Return the primary type.
88       * @return the primary type
89       */
90      public String getPrimaryType() {
91              return primaryType;
92      }
93  
94      /**
95       * Return the subType.
96       * @return the subType
97       */
98      public String getSubType() {
99              return subType;
100     }
101 
102     /**
103      * Return the MIME type string, without the parameters.
104      * The returned value is basically the concatenation of
105      * the primaryType, the '/' character and the secondaryType.
106      *
107      * @return the type
108      */
109     public String getBaseType() {
110             return primaryType + '/' + subType;
111     }
112 
113     /**
114      * Return the specified parameter value. Returns <code>null</code>
115      * if this parameter is absent.
116      *
117      * @param name parameter name
118      * @return  parameter value
119      */
120     public String getParameter(String name) {
121         if (list == null)
122             return null;
123 
124         return list.get(name);
125     }
126 
127     /**
128      * Return a ParameterList object that holds all the available
129      * parameters. Returns null if no parameters are available.
130      *
131      * @return  ParameterList
132      */
133     public ParameterList getParameterList() {
134             return list;
135     }
136 
137 }