Commit 4486b679 authored by Liang Ding's avatar Liang Ding

#199

完成社区同步更新博客(issue 描述里的第一点:1. 提供文章更新接口给社区)
parent 9e1d40df
......@@ -33,6 +33,7 @@ import org.b3log.solo.model.Article;
import org.b3log.solo.model.Common;
import org.b3log.solo.model.Preference;
import org.b3log.solo.service.ArticleMgmtService;
import org.b3log.solo.service.ArticleQueryService;
import org.b3log.solo.service.PreferenceQueryService;
import org.b3log.solo.service.UserQueryService;
import org.b3log.solo.util.QueryResults;
......@@ -44,7 +45,7 @@ import org.jsoup.Jsoup;
* Article receiver (from B3log Symphony).
*
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.4, Jan 4, 2013
* @version 1.0.0.5, Mar 18, 2013
* @since 0.5.5
*/
@RequestProcessor
......@@ -65,6 +66,11 @@ public final class ArticleReceiver {
*/
private ArticleMgmtService articleMgmtService = ArticleMgmtService.getInstance();
/**
* Article query service.
*/
private ArticleQueryService articleQueryService = ArticleQueryService.getInstance();
/**
* Article abstract length.
*/
......@@ -101,7 +107,7 @@ public final class ArticleReceiver {
* @param context the specified http request context
* @throws Exception exception
*/
@RequestProcessing(value = "/apis/symphony/article", method = HTTPRequestMethod.PUT)
@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();
......@@ -142,7 +148,7 @@ public final class ArticleReceiver {
final String articleId = article.getString(Keys.OBJECT_ID);
content += "<br/><br/><p style='font-size: 12px;'><i>该文章同步自 <a href='http://symphony.b3log.org/article/" + articleId
+ "' target='_blank>B3log 社区</a></i></p>";
+ "' target='_blank'>B3log 社区</a></i></p>";
article.put(Article.ARTICLE_CONTENT, content);
articleMgmtService.addArticle(requestJSONObject);
......@@ -161,4 +167,98 @@ public final class ArticleReceiver {
jsonObject.put(Keys.MSG, e.getMessage());
}
}
/**
* Updates an article with the specified request.
*
* <p>
* Renders the response with a json object, for example,
* <pre>
* {
* "sc": boolean,
* "msg": ""
* }
* </pre>
* </p>
*
* @param request the specified http servlet request, for example,
* <pre>
* {
* "article": {
* "oId": "", // Symphony Article#clientArticleId
* "articleTitle": "",
* "articleContent": "",
* "articleTags": "tag1,tag2,tag3",
* "userB3Key": "",
* "articleEditorType": ""
* }
* }
* </pre>
* @param response the specified http servlet response
* @param context the specified http request context
* @throws Exception exception
*/
@RequestProcessing(value = "/apis/symphony/article", method = HTTPRequestMethod.PUT)
public void updateArticle(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);
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(Preference.KEY_OF_SOLO))) {
LOGGER.log(Level.WARNING, "B3 key not match, ignored update article");
return;
}
article.remove("userB3Key");
final String articleId = article.getString(Keys.OBJECT_ID);
if (null == articleQueryService.getArticleById(articleId)) {
ret.put(Keys.MSG, "No found article[oId=" + articleId + "] to update");
ret.put(Keys.STATUS_CODE, false);
return;
}
final String plainTextContent = Jsoup.parse(article.optString(Article.ARTICLE_CONTENT)).text();
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);
article.put(Common.POST_TO_COMMUNITY, false); // Do not send to rhythm
article.put(Article.ARTICLE_COMMENTABLE, true);
article.put(Article.ARTICLE_VIEW_PWD, "");
String content = article.getString(Article.ARTICLE_CONTENT);
content += "<br/><br/><p style='font-size: 12px;'><i>该文章同步自 <a href='http://symphony.b3log.org/article/" + articleId
+ "' target='_blank'>B3log 社区</a></i></p>";
article.put(Article.ARTICLE_CONTENT, content);
articleMgmtService.updateArticle(requestJSONObject);
ret.put(Keys.MSG, "update article succ");
ret.put(Keys.STATUS_CODE, true);
} catch (final ServiceException e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
final JSONObject jsonObject = QueryResults.defaultResult();
renderer.setJSONObject(jsonObject);
jsonObject.put(Keys.MSG, e.getMessage());
}
}
}
......@@ -15,6 +15,7 @@
*/
package org.b3log.solo.processor;
import java.io.IOException;
import java.util.Calendar;
import java.util.Map;
......@@ -54,6 +55,7 @@ import org.b3log.solo.util.Randoms;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Login/logout processor.
*
......@@ -72,30 +74,37 @@ public final class LoginProcessor {
* Logger.
*/
private static final Logger LOGGER = Logger.getLogger(LoginProcessor.class.getName());
/**
* User query service.
*/
private static UserQueryService userQueryService = UserQueryService.getInstance();
/**
* User service.
*/
private UserService userService = UserServiceFactory.getUserService();
/**
* Mail service.
*/
private MailService mailService = MailServiceFactory.getMailService();
/**
* User management service.
*/
private UserMgmtService userMgmtService = UserMgmtService.getInstance();
/**
* Language service.
*/
private LangPropsService langPropsService = LangPropsService.getInstance();
/**
* Filler.
*/
private Filler filler = Filler.getInstance();
/**
* Preference query service.
*/
......@@ -142,7 +151,7 @@ public final class LoginProcessor {
*
* @param context the specified context
*/
@RequestProcessing(value = {"/login"}, method = HTTPRequestMethod.POST)
@RequestProcessing(value = { "/login"}, method = HTTPRequestMethod.POST)
public void login(final HTTPRequestContext context) {
final HttpServletRequest request = context.getRequest();
......@@ -200,7 +209,7 @@ public final class LoginProcessor {
* @param context the specified context
* @throws IOException io exception
*/
@RequestProcessing(value = {"/logout"}, method = HTTPRequestMethod.GET)
@RequestProcessing(value = { "/logout"}, method = HTTPRequestMethod.GET)
public void logout(final HTTPRequestContext context) throws IOException {
final HttpServletRequest httpServletRequest = context.getRequest();
......@@ -249,7 +258,7 @@ public final class LoginProcessor {
*
* @param context the specified context
*/
@RequestProcessing(value = {"/forgot"}, method = HTTPRequestMethod.POST)
@RequestProcessing(value = { "/forgot"}, method = HTTPRequestMethod.POST)
public void forgot(final HTTPRequestContext context) {
final HttpServletRequest request = context.getRequest();
......@@ -376,7 +385,8 @@ public final class LoginProcessor {
final String mailSubject = langPropsService.get("resetPwdMailSubject");
final String mailBody = langPropsService.get("resetPwdMailBody") + randomPwd;
final MailService.Message message = new MailService.Message();
//FIXME whether we should put the ever-hashed password here, rather during updating?
// FIXME whether we should put the ever-hashed password here, rather during updating?
user.put(User.USER_PASSWORD, randomPwd);
userMgmtService.updateUser(user);
......@@ -391,8 +401,7 @@ public final class LoginProcessor {
jsonObject.put("to", Latkes.getServePath() + "/login");
jsonObject.put(Keys.MSG, langPropsService.get("resetPwdSuccessMsg"));
LOGGER.log(Level.FINER, "Sending a mail[mailSubject={0}, mailBody=[{1}] to [{2}]",
new Object[]{mailSubject, mailBody, userEmail});
LOGGER.log(Level.FINER, "Sending a mail[mailSubject={0}, mailBody=[{1}] to [{2}]", new Object[] {mailSubject, mailBody, userEmail});
}
/**
......
......@@ -13,14 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.b3log.solo.util;
import java.util.Random;
/**
* Generate random stuff, <p> currently only support random alpha and digital
* string, whose length is also random between 8 and 16.
......@@ -34,27 +33,32 @@ public class Randoms {
* String's length should be positive.
*/
private static final int LEN_LIM = 1;
/**
* String's length maximum limit.
*/
private static final int MAX_LEN = 16;
/**
* String's length minimum limit.
*/
private static final int MIN_LEN = 8;
/**
* String's random length.
*/
private static final int RANDOM_LEN = new Random()
.nextInt(MAX_LEN - MIN_LEN + LEN_LIM) + MIN_LEN;
private static final int RANDOM_LEN = new Random().nextInt(MAX_LEN - MIN_LEN + LEN_LIM) + MIN_LEN;
/**
* Characters set table, can be extended.
*/
private final char[] table;
/**
* String's random seed.
*/
private final Random random = new Random();
/**
* String's random characters buffer.
*/
......@@ -76,8 +80,7 @@ public class Randoms {
if (length < LEN_LIM) {
throw new IllegalArgumentException("length < 1: " + length);
}
table = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
.toCharArray();
table = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
buf = new char[length];
}
......
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