Commit 830816d2 authored by Liang Ding's avatar Liang Ding

Fixed #60

parent 0dae2364
...@@ -18,7 +18,6 @@ package org.b3log.solo.service; ...@@ -18,7 +18,6 @@ package org.b3log.solo.service;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
...@@ -54,7 +53,7 @@ import org.json.JSONObject; ...@@ -54,7 +53,7 @@ import org.json.JSONObject;
* Comment management service. * Comment management service.
* *
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a> * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.2, Feb 3, 2012 * @version 1.0.0.3, Aug 12, 2012
* @since 0.3.5 * @since 0.3.5
*/ */
public final class CommentMgmtService { public final class CommentMgmtService {
...@@ -398,7 +397,7 @@ public final class CommentMgmtService { ...@@ -398,7 +397,7 @@ public final class CommentMgmtService {
throws JSONException, RepositoryException { throws JSONException, RepositoryException {
final JSONObject page = pageRepository.get(pageId); final JSONObject page = pageRepository.get(pageId);
final JSONObject newPage = final JSONObject newPage =
new JSONObject(page, JSONObject.getNames(page)); new JSONObject(page, JSONObject.getNames(page));
final int commentCnt = page.getInt(Page.PAGE_COMMENT_COUNT); final int commentCnt = page.getInt(Page.PAGE_COMMENT_COUNT);
newPage.put(Page.PAGE_COMMENT_COUNT, commentCnt + 1); newPage.put(Page.PAGE_COMMENT_COUNT, commentCnt + 1);
pageRepository.update(pageId, newPage); pageRepository.update(pageId, newPage);
...@@ -433,7 +432,7 @@ public final class CommentMgmtService { ...@@ -433,7 +432,7 @@ public final class CommentMgmtService {
throws JSONException, RepositoryException { throws JSONException, RepositoryException {
final JSONObject page = pageRepository.get(pageId); final JSONObject page = pageRepository.get(pageId);
final JSONObject newPage = final JSONObject newPage =
new JSONObject(page, JSONObject.getNames(page)); new JSONObject(page, JSONObject.getNames(page));
final int commentCnt = page.getInt(Page.PAGE_COMMENT_COUNT); final int commentCnt = page.getInt(Page.PAGE_COMMENT_COUNT);
newPage.put(Page.PAGE_COMMENT_COUNT, commentCnt - 1); newPage.put(Page.PAGE_COMMENT_COUNT, commentCnt - 1);
pageRepository.update(pageId, newPage); pageRepository.update(pageId, newPage);
...@@ -441,68 +440,39 @@ public final class CommentMgmtService { ...@@ -441,68 +440,39 @@ public final class CommentMgmtService {
/** /**
* Sets commenter thumbnail URL for the specified comment. * Sets commenter thumbnail URL for the specified comment.
*
* <p>
* Try to set thumbnail URL using Gravatar service.
* </p>
* *
* @param comment the specified comment * @param comment the specified comment
* @throws Exception exception * @throws Exception exception
*/ */
public static void setCommentThumbnailURL(final JSONObject comment) public static void setCommentThumbnailURL(final JSONObject comment) throws Exception {
throws Exception {
final String commentEmail = comment.getString(Comment.COMMENT_EMAIL); final String commentEmail = comment.getString(Comment.COMMENT_EMAIL);
String thumbnailURL = null;
// Try to set thumbnail URL using Gravatar service
final String hashedEmail = MD5.hash(commentEmail.toLowerCase()); final String hashedEmail = MD5.hash(commentEmail.toLowerCase());
final int size = 60; String thumbnailURL = "http://secure.gravatar.com/avatar/" + hashedEmail + "?s=60&d="
final URL gravatarURL = + Latkes.getStaticServePath() + "/images/default-user-thumbnail.png";
new URL("http://secure.gravatar.com/avatar/" + hashedEmail + "?s="
+ size + "&d=" + Latkes.getServePath() + "/images/default-user-thumbnail.png"); final URL gravatarURL = new URL(thumbnailURL);
int statusCode = HttpServletResponse.SC_OK;
try { try {
final HTTPRequest request = new HTTPRequest(); final HTTPRequest request = new HTTPRequest();
request.setURL(gravatarURL); request.setURL(gravatarURL);
final HTTPResponse response = urlFetchService.fetch(request); 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;
}
}
}
if (!defaultFileLengthMatched) {
thumbnailURL = "http://secure.gravatar.com/avatar/"
+ hashedEmail + "?s=" + size + "&d="
+ Latkes.getServePath() + "/images/default-user-thumbnail.png";
comment.put(Comment.COMMENT_THUMBNAIL_URL, thumbnailURL);
LOGGER.log(Level.FINEST, "Comment thumbnail[URL={0}]",
thumbnailURL);
return; statusCode = response.getResponseCode();
}
} else {
LOGGER.log(Level.WARNING,
"Can not fetch thumbnail from Gravatar[commentEmail={0}, statusCode={1}]",
new Object[]{commentEmail, statusCode});
}
} catch (final IOException e) { } catch (final IOException e) {
LOGGER.warning(e.getMessage()); LOGGER.log(Level.WARNING, "Can not fetch thumbnail from Gravatar[commentEmail={0}]", commentEmail);
LOGGER.log(Level.WARNING, } finally {
"Can not fetch thumbnail from Gravatar[commentEmail={0}]", if (HttpServletResponse.SC_OK != statusCode) {
commentEmail); thumbnailURL = Latkes.getStaticServePath() + "/images/" + DEFAULT_USER_THUMBNAIL;
}
} }
if (null == thumbnailURL) { comment.put(Comment.COMMENT_THUMBNAIL_URL, thumbnailURL);
LOGGER.log(Level.WARNING,
"Not supported yet for comment thumbnail for email[{0}]",
commentEmail);
thumbnailURL = "/images/" + DEFAULT_USER_THUMBNAIL;
comment.put(Comment.COMMENT_THUMBNAIL_URL, thumbnailURL);
}
} }
/** /**
...@@ -532,7 +502,7 @@ public final class CommentMgmtService { ...@@ -532,7 +502,7 @@ public final class CommentMgmtService {
* Singleton. * Singleton.
*/ */
private static final CommentMgmtService SINGLETON = private static final CommentMgmtService SINGLETON =
new CommentMgmtService(); new CommentMgmtService();
/** /**
* Private default constructor. * Private default constructor.
......
...@@ -252,7 +252,7 @@ public final class InitService { ...@@ -252,7 +252,7 @@ public final class InitService {
+ "to delete them.")); + "to delete them."));
comment.put(Comment.COMMENT_ORIGINAL_COMMENT_ID, ""); comment.put(Comment.COMMENT_ORIGINAL_COMMENT_ID, "");
comment.put(Comment.COMMENT_ORIGINAL_COMMENT_NAME, ""); comment.put(Comment.COMMENT_ORIGINAL_COMMENT_NAME, "");
comment.put(Comment.COMMENT_THUMBNAIL_URL, "http://secure.gravatar.com/avatar/59a5e8209c780307dbe9c9ba728073f5??s=60&d=" + Latkes.getServePath() + "/images/default-user-thumbnail.png"); comment.put(Comment.COMMENT_THUMBNAIL_URL, "http://secure.gravatar.com/avatar/59a5e8209c780307dbe9c9ba728073f5??s=60&r=G");
comment.put(Comment.COMMENT_DATE, date); comment.put(Comment.COMMENT_DATE, date);
comment.put(Comment.COMMENT_ON_ID, articleId); comment.put(Comment.COMMENT_ON_ID, articleId);
comment.put(Comment.COMMENT_ON_TYPE, Article.ARTICLE); comment.put(Comment.COMMENT_ON_TYPE, Article.ARTICLE);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment