Commit 37d479e9 authored by Liang Ding's avatar Liang Ding

🎨 #12764

parent ac6c778c
...@@ -60,7 +60,7 @@ import java.util.*; ...@@ -60,7 +60,7 @@ import java.util.*;
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @author <a href="http://zephyr.b3log.org">Zephyr</a> * @author <a href="http://zephyr.b3log.org">Zephyr</a>
* @version 1.4.5.5, Apr 16, 2019 * @version 1.4.5.6, Apr 18, 2019
* @since 0.3.1 * @since 0.3.1
*/ */
@RequestProcessor @RequestProcessor
...@@ -699,7 +699,7 @@ public class ArticleProcessor { ...@@ -699,7 +699,7 @@ public class ArticleProcessor {
*/ */
@RequestProcessing(value = "/article", method = HttpMethod.GET) @RequestProcessing(value = "/article", method = HttpMethod.GET)
public void showArticle(final RequestContext context) { public void showArticle(final RequestContext context) {
// See PermalinkHandler#dispatchToArticleOrPageProcessor() // See PermalinkHandler#dispatchToArticleProcessor()
final JSONObject article = (JSONObject) context.attr(Article.ARTICLE); final JSONObject article = (JSONObject) context.attr(Article.ARTICLE);
if (null == article) { if (null == article) {
context.sendError(HttpServletResponse.SC_NOT_FOUND); context.sendError(HttpServletResponse.SC_NOT_FOUND);
......
/*
* Solo - A small and beautiful blogging system written in Java.
* Copyright (c) 2010-present, b3log.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.b3log.solo.processor;
import org.b3log.latke.Keys;
import org.b3log.latke.ioc.Inject;
import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger;
import org.b3log.latke.service.LangPropsService;
import org.b3log.latke.servlet.HttpMethod;
import org.b3log.latke.servlet.RequestContext;
import org.b3log.latke.servlet.annotation.RequestProcessing;
import org.b3log.latke.servlet.annotation.RequestProcessor;
import org.b3log.latke.servlet.renderer.AbstractFreeMarkerRenderer;
import org.b3log.latke.util.Stopwatchs;
import org.b3log.solo.model.Common;
import org.b3log.solo.model.Option;
import org.b3log.solo.model.Page;
import org.b3log.solo.service.CommentQueryService;
import org.b3log.solo.service.DataModelService;
import org.b3log.solo.service.OptionQueryService;
import org.b3log.solo.service.StatisticMgmtService;
import org.b3log.solo.util.Emotions;
import org.b3log.solo.util.Markdowns;
import org.b3log.solo.util.Skins;
import org.json.JSONObject;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
/**
* Page processor.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.1.0.10, Feb 6, 2019
* @since 0.3.1
*/
@RequestProcessor
public class PageProcessor {
/**
* Logger.
*/
private static final Logger LOGGER = Logger.getLogger(PageProcessor.class);
/**
* Language service.
*/
@Inject
private LangPropsService langPropsService;
/**
* DataModelService.
*/
@Inject
private DataModelService dataModelService;
/**
* Option query service.
*/
@Inject
private OptionQueryService optionQueryService;
/**
* Comment query service.
*/
@Inject
private CommentQueryService commentQueryService;
/**
* Statistic management service.
*/
@Inject
private StatisticMgmtService statisticMgmtService;
/**
* Shows page with the specified context.
*
* @param context the specified context
*/
@RequestProcessing(value = "/page", method = HttpMethod.GET)
public void showPage(final RequestContext context) {
final AbstractFreeMarkerRenderer renderer = new SkinRenderer(context, "page.ftl");
final Map<String, Object> dataModel = renderer.getDataModel();
final HttpServletResponse response = context.getResponse();
try {
final JSONObject preference = optionQueryService.getPreference();
if (null == preference) {
context.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
Skins.fillLangs(preference.getString(Option.ID_C_LOCALE_STRING), (String) context.attr(Keys.TEMAPLTE_DIR_NAME), dataModel);
// See PermalinkHandler#dispatchToArticleOrPageProcessor()
final JSONObject page = (JSONObject) context.attr(Page.PAGE);
if (null == page) {
context.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
final String pageId = page.getString(Keys.OBJECT_ID);
page.put(Common.COMMENTABLE, preference.getBoolean(Option.ID_C_COMMENTABLE) && page.getBoolean(Page.PAGE_COMMENTABLE));
page.put(Common.PERMALINK, page.getString(Page.PAGE_PERMALINK));
dataModel.put(Page.PAGE, page);
final List<JSONObject> comments = commentQueryService.getComments(pageId);
dataModel.put(Page.PAGE_COMMENTS_REF, comments);
// Markdown
Stopwatchs.start("Markdown Page [id=" + page.optString(Keys.OBJECT_ID) + "]");
String content = page.optString(Page.PAGE_CONTENT);
content = Emotions.convert(content);
content = Markdowns.toHTML(content);
page.put(Page.PAGE_CONTENT, content);
Stopwatchs.end();
dataModelService.fillCommon(context, dataModel, preference);
dataModelService.fillFaviconURL(dataModel, preference);
dataModelService.fillUsite(dataModel);
statisticMgmtService.incBlogViewCount(context, response);
} catch (final Exception e) {
LOGGER.log(Level.ERROR, e.getMessage(), e);
context.sendError(HttpServletResponse.SC_NOT_FOUND);
}
}
}
...@@ -29,9 +29,7 @@ import org.b3log.latke.servlet.HttpMethod; ...@@ -29,9 +29,7 @@ import org.b3log.latke.servlet.HttpMethod;
import org.b3log.latke.servlet.RequestContext; import org.b3log.latke.servlet.RequestContext;
import org.b3log.latke.servlet.handler.Handler; import org.b3log.latke.servlet.handler.Handler;
import org.b3log.solo.model.Article; import org.b3log.solo.model.Article;
import org.b3log.solo.model.Page;
import org.b3log.solo.repository.ArticleRepository; import org.b3log.solo.repository.ArticleRepository;
import org.b3log.solo.repository.PageRepository;
import org.b3log.solo.service.InitService; import org.b3log.solo.service.InitService;
import org.b3log.solo.service.PermalinkQueryService; import org.b3log.solo.service.PermalinkQueryService;
import org.b3log.solo.util.Solos; import org.b3log.solo.util.Solos;
...@@ -40,10 +38,10 @@ import org.json.JSONObject; ...@@ -40,10 +38,10 @@ import org.json.JSONObject;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
/** /**
* Article/Page permalink handler. * Article permalink handler.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.0, Mar 1, 2019 * @version 1.0.0.1, Apr 18, 2019
* @since 3.2.0 * @since 3.2.0
*/ */
public class PermalinkHandler implements Handler { public class PermalinkHandler implements Handler {
...@@ -61,7 +59,6 @@ public class PermalinkHandler implements Handler { ...@@ -61,7 +59,6 @@ public class PermalinkHandler implements Handler {
@Override @Override
public void handle(final RequestContext context) { public void handle(final RequestContext context) {
JSONObject article; JSONObject article;
JSONObject page = null;
try { try {
final BeanManager beanManager = BeanManager.getInstance(); final BeanManager beanManager = BeanManager.getInstance();
final InitService initService = beanManager.getReference(InitService.class); final InitService initService = beanManager.getReference(InitService.class);
...@@ -84,12 +81,7 @@ public class PermalinkHandler implements Handler { ...@@ -84,12 +81,7 @@ public class PermalinkHandler implements Handler {
final ArticleRepository articleRepository = beanManager.getReference(ArticleRepository.class); final ArticleRepository articleRepository = beanManager.getReference(ArticleRepository.class);
article = articleRepository.getByPermalink(permalink); article = articleRepository.getByPermalink(permalink);
if (null == article) { if (null == article) {
final PageRepository pageRepository = beanManager.getReference(PageRepository.class); LOGGER.log(Level.DEBUG, "Not found article with permalink [{0}]", permalink);
page = pageRepository.getByPermalink(permalink);
}
if (null == page && null == article) {
LOGGER.log(Level.DEBUG, "Not found article/page with permalink [{0}]", permalink);
context.handle(); context.handle();
return; return;
...@@ -102,7 +94,7 @@ public class PermalinkHandler implements Handler { ...@@ -102,7 +94,7 @@ public class PermalinkHandler implements Handler {
} }
// If requests an article and the article need view password, sends redirect to the password form // If requests an article and the article need view password, sends redirect to the password form
if (null != article && Solos.needViewPwd(context, article)) { if (Solos.needViewPwd(context, article)) {
try { try {
context.sendRedirect(Latkes.getServePath() + "/console/article-pwd?articleId=" + article.optString(Keys.OBJECT_ID)); context.sendRedirect(Latkes.getServePath() + "/console/article-pwd?articleId=" + article.optString(Keys.OBJECT_ID));
...@@ -114,25 +106,21 @@ public class PermalinkHandler implements Handler { ...@@ -114,25 +106,21 @@ public class PermalinkHandler implements Handler {
} }
} }
dispatchToArticleOrPageProcessor(context, article, page); dispatchToArticleProcessor(context, article);
context.handle(); context.handle();
} }
/** /**
* Dispatches the specified request to the specified article or page processor with the specified response. * Dispatches the specified request to the specified article processor with the specified response.
* *
* @param context the specified request context * @param context the specified request context
* @param article the specified article * @param article the specified article
* @param page the specified page
* @see DispatcherServlet#result(RequestContext) * @see DispatcherServlet#result(RequestContext)
*/ */
private void dispatchToArticleOrPageProcessor(final RequestContext context, final JSONObject article, final JSONObject page) { private void dispatchToArticleProcessor(final RequestContext context, final JSONObject article) {
if (null != article) { if (null != article) {
context.attr(Article.ARTICLE, article); context.attr(Article.ARTICLE, article);
context.attr(Keys.HttpRequest.REQUEST_URI, Latkes.getContextPath() + "/article"); context.attr(Keys.HttpRequest.REQUEST_URI, Latkes.getContextPath() + "/article");
} else {
context.attr(Page.PAGE, page);
context.attr(Keys.HttpRequest.REQUEST_URI, Latkes.getContextPath() + "/page");
} }
context.attr(Keys.HttpRequest.REQUEST_METHOD, HttpMethod.GET.name()); context.attr(Keys.HttpRequest.REQUEST_METHOD, HttpMethod.GET.name());
} }
......
...@@ -56,37 +56,6 @@ public class CommentProcessorTestCase extends AbstractTestCase { ...@@ -56,37 +56,6 @@ public class CommentProcessorTestCase extends AbstractTestCase {
super.init(); super.init();
} }
/**
* addPageComment.
*
* @throws Exception exception
*/
@Test(dependsOnMethods = "init")
public void addPageComment() throws Exception {
final MockHttpServletRequest request = mockRequest();
request.setRequestURI("/page/comments");
request.setMethod("POST");
request.setAttribute(Keys.TEMAPLTE_DIR_NAME, Option.DefaultPreference.DEFAULT_SKIN_DIR_NAME);
final JSONObject requestJSON = new JSONObject();
requestJSON.put("oId", addPage());
requestJSON.put("commentName", "88250");
requestJSON.put("commentEmail", "d@hacpai.com");
requestJSON.put("commentURL", "https://hacpai.com");
requestJSON.put("commentContent", "测试评论");
final BufferedReader reader = new BufferedReader(new StringReader(requestJSON.toString()));
request.setReader(reader);
mockAdminLogin(request);
final MockHttpServletResponse response = mockResponse();
mockDispatcherServletService(request, response);
final String content = response.body();
Assert.assertTrue(StringUtils.contains(content, "\"sc\":true"));
}
/** /**
* addArticleComment. * addArticleComment.
* *
......
/*
* Solo - A small and beautiful blogging system written in Java.
* Copyright (c) 2010-present, b3log.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.b3log.solo.processor;
import org.apache.commons.lang.StringUtils;
import org.b3log.latke.Keys;
import org.b3log.solo.AbstractTestCase;
import org.b3log.solo.MockHttpServletRequest;
import org.b3log.solo.MockHttpServletResponse;
import org.b3log.solo.model.Option;
import org.b3log.solo.model.Page;
import org.b3log.solo.repository.PageRepository;
import org.b3log.solo.service.PageMgmtService;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.Test;
/**
* {@link PageProcessor} test case.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.1.0, Feb 18, 2017
* @since 1.7.0
*/
@Test(suiteName = "processor")
public class PageProcessorTestCase extends AbstractTestCase {
/**
* Init.
*
* @throws Exception exception
*/
@Test
public void init() throws Exception {
super.init();
}
/**
* showPage.
*
* @throws Exception exception
*/
@Test(dependsOnMethods = "init")
public void showPage() throws Exception {
final JSONObject page = addPage();
final MockHttpServletRequest request = mockRequest();
request.setRequestURI("/page");
request.setAttribute(Page.PAGE, page);
request.setAttribute(Keys.TEMAPLTE_DIR_NAME, Option.DefaultPreference.DEFAULT_SKIN_DIR_NAME);
final MockHttpServletResponse response = mockResponse();
mockDispatcherServletService(request, response);
final String content = response.body();
Assert.assertTrue(StringUtils.contains(content, "page1 title - Solo 的个人博客"));
}
private JSONObject addPage() throws Exception {
final PageMgmtService pageMgmtService = getPageMgmtService();
final JSONObject requestJSONObject = new JSONObject();
final JSONObject page = new JSONObject();
requestJSONObject.put(Page.PAGE, page);
page.put(Page.PAGE_CONTENT, "page1 content");
page.put(Page.PAGE_PERMALINK, "pagepermalink");
page.put(Page.PAGE_TITLE, "page1 title");
page.put(Page.PAGE_COMMENTABLE, true);
page.put(Page.PAGE_TYPE, "page");
page.put(Page.PAGE_OPEN_TARGET, "_self");
final String pageId = pageMgmtService.addPage(requestJSONObject);
final PageRepository pageRepository = getPageRepository();
return pageRepository.get(pageId);
}
}
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