WebsiteTemplateArticleUtil.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. package space.apps.moudules.website.template.util;
  2. import java.sql.Timestamp;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import java.util.Locale;
  8. import java.util.Map;
  9. import java.util.regex.Pattern;
  10. import javax.servlet.http.HttpServletRequest;
  11. import apps.pojo.Article;
  12. import apps.pojo.Website;
  13. import apps.pojo.WebsiteNavigation;
  14. import space.apps.moudules.website.template.common.WebsiteArticleManager;
  15. import space.common.exception.SpaceNullPointerException;
  16. import space.core.common.CommonVars;
  17. import space.core.common.utils.DateTimeUtil;
  18. import space.core.common.utils.FileUtil;
  19. import space.core.common.utils.GsonUtil;
  20. import space.core.common.utils.StringUtil;
  21. import space.core.db.bean.base.PageBean;
  22. import space.core.db.jdbc.GeneralDBAccess;
  23. import space.core.db.jdbc.SqlCommandImpl;
  24. /**
  25. * 文章工具类
  26. *
  27. * @author SPACE
  28. * @date 2020年02月15日
  29. */
  30. public class WebsiteTemplateArticleUtil {
  31. private static final Long ARTICLE_DCODE_USE = 61L;
  32. private static final Long ARTICLE_DCODE_CHANGING = 31L;
  33. private static final Long ARTICLE_ASCODE_RELEASED = 10010L;
  34. private static final String CHANGE_LOG_OBJECT_ARTICLE = "Article";
  35. private static final String PUBLISHED_CONTENT_SNAPSHOT_FIELD = "publishedContentSnapshot";
  36. private static final String PUBLISHED_EDIT_CONTENT_SNAPSHOT_FIELD = "publishedEditContentSnapshot";
  37. private static final Pattern LOCAL_UPLOAD_ABSOLUTE_URL_PATTERN = Pattern.compile(
  38. "(?i)(?:https?:)?//(?:ht\\.gzhxtc\\.edu\\.cn|localhost|127\\.0\\.0\\.1|ffkj-[a-z0-9-]+\\.localhost)(?::\\d+)?(/web/upload/[^\"'<>\\s]*)");
  39. /**
  40. * 获取栏目的子栏目文章列表
  41. *
  42. * @param gdba 数据库
  43. * @param navId 栏目ID
  44. * @param isThumbnailFlag 是否只查询有上传缩略图的文章,为真则查询,否则不查询
  45. * @param pb 分页对象
  46. * @return ArticleList
  47. * @throws Exception
  48. * @author SPACE
  49. * @log Oct 9, 2020 8:48:37 AM
  50. */
  51. public static List<Map<String, Object>> getAllChildrenArticleList(GeneralDBAccess gdba, Long navId,
  52. boolean isThumbnailFlag, PageBean pb) throws Exception {
  53. String sql = "SELECT a.*, w.protocol, w.port, w.domains FROM article a left join website w on a.websiteid = w.websiteid where w.dcode = 61 and "
  54. + getPublishedOrChangingArticleSql("a")
  55. + " and (a.navigationid in (Select navigationid from website_navigation where parent_nid = :navigationid1 ) "
  56. + " or a.articleid in (Select ac.articleid from article_copy ac, article ca where ca.articleid = ac.articleid and ac.dcode = 61 "
  57. + " and " + getPublishedOrChangingArticleSql("ca")
  58. + " and ac.navigationid in (Select navigationid from website_navigation where parent_nid = :navigationid2 ) ))"
  59. + (isThumbnailFlag ? " and a.thumbnail is not null" : "") // thumbnail
  60. + " order by a.sticktop desc, a.release_time desc, a.articleid desc";
  61. SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
  62. sqlCmd.setString("navigationid1", navId).setString("navigationid2", navId);
  63. List<Map<String, Object>> articleList = gdba.selectAsMapList(sqlCmd, pb);
  64. restoreChangingArticleListPublishedVersion(gdba, articleList);
  65. transformArticleURL(articleList);
  66. return articleList;
  67. }
  68. /**
  69. * 根据 Request 获取文章对象
  70. *
  71. * @param gdba 数据库对象
  72. * @param website 网站对象
  73. * @param request request
  74. * @return Article
  75. * @throws Exception
  76. * @author SPACE
  77. * @date 2020年02月15日
  78. */
  79. public static Article getArticleByRequest(GeneralDBAccess gdba, Website website, HttpServletRequest request)
  80. throws Exception {
  81. return getArticleByURI(gdba, website, request.getRequestURI());
  82. }
  83. /**
  84. * 根据 URI 获取文章对象
  85. *
  86. * @param gdba 数据库对象
  87. * @param website 网站对象
  88. * @param uri URI
  89. * @return Article
  90. * @throws Exception
  91. * @author SPACE
  92. * @date 2020年02月15日
  93. */
  94. public static Article getArticleByURI(GeneralDBAccess gdba, Website website, String uri) throws Exception {
  95. String sql = "SELECT a.* FROM article a, website_navigation wn where a.navigationid = wn.navigationid "
  96. + "and wn.websiteid = :websiteid and wn.dcode = 61 and a.dcode = 61 and a.ascode = '10010' and a.url = :uri ";
  97. SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
  98. sqlCmd.setString("websiteid", website.getWebsiteid()).setString("uri", uri);
  99. Article a = (Article) gdba.selectAsBean(sqlCmd, Article.class);
  100. return a == null ? getChangingArticlePublishedVersionByURI(gdba, website, uri) : a;
  101. }
  102. /**
  103. * 根据文章获取文章内容
  104. *
  105. * @param article 文章对象
  106. * @return html
  107. * @throws Exception 如文件不存在或读取文件时出现问题,则抛出异常
  108. * @author SPACE
  109. * @date 2020年02月15日
  110. */
  111. public static String getArticleContentByArticle(Article article) throws Exception {
  112. return getArticleContentByPath(article.getContent());
  113. }
  114. /**
  115. * 根据文章路径读取文件内容
  116. *
  117. * @param contentPath 文章文件路径
  118. * @return 文章内容
  119. * @throws Exception
  120. * @author SPACE
  121. * @date 2020年02月27日
  122. */
  123. public static String getArticleContentByPath(String contentPath) throws Exception {
  124. if (StringUtil.isNull(contentPath)) {
  125. throw new SpaceNullPointerException(" Article content path is null ");
  126. }
  127. return FileUtil.readFile(CommonVars.getWebRootPath() + contentPath);
  128. }
  129. /**
  130. * 获取文章列表
  131. *
  132. * @param gdba 数据库对象
  133. * @param navId 栏目ID
  134. * @param isThumbnailFlag 是否只查询有上传缩略图的文章,为真则查询,否则不查询
  135. * @param pb 分页对象
  136. * @return ArticleList
  137. * @author SPACE
  138. * @throws Exception
  139. * @date 2020年02月13日
  140. */
  141. public static List<Map<String, Object>> getArticleList(GeneralDBAccess gdba, Long navId, boolean isThumbnailFlag,
  142. PageBean pb) throws Exception {
  143. String sql = "SELECT a.*, w.protocol, w.port, w.domains FROM article a left join website w on a.websiteid = w.websiteid "
  144. + "where w.dcode = 61 and " + getPublishedOrChangingArticleSql("a")
  145. + " and (a.navigationid = :navigationid1 "
  146. + "or a.articleid in (Select ac.articleid from article_copy ac, article ca "
  147. + "where ca.articleid = ac.articleid and ac.dcode = 61 and "
  148. + getPublishedOrChangingArticleSql("ca") + " and ac.navigationid = :navigationid2 ))"
  149. + (isThumbnailFlag ? " and a.thumbnail is not null" : "") // thumbnail
  150. + " order by a.sticktop desc, a.release_time desc ";
  151. SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
  152. sqlCmd.setString("navigationid1", navId).setString("navigationid2", navId);
  153. List<Map<String, Object>> articleList = gdba.selectAsMapList(sqlCmd, pb);
  154. restoreChangingArticleListPublishedVersion(gdba, articleList);
  155. transformArticleURL(articleList);
  156. return articleList;
  157. }
  158. /**
  159. * 获取文章列表
  160. *
  161. * @param gdba 数据库对象
  162. * @param navId 栏目ID
  163. * @param pb 分页对象
  164. * @return ArticleList
  165. * @throws Exception
  166. * @author SPACE
  167. * @log Oct 9, 2020 5:11:26 AM
  168. */
  169. public static List<Map<String, Object>> getArticleList(GeneralDBAccess gdba, Long navId, PageBean pb)
  170. throws Exception {
  171. return getArticleList(gdba, navId, false, pb);
  172. }
  173. /**
  174. * 查询文章列表
  175. *
  176. * @param gdba 数据库
  177. * @param keyword 文章标题关键字
  178. * @param pb 分页对象
  179. * @return 文章列表
  180. * @throws Exception
  181. * @author SPACE
  182. * @log Oct 9, 2020 10:37:47 AM
  183. */
  184. public static List<Map<String, Object>> getArticleList(GeneralDBAccess gdba, String keyword, PageBean pb)
  185. throws Exception {
  186. String sql = "SELECT a.*, w.protocol, w.port, w.domains FROM article a left join website w on a.websiteid = w.websiteid where w.dcode = 61 and "
  187. + getPublishedOrChangingArticleSql("a")
  188. + " and title like :keyword order by a.sticktop desc, a.release_time desc";
  189. SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
  190. sqlCmd.setString("keyword", "%" + keyword + "%");
  191. List<Map<String, Object>> articleList = gdba.selectAsMapList(sqlCmd, pb);
  192. restoreChangingArticleListPublishedVersion(gdba, articleList);
  193. transformArticleURL(articleList);
  194. return articleList;
  195. }
  196. /**
  197. * 查询当前站群内文章列表。主站搜索主站及下级站点,子站搜索其所属主站站群。
  198. *
  199. * @param gdba 数据库
  200. * @param website 当前网站
  201. * @param keyword 文章标题或摘要关键字
  202. * @param pb 分页对象
  203. * @return 文章列表
  204. * @throws Exception
  205. */
  206. public static List<Map<String, Object>> getArticleList(GeneralDBAccess gdba, Website website, String keyword,
  207. PageBean pb) throws Exception {
  208. Long rootWebsiteId = website.getParentWid() == null ? website.getWebsiteid() : website.getParentWid();
  209. String sql = "SELECT a.*, w.protocol, w.port, w.domains, w.name website_name, wn.name navigation_name "
  210. + "FROM article a "
  211. + "left join website w on a.websiteid = w.websiteid "
  212. + "left join website_navigation wn on a.navigationid = wn.navigationid "
  213. + "where w.dcode = 61 and " + getPublishedOrChangingArticleSql("a") + " "
  214. + "and (w.websiteid = :rootWebsiteId1 "
  215. + "or w.parent_wid = :rootWebsiteId2 "
  216. + "or w.parent_wid in (select sw.websiteid from website sw where sw.dcode = 61 and sw.parent_wid = :rootWebsiteId3) "
  217. + "or w.parent_wid in (select tw.websiteid from website tw where tw.dcode = 61 and tw.parent_wid in "
  218. + "(select sw2.websiteid from website sw2 where sw2.dcode = 61 and sw2.parent_wid = :rootWebsiteId4))) "
  219. + "and (a.title like :keyword1 or a.`desc` like :keyword2) "
  220. + "order by a.sticktop desc, a.release_time desc, a.articleid desc";
  221. SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
  222. sqlCmd.setString("rootWebsiteId1", rootWebsiteId).setString("rootWebsiteId2", rootWebsiteId)
  223. .setString("rootWebsiteId3", rootWebsiteId).setString("rootWebsiteId4", rootWebsiteId)
  224. .setString("keyword1", "%" + keyword + "%").setString("keyword2", "%" + keyword + "%");
  225. List<Map<String, Object>> articleList = gdba.selectAsMapList(sqlCmd, pb);
  226. restoreChangingArticleListPublishedVersion(gdba, articleList);
  227. transformArticleURL(articleList);
  228. return articleList;
  229. }
  230. private static String getPublishedOrChangingArticleSql(String alias) {
  231. return "((" + alias + ".dcode = 61 and " + alias + ".ascode = '10010') or " + alias + ".dcode = 31)";
  232. }
  233. private static Article getChangingArticlePublishedVersionByURI(GeneralDBAccess gdba, Website website, String uri)
  234. throws Exception {
  235. String sql = "SELECT cl.content FROM change_log cl where cl.object = :object and cl.content like :uriLike "
  236. + "and cl.objectid in (select ca.articleid from article ca where ca.dcode = :changingDcode) "
  237. + "order by cl.change_logid desc limit 0,20";
  238. SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
  239. sqlCmd.setString("object", CHANGE_LOG_OBJECT_ARTICLE).setString("uriLike", "%" + uri + "%")
  240. .setString("changingDcode", ARTICLE_DCODE_CHANGING);
  241. List<Map<String, Object>> logList = gdba.selectAsMapList(sqlCmd);
  242. if (logList == null || logList.size() == 0) {
  243. return null;
  244. }
  245. for (Map<String, Object> log : logList) {
  246. Article article = buildPublishedArticleFromChangeLog(log.get("content"), website.getWebsiteid(), uri);
  247. if (article != null) {
  248. return article;
  249. }
  250. }
  251. return null;
  252. }
  253. private static Article getChangingArticlePublishedVersionByArticleID(GeneralDBAccess gdba, Long articleid)
  254. throws Exception {
  255. String sql = "SELECT content FROM change_log where object = :object and objectid = :articleid "
  256. + "order by change_logid desc limit 0,20";
  257. SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
  258. sqlCmd.setString("object", CHANGE_LOG_OBJECT_ARTICLE).setString("articleid", articleid);
  259. List<Map<String, Object>> logList = gdba.selectAsMapList(sqlCmd);
  260. if (logList == null || logList.size() == 0) {
  261. return null;
  262. }
  263. for (Map<String, Object> log : logList) {
  264. Article article = buildPublishedArticleFromChangeLog(log.get("content"), null, null);
  265. if (article != null) {
  266. return article;
  267. }
  268. }
  269. return null;
  270. }
  271. private static Article buildPublishedArticleFromChangeLog(Object logContent, Long websiteid, String uri) {
  272. if (StringUtil.isNull(logContent)) {
  273. return null;
  274. }
  275. Map<String, Object> articleMap = GsonUtil.jsonToMap(logContent.toString());
  276. if (!ARTICLE_DCODE_USE.equals(getLongValue(articleMap, "dcode"))
  277. || !ARTICLE_ASCODE_RELEASED.equals(getLongValue(articleMap, "ascode"))) {
  278. return null;
  279. }
  280. if (websiteid != null && !websiteid.equals(getLongValue(articleMap, "websiteid"))) {
  281. return null;
  282. }
  283. if (uri != null && !uri.equals(getStringValue(articleMap, "url"))) {
  284. return null;
  285. }
  286. Article article = new Article();
  287. article.setArticleid(getLongValue(articleMap, "articleid"));
  288. article.setDcode(ARTICLE_DCODE_USE);
  289. article.setWebsiteid(getLongValue(articleMap, "websiteid"));
  290. article.setUnitid(getLongValue(articleMap, "unitid"));
  291. article.setAscode(ARTICLE_ASCODE_RELEASED);
  292. article.setNavigationid(getLongValue(articleMap, "navigationid"));
  293. article.setTitle(getStringValue(articleMap, "title"));
  294. article.setAjtcode(getLongValue(articleMap, "ajtcode"));
  295. article.setDesc(getStringValue(articleMap, "desc"));
  296. article.setSource(getStringValue(articleMap, "source"));
  297. article.setThumbnail(getStringValue(articleMap, "thumbnail"));
  298. article.setSticktop(getLongValue(articleMap, "sticktop"));
  299. article.setSticktopEndTime(getTimestampValue(articleMap, "sticktopEndTime"));
  300. article.setSticktopSchedulesid(getLongValue(articleMap, "sticktopSchedulesid"));
  301. article.setRelease(getLongValue(articleMap, "release"));
  302. article.setReleaseTime(getTimestampValue(articleMap, "releaseTime"));
  303. article.setReleaseTaskSchedulesid(getLongValue(articleMap, "releaseTaskSchedulesid"));
  304. article.setContent(getStringValue(articleMap, PUBLISHED_CONTENT_SNAPSHOT_FIELD));
  305. if (StringUtil.isNull(article.getContent())) {
  306. article.setContent(getStringValue(articleMap, "content"));
  307. }
  308. article.setEditContent(getStringValue(articleMap, PUBLISHED_EDIT_CONTENT_SNAPSHOT_FIELD));
  309. if (StringUtil.isNull(article.getEditContent())) {
  310. article.setEditContent(getStringValue(articleMap, "editContent"));
  311. }
  312. article.setUrl(getStringValue(articleMap, "url"));
  313. article.setReadCount(getLongValue(articleMap, "readCount"));
  314. article.setCreateTime(getTimestampValue(articleMap, "createTime"));
  315. article.setLastChangeTime(getTimestampValue(articleMap, "lastChangeTime"));
  316. article.setCreatePid(getLongValue(articleMap, "createPid"));
  317. article.setLastChangePid(getLongValue(articleMap, "lastChangePid"));
  318. return article;
  319. }
  320. private static void restoreChangingArticleListPublishedVersion(GeneralDBAccess gdba,
  321. List<Map<String, Object>> articleList) throws Exception {
  322. if (articleList == null || articleList.size() == 0) {
  323. return;
  324. }
  325. Iterator<Map<String, Object>> iterator = articleList.iterator();
  326. while (iterator.hasNext()) {
  327. Map<String, Object> articleMap = iterator.next();
  328. if (!ARTICLE_DCODE_CHANGING.equals(getLongValue(articleMap, "dcode"))) {
  329. continue;
  330. }
  331. Article article = getChangingArticlePublishedVersionByArticleID(gdba, getLongValue(articleMap, "articleid"));
  332. if (article == null) {
  333. iterator.remove();
  334. continue;
  335. }
  336. putArticleValueToMap(articleMap, article);
  337. }
  338. }
  339. private static void putArticleValueToMap(Map<String, Object> articleMap, Article article) {
  340. articleMap.put("articleid", article.getArticleid());
  341. articleMap.put("dcode", article.getDcode());
  342. articleMap.put("websiteid", article.getWebsiteid());
  343. articleMap.put("unitid", article.getUnitid());
  344. articleMap.put("ascode", article.getAscode());
  345. articleMap.put("navigationid", article.getNavigationid());
  346. articleMap.put("title", article.getTitle());
  347. articleMap.put("ajtcode", article.getAjtcode());
  348. articleMap.put("desc", article.getDesc());
  349. articleMap.put("source", article.getSource());
  350. articleMap.put("thumbnail", article.getThumbnail());
  351. articleMap.put("sticktop", article.getSticktop());
  352. articleMap.put("sticktop_end_time", article.getSticktopEndTime());
  353. articleMap.put("sticktop_schedulesid", article.getSticktopSchedulesid());
  354. articleMap.put("release", article.getRelease());
  355. articleMap.put("release_time", article.getReleaseTime());
  356. articleMap.put("release_task_schedulesid", article.getReleaseTaskSchedulesid());
  357. articleMap.put("content", article.getContent());
  358. articleMap.put("edit_content", article.getEditContent());
  359. articleMap.put("url", article.getUrl());
  360. articleMap.put("read_count", article.getReadCount());
  361. articleMap.put("create_time", article.getCreateTime());
  362. articleMap.put("last_change_time", article.getLastChangeTime());
  363. articleMap.put("create_pid", article.getCreatePid());
  364. articleMap.put("last_change_pid", article.getLastChangePid());
  365. }
  366. private static String getStringValue(Map<String, Object> map, String key) {
  367. Object value = map.get(key);
  368. return StringUtil.isNull(value) ? null : value.toString();
  369. }
  370. private static Long getLongValue(Map<String, Object> map, String key) {
  371. Object value = map.get(key);
  372. if (StringUtil.isNull(value)) {
  373. return null;
  374. }
  375. if (value instanceof Number) {
  376. return ((Number) value).longValue();
  377. }
  378. return Long.valueOf(value.toString());
  379. }
  380. private static Timestamp getTimestampValue(Map<String, Object> map, String key) {
  381. String value = getStringValue(map, key);
  382. if (StringUtil.isNull(value)) {
  383. return null;
  384. }
  385. String[] patterns = new String[] { "MMM d, yyyy h:mm:ss a", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm" };
  386. for (String pattern : patterns) {
  387. try {
  388. Date date = new SimpleDateFormat(pattern, Locale.ENGLISH).parse(value);
  389. return new Timestamp(date.getTime());
  390. } catch (Exception e) {
  391. // 尝试下一种历史日志里的时间格式。
  392. }
  393. }
  394. try {
  395. return Timestamp.valueOf(value);
  396. } catch (Exception e) {
  397. return null;
  398. }
  399. }
  400. /**
  401. * 获取文章列表
  402. *
  403. * @param gdba 数据库
  404. * @param wn 栏目
  405. * @param pb 分页
  406. * @return 文章列表
  407. * @throws Exception
  408. * @author SPACE
  409. * @date 2020年02月27日
  410. */
  411. public static List<Map<String, Object>> getArticleList(GeneralDBAccess gdba, WebsiteNavigation wn, PageBean pb)
  412. throws Exception {
  413. return getArticleList(gdba, wn.getNavigationid(), pb);
  414. }
  415. /**
  416. * 生成文章的URL
  417. *
  418. * @param wn 栏目对象
  419. * @return /栏目URL/12为随机数.html
  420. * @author SPACE
  421. * @throws SpaceNullPointerException
  422. * @date 2020年02月14日
  423. */
  424. public static String getArticleURL(WebsiteNavigation wn) throws SpaceNullPointerException {
  425. if (StringUtil.isNull(wn.getUrl())) {
  426. throw new SpaceNullPointerException("栏目URL为空");
  427. }
  428. String wnUrl = wn.getUrl().endsWith("/") ? wn.getUrl() : wn.getUrl() + "/";
  429. return wnUrl + DateTimeUtil.getSimpleYear() + DateTimeUtil.getNowDate("MMdd") + "/" + StringUtil.getNonceStr(12)
  430. + ".htm";
  431. }
  432. /**
  433. * 获取栏目下的第一篇文章
  434. *
  435. * @param wn 栏目对象
  436. * @return 文章对象Map
  437. * @author SPACE
  438. * @throws Exception
  439. * @date 2020年02月26日
  440. */
  441. public static Map<String, Object> getFristArticleByWn(GeneralDBAccess gdba, WebsiteNavigation wn) throws Exception {
  442. PageBean pb = new PageBean();
  443. pb.setRowCountPerPage(1);
  444. List<Map<String, Object>> articleList = getArticleList(gdba, wn, pb);
  445. return (articleList == null || articleList.size() == 0) ? null : articleList.get(0);
  446. }
  447. private static void setArticleAttrToRequest(HttpServletRequest request, Website website, Long articleID,
  448. String title, Object desc, String source, String releaseTime, String contentPath) throws Exception {
  449. request.setAttribute("articleTitle", title);
  450. request.setAttribute("articleSource", source);
  451. request.setAttribute("articleReleaseTime", releaseTime);
  452. request.setAttribute("articleDescription", StringUtil.isNull(desc) ? title : desc);
  453. request.setAttribute("articleContent",
  454. normalizeLocalUploadResourceUrl(
  455. WebsiteArticleManager.getArticleContent(website, articleID, contentPath)));
  456. }
  457. private static String normalizeLocalUploadResourceUrl(String content) {
  458. if (StringUtil.isNull(content) || content.indexOf("/web/upload/") < 0) {
  459. return content;
  460. }
  461. return LOCAL_UPLOAD_ABSOLUTE_URL_PATTERN.matcher(content).replaceAll("$1");
  462. }
  463. /**
  464. * 将页面需要的文章属性放到 Request 中<br>
  465. * articleTitle: 文章标题<br>
  466. * articleSource: 文章来源<br>
  467. * articleReleaseTime: 文章发布时间<br>
  468. * articleDescription: 文章摘要<br>
  469. * articleContent: 文章正文(HTML)<br>
  470. *
  471. * @param article 文章对象
  472. * @param request 请求
  473. * @throws Exception
  474. * @author SPACE
  475. * @log Oct 7, 2020 10:37:32 PM
  476. */
  477. public static void setArticleAttrToRequest(Website website, Article article, HttpServletRequest request)
  478. throws Exception {
  479. setArticleAttrToRequest(request, website, article.getArticleid(), article.getTitle(), article.getDesc(),
  480. article.getSource(), DateTimeUtil.formatDate(article.getReleaseTime(), "yyyy-MM-dd HH:mm"),
  481. article.getContent());
  482. }
  483. /**
  484. * 将页面需要的文章属性放到 Request 中<br>
  485. * articleTitle: 文章标题<br>
  486. * articleSource: 文章来源<br>
  487. * articleReleaseTime: 文章发布时间<br>
  488. * articleDescription: 文章摘要<br>
  489. * articleContent: 文章正文(HTML)<br>
  490. *
  491. * @param articleMap 文章 Map
  492. * @param request 请求
  493. * @throws Exception
  494. * @author SPACE
  495. * @log Oct 7, 2020 10:48:01 PM
  496. */
  497. public static void setArticleAttrToRequest(Website website, Map<String, Object> articleMap,
  498. HttpServletRequest request) throws Exception {
  499. setArticleAttrToRequest(request, website, Long.valueOf(articleMap.get("articleid").toString()),
  500. articleMap.get("title").toString(), articleMap.get("desc"), articleMap.get("source").toString(),
  501. DateTimeUtil.formatDate(articleMap.get("release_time").toString(), "yyyy-MM-dd HH:mm"),
  502. articleMap.get("content").toString());
  503. }
  504. /**
  505. * 转换文章的URL
  506. *
  507. * @param articleList 文章列表
  508. * @author SPACE
  509. * @log Oct 9, 2020 8:27:29 AM
  510. */
  511. private static void transformArticleURL(List<Map<String, Object>> articleList) {
  512. if (articleList == null || articleList.size() == 0) {
  513. return;
  514. }
  515. // 文章URL加入网站域名,如一个网站绑定了多个域名,默认取第一个
  516. for (Map<String, Object> map : articleList) {
  517. String url = map.get("url").toString();
  518. String ajtCode = map.get("ajtcode").toString();
  519. String href = null; // 最终跳转的链接
  520. String target = null; // 跳转方式
  521. // 动作:跳转正文页
  522. if ("10000".equals(ajtCode)) {
  523. String domains = map.get("domains").toString(); // 域名
  524. String protocol = map.get("protocol").toString(); // 协议
  525. String port = map.get("port").toString(); // 端口
  526. String[] domainsAry = domains.split(";");
  527. if (domainsAry.length > 0) {
  528. port = "80".equals(port) ? "" : (":" + port); // 非 80 端口进行特殊处理
  529. if (domainsAry[0].endsWith("/")) {
  530. domainsAry[0] = domainsAry[0].substring(0, domainsAry[0].lastIndexOf("/"));
  531. }
  532. href = protocol + "://" + domainsAry[0] + port + url;
  533. target = "_blank";
  534. }
  535. // 未找到域名
  536. else {
  537. href = "javascript:alert('website domain is null!');";
  538. target = "_self";
  539. }
  540. }
  541. // 动作:自定义链接
  542. else if ("10020".equals(ajtCode)) {
  543. href = url;
  544. target = "_blank";
  545. }
  546. // 动作:无动作
  547. else if ("10030".equals(ajtCode)) {
  548. href = "javascript:void(0);";
  549. target = "_self";
  550. }
  551. map.put("url", href);
  552. map.put("target", target);
  553. Object thumbnail = map.get("thumbnail"); // 缩略图
  554. if (StringUtil.notNull(thumbnail)) {
  555. map.put("thumbnail", normalizeThumbnailURL(thumbnail.toString()));
  556. }
  557. }
  558. }
  559. private static String normalizeThumbnailURL(String thumbnail) {
  560. if (thumbnail == null) {
  561. return thumbnail;
  562. }
  563. String value = thumbnail.trim();
  564. if (value.length() == 0) {
  565. return value;
  566. }
  567. int uploadPathIndex = value.indexOf("/web/upload/");
  568. if (uploadPathIndex >= 0) {
  569. return normalizeLocalPath(value.substring(uploadPathIndex));
  570. }
  571. if (value.startsWith("http://") || value.startsWith("https://") || value.startsWith("//")) {
  572. return value;
  573. }
  574. return normalizeLocalPath(value.startsWith("/") ? value : "/" + value);
  575. }
  576. private static String normalizeLocalPath(String path) {
  577. return path.replaceAll("/{2,}", "/");
  578. }
  579. }