01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.tomcat.util.http;
19:
20: /**
21: * Usefull methods for Content-Type processing
22: *
23: * @author James Duncan Davidson [duncan@eng.sun.com]
24: * @author James Todd [gonzo@eng.sun.com]
25: * @author Jason Hunter [jch@eng.sun.com]
26: * @author Harish Prabandham
27: * @author costin@eng.sun.com
28: */
29: public class ContentType {
30:
31: /**
32: * Parse the character encoding from the specified content type header.
33: * If the content type is null, or there is no explicit character encoding,
34: * <code>null</code> is returned.
35: *
36: * @param contentType a content type header
37: */
38: public static String getCharsetFromContentType(String contentType) {
39:
40: if (contentType == null)
41: return (null);
42: int start = contentType.indexOf("charset=");
43: if (start < 0)
44: return (null);
45: String encoding = contentType.substring(start + 8);
46: int end = encoding.indexOf(';');
47: if (end >= 0)
48: encoding = encoding.substring(0, end);
49: encoding = encoding.trim();
50: if ((encoding.length() > 2) && (encoding.startsWith("\""))
51: && (encoding.endsWith("\"")))
52: encoding = encoding.substring(1, encoding.length() - 1);
53: return (encoding.trim());
54:
55: }
56:
57: /**
58: * Returns true if the given content type contains a charset component,
59: * false otherwise.
60: *
61: * @param type Content type
62: * @return true if the given content type contains a charset component,
63: * false otherwise
64: */
65: public static boolean hasCharset(String type) {
66:
67: boolean hasCharset = false;
68:
69: int len = type.length();
70: int index = type.indexOf(';');
71: while (index != -1) {
72: index++;
73: while (index < len && Character.isSpace(type.charAt(index))) {
74: index++;
75: }
76: if (index + 8 < len && type.charAt(index) == 'c'
77: && type.charAt(index + 1) == 'h'
78: && type.charAt(index + 2) == 'a'
79: && type.charAt(index + 3) == 'r'
80: && type.charAt(index + 4) == 's'
81: && type.charAt(index + 5) == 'e'
82: && type.charAt(index + 6) == 't'
83: && type.charAt(index + 7) == '=') {
84: hasCharset = true;
85: break;
86: }
87: index = type.indexOf(';', index);
88: }
89:
90: return hasCharset;
91: }
92:
93: }
|