1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.sun.syndication.fetcher.impl;
18
19 import java.net.URLConnection;
20 import java.util.regex.Matcher;
21 import java.util.regex.Pattern;
22
23 /***
24 * Utility class to help deal with HTTP responses
25 *
26 */
27 public class ResponseHandler {
28 public static final String defaultCharacterEncoding = "ISO-8859-1";
29
30 private final static Pattern characterEncodingPattern = Pattern.compile("charset=([.[^; ]]*)");
31
32 public static String getCharacterEncoding(URLConnection connection) {
33 return getCharacterEncoding(connection.getContentType());
34 }
35
36 /***
37 *
38 * <p>Gets the character encoding of a response. (Note that this is different to
39 * the content-encoding)</p>
40 *
41 * @param contentTypeHeader the value of the content-type HTTP header eg: text/html; charset=ISO-8859-4
42 * @return the character encoding, eg: ISO-8859-4
43 */
44 public static String getCharacterEncoding(String contentTypeHeader) {
45 if (contentTypeHeader == null) {
46 return defaultCharacterEncoding;
47 }
48
49 Matcher m = characterEncodingPattern.matcher(contentTypeHeader);
50
51 if (!m.find()) {
52 return defaultCharacterEncoding;
53 } else {
54 return m.group(1);
55 }
56 }
57 }