Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
solo
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
solo
Commits
9e1d40df
Commit
9e1d40df
authored
Mar 18, 2013
by
Liang Ding
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
接收来自社区的评论时评论人 URL 判断增强
parent
2ee3d9e1
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
369 additions
and
356 deletions
+369
-356
core/src/main/java/org/b3log/solo/api/symphony/CommentReceiver.java
...ain/java/org/b3log/solo/api/symphony/CommentReceiver.java
+369
-356
No files found.
core/src/main/java/org/b3log/solo/api/symphony/CommentReceiver.java
View file @
9e1d40df
/*
/*
* Copyright (c) 2009, 2010, 2011, 2012, 2013, B3log Team
* Copyright (c) 2009, 2010, 2011, 2012, 2013, B3log Team
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* See the License for the specific language governing permissions and
* limitations under the License.
* limitations under the License.
*/
*/
package
org
.
b3log
.
solo
.
api
.
symphony
;
package
org
.
b3log
.
solo
.
api
.
symphony
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.net.URL
;
import
java.net.MalformedURLException
;
import
java.util.Date
;
import
java.net.URL
;
import
java.util.List
;
import
java.util.Date
;
import
java.util.logging.Level
;
import
java.util.List
;
import
java.util.logging.Logger
;
import
java.util.logging.Level
;
import
javax.servlet.http.HttpServletRequest
;
import
java.util.logging.Logger
;
import
javax.servlet.http.HttpServletResponse
;
import
javax.servlet.http.HttpServletRequest
;
import
org.apache.commons.lang.time.DateFormatUtils
;
import
javax.servlet.http.HttpServletResponse
;
import
org.b3log.latke.Keys
;
import
org.apache.commons.lang.time.DateFormatUtils
;
import
org.b3log.latke.Latkes
;
import
org.b3log.latke.Keys
;
import
org.b3log.latke.event.Event
;
import
org.b3log.latke.Latkes
;
import
org.b3log.latke.event.EventManager
;
import
org.b3log.latke.event.Event
;
import
org.b3log.latke.repository.Transaction
;
import
org.b3log.latke.event.EventManager
;
import
org.b3log.latke.service.ServiceException
;
import
org.b3log.latke.repository.Transaction
;
import
org.b3log.latke.servlet.HTTPRequestContext
;
import
org.b3log.latke.service.ServiceException
;
import
org.b3log.latke.servlet.HTTPRequestMethod
;
import
org.b3log.latke.servlet.HTTPRequestContext
;
import
org.b3log.latke.servlet.annotation.RequestProcessing
;
import
org.b3log.latke.servlet.HTTPRequestMethod
;
import
org.b3log.latke.servlet.annotation.RequestProcessor
;
import
org.b3log.latke.servlet.annotation.RequestProcessing
;
import
org.b3log.latke.servlet.renderer.JSONRenderer
;
import
org.b3log.latke.servlet.annotation.RequestProcessor
;
import
org.b3log.latke.urlfetch.HTTPHeader
;
import
org.b3log.latke.servlet.renderer.JSONRenderer
;
import
org.b3log.latke.urlfetch.HTTPRequest
;
import
org.b3log.latke.urlfetch.HTTPHeader
;
import
org.b3log.latke.urlfetch.HTTPResponse
;
import
org.b3log.latke.urlfetch.HTTPRequest
;
import
org.b3log.latke.urlfetch.URLFetchService
;
import
org.b3log.latke.urlfetch.HTTPResponse
;
import
org.b3log.latke.urlfetch.URLFetchServiceFactory
;
import
org.b3log.latke.urlfetch.URLFetchService
;
import
org.b3log.latke.util.MD5
;
import
org.b3log.latke.urlfetch.URLFetchServiceFactory
;
import
org.b3log.latke.util.Requests
;
import
org.b3log.latke.util.MD5
;
import
org.b3log.latke.util.Strings
;
import
org.b3log.latke.util.Requests
;
import
org.b3log.solo.event.EventTypes
;
import
org.b3log.latke.util.Strings
;
import
org.b3log.solo.model.Article
;
import
org.b3log.solo.event.EventTypes
;
import
org.b3log.solo.model.Comment
;
import
org.b3log.solo.model.Article
;
import
org.b3log.solo.model.Preference
;
import
org.b3log.solo.model.Comment
;
import
org.b3log.solo.repository.ArticleRepository
;
import
org.b3log.solo.model.Preference
;
import
org.b3log.solo.repository.CommentRepository
;
import
org.b3log.solo.repository.ArticleRepository
;
import
org.b3log.solo.repository.impl.ArticleRepositoryImpl
;
import
org.b3log.solo.repository.CommentRepository
;
import
org.b3log.solo.repository.impl.CommentRepositoryImpl
;
import
org.b3log.solo.repository.impl.ArticleRepositoryImpl
;
import
org.b3log.solo.service.PreferenceQueryService
;
import
org.b3log.solo.repository.impl.CommentRepositoryImpl
;
import
org.b3log.solo.util.Articles
;
import
org.b3log.solo.service.PreferenceQueryService
;
import
org.b3log.solo.util.Comments
;
import
org.b3log.solo.util.Articles
;
import
org.b3log.solo.util.QueryResults
;
import
org.b3log.solo.util.Comments
;
import
org.b3log.solo.util.Statistics
;
import
org.b3log.solo.util.QueryResults
;
import
org.b3log.solo.util.TimeZones
;
import
org.b3log.solo.util.Statistics
;
import
org.json.JSONException
;
import
org.b3log.solo.util.TimeZones
;
import
org.json.JSONObject
;
import
org.json.JSONException
;
import
org.json.JSONObject
;
/**
* Comment receiver (from B3log Symphony).
/**
*
* Comment receiver (from B3log Symphony).
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
*
* @version 1.0.0.5, Jan 18, 2013
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @since 0.5.5
* @version 1.0.0.6, Mar 18, 2013
*/
* @since 0.5.5
@RequestProcessor
*/
public
final
class
CommentReceiver
{
@RequestProcessor
public
final
class
CommentReceiver
{
/**
* Logger.
/**
*/
* Logger.
private
static
final
Logger
LOGGER
=
Logger
.
getLogger
(
CommentReceiver
.
class
.
getName
());
*/
private
static
final
Logger
LOGGER
=
Logger
.
getLogger
(
CommentReceiver
.
class
.
getName
());
/**
* Comment repository.
/**
*/
* Comment repository.
private
static
CommentRepository
commentRepository
=
CommentRepositoryImpl
.
getInstance
();
*/
private
static
CommentRepository
commentRepository
=
CommentRepositoryImpl
.
getInstance
();
/**
* Article utilities.
/**
*/
* Article utilities.
private
static
Articles
articleUtils
=
Articles
.
getInstance
();
*/
private
static
Articles
articleUtils
=
Articles
.
getInstance
();
/**
* Preference query service.
/**
*/
* Preference query service.
private
PreferenceQueryService
preferenceQueryService
=
PreferenceQueryService
.
getInstance
();
*/
private
PreferenceQueryService
preferenceQueryService
=
PreferenceQueryService
.
getInstance
();
/**
* Article repository.
/**
*/
* Article repository.
private
static
ArticleRepository
articleRepository
=
ArticleRepositoryImpl
.
getInstance
();
*/
private
static
ArticleRepository
articleRepository
=
ArticleRepositoryImpl
.
getInstance
();
/**
* Statistic utilities.
/**
*/
* Statistic utilities.
private
static
Statistics
statistics
=
Statistics
.
getInstance
();
*/
private
static
Statistics
statistics
=
Statistics
.
getInstance
();
/**
* Default user thumbnail.
/**
*/
* Default user thumbnail.
private
static
final
String
DEFAULT_USER_THUMBNAIL
=
"default-user-thumbnail.png"
;
*/
private
static
final
String
DEFAULT_USER_THUMBNAIL
=
"default-user-thumbnail.png"
;
/**
* URL fetch service.
/**
*/
* URL fetch service.
private
static
URLFetchService
urlFetchService
=
URLFetchServiceFactory
.
getURLFetchService
();
*/
private
static
URLFetchService
urlFetchService
=
URLFetchServiceFactory
.
getURLFetchService
();
/**
* Event manager.
/**
*/
* Event manager.
private
static
EventManager
eventManager
=
EventManager
.
getInstance
();
*/
private
static
EventManager
eventManager
=
EventManager
.
getInstance
();
/**
* Adds an article with the specified request.
/**
*
* Adds an article with the specified request.
* <p>
*
* Renders the response with a json object, for example,
* <p>
* <pre>
* Renders the response with a json object, for example,
* {
* <pre>
* "sc": true
* {
* }
* "sc": true
* </pre>
* }
* </p>
* </pre>
*
* </p>
* @param request the specified http servlet request, for example,
*
* <pre>
* @param request the specified http servlet request, for example,
* {
* <pre>
* "comment": {
* {
* "userB3Key": "",
* "comment": {
* "oId": "",
* "userB3Key": "",
* "commentSymphonyArticleId": "",
* "oId": "",
* "commentOnArticleId": "",
* "commentSymphonyArticleId": "",
* "commentAuthorName": "",
* "commentOnArticleId": "",
* "commentAuthorEmail": "",
* "commentAuthorName": "",
* "commentAuthorURL": "",
* "commentAuthorEmail": "",
* "commentContent": "",
* "commentAuthorURL": "",
* "commentOriginalCommentId": "" // optional, if exists this key, the comment is an reply
* "commentContent": "",
* }
* "commentOriginalCommentId": "" // optional, if exists this key, the comment is an reply
* }
* }
* </pre>
* }
* @param response the specified http servlet response
* </pre>
* @param context the specified http request context
* @param response the specified http servlet response
* @throws Exception exception
* @param context the specified http request context
*/
* @throws Exception exception
@RequestProcessing
(
value
=
"/apis/symphony/comment"
,
method
=
HTTPRequestMethod
.
PUT
)
*/
public
void
addComment
(
final
HttpServletRequest
request
,
final
HttpServletResponse
response
,
final
HTTPRequestContext
context
)
@RequestProcessing
(
value
=
"/apis/symphony/comment"
,
method
=
HTTPRequestMethod
.
PUT
)
throws
Exception
{
public
void
addComment
(
final
HttpServletRequest
request
,
final
HttpServletResponse
response
,
final
HTTPRequestContext
context
)
final
JSONRenderer
renderer
=
new
JSONRenderer
();
throws
Exception
{
final
JSONRenderer
renderer
=
new
JSONRenderer
();
context
.
setRenderer
(
renderer
);
final
JSONObject
ret
=
new
JSONObject
();
context
.
setRenderer
(
renderer
);
final
JSONObject
ret
=
new
JSONObject
();
renderer
.
setJSONObject
(
ret
);
renderer
.
setJSONObject
(
ret
);
final
Transaction
transaction
=
commentRepository
.
beginTransaction
();
final
Transaction
transaction
=
commentRepository
.
beginTransaction
();
try
{
final
JSONObject
requestJSONObject
=
Requests
.
parseRequestJSONObject
(
request
,
response
);
try
{
final
JSONObject
symphonyCmt
=
requestJSONObject
.
optJSONObject
(
Comment
.
COMMENT
);
final
JSONObject
requestJSONObject
=
Requests
.
parseRequestJSONObject
(
request
,
response
);
final
JSONObject
preference
=
preferenceQueryService
.
getPreference
();
final
JSONObject
symphonyCmt
=
requestJSONObject
.
optJSONObject
(
Comment
.
COMMENT
);
final
String
keyOfSolo
=
preference
.
optString
(
Preference
.
KEY_OF_SOLO
);
final
JSONObject
preference
=
preferenceQueryService
.
getPreference
();
final
String
key
=
symphonyCmt
.
optString
(
"userB3Key"
);
final
String
keyOfSolo
=
preference
.
optString
(
Preference
.
KEY_OF_SOLO
);
final
String
key
=
symphonyCmt
.
optString
(
"userB3Key"
);
if
(
Strings
.
isEmptyOrNull
(
keyOfSolo
)
||
!
keyOfSolo
.
equals
(
key
))
{
ret
.
put
(
Keys
.
STATUS_CODE
,
HttpServletResponse
.
SC_FORBIDDEN
);
if
(
Strings
.
isEmptyOrNull
(
keyOfSolo
)
||
!
keyOfSolo
.
equals
(
key
))
{
ret
.
put
(
Keys
.
MSG
,
"Wrong key"
);
ret
.
put
(
Keys
.
STATUS_CODE
,
HttpServletResponse
.
SC_FORBIDDEN
);
ret
.
put
(
Keys
.
MSG
,
"Wrong key"
);
return
;
}
return
;
}
final
String
articleId
=
symphonyCmt
.
getString
(
"commentOnArticleId"
);
final
JSONObject
article
=
articleRepository
.
get
(
articleId
);
final
String
articleId
=
symphonyCmt
.
getString
(
"commentOnArticleId"
);
final
JSONObject
article
=
articleRepository
.
get
(
articleId
);
if
(
null
==
article
)
{
ret
.
put
(
Keys
.
STATUS_CODE
,
HttpServletResponse
.
SC_NOT_FOUND
);
if
(
null
==
article
)
{
ret
.
put
(
Keys
.
MSG
,
"Not found the specified article[id="
+
articleId
+
"]"
);
ret
.
put
(
Keys
.
STATUS_CODE
,
HttpServletResponse
.
SC_NOT_FOUND
);
ret
.
put
(
Keys
.
MSG
,
"Not found the specified article[id="
+
articleId
+
"]"
);
return
;
}
return
;
}
final
String
commentName
=
symphonyCmt
.
getString
(
"commentAuthorName"
);
final
String
commentEmail
=
symphonyCmt
.
getString
(
"commentAuthorEmail"
).
trim
().
toLowerCase
();
final
String
commentName
=
symphonyCmt
.
getString
(
"commentAuthorName"
);
final
String
commentURL
=
"http://"
+
symphonyCmt
.
optString
(
"commentAuthorURL"
);
final
String
commentEmail
=
symphonyCmt
.
getString
(
"commentAuthorEmail"
).
trim
().
toLowerCase
();
final
String
commentId
=
symphonyCmt
.
optString
(
Keys
.
OBJECT_ID
);
String
commentURL
=
symphonyCmt
.
optString
(
"commentAuthorURL"
);
String
commentContent
=
symphonyCmt
.
getString
(
Comment
.
COMMENT_CONTENT
);
if
(!
commentURL
.
contains
(
"://"
))
{
commentContent
+=
"<br/><br/><p style='font-size: 12px;'><i>该评论同步自 <a href='http://symphony.b3log.org/article/"
commentURL
=
"http://"
+
commentURL
;
+
symphonyCmt
.
optString
(
"commentSymphonyArticleId"
)
+
"#"
+
commentId
+
"' target='_blank'>B3log 社区</a></i></p>"
;
}
final
String
originalCommentId
=
symphonyCmt
.
optString
(
Comment
.
COMMENT_ORIGINAL_COMMENT_ID
);
// Step 1: Add comment
try
{
final
JSONObject
comment
=
new
JSONObject
();
new
URL
(
commentURL
);
JSONObject
originalComment
=
null
;
}
catch
(
final
MalformedURLException
e
)
{
LOGGER
.
log
(
Level
.
WARNING
,
"The comment URL is invalid [{0}]"
,
commentURL
);
comment
.
put
(
Keys
.
OBJECT_ID
,
commentId
);
commentURL
=
""
;
comment
.
put
(
Comment
.
COMMENT_NAME
,
commentName
);
}
comment
.
put
(
Comment
.
COMMENT_EMAIL
,
commentEmail
);
comment
.
put
(
Comment
.
COMMENT_URL
,
commentURL
);
final
String
commentId
=
symphonyCmt
.
optString
(
Keys
.
OBJECT_ID
);
comment
.
put
(
Comment
.
COMMENT_CONTENT
,
commentContent
);
String
commentContent
=
symphonyCmt
.
getString
(
Comment
.
COMMENT_CONTENT
);
final
String
timeZoneId
=
preference
.
getString
(
Preference
.
TIME_ZONE_ID
);
final
Date
date
=
TimeZones
.
getTime
(
timeZoneId
);
commentContent
+=
"<br/><br/><p style='font-size: 12px;'><i>该评论同步自 <a href='http://symphony.b3log.org/article/"
+
symphonyCmt
.
optString
(
"commentSymphonyArticleId"
)
+
"#"
+
commentId
+
"' target='_blank'>B3log 社区</a></i></p>"
;
comment
.
put
(
Comment
.
COMMENT_DATE
,
date
);
final
String
originalCommentId
=
symphonyCmt
.
optString
(
Comment
.
COMMENT_ORIGINAL_COMMENT_ID
);
ret
.
put
(
Comment
.
COMMENT_DATE
,
DateFormatUtils
.
format
(
date
,
"yyyy-MM-dd hh:mm:ss"
));
// Step 1: Add comment
if
(!
Strings
.
isEmptyOrNull
(
originalCommentId
))
{
final
JSONObject
comment
=
new
JSONObject
();
originalComment
=
commentRepository
.
get
(
originalCommentId
);
JSONObject
originalComment
=
null
;
if
(
null
!=
originalComment
)
{
comment
.
put
(
Comment
.
COMMENT_ORIGINAL_COMMENT_ID
,
originalCommentId
);
comment
.
put
(
Keys
.
OBJECT_ID
,
commentId
);
final
String
originalCommentName
=
originalComment
.
getString
(
Comment
.
COMMENT_NAME
);
comment
.
put
(
Comment
.
COMMENT_NAME
,
commentName
);
comment
.
put
(
Comment
.
COMMENT_EMAIL
,
commentEmail
);
comment
.
put
(
Comment
.
COMMENT_ORIGINAL_COMMENT_NAME
,
originalCommentName
);
comment
.
put
(
Comment
.
COMMENT_URL
,
commentURL
);
ret
.
put
(
Comment
.
COMMENT_ORIGINAL_COMMENT_NAME
,
originalCommentName
);
comment
.
put
(
Comment
.
COMMENT_CONTENT
,
commentContent
);
}
else
{
final
String
timeZoneId
=
preference
.
getString
(
Preference
.
TIME_ZONE_ID
);
comment
.
put
(
Comment
.
COMMENT_ORIGINAL_COMMENT_ID
,
""
);
final
Date
date
=
TimeZones
.
getTime
(
timeZoneId
);
comment
.
put
(
Comment
.
COMMENT_ORIGINAL_COMMENT_NAME
,
""
);
LOGGER
.
log
(
Level
.
WARNING
,
"Not found orginal comment[id={0}] of reply[name={1}, content={2}]"
,
comment
.
put
(
Comment
.
COMMENT_DATE
,
date
);
new
String
[]
{
originalCommentId
,
commentName
,
commentContent
});
ret
.
put
(
Comment
.
COMMENT_DATE
,
DateFormatUtils
.
format
(
date
,
"yyyy-MM-dd hh:mm:ss"
));
}
if
(!
Strings
.
isEmptyOrNull
(
originalCommentId
))
{
}
else
{
originalComment
=
commentRepository
.
get
(
originalCommentId
);
comment
.
put
(
Comment
.
COMMENT_ORIGINAL_COMMENT_ID
,
""
);
if
(
null
!=
originalComment
)
{
comment
.
put
(
Comment
.
COMMENT_ORIGINAL_COMMENT_NAME
,
""
);
comment
.
put
(
Comment
.
COMMENT_ORIGINAL_COMMENT_ID
,
originalCommentId
);
}
final
String
originalCommentName
=
originalComment
.
getString
(
Comment
.
COMMENT_NAME
);
setCommentThumbnailURL
(
comment
);
comment
.
put
(
Comment
.
COMMENT_ORIGINAL_COMMENT_NAME
,
originalCommentName
);
ret
.
put
(
Comment
.
COMMENT_THUMBNAIL_URL
,
comment
.
getString
(
Comment
.
COMMENT_THUMBNAIL_URL
));
ret
.
put
(
Comment
.
COMMENT_ORIGINAL_COMMENT_NAME
,
originalCommentName
);
// Sets comment on article....
}
else
{
comment
.
put
(
Comment
.
COMMENT_ON_ID
,
articleId
);
comment
.
put
(
Comment
.
COMMENT_ORIGINAL_COMMENT_ID
,
""
);
comment
.
put
(
Comment
.
COMMENT_ON_TYPE
,
Article
.
ARTICLE
);
comment
.
put
(
Comment
.
COMMENT_ORIGINAL_COMMENT_NAME
,
""
);
LOGGER
.
log
(
Level
.
WARNING
,
"Not found orginal comment[id={0}] of reply[name={1}, content={2}]"
,
final
String
commentSharpURL
=
getCommentSharpURLForArticle
(
article
,
commentId
);
new
String
[]
{
originalCommentId
,
commentName
,
commentContent
});
}
comment
.
put
(
Comment
.
COMMENT_SHARP_URL
,
commentSharpURL
);
}
else
{
comment
.
put
(
Comment
.
COMMENT_ORIGINAL_COMMENT_ID
,
""
);
commentRepository
.
add
(
comment
);
comment
.
put
(
Comment
.
COMMENT_ORIGINAL_COMMENT_NAME
,
""
);
// Step 2: Update article comment count
}
articleUtils
.
incArticleCommentCount
(
articleId
);
// Step 3: Update blog statistic comment count
setCommentThumbnailURL
(
comment
);
statistics
.
incBlogCommentCount
();
ret
.
put
(
Comment
.
COMMENT_THUMBNAIL_URL
,
comment
.
getString
(
Comment
.
COMMENT_THUMBNAIL_URL
));
statistics
.
incPublishedBlogCommentCount
();
// Sets comment on article....
// Step 4: Send an email to admin
comment
.
put
(
Comment
.
COMMENT_ON_ID
,
articleId
);
try
{
comment
.
put
(
Comment
.
COMMENT_ON_TYPE
,
Article
.
ARTICLE
);
Comments
.
sendNotificationMail
(
article
,
comment
,
originalComment
,
preference
);
}
catch
(
final
Exception
e
)
{
final
String
commentSharpURL
=
getCommentSharpURLForArticle
(
article
,
commentId
);
LOGGER
.
log
(
Level
.
WARNING
,
"Send mail failed"
,
e
);
}
comment
.
put
(
Comment
.
COMMENT_SHARP_URL
,
commentSharpURL
);
// Step 5: Fire add comment event
final
JSONObject
eventData
=
new
JSONObject
();
commentRepository
.
add
(
comment
);
// Step 2: Update article comment count
eventData
.
put
(
Comment
.
COMMENT
,
comment
);
articleUtils
.
incArticleCommentCount
(
articleId
);
eventData
.
put
(
Article
.
ARTICLE
,
article
);
// Step 3: Update blog statistic comment count
eventManager
.
fireEventSynchronously
(
new
Event
<
JSONObject
>(
EventTypes
.
ADD_COMMENT_TO_ARTICLE_FROM_SYMPHONY
,
eventData
));
statistics
.
incBlogCommentCount
();
statistics
.
incPublishedBlogCommentCount
();
transaction
.
commit
();
// Step 4: Send an email to admin
ret
.
put
(
Keys
.
STATUS_CODE
,
true
);
try
{
ret
.
put
(
Keys
.
OBJECT_ID
,
commentId
);
Comments
.
sendNotificationMail
(
article
,
comment
,
originalComment
,
preference
);
}
catch
(
final
Exception
e
)
{
ret
.
put
(
Keys
.
OBJECT_ID
,
articleId
);
LOGGER
.
log
(
Level
.
WARNING
,
"Send mail failed"
,
e
);
ret
.
put
(
Keys
.
MSG
,
"add a comment to an article from symphony succ"
);
}
ret
.
put
(
Keys
.
STATUS_CODE
,
true
);
// Step 5: Fire add comment event
final
JSONObject
eventData
=
new
JSONObject
();
renderer
.
setJSONObject
(
ret
);
}
catch
(
final
ServiceException
e
)
{
eventData
.
put
(
Comment
.
COMMENT
,
comment
);
LOGGER
.
log
(
Level
.
SEVERE
,
e
.
getMessage
(),
e
);
eventData
.
put
(
Article
.
ARTICLE
,
article
);
eventManager
.
fireEventSynchronously
(
new
Event
<
JSONObject
>(
EventTypes
.
ADD_COMMENT_TO_ARTICLE_FROM_SYMPHONY
,
eventData
));
final
JSONObject
jsonObject
=
QueryResults
.
defaultResult
();
transaction
.
commit
();
renderer
.
setJSONObject
(
jsonObject
);
ret
.
put
(
Keys
.
STATUS_CODE
,
true
);
jsonObject
.
put
(
Keys
.
MSG
,
e
.
getMessage
());
ret
.
put
(
Keys
.
OBJECT_ID
,
commentId
);
}
}
ret
.
put
(
Keys
.
OBJECT_ID
,
articleId
);
ret
.
put
(
Keys
.
MSG
,
"add a comment to an article from symphony succ"
);
/**
ret
.
put
(
Keys
.
STATUS_CODE
,
true
);
* Gets comment sharp URL with the specified article and comment id.
*
renderer
.
setJSONObject
(
ret
);
* @param article the specified article
}
catch
(
final
ServiceException
e
)
{
* @param commentId the specified comment id
LOGGER
.
log
(
Level
.
SEVERE
,
e
.
getMessage
(),
e
);
* @return comment sharp URL
* @throws JSONException json exception
final
JSONObject
jsonObject
=
QueryResults
.
defaultResult
();
*/
private
static
String
getCommentSharpURLForArticle
(
final
JSONObject
article
,
renderer
.
setJSONObject
(
jsonObject
);
final
String
commentId
)
jsonObject
.
put
(
Keys
.
MSG
,
e
.
getMessage
());
throws
JSONException
{
}
final
String
articleLink
=
article
.
getString
(
Article
.
ARTICLE_PERMALINK
);
}
return
articleLink
+
"#"
+
commentId
;
/**
}
* Gets comment sharp URL with the specified article and comment id.
*
/**
* @param article the specified article
* Sets commenter thumbnail URL for the specified comment.
* @param commentId the specified comment id
*
* @return comment sharp URL
* @param comment the specified comment
* @throws JSONException json exception
* @throws Exception exception
*/
*/
private
static
String
getCommentSharpURLForArticle
(
final
JSONObject
article
,
private
static
void
setCommentThumbnailURL
(
final
JSONObject
comment
)
final
String
commentId
)
throws
Exception
{
throws
JSONException
{
final
String
commentEmail
=
comment
.
getString
(
Comment
.
COMMENT_EMAIL
);
final
String
articleLink
=
article
.
getString
(
Article
.
ARTICLE_PERMALINK
);
final
String
id
=
commentEmail
.
split
(
"@"
)[
0
];
final
String
domain
=
commentEmail
.
split
(
"@"
)[
1
];
return
articleLink
+
"#"
+
commentId
;
String
thumbnailURL
=
null
;
}
// Try to set thumbnail URL using Gravatar service
/**
final
String
hashedEmail
=
MD5
.
hash
(
commentEmail
.
toLowerCase
());
* Sets commenter thumbnail URL for the specified comment.
final
int
size
=
60
;
*
final
URL
gravatarURL
=
new
URL
(
* @param comment the specified comment
"http://secure.gravatar.com/avatar/"
+
hashedEmail
+
"?s="
+
size
+
"&d="
+
Latkes
.
getServePath
()
* @throws Exception exception
+
"/images/default-user-thumbnail.png"
);
*/
private
static
void
setCommentThumbnailURL
(
final
JSONObject
comment
)
try
{
throws
Exception
{
final
HTTPRequest
request
=
new
HTTPRequest
();
final
String
commentEmail
=
comment
.
getString
(
Comment
.
COMMENT_EMAIL
);
final
String
id
=
commentEmail
.
split
(
"@"
)[
0
];
request
.
setURL
(
gravatarURL
);
final
String
domain
=
commentEmail
.
split
(
"@"
)[
1
];
final
HTTPResponse
response
=
urlFetchService
.
fetch
(
request
);
String
thumbnailURL
=
null
;
final
int
statusCode
=
response
.
getResponseCode
();
// Try to set thumbnail URL using Gravatar service
if
(
HttpServletResponse
.
SC_OK
==
statusCode
)
{
final
String
hashedEmail
=
MD5
.
hash
(
commentEmail
.
toLowerCase
());
final
List
<
HTTPHeader
>
headers
=
response
.
getHeaders
();
final
int
size
=
60
;
boolean
defaultFileLengthMatched
=
false
;
final
URL
gravatarURL
=
new
URL
(
"http://secure.gravatar.com/avatar/"
+
hashedEmail
+
"?s="
+
size
+
"&d="
+
Latkes
.
getServePath
()
for
(
final
HTTPHeader
httpHeader
:
headers
)
{
+
"/images/default-user-thumbnail.png"
);
if
(
"Content-Length"
.
equalsIgnoreCase
(
httpHeader
.
getName
()))
{
if
(
httpHeader
.
getValue
().
equals
(
"2147"
))
{
try
{
defaultFileLengthMatched
=
true
;
final
HTTPRequest
request
=
new
HTTPRequest
();
}
}
request
.
setURL
(
gravatarURL
);
}
final
HTTPResponse
response
=
urlFetchService
.
fetch
(
request
);
final
int
statusCode
=
response
.
getResponseCode
();
if
(!
defaultFileLengthMatched
)
{
thumbnailURL
=
"http://secure.gravatar.com/avatar/"
+
hashedEmail
+
"?s="
+
size
+
"&d="
+
Latkes
.
getServePath
()
if
(
HttpServletResponse
.
SC_OK
==
statusCode
)
{
+
"/images/default-user-thumbnail.png"
;
final
List
<
HTTPHeader
>
headers
=
response
.
getHeaders
();
comment
.
put
(
Comment
.
COMMENT_THUMBNAIL_URL
,
thumbnailURL
);
boolean
defaultFileLengthMatched
=
false
;
LOGGER
.
log
(
Level
.
FINEST
,
"Comment thumbnail[URL={0}]"
,
thumbnailURL
);
for
(
final
HTTPHeader
httpHeader
:
headers
)
{
return
;
if
(
"Content-Length"
.
equalsIgnoreCase
(
httpHeader
.
getName
()))
{
}
if
(
httpHeader
.
getValue
().
equals
(
"2147"
))
{
}
else
{
defaultFileLengthMatched
=
true
;
LOGGER
.
log
(
Level
.
WARNING
,
"Can not fetch thumbnail from Gravatar[commentEmail={0}, statusCode={1}]"
,
}
new
Object
[]
{
commentEmail
,
statusCode
});
}
}
}
}
catch
(
final
IOException
e
)
{
LOGGER
.
warning
(
e
.
getMessage
());
if
(!
defaultFileLengthMatched
)
{
LOGGER
.
log
(
Level
.
WARNING
,
"Can not fetch thumbnail from Gravatar[commentEmail={0}]"
,
commentEmail
);
thumbnailURL
=
"http://secure.gravatar.com/avatar/"
+
hashedEmail
+
"?s="
+
size
+
"&d="
+
Latkes
.
getServePath
()
}
+
"/images/default-user-thumbnail.png"
;
comment
.
put
(
Comment
.
COMMENT_THUMBNAIL_URL
,
thumbnailURL
);
if
(
null
==
thumbnailURL
)
{
LOGGER
.
log
(
Level
.
FINEST
,
"Comment thumbnail[URL={0}]"
,
thumbnailURL
);
LOGGER
.
log
(
Level
.
WARNING
,
"Not supported yet for comment thumbnail for email[{0}]"
,
commentEmail
);
thumbnailURL
=
"/images/"
+
DEFAULT_USER_THUMBNAIL
;
return
;
comment
.
put
(
Comment
.
COMMENT_THUMBNAIL_URL
,
thumbnailURL
);
}
}
}
else
{
}
LOGGER
.
log
(
Level
.
WARNING
,
"Can not fetch thumbnail from Gravatar[commentEmail={0}, statusCode={1}]"
,
}
new
Object
[]
{
commentEmail
,
statusCode
});
}
}
catch
(
final
IOException
e
)
{
LOGGER
.
warning
(
e
.
getMessage
());
LOGGER
.
log
(
Level
.
WARNING
,
"Can not fetch thumbnail from Gravatar[commentEmail={0}]"
,
commentEmail
);
}
if
(
null
==
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
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment