Commit 1df9df32 authored by Liang Ding's avatar Liang Ding

🎨 Fix #12307

parent 2913b9ef
...@@ -65,7 +65,7 @@ import java.util.*; ...@@ -65,7 +65,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.2.16, Nov 17, 2016 * @version 1.4..16, Jun 5, 2017
* @since 0.3.1 * @since 0.3.1
*/ */
@RequestProcessor @RequestProcessor
...@@ -74,7 +74,7 @@ public class ArticleProcessor { ...@@ -74,7 +74,7 @@ public class ArticleProcessor {
/** /**
* Logger. * Logger.
*/ */
private static final Logger LOGGER = Logger.getLogger(ArticleProcessor.class.getName()); private static final Logger LOGGER = Logger.getLogger(ArticleProcessor.class);
/** /**
* Article query service. * Article query service.
...@@ -142,6 +142,152 @@ public class ArticleProcessor { ...@@ -142,6 +142,152 @@ public class ArticleProcessor {
@Inject @Inject
private EventManager eventManager; private EventManager eventManager;
/**
* Gets archive date from the specified URI.
*
* @param requestURI the specified request URI
* @return archive date
*/
private static String getArchiveDate(final String requestURI) {
final String path = requestURI.substring((Latkes.getContextPath() + "/archives/").length());
return StringUtils.substring(path, 0, "yyyy/MM".length());
}
/**
* Gets the request page number from the specified request URI.
*
* @param requestURI the specified request URI
* @return page number, returns {@code -1} if the specified request URI can not convert to an number
*/
private static int getArchiveCurrentPageNum(final String requestURI) {
final String pageNumString =
StringUtils.substring(requestURI, (Latkes.getContextPath() + "/archives/yyyy/MM/").length());
return Requests.getCurrentPageNum(pageNumString);
}
/**
* Gets author id from the specified URI.
*
* @param requestURI the specified request URI
* @return author id
*/
private static String getAuthorId(final String requestURI) {
final String path = requestURI.substring((Latkes.getContextPath() + "/authors/").length());
final int idx = path.indexOf("/");
if (-1 == idx) {
return path.substring(0);
} else {
return path.substring(0, idx);
}
}
/**
* Gets the request page number from the specified request URI.
*
* @param requestURI the specified request URI
* @return page number
*/
private static int getArticlesPagedCurrentPageNum(final String requestURI) {
final String pageNumString = requestURI.substring((Latkes.getContextPath() + "/articles/").length());
return Requests.getCurrentPageNum(pageNumString);
}
/**
* Gets the request page number from the specified request URI.
*
* @param requestURI the specified request URI
* @return page number
*/
private static int getTagArticlesPagedCurrentPageNum(final String requestURI) {
return Requests.getCurrentPageNum(StringUtils.substringAfterLast(requestURI, "/"));
}
/**
* Gets the request tag from the specified request URI.
*
* @param requestURI the specified request URI
* @return tag
*/
private static String getTagArticlesPagedTag(final String requestURI) {
String tagAndPageNum = requestURI.substring((Latkes.getContextPath() + "/articles/tags/").length());
if (tagAndPageNum.endsWith("/")) {
tagAndPageNum = StringUtils.removeEnd(tagAndPageNum, "/");
}
return StringUtils.substringBefore(tagAndPageNum, "/");
}
/**
* Gets the request page number from the specified request URI.
*
* @param requestURI the specified request URI
* @return page number
*/
private static int getArchivesArticlesPagedCurrentPageNum(final String requestURI) {
return Requests.getCurrentPageNum(StringUtils.substringAfterLast(requestURI, "/"));
}
/**
* Gets the request archive from the specified request URI.
*
* @param requestURI the specified request URI
* @return archive, for example "2012/05"
*/
private static String getArchivesArticlesPagedArchive(final String requestURI) {
String archiveAndPageNum = requestURI.substring((Latkes.getContextPath() + "/articles/archives/").length());
if (archiveAndPageNum.endsWith("/")) {
archiveAndPageNum = StringUtils.removeEnd(archiveAndPageNum, "/");
}
return StringUtils.substringBeforeLast(archiveAndPageNum, "/");
}
/**
* Gets the request page number from the specified request URI.
*
* @param requestURI the specified request URI
* @return page number
*/
private static int getAuthorsArticlesPagedCurrentPageNum(final String requestURI) {
return Requests.getCurrentPageNum(StringUtils.substringAfterLast(requestURI, "/"));
}
/**
* Gets the request author id from the specified request URI.
*
* @param requestURI the specified request URI
* @return author id
*/
private static String getAuthorsArticlesPagedAuthorId(final String requestURI) {
String authorIdAndPageNum = requestURI.substring((Latkes.getContextPath() + "/articles/authors/").length());
if (authorIdAndPageNum.endsWith("/")) {
authorIdAndPageNum = StringUtils.removeEnd(authorIdAndPageNum, "/");
}
return StringUtils.substringBefore(authorIdAndPageNum, "/");
}
/**
* Gets the request page number from the specified request URI and author id.
*
* @param requestURI the specified request URI
* @param authorId the specified author id
* @return page number
*/
private static int getAuthorCurrentPageNum(final String requestURI, final String authorId) {
final String pageNumString = requestURI.substring((Latkes.getContextPath() + "/authors/" + authorId + "/").length());
return Requests.getCurrentPageNum(pageNumString);
}
/** /**
* Shows the article view password form. * Shows the article view password form.
* *
...@@ -528,9 +674,8 @@ public class ArticleProcessor { ...@@ -528,9 +674,8 @@ public class ArticleProcessor {
final int pageSize = preference.getInt(Option.ID_C_ARTICLE_LIST_DISPLAY_COUNT); final int pageSize = preference.getInt(Option.ID_C_ARTICLE_LIST_DISPLAY_COUNT);
final JSONObject archiveQueryResult = archiveDateQueryService.getByArchiveDateString(archiveDateString); final JSONObject archiveQueryResult = archiveDateQueryService.getByArchiveDateString(archiveDateString);
if (null == archiveQueryResult) { if (null == archiveQueryResult) {
throw new Exception("Can not found archive[archiveDate=" + archiveDateString + "]"); throw new Exception("Can not found archive [archiveDate=" + archiveDateString + "]");
} }
final JSONObject archiveDate = archiveQueryResult.getJSONObject(ArchiveDate.ARCHIVE_DATE); final JSONObject archiveDate = archiveQueryResult.getJSONObject(ArchiveDate.ARCHIVE_DATE);
...@@ -765,20 +910,21 @@ public class ArticleProcessor { ...@@ -765,20 +910,21 @@ public class ArticleProcessor {
requestURI += "/"; requestURI += "/";
} }
final String archiveDateString = getArchiveDate(requestURI);
final int currentPageNum = getArchiveCurrentPageNum(requestURI); final int currentPageNum = getArchiveCurrentPageNum(requestURI);
if (-1 == currentPageNum) { if (-1 == currentPageNum) {
response.sendError(HttpServletResponse.SC_NOT_FOUND); response.sendError(HttpServletResponse.SC_NOT_FOUND);
return; return;
} }
final String archiveDateString = getArchiveDate(requestURI);
LOGGER.log(Level.DEBUG, "Request archive date[string={0}, currentPageNum={1}]", archiveDateString, currentPageNum); LOGGER.log(Level.DEBUG, "Request archive date[string={0}, currentPageNum={1}]", archiveDateString, currentPageNum);
final JSONObject result = archiveDateQueryService.getByArchiveDateString(archiveDateString); final JSONObject result = archiveDateQueryService.getByArchiveDateString(archiveDateString);
if (null == result) { if (null == result) {
LOGGER.log(Level.WARN, "Can not find articles for the specified archive date[string={0}]", archiveDateString); LOGGER.log(Level.DEBUG, "Can not find articles for the specified archive date[string={0}]", archiveDateString);
response.sendError(HttpServletResponse.SC_NOT_FOUND); response.sendError(HttpServletResponse.SC_NOT_FOUND);
return; return;
} }
...@@ -979,151 +1125,6 @@ public class ArticleProcessor { ...@@ -979,151 +1125,6 @@ public class ArticleProcessor {
// } // }
} }
/**
* Gets archive date from the specified URI.
*
* @param requestURI the specified request URI
* @return archive date
*/
private static String getArchiveDate(final String requestURI) {
final String path = requestURI.substring((Latkes.getContextPath() + "/archives/").length());
return path.substring(0, "yyyy/MM".length());
}
/**
* Gets the request page number from the specified request URI.
*
* @param requestURI the specified request URI
* @return page number, returns {@code -1} if the specified request URI can not convert to an number
*/
private static int getArchiveCurrentPageNum(final String requestURI) {
final String pageNumString = requestURI.substring((Latkes.getContextPath() + "/archives/yyyy/MM/").length());
return Requests.getCurrentPageNum(pageNumString);
}
/**
* Gets author id from the specified URI.
*
* @param requestURI the specified request URI
* @return author id
*/
private static String getAuthorId(final String requestURI) {
final String path = requestURI.substring((Latkes.getContextPath() + "/authors/").length());
final int idx = path.indexOf("/");
if (-1 == idx) {
return path.substring(0);
} else {
return path.substring(0, idx);
}
}
/**
* Gets the request page number from the specified request URI.
*
* @param requestURI the specified request URI
* @return page number
*/
private static int getArticlesPagedCurrentPageNum(final String requestURI) {
final String pageNumString = requestURI.substring((Latkes.getContextPath() + "/articles/").length());
return Requests.getCurrentPageNum(pageNumString);
}
/**
* Gets the request page number from the specified request URI.
*
* @param requestURI the specified request URI
* @return page number
*/
private static int getTagArticlesPagedCurrentPageNum(final String requestURI) {
return Requests.getCurrentPageNum(StringUtils.substringAfterLast(requestURI, "/"));
}
/**
* Gets the request tag from the specified request URI.
*
* @param requestURI the specified request URI
* @return tag
*/
private static String getTagArticlesPagedTag(final String requestURI) {
String tagAndPageNum = requestURI.substring((Latkes.getContextPath() + "/articles/tags/").length());
if (tagAndPageNum.endsWith("/")) {
tagAndPageNum = StringUtils.removeEnd(tagAndPageNum, "/");
}
return StringUtils.substringBefore(tagAndPageNum, "/");
}
/**
* Gets the request page number from the specified request URI.
*
* @param requestURI the specified request URI
* @return page number
*/
private static int getArchivesArticlesPagedCurrentPageNum(final String requestURI) {
return Requests.getCurrentPageNum(StringUtils.substringAfterLast(requestURI, "/"));
}
/**
* Gets the request archive from the specified request URI.
*
* @param requestURI the specified request URI
* @return archive, for example "2012/05"
*/
private static String getArchivesArticlesPagedArchive(final String requestURI) {
String archiveAndPageNum = requestURI.substring((Latkes.getContextPath() + "/articles/archives/").length());
if (archiveAndPageNum.endsWith("/")) {
archiveAndPageNum = StringUtils.removeEnd(archiveAndPageNum, "/");
}
return StringUtils.substringBeforeLast(archiveAndPageNum, "/");
}
/**
* Gets the request page number from the specified request URI.
*
* @param requestURI the specified request URI
* @return page number
*/
private static int getAuthorsArticlesPagedCurrentPageNum(final String requestURI) {
return Requests.getCurrentPageNum(StringUtils.substringAfterLast(requestURI, "/"));
}
/**
* Gets the request author id from the specified request URI.
*
* @param requestURI the specified request URI
* @return author id
*/
private static String getAuthorsArticlesPagedAuthorId(final String requestURI) {
String authorIdAndPageNum = requestURI.substring((Latkes.getContextPath() + "/articles/authors/").length());
if (authorIdAndPageNum.endsWith("/")) {
authorIdAndPageNum = StringUtils.removeEnd(authorIdAndPageNum, "/");
}
return StringUtils.substringBefore(authorIdAndPageNum, "/");
}
/**
* Gets the request page number from the specified request URI and author id.
*
* @param requestURI the specified request URI
* @param authorId the specified author id
* @return page number
*/
private static int getAuthorCurrentPageNum(final String requestURI, final String authorId) {
final String pageNumString = requestURI.substring((Latkes.getContextPath() + "/authors/" + authorId + "/").length());
return Requests.getCurrentPageNum(pageNumString);
}
/** /**
* Gets the random articles. * Gets the random articles.
* *
......
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
*/ */
package org.b3log.solo.repository.impl; package org.b3log.solo.repository.impl;
import org.apache.commons.lang.time.DateUtils; import org.apache.commons.lang.time.DateUtils;
import org.b3log.latke.Keys; import org.b3log.latke.Keys;
import org.b3log.latke.logging.Level; import org.b3log.latke.logging.Level;
...@@ -32,12 +31,11 @@ import java.text.ParseException; ...@@ -32,12 +31,11 @@ import java.text.ParseException;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
/** /**
* Archive date repository. * Archive date repository.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.8, Jan 31, 2013 * @version 1.0.0.9, Jun 6, 2017
* @since 0.3.1 * @since 0.3.1
*/ */
@Repository @Repository
...@@ -60,10 +58,9 @@ public class ArchiveDateRepositoryImpl extends AbstractRepository implements Arc ...@@ -60,10 +58,9 @@ public class ArchiveDateRepositoryImpl extends AbstractRepository implements Arc
long time = 0L; long time = 0L;
try { try {
time = DateUtils.parseDate(archiveDate, new String[] {"yyyy/MM"}).getTime(); time = DateUtils.parseDate(archiveDate, new String[]{"yyyy/MM"}).getTime();
} catch (final ParseException e) { } catch (final ParseException e) {
LOGGER.log(Level.ERROR, "Can not parse archive date [" + archiveDate + "]", e); return null;
throw new RepositoryException("Can not parse archive date [" + archiveDate + "]");
} }
LOGGER.log(Level.TRACE, "Archive date [{0}] parsed to time [{1}]", archiveDate, time); LOGGER.log(Level.TRACE, "Archive date [{0}] parsed to time [{1}]", archiveDate, time);
......
...@@ -87,7 +87,6 @@ public class ArchiveDateQueryService { ...@@ -87,7 +87,6 @@ public class ArchiveDateQueryService {
try { try {
final JSONObject archiveDate = archiveDateRepository.getByArchiveDate(archiveDateString); final JSONObject archiveDate = archiveDateRepository.getByArchiveDate(archiveDateString);
if (null == archiveDate) { if (null == archiveDate) {
return null; return null;
} }
......
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