/
HttpUtils.java
97 lines (90 loc) · 3.36 KB
/
HttpUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*******************************************************************************
* Copyright (c) 2008 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Xavier Coulon - Initial API and implementation
******************************************************************************/
package org.jboss.tools.livereload.core.internal.util;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.StringTokenizer;
/**
* Utility for HTTP Requests
* @author xcoulon
*
*/
public class HttpUtils {
/**
* Private constructor of this utiliy class
*/
private HttpUtils() {
// TODO Auto-generated constructor stub
}
/**
* <p>
* Iterates over the given acceptedContentTypes, looking for one of those
* values:
* <ul>
* <li>text/html</li>
* <li>application/xhtml+xml</li>
* <li>application/xml</li>
* </ul>
* </p>
*
* @param acceptedContentTypes
* @return true if one of the values above was found, false otherwise
*/
public static boolean isHtmlContentType(final String acceptedContentTypes) {
if (acceptedContentTypes == null) {
return false;
}
// first, let's remove everything behind the comma character
int location = acceptedContentTypes.indexOf(";");
final String contentTypes = (location != -1) ? acceptedContentTypes.substring(0, location):acceptedContentTypes;
// now, let's analyze each type
final StringTokenizer tokenizer = new StringTokenizer(contentTypes, ",");
while (tokenizer.hasMoreElements()) {
final String acceptedContentType = tokenizer.nextToken();
if ("text/html".equals(acceptedContentType) || "application/xhtml+xml".equals(acceptedContentType)
|| "application/xml".equals(acceptedContentType)) {
return true;
}
}
return false;
}
/**
* Returns the {@link Charset} from the given content-type.
*
* @param contentType
* the given content type that may contain some ";charset=...".
* @param defaultCharsetName
* the name of the default charset to return in case when the given
* contentType would be null or would not contain any specific
* charset. If this name cannot be resolved into a valid charset, then "UTF-8" is used.
* @return the value of the "charset" token in the given contentType, or
* the given defaultCharsetName, or "UTF-8" as a last resort.
*/
public static Charset getContentCharSet(final String contentType, final String defaultCharsetName) {
if(contentType != null) {
final StringTokenizer stk = new StringTokenizer(contentType, ";");
while(stk.hasMoreTokens()) {
final String token = stk.nextToken().toLowerCase().replace(" ", "");
if(token.startsWith("charset=")) {
final StringTokenizer tokenSplitter = new StringTokenizer(token, "=");
tokenSplitter.nextToken(); // skip the 'charset' part as we already know it
final String value = tokenSplitter.hasMoreTokens()? tokenSplitter.nextToken() : null;
return Charset.forName(value.toUpperCase());
}
}
}
try {
return Charset.forName(defaultCharsetName);
} catch(UnsupportedCharsetException e) {
return Charset.forName("UTF-8");
}
}
}