new URL(Google.GOOGLE_PROFILE_RETRIEVAL.replace("{userId}",
id));
try {
final HTTPRequest request = new HTTPRequest();
request.setURL(googleProfileURL);
final HTTPResponse response = urlFetchService.fetch(request);
final int statusCode = response.getResponseCode();
if (HttpServletResponse.SC_OK == statusCode) {
final byte[] content = response.getContent();
final String profileJSONString =
new String(content, "UTF-8");
LOGGER.log(Level.FINEST, "Google profile[jsonString={0}]",
profileJSONString);
final JSONObject profile = new JSONObject(profileJSONString);
final JSONObject profileData = profile.getJSONObject("data");
thumbnailURL = profileData.getString("thumbnailUrl");
comment.put(Comment.COMMENT_THUMBNAIL_URL, thumbnailURL);
LOGGER.log(Level.FINEST, "Comment thumbnail[URL={0}]",
thumbnailURL);
return;
} else {
LOGGER.log(Level.WARNING,
"Can not fetch google profile[userId={0}, statusCode={1}]",
new Object[]{id, statusCode});
}
} catch (final Exception e) {
LOGGER.log(Level.WARNING,
"Can not fetch google profile[userId=" + id + "", e);
}
}
// Try to set thumbnail URL using Gravatar service
final String hashedEmail = MD5.hash(commentEmail.toLowerCase());
final int size = 60;
final URL gravatarURL =
new URL("http://www.gravatar.com/avatar/" + hashedEmail + "?s="
+ size + "&r=G");
try {
final HTTPRequest request = new HTTPRequest();
request.setURL(gravatarURL);
final HTTPResponse response = urlFetchService.fetch(request);
final int statusCode = response.getResponseCode();
if (HttpServletResponse.SC_OK == statusCode) {
final List<HTTPHeader> headers = response.getHeaders();
boolean defaultFileLengthMatched = false;
for (final HTTPHeader httpHeader : headers) {
if ("Content-Length".equalsIgnoreCase(httpHeader.getName())) {
if (httpHeader.getValue().equals("2147")) {
defaultFileLengthMatched = true;