use of org.b3log.latke.service.ServiceException in project solo by b3log.
the class ArticleReceiver method addArticle.
/**
* Adds an article with the specified request.
* <p>
* Renders the response with a json object, for example,
* <pre>
* {
* "sc": boolean,
* "oId": "", // Generated article id
* "msg": ""
* }
* </pre>
* </p>
*
* @param request the specified http servlet request, for example,
* "article": {
* "oId": "",
* "articleTitle": "",
* "articleContent": "",
* "articleTags": "tag1,tag2,tag3",
* "userB3Key": "",
* "articleEditorType": ""
* }
* @param response the specified http servlet response
* @param context the specified http request context
* @throws Exception exception
*/
@RequestProcessing(value = "/apis/symphony/article", method = HTTPRequestMethod.POST)
public void addArticle(final HttpServletRequest request, final HttpServletResponse response, final HTTPRequestContext context) throws Exception {
final JSONRenderer renderer = new JSONRenderer();
context.setRenderer(renderer);
final JSONObject ret = new JSONObject();
try {
final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, response);
final JSONObject article = requestJSONObject.optJSONObject(Article.ARTICLE);
final String userB3Key = article.optString("userB3Key");
final JSONObject preference = preferenceQueryService.getPreference();
if (!userB3Key.equals(preference.optString(Option.ID_C_KEY_OF_SOLO))) {
LOGGER.log(Level.WARN, "B3 key not match, ignored add article");
return;
}
article.remove("userB3Key");
final JSONObject admin = userQueryService.getAdmin();
article.put(Article.ARTICLE_AUTHOR_EMAIL, admin.getString(User.USER_EMAIL));
final String articleContent = article.optString(Article.ARTICLE_CONTENT);
final String plainTextContent = Jsoup.clean(Markdowns.toHTML(articleContent), Whitelist.none());
if (plainTextContent.length() > ARTICLE_ABSTRACT_LENGTH) {
article.put(Article.ARTICLE_ABSTRACT, plainTextContent.substring(0, ARTICLE_ABSTRACT_LENGTH) + "....");
} else {
article.put(Article.ARTICLE_ABSTRACT, plainTextContent);
}
article.put(Article.ARTICLE_IS_PUBLISHED, true);
// Do not send to rhythm
article.put(Common.POST_TO_COMMUNITY, false);
article.put(Article.ARTICLE_COMMENTABLE, true);
article.put(Article.ARTICLE_VIEW_PWD, "");
String content = article.getString(Article.ARTICLE_CONTENT);
final String articleId = article.getString(Keys.OBJECT_ID);
// content += "\n\n<p style='font-size: 12px;'><i>该文章同步自 <a href='https://hacpai.com/article/" + articleId
// + "' target='_blank'>黑客派</a></i></p>";
article.put(Article.ARTICLE_CONTENT, content);
articleMgmtService.addArticle(requestJSONObject);
ret.put(Keys.OBJECT_ID, articleId);
ret.put(Keys.MSG, "add article succ");
ret.put(Keys.STATUS_CODE, true);
renderer.setJSONObject(ret);
} catch (final ServiceException e) {
LOGGER.log(Level.ERROR, e.getMessage(), e);
final JSONObject jsonObject = QueryResults.defaultResult();
renderer.setJSONObject(jsonObject);
jsonObject.put(Keys.MSG, e.getMessage());
}
}
use of org.b3log.latke.service.ServiceException in project solo by b3log.
the class CommentReceiver method addComment.
/**
* Adds an article with the specified request.
*
* <p>
* Renders the response with a json object, for example,
* <pre>
* {
* "sc": true
* }
* </pre>
* </p>
*
* @param request the specified http servlet request, for example, <pre>
* {
* "comment": {
* "userB3Key": "",
* "oId": "",
* "commentSymphonyArticleId": "",
* "commentOnArticleId": "",
* "commentAuthorName": "",
* "commentAuthorEmail": "",
* "commentAuthorURL": "",
* "commentContent": "",
* "commentOriginalCommentId": "" // optional, if exists this key, the comment is an reply
* }
* }
* </pre>
*
* @param response the specified http servlet response
* @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) throws Exception {
final JSONRenderer renderer = new JSONRenderer();
context.setRenderer(renderer);
final JSONObject ret = new JSONObject();
renderer.setJSONObject(ret);
final Transaction transaction = commentRepository.beginTransaction();
try {
final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, response);
final JSONObject symphonyCmt = requestJSONObject.optJSONObject(Comment.COMMENT);
final JSONObject preference = preferenceQueryService.getPreference();
final String keyOfSolo = preference.optString(Option.ID_C_KEY_OF_SOLO);
final String key = symphonyCmt.optString("userB3Key");
if (Strings.isEmptyOrNull(keyOfSolo) || !keyOfSolo.equals(key)) {
ret.put(Keys.STATUS_CODE, HttpServletResponse.SC_FORBIDDEN);
ret.put(Keys.MSG, "Wrong key");
return;
}
final String articleId = symphonyCmt.getString("commentOnArticleId");
final JSONObject article = articleRepository.get(articleId);
if (null == article) {
ret.put(Keys.STATUS_CODE, HttpServletResponse.SC_NOT_FOUND);
ret.put(Keys.MSG, "Not found the specified article[id=" + articleId + "]");
return;
}
final String commentName = symphonyCmt.getString("commentAuthorName");
final String commentEmail = symphonyCmt.getString("commentAuthorEmail").trim().toLowerCase();
String commentURL = symphonyCmt.optString("commentAuthorURL");
if (!commentURL.contains("://")) {
commentURL = "http://" + commentURL;
}
try {
new URL(commentURL);
} catch (final MalformedURLException e) {
LOGGER.log(Level.WARN, "The comment URL is invalid [{0}]", commentURL);
commentURL = "";
}
final String commentId = symphonyCmt.optString(Keys.OBJECT_ID);
String commentContent = symphonyCmt.getString(Comment.COMMENT_CONTENT);
// commentContent += "<p class='cmtFromSym'><i>该评论同步自 <a href='" + SoloServletListener.B3LOG_SYMPHONY_SERVE_PATH
// + "/article/" + symphonyCmt.optString("commentSymphonyArticleId") + "#" + commentId
// + "' target='_blank'>黑客派</a></i></p>";
final String originalCommentId = symphonyCmt.optString(Comment.COMMENT_ORIGINAL_COMMENT_ID);
// Step 1: Add comment
final JSONObject comment = new JSONObject();
JSONObject originalComment = null;
comment.put(Keys.OBJECT_ID, commentId);
comment.put(Comment.COMMENT_NAME, commentName);
comment.put(Comment.COMMENT_EMAIL, commentEmail);
comment.put(Comment.COMMENT_URL, commentURL);
comment.put(Comment.COMMENT_CONTENT, commentContent);
final Date date = new Date();
comment.put(Comment.COMMENT_DATE, date);
ret.put(Comment.COMMENT_DATE, DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss"));
if (!Strings.isEmptyOrNull(originalCommentId)) {
originalComment = commentRepository.get(originalCommentId);
if (null != originalComment) {
comment.put(Comment.COMMENT_ORIGINAL_COMMENT_ID, originalCommentId);
final String originalCommentName = originalComment.getString(Comment.COMMENT_NAME);
comment.put(Comment.COMMENT_ORIGINAL_COMMENT_NAME, originalCommentName);
ret.put(Comment.COMMENT_ORIGINAL_COMMENT_NAME, originalCommentName);
} else {
comment.put(Comment.COMMENT_ORIGINAL_COMMENT_ID, "");
comment.put(Comment.COMMENT_ORIGINAL_COMMENT_NAME, "");
LOGGER.log(Level.WARN, "Not found orginal comment[id={0}] of reply[name={1}, content={2}]", new String[] { originalCommentId, commentName, commentContent });
}
} else {
comment.put(Comment.COMMENT_ORIGINAL_COMMENT_ID, "");
comment.put(Comment.COMMENT_ORIGINAL_COMMENT_NAME, "");
}
commentMgmtService.setCommentThumbnailURL(comment);
ret.put(Comment.COMMENT_THUMBNAIL_URL, comment.getString(Comment.COMMENT_THUMBNAIL_URL));
// Sets comment on article....
comment.put(Comment.COMMENT_ON_ID, articleId);
comment.put(Comment.COMMENT_ON_TYPE, Article.ARTICLE);
final String commentSharpURL = Comments.getCommentSharpURLForArticle(article, commentId);
comment.put(Comment.COMMENT_SHARP_URL, commentSharpURL);
commentRepository.add(comment);
// Step 2: Update article comment count
articleMgmtService.incArticleCommentCount(articleId);
// Step 3: Update blog statistic comment count
statisticMgmtService.incBlogCommentCount();
statisticMgmtService.incPublishedBlogCommentCount();
// Step 4: Send an email to admin
try {
commentMgmtService.sendNotificationMail(article, comment, originalComment, preference);
} catch (final Exception e) {
LOGGER.log(Level.WARN, "Send mail failed", e);
}
// Step 5: Fire add comment event
final JSONObject eventData = new JSONObject();
eventData.put(Comment.COMMENT, comment);
eventData.put(Article.ARTICLE, article);
eventManager.fireEventSynchronously(new Event<JSONObject>(EventTypes.ADD_COMMENT_TO_ARTICLE_FROM_SYMPHONY, eventData));
transaction.commit();
ret.put(Keys.STATUS_CODE, true);
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);
renderer.setJSONObject(ret);
} catch (final ServiceException e) {
LOGGER.log(Level.ERROR, e.getMessage(), e);
final JSONObject jsonObject = QueryResults.defaultResult();
renderer.setJSONObject(jsonObject);
jsonObject.put(Keys.MSG, e.getMessage());
}
}
use of org.b3log.latke.service.ServiceException in project solo by b3log.
the class ArticleProcessor method getArticleContent.
/**
* Gets article content with the specified context.
*
* @param context the specified context
* @param request the specified request
*/
@RequestProcessing(value = "/get-article-content", method = HTTPRequestMethod.GET)
public void getArticleContent(final HTTPRequestContext context, final HttpServletRequest request) {
final String articleId = request.getParameter("id");
if (Strings.isEmptyOrNull(articleId)) {
return;
}
final TextHTMLRenderer renderer = new TextHTMLRenderer();
context.setRenderer(renderer);
String content;
try {
content = articleQueryService.getArticleContent(request, articleId);
} catch (final ServiceException e) {
LOGGER.log(Level.ERROR, "Can not get article content", e);
return;
}
if (null == content) {
return;
}
renderer.setContent(content);
}
use of org.b3log.latke.service.ServiceException in project solo by b3log.
the class AdminConsole method showAdminPreferenceFunction.
/**
* Shows administrator preference function with the specified context.
*
* @param request the specified request
* @param context the specified context
*/
@RequestProcessing(value = "/admin-preference.do", method = HTTPRequestMethod.GET)
public void showAdminPreferenceFunction(final HttpServletRequest request, final HTTPRequestContext context) {
final AbstractFreeMarkerRenderer renderer = new ConsoleRenderer();
context.setRenderer(renderer);
final String templateName = "admin-preference.ftl";
renderer.setTemplateName(templateName);
final Locale locale = Latkes.getLocale();
final Map<String, String> langs = langPropsService.getAll(locale);
final Map<String, Object> dataModel = renderer.getDataModel();
dataModel.putAll(langs);
dataModel.put(Option.ID_C_LOCALE_STRING, locale.toString());
JSONObject preference = null;
try {
preference = preferenceQueryService.getPreference();
} catch (final ServiceException e) {
LOGGER.log(Level.ERROR, "Loads preference failed", e);
}
final StringBuilder timeZoneIdOptions = new StringBuilder();
final String[] availableIDs = TimeZone.getAvailableIDs();
for (int i = 0; i < availableIDs.length; i++) {
final String id = availableIDs[i];
String option;
if (id.equals(preference.optString(Option.ID_C_TIME_ZONE_ID))) {
option = "<option value=\"" + id + "\" selected=\"true\">" + id + "</option>";
} else {
option = "<option value=\"" + id + "\">" + id + "</option>";
}
timeZoneIdOptions.append(option);
}
dataModel.put("timeZoneIdOptions", timeZoneIdOptions.toString());
fireFreeMarkerActionEvent(templateName, dataModel);
}
use of org.b3log.latke.service.ServiceException in project solo by b3log.
the class ArticleConsole method addArticle.
/**
* Adds an article with the specified request.
*
* <p>
* Renders the response with a json object, for example,
* <pre>
* {
* "sc": boolean,
* "oId": "", // Generated article id
* "msg": ""
* }
* </pre>
* </p>
*
* @param request the specified http servlet request, for example, <pre>
* {
* "article": {
* "articleTitle": "",
* "articleAbstract": "",
* "articleContent": "",
* "articleTags": "tag1,tag2,tag3",
* "articlePermalink": "", // optional
* "articleIsPublished": boolean,
* "postToCommunity": boolean,
* "articleSignId": "" // optional
* "articleCommentable": boolean,
* "articleViewPwd": ""
* }
* }
* </pre>
*
* @param response the specified http servlet response
* @param context the specified http request context
* @throws Exception exception
*/
@RequestProcessing(value = "/console/article/", method = HTTPRequestMethod.POST)
public void addArticle(final HttpServletRequest request, final HttpServletResponse response, final HTTPRequestContext context) throws Exception {
if (!userQueryService.isLoggedIn(request, response)) {
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
final JSONRenderer renderer = new JSONRenderer();
context.setRenderer(renderer);
final JSONObject ret = new JSONObject();
try {
final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, response);
final JSONObject currentUser = userQueryService.getCurrentUser(request);
requestJSONObject.getJSONObject(Article.ARTICLE).put(Article.ARTICLE_AUTHOR_EMAIL, currentUser.getString(User.USER_EMAIL));
final String articleId = articleMgmtService.addArticle(requestJSONObject);
ret.put(Keys.OBJECT_ID, articleId);
ret.put(Keys.MSG, langPropsService.get("addSuccLabel"));
ret.put(Keys.STATUS_CODE, true);
renderer.setJSONObject(ret);
} catch (final ServiceException e) {
LOGGER.log(Level.ERROR, e.getMessage());
final JSONObject jsonObject = QueryResults.defaultResult();
renderer.setJSONObject(jsonObject);
jsonObject.put(Keys.MSG, e.getMessage());
}
}
Aggregations