01: package com.opensymphony.module.sitemesh.filter;
02:
03: /**
04: * Extracts the type and encoding from an HTTP Content-Type header.
05: *
06: * @author Scott Farquhar
07: */
08: public class HttpContentType {
09:
10: private final String type;
11: private final String encoding;
12:
13: public HttpContentType(String fullValue) {
14: // this is the content type + charset. eg: text/html;charset=UTF-8
15: int offset = fullValue.lastIndexOf("charset=");
16: encoding = offset != -1 ? extractContentTypeValue(fullValue,
17: offset + 8) : null;
18: type = extractContentTypeValue(fullValue, 0);
19: }
20:
21: private String extractContentTypeValue(String type, int startIndex) {
22: if (startIndex < 0)
23: return null;
24:
25: // Skip over any leading spaces
26: while (startIndex < type.length()
27: && type.charAt(startIndex) == ' ')
28: startIndex++;
29:
30: if (startIndex >= type.length()) {
31: return null;
32: }
33:
34: int endIndex = startIndex;
35:
36: if (type.charAt(startIndex) == '"') {
37: startIndex++;
38: endIndex = type.indexOf('"', startIndex);
39: if (endIndex == -1)
40: endIndex = type.length();
41: } else {
42: // Scan through until we hit either the end of the string or a
43: // special character (as defined in RFC-2045). Note that we ignore '/'
44: // since we want to capture it as part of the value.
45: char ch;
46: while (endIndex < type.length()
47: && (ch = type.charAt(endIndex)) != ' ' && ch != ';'
48: && ch != '(' && ch != ')' && ch != '[' && ch != ']'
49: && ch != '<' && ch != '>' && ch != ':' && ch != ','
50: && ch != '=' && ch != '?' && ch != '@' && ch != '"'
51: && ch != '\\')
52: endIndex++;
53: }
54: return type.substring(startIndex, endIndex);
55: }
56:
57: public String getType() {
58: return type;
59: }
60:
61: public String getEncoding() {
62: return encoding;
63: }
64: }
|