001: /*
002: * Portions Copyright 2000-2007 Sun Microsystems, Inc. All Rights
003: * Reserved. Use is subject to license terms.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025: /*
026: */
027: package gov.nist.siplite.header;
028:
029: import gov.nist.core.*;
030:
031: /**
032: * ContentTypeHeader SIP Header
033: * <pre>
034: *14.17 Content-Type
035: *
036: * The Content-Type entity-header field indicates the media type of the
037: * entity-body sent to the recipient or, in the case of the HEAD method,
038: * the media type that would have been sent had the request been a GET.
039: *
040: * Format: Content-Type = "Content-Type" ":" media-type
041: *
042: * For details please see RFC 2616, section 14.17
043: * </pre>
044: *
045: *
046: * @version JAIN-SIP-1.1
047: *
048: * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
049: *
050: */
051: public class ContentTypeHeader extends ParametersHeader {
052:
053: /**
054: * Media range header field.
055: */
056: protected MediaRange mediaRange;
057: /** Handle for class. */
058: public static Class clazz;
059: /** Media range header field label. */
060: public static final String NAME = Header.CONTENT_TYPE;
061:
062: static {
063: clazz = new ContentTypeHeader().getClass();
064: }
065:
066: /**
067: * Default constructor.
068: */
069: public ContentTypeHeader() {
070: super (CONTENT_TYPE);
071: }
072:
073: /**
074: * Constructor given a content type and subtype.
075: * @param contentType is the content type.
076: * @param contentSubtype is the content subtype
077: */
078: public ContentTypeHeader(String contentType, String contentSubtype) {
079: this ();
080: this .setContentType(contentType, contentSubtype);
081: }
082:
083: /**
084: * Compares two MediaRange headers.
085: * @param media String to set
086: * @return int.
087: */
088: public int compareMediaRange(String media) {
089: return compareToIgnoreCase(mediaRange.type + "/"
090: + mediaRange.subtype, media);
091: }
092:
093: /**
094: * Encodes into a canonical string.
095: * @return String.
096: */
097: public String encodeBody() {
098: if (hasParameters())
099: return new StringBuffer(mediaRange.encode()).append(
100: encodeWithSep()).toString();
101: else
102: return mediaRange.encode();
103: }
104:
105: /**
106: * Gets the mediaRange field.
107: * @return MediaRange.
108: */
109: public MediaRange getMediaRange() {
110: return mediaRange;
111: }
112:
113: /**
114: * Gets the Media Type.
115: * @return String.
116: */
117: public String getMediaType() {
118: return mediaRange.type;
119: }
120:
121: /**
122: * Gets the MediaSubType field.
123: * @return String.
124: */
125: public String getMediaSubType() {
126: return mediaRange.subtype;
127: }
128:
129: /**
130: * Gets the content subtype.
131: * @return the content subtype string (or null if not set).
132: */
133: public String getContentSubType() {
134: return mediaRange == null ? null : mediaRange.getSubtype();
135: }
136:
137: /**
138: * Get the content subtype.
139: * @return the content tyep string (or null if not set).
140: */
141:
142: public String getContentTypeHeader() {
143: return mediaRange == null ? null : mediaRange.getType();
144: }
145:
146: /**
147: * Gets the charset parameter.
148: * @return the content type header value
149: */
150: public String getCharset() {
151: return this .getParameter("charset");
152: }
153:
154: /**
155: * Sets the mediaRange member
156: * @param m mediaRange field.
157: */
158: public void setMediaRange(MediaRange m) {
159: mediaRange = m;
160: }
161:
162: /**
163: * Sets the content type and subtype.
164: * @param contentType Content type string.
165: * @param contentSubType content subtype string
166: */
167: public void setContentType(String contentType, String contentSubType) {
168: if (mediaRange == null)
169: mediaRange = new MediaRange();
170: mediaRange.setType(contentType);
171: mediaRange.setSubtype(contentSubType);
172: }
173:
174: /**
175: * Sets the content type.
176: * @param contentType Content type string.
177: */
178:
179: public void setContentType(String contentType) {
180: if (contentType == null)
181: throw new NullPointerException("null arg");
182: if (mediaRange == null)
183: mediaRange = new MediaRange();
184: mediaRange.setType(contentType);
185: }
186:
187: /**
188: * Sets the content subtype.
189: * @param contentType String to set
190: */
191: public void setContentSubType(String contentType) {
192: if (contentType == null)
193: throw new NullPointerException("null arg");
194: if (mediaRange == null)
195: mediaRange = new MediaRange();
196: mediaRange.setSubtype(contentType);
197: }
198:
199: /**
200: * Gets the content type header value.
201: * @return the content type
202: */
203: public Object getValue() {
204: return this .mediaRange;
205: }
206:
207: /**
208: * Copies the current instance.
209: * @return copy od the current object
210: */
211: public Object clone() {
212: ContentTypeHeader retval = new ContentTypeHeader();
213: retval.parameters = (NameValueList) this .parameters.clone();
214: retval.mediaRange = (MediaRange) this.mediaRange.clone();
215: return retval;
216: }
217:
218: }
|