public static final Charset DEFAULT_CHARSET = Charsets.UTF_8;
public static ContentType getContentType(String header) {
final String[] arr = header.split("\\s*;\\s*");
if(arr.length == 1) {
return new ContentTypeBean(header, null);
}
else {
final String contentType = arr[0];
for(int i=1; i<arr.length; i++) {
final String headerPart = arr[i];
if(headerPart.contains("charset=")) {
Pattern p = Pattern.compile("charset=(.+)");
Matcher m = p.matcher(headerPart);
if(m.matches()) {
Charset charset = Charset.forName(m.group(1));
return new ContentTypeBean(contentType, charset);
}
}
}
return new ContentTypeBean(contentType, null);
}
}