Refactor OPTIONS and remove it

pull/137/head
johnniang 2019-03-22 19:35:11 +08:00
parent 2b7753bccf
commit a7af1e7f77
12 changed files with 215 additions and 193 deletions

View File

@ -0,0 +1,18 @@
package cc.ryanc.halo.exception;
/**
* Missing property value exception.
*
* @author johnniang
* @date 3/22/19
*/
public class MissingPropertyValueException extends BadRequestException {
public MissingPropertyValueException(String message) {
super(message);
}
public MissingPropertyValueException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -25,7 +25,6 @@ import java.nio.charset.Charset;
import java.util.List; import java.util.List;
import static cc.ryanc.halo.model.support.HaloConst.DEFAULT_THEME_NAME; import static cc.ryanc.halo.model.support.HaloConst.DEFAULT_THEME_NAME;
import static cc.ryanc.halo.model.support.HaloConst.OPTIONS;
/** /**
* <pre> * <pre>
@ -54,22 +53,12 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
@Override @Override
public void onApplicationEvent(ApplicationStartedEvent event) { public void onApplicationEvent(ApplicationStartedEvent event) {
// save halo version to database // save halo version to database
// TODO Complete cache option value
this.cacheOptions();
this.cacheThemes(); this.cacheThemes();
this.cacheOwo(); this.cacheOwo();
this.getActiveTheme(); this.getActiveTheme();
this.printStartInfo(); this.printStartInfo();
} }
/**
* Cache options to map
*/
private void cacheOptions() {
OPTIONS.clear();
OPTIONS.putAll(optionService.listOptions());
}
/** /**
* Cache themes to map * Cache themes to map
*/ */
@ -115,7 +104,7 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
// Get server port // Get server port
String serverPort = applicationContext.getEnvironment().getProperty("server.port"); String serverPort = applicationContext.getEnvironment().getProperty("server.port");
String blogUrl = OPTIONS.get(BlogProperties.BLOG_URL); String blogUrl = optionService.getByPropertyOfNullable(BlogProperties.BLOG_URL);
if (StrUtil.isNotBlank(blogUrl)) { if (StrUtil.isNotBlank(blogUrl)) {
blogUrl = StrUtil.removeSuffix(blogUrl, "/"); blogUrl = StrUtil.removeSuffix(blogUrl, "/");

View File

@ -4,8 +4,6 @@ import cn.hutool.json.JSONObject;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/** /**
* <pre> * <pre>
@ -33,12 +31,6 @@ public class HaloConst {
*/ */
public static final String TOKEN_HEADER = "token"; public static final String TOKEN_HEADER = "token";
/**
* All of the options
*/
@Deprecated
public final static ConcurrentMap<String, String> OPTIONS = new ConcurrentHashMap<>();
/** /**
* All of the Owo * All of the Owo
*/ */

View File

@ -1,5 +1,6 @@
package cc.ryanc.halo.service; package cc.ryanc.halo.service;
import cc.ryanc.halo.exception.MissingPropertyValueException;
import cc.ryanc.halo.model.dto.OptionOutputDTO; import cc.ryanc.halo.model.dto.OptionOutputDTO;
import cc.ryanc.halo.model.entity.Option; import cc.ryanc.halo.model.entity.Option;
import cc.ryanc.halo.model.enums.BlogProperties; import cc.ryanc.halo.model.enums.BlogProperties;
@ -24,6 +25,8 @@ public interface OptionService extends CrudService<Option, Integer> {
int DEFAULT_COMMENT_PAGE_SIZE = 10; int DEFAULT_COMMENT_PAGE_SIZE = 10;
int DEFAULT_RSS_PAGE_SIZE = 20;
/** /**
* Save one option * Save one option
* *
@ -80,6 +83,15 @@ public interface OptionService extends CrudService<Option, Integer> {
@Nullable @Nullable
String getByKeyOfNullable(@NonNull String key); String getByKeyOfNullable(@NonNull String key);
/**
* Gets option value of non null.
*
* @param key option key must not be null
* @return option value of non null
*/
@NonNull
String getByKeyOfNonNull(@NonNull String key);
/** /**
* Get option by key * Get option by key
* *
@ -90,7 +102,7 @@ public interface OptionService extends CrudService<Option, Integer> {
Optional<String> getByKey(@NonNull String key); Optional<String> getByKey(@NonNull String key);
/** /**
* Gets option by blog property. * Gets option value by blog property.
* *
* @param property blog property must not be null * @param property blog property must not be null
* @return an option value * @return an option value
@ -99,7 +111,17 @@ public interface OptionService extends CrudService<Option, Integer> {
String getByPropertyOfNullable(@NonNull BlogProperties property); String getByPropertyOfNullable(@NonNull BlogProperties property);
/** /**
* Gets option by blog property. * Gets option value by blog property.
*
* @param property blog property
* @return an optiona value
* @throws MissingPropertyValueException throws when property value dismisses
*/
@NonNull
String getByPropertyOfNonNull(@NonNull BlogProperties property);
/**
* Gets option value by blog property.
* *
* @param property blog property must not be null * @param property blog property must not be null
* @return an optional option value * @return an optional option value
@ -121,6 +143,13 @@ public interface OptionService extends CrudService<Option, Integer> {
*/ */
int getCommentPageSize(); int getCommentPageSize();
/**
* Gets rss page size.
*
* @return page size
*/
int getRssPageSize();
/** /**
* Get quniu zone. * Get quniu zone.
* *

View File

@ -2,6 +2,7 @@ package cc.ryanc.halo.service.impl;
import cc.ryanc.halo.model.enums.BlogProperties; import cc.ryanc.halo.model.enums.BlogProperties;
import cc.ryanc.halo.service.MailService; import cc.ryanc.halo.service.MailService;
import cc.ryanc.halo.service.OptionService;
import cc.ryanc.halo.utils.HaloUtils; import cc.ryanc.halo.utils.HaloUtils;
import cn.hutool.core.text.StrBuilder; import cn.hutool.core.text.StrBuilder;
import freemarker.template.Template; import freemarker.template.Template;
@ -13,8 +14,6 @@ import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import java.io.File; import java.io.File;
import java.util.Map; import java.util.Map;
import static cc.ryanc.halo.model.support.HaloConst.OPTIONS;
/** /**
* @author : RYAN0UP * @author : RYAN0UP
* @date : 2019-03-17 * @date : 2019-03-17
@ -24,8 +23,12 @@ public class MailServiceImpl implements MailService {
private final FreeMarkerConfigurer freeMarker; private final FreeMarkerConfigurer freeMarker;
public MailServiceImpl(FreeMarkerConfigurer freeMarker) { private final OptionService optionService;
public MailServiceImpl(FreeMarkerConfigurer freeMarker,
OptionService optionService) {
this.freeMarker = freeMarker; this.freeMarker = freeMarker;
this.optionService = optionService;
} }
/** /**
@ -38,12 +41,12 @@ public class MailServiceImpl implements MailService {
@Override @Override
public void sendMail(String to, String subject, String content) { public void sendMail(String to, String subject, String content) {
HaloUtils.configMail( HaloUtils.configMail(
OPTIONS.get(BlogProperties.MAIL_SMTP_HOST.getValue()), optionService.getByPropertyOfNonNull(BlogProperties.MAIL_SMTP_HOST),
OPTIONS.get(BlogProperties.MAIL_SMTP_USERNAME.getValue()), optionService.getByPropertyOfNonNull(BlogProperties.MAIL_SMTP_USERNAME),
OPTIONS.get(BlogProperties.MAIL_SMTP_PASSWORD.getValue())); optionService.getByPropertyOfNonNull(BlogProperties.MAIL_SMTP_PASSWORD));
try { try {
OhMyEmail.subject(subject) OhMyEmail.subject(subject)
.from(OPTIONS.get(BlogProperties.MAIL_FROM_NAME.getValue())) .from(optionService.getByPropertyOfNonNull(BlogProperties.MAIL_FROM_NAME))
.to(to) .to(to)
.text(content) .text(content)
.send(); .send();
@ -64,15 +67,15 @@ public class MailServiceImpl implements MailService {
@Override @Override
public void sendTemplateMail(String to, String subject, Map<String, Object> content, String templateName) { public void sendTemplateMail(String to, String subject, Map<String, Object> content, String templateName) {
HaloUtils.configMail( HaloUtils.configMail(
OPTIONS.get(BlogProperties.MAIL_SMTP_HOST.getValue()), optionService.getByPropertyOfNonNull(BlogProperties.MAIL_SMTP_HOST),
OPTIONS.get(BlogProperties.MAIL_SMTP_USERNAME.getValue()), optionService.getByPropertyOfNonNull(BlogProperties.MAIL_SMTP_USERNAME),
OPTIONS.get(BlogProperties.MAIL_SMTP_PASSWORD.getValue())); optionService.getByPropertyOfNonNull(BlogProperties.MAIL_SMTP_PASSWORD));
StrBuilder text = new StrBuilder(); StrBuilder text = new StrBuilder();
try { try {
final Template template = freeMarker.getConfiguration().getTemplate(templateName); final Template template = freeMarker.getConfiguration().getTemplate(templateName);
text.append(FreeMarkerTemplateUtils.processTemplateIntoString(template, content)); text.append(FreeMarkerTemplateUtils.processTemplateIntoString(template, content));
OhMyEmail.subject(subject) OhMyEmail.subject(subject)
.from(OPTIONS.get(BlogProperties.MAIL_FROM_NAME.getValue())) .from(optionService.getByPropertyOfNonNull(BlogProperties.MAIL_FROM_NAME))
.to(to) .to(to)
.html(text.toString()) .html(text.toString())
.send(); .send();
@ -94,16 +97,16 @@ public class MailServiceImpl implements MailService {
@Override @Override
public void sendAttachMail(String to, String subject, Map<String, Object> content, String templateName, String attachSrc) { public void sendAttachMail(String to, String subject, Map<String, Object> content, String templateName, String attachSrc) {
HaloUtils.configMail( HaloUtils.configMail(
OPTIONS.get(BlogProperties.MAIL_SMTP_HOST.getValue()), optionService.getByPropertyOfNonNull(BlogProperties.MAIL_SMTP_HOST),
OPTIONS.get(BlogProperties.MAIL_SMTP_USERNAME.getValue()), optionService.getByPropertyOfNonNull(BlogProperties.MAIL_SMTP_USERNAME),
OPTIONS.get(BlogProperties.MAIL_SMTP_PASSWORD.getValue())); optionService.getByPropertyOfNonNull(BlogProperties.MAIL_SMTP_PASSWORD));
File file = new File(attachSrc); File file = new File(attachSrc);
StrBuilder text = new StrBuilder(); StrBuilder text = new StrBuilder();
try { try {
final Template template = freeMarker.getConfiguration().getTemplate(templateName); final Template template = freeMarker.getConfiguration().getTemplate(templateName);
text.append(FreeMarkerTemplateUtils.processTemplateIntoString(template, content)); text.append(FreeMarkerTemplateUtils.processTemplateIntoString(template, content));
OhMyEmail.subject(subject) OhMyEmail.subject(subject)
.from(OPTIONS.get(BlogProperties.MAIL_FROM_NAME.getValue())) .from(optionService.getByPropertyOfNonNull(BlogProperties.MAIL_FROM_NAME))
.to(to) .to(to)
.html(text.toString()) .html(text.toString())
.attach(file, file.getName()) .attach(file, file.getName())

View File

@ -1,5 +1,6 @@
package cc.ryanc.halo.service.impl; package cc.ryanc.halo.service.impl;
import cc.ryanc.halo.exception.MissingPropertyValueException;
import cc.ryanc.halo.model.dto.OptionOutputDTO; import cc.ryanc.halo.model.dto.OptionOutputDTO;
import cc.ryanc.halo.model.entity.Option; import cc.ryanc.halo.model.entity.Option;
import cc.ryanc.halo.model.enums.BlogProperties; import cc.ryanc.halo.model.enums.BlogProperties;
@ -133,6 +134,11 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
return getByKey(key).orElse(null); return getByKey(key).orElse(null);
} }
@Override
public String getByKeyOfNonNull(String key) {
return getByKey(key).orElseThrow(() -> new MissingPropertyValueException("You have to config " + key + " setting"));
}
@Override @Override
public Optional<String> getByKey(String key) { public Optional<String> getByKey(String key) {
Assert.hasText(key, "Option key must not be blank"); Assert.hasText(key, "Option key must not be blank");
@ -145,6 +151,13 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
return getByProperty(property).orElse(null); return getByProperty(property).orElse(null);
} }
@Override
public String getByPropertyOfNonNull(BlogProperties property) {
Assert.notNull(property, "Blog property must not be null");
return getByKeyOfNonNull(property.getValue());
}
@Override @Override
public Optional<String> getByProperty(BlogProperties property) { public Optional<String> getByProperty(BlogProperties property) {
Assert.notNull(property, "Blog property must not be null"); Assert.notNull(property, "Blog property must not be null");
@ -157,7 +170,7 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
try { try {
return getByProperty(BlogProperties.INDEX_POSTS).map(Integer::valueOf).orElse(DEFAULT_POST_PAGE_SIZE); return getByProperty(BlogProperties.INDEX_POSTS).map(Integer::valueOf).orElse(DEFAULT_POST_PAGE_SIZE);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
log.error(BlogProperties.INDEX_POSTS + " option was not a number format", e); log.error(BlogProperties.INDEX_POSTS + " option is not a number format", e);
return DEFAULT_POST_PAGE_SIZE; return DEFAULT_POST_PAGE_SIZE;
} }
} }
@ -167,11 +180,21 @@ public class OptionServiceImpl extends AbstractCrudService<Option, Integer> impl
try { try {
return getByProperty(BlogProperties.INDEX_COMMENTS).map(Integer::valueOf).orElse(DEFAULT_COMMENT_PAGE_SIZE); return getByProperty(BlogProperties.INDEX_COMMENTS).map(Integer::valueOf).orElse(DEFAULT_COMMENT_PAGE_SIZE);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
log.error(BlogProperties.INDEX_COMMENTS + " option was not a number format", e); log.error(BlogProperties.INDEX_COMMENTS + " option is not a number format", e);
return DEFAULT_COMMENT_PAGE_SIZE; return DEFAULT_COMMENT_PAGE_SIZE;
} }
} }
@Override
public int getRssPageSize() {
try {
return getByProperty(BlogProperties.RSS_POSTS).map(Integer::valueOf).orElse(DEFAULT_RSS_PAGE_SIZE);
} catch (NumberFormatException e) {
log.error(BlogProperties.RSS_POSTS + " setting is not a number format", e);
return DEFAULT_RSS_PAGE_SIZE;
}
}
@Override @Override
public Zone getQiniuZone() { public Zone getQiniuZone() {
return getByProperty(BlogProperties.QINIU_ZONE).map(qiniuZone -> { return getByProperty(BlogProperties.QINIU_ZONE).map(qiniuZone -> {

View File

@ -36,8 +36,6 @@ import java.util.Properties;
@Slf4j @Slf4j
public class HaloUtils { public class HaloUtils {
public final static int DEFAULT_PAGE_SIZE = 10;
/** /**
* Initialize url if blank. * Initialize url if blank.
* *

View File

@ -17,8 +17,6 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static cc.ryanc.halo.model.support.HaloConst.OPTIONS;
/** /**
* @author : RYAN0UP * @author : RYAN0UP
* @date : 2019/3/20 * @date : 2019/3/20
@ -61,9 +59,7 @@ public class ThemeController {
properties.put(BlogProperties.THEME, themeName); properties.put(BlogProperties.THEME, themeName);
optionService.saveProperties(properties, "system"); optionService.saveProperties(properties, "system");
BaseContentController.THEME = themeName; BaseContentController.THEME = themeName;
OPTIONS.clear();
OPTIONS.putAll(optionService.listOptions());
configuration.setSharedVariable("themeName", themeName); configuration.setSharedVariable("themeName", themeName);
configuration.setSharedVariable("options", OPTIONS); configuration.setSharedVariable("options", optionService.listOptions());
} }
} }

View File

@ -6,7 +6,6 @@ import freemarker.template.Configuration;
import freemarker.template.TemplateModelException; import freemarker.template.TemplateModelException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import static cc.ryanc.halo.model.support.HaloConst.OPTIONS;
import static cc.ryanc.halo.model.support.HaloConst.THEMES; import static cc.ryanc.halo.model.support.HaloConst.THEMES;
/** /**
@ -28,11 +27,9 @@ public abstract class BaseController {
*/ */
public void refreshCache() { public void refreshCache() {
try { try {
OPTIONS.clear();
THEMES.clear(); THEMES.clear();
OPTIONS.putAll(optionService.listOptions());
THEMES = ThemeUtils.getThemes(); THEMES = ThemeUtils.getThemes();
configuration.setSharedVariable("options", OPTIONS); configuration.setSharedVariable("options", optionService.listOptions());
} catch (TemplateModelException e) { } catch (TemplateModelException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -1,7 +1,6 @@
package cc.ryanc.halo.web.controller.content; package cc.ryanc.halo.web.controller.content;
import cc.ryanc.halo.model.entity.Post; import cc.ryanc.halo.model.entity.Post;
import cc.ryanc.halo.model.enums.BlogProperties;
import cc.ryanc.halo.model.enums.PostStatus; import cc.ryanc.halo.model.enums.PostStatus;
import cc.ryanc.halo.model.enums.PostType; import cc.ryanc.halo.model.enums.PostType;
import cc.ryanc.halo.service.OptionService; import cc.ryanc.halo.service.OptionService;
@ -23,8 +22,6 @@ import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import static cc.ryanc.halo.model.support.HaloConst.OPTIONS;
/** /**
* @author : RYAN0UP * @author : RYAN0UP
* @date : 2019-03-21 * @date : 2019-03-21
@ -57,12 +54,9 @@ public class ContentFeedController {
@GetMapping(value = {"feed", "feed.xml", "rss", "rss.xml"}, produces = "application/xml;charset=UTF-8") @GetMapping(value = {"feed", "feed.xml", "rss", "rss.xml"}, produces = "application/xml;charset=UTF-8")
@ResponseBody @ResponseBody
public String feed(Model model) throws IOException, TemplateException { public String feed(Model model) throws IOException, TemplateException {
String rssPosts = OPTIONS.get(BlogProperties.RSS_POSTS.getValue()); int rssPageSize = optionService.getRssPageSize();
if (StrUtil.isBlank(rssPosts)) {
rssPosts = "20";
}
final Sort sort = new Sort(Sort.Direction.DESC, "postDate"); final Sort sort = new Sort(Sort.Direction.DESC, "postDate");
final Pageable pageable = PageRequest.of(0, Integer.parseInt(rssPosts), sort); final Pageable pageable = PageRequest.of(0, rssPageSize, sort);
model.addAttribute("posts", buildPosts(pageable)); model.addAttribute("posts", buildPosts(pageable));
final Template template = freeMarker.getConfiguration().getTemplate("common/web/rss.ftl"); final Template template = freeMarker.getConfiguration().getTemplate("common/web/rss.ftl");
return FreeMarkerTemplateUtils.processTemplateIntoString(template, model); return FreeMarkerTemplateUtils.processTemplateIntoString(template, model);

View File

@ -4,33 +4,26 @@ import cc.ryanc.halo.exception.BadRequestException;
import cc.ryanc.halo.model.entity.*; import cc.ryanc.halo.model.entity.*;
import cc.ryanc.halo.model.enums.AttachOrigin; import cc.ryanc.halo.model.enums.AttachOrigin;
import cc.ryanc.halo.model.enums.BlogProperties; import cc.ryanc.halo.model.enums.BlogProperties;
import cc.ryanc.halo.model.enums.CommentStatus;
import cc.ryanc.halo.model.enums.PostStatus;
import cc.ryanc.halo.model.params.InstallParam; import cc.ryanc.halo.model.params.InstallParam;
import cc.ryanc.halo.model.support.BaseResponse; import cc.ryanc.halo.model.support.BaseResponse;
import cc.ryanc.halo.model.support.JsonResult;
import cc.ryanc.halo.service.*; import cc.ryanc.halo.service.*;
import cc.ryanc.halo.utils.MarkdownUtils;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.BCrypt; import cn.hutool.crypto.digest.BCrypt;
import freemarker.template.Configuration; import freemarker.template.Configuration;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid; import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import static cc.ryanc.halo.model.support.HaloConst.DEFAULT_THEME_NAME; import static cc.ryanc.halo.model.support.HaloConst.DEFAULT_THEME_NAME;
import static cc.ryanc.halo.model.support.HaloConst.OPTIONS;
/** /**
* Installation controller. * Installation controller.
@ -82,7 +75,7 @@ public class InstallController {
@GetMapping @GetMapping
public String install(Model model) { public String install(Model model) {
try { try {
if (StrUtil.equals("true", OPTIONS.get("is_install"))) { if (StrUtil.equals(Boolean.TRUE.toString(), optionService.getByProperty(BlogProperties.IS_INSTALL).orElse(Boolean.FALSE.toString()))) {
model.addAttribute("isInstall", true); model.addAttribute("isInstall", true);
} else { } else {
model.addAttribute("isInstall", false); model.addAttribute("isInstall", false);
@ -98,7 +91,7 @@ public class InstallController {
public BaseResponse<?> installBlog(@Valid InstallParam installParam) { public BaseResponse<?> installBlog(@Valid InstallParam installParam) {
// TODO Install blog. // TODO Install blog.
// Check is installed // Check is installed
boolean isInstalled = Boolean.parseBoolean(OPTIONS.getOrDefault(BlogProperties.IS_INSTALL, "false")); boolean isInstalled = Boolean.parseBoolean(optionService.getByProperty(BlogProperties.IS_INSTALL).orElse(Boolean.FALSE.toString()));
if (isInstalled) { if (isInstalled) {
throw new BadRequestException("该博客已初始化,不能再次安装!"); throw new BadRequestException("该博客已初始化,不能再次安装!");
@ -194,114 +187,114 @@ public class InstallController {
optionService.saveProperties(properties, "system"); optionService.saveProperties(properties, "system");
} }
/** // /**
* Do install // * Do install
* // *
* @param blogLocale language // * @param blogLocale language
* @param blogTitle blog title // * @param blogTitle blog title
* @param blogUrl blog url // * @param blogUrl blog url
* @param userName user name // * @param userName user name
* @param nickName nick name // * @param nickName nick name
* @param userEmail user email // * @param userEmail user email
* @param userPwd user password // * @param userPwd user password
* @param request request // * @param request request
* @return JsonResult // * @return JsonResult
*/ // */
@PostMapping(value = "/do") // @PostMapping(value = "/do")
@ResponseBody // @ResponseBody
@Deprecated // @Deprecated
public JsonResult doInstall(@RequestParam("blogLocale") String blogLocale, // public JsonResult doInstall(@RequestParam("blogLocale") String blogLocale,
@RequestParam("blogTitle") String blogTitle, // @RequestParam("blogTitle") String blogTitle,
@RequestParam("blogUrl") String blogUrl, // @RequestParam("blogUrl") String blogUrl,
@RequestParam("userName") String userName, // @RequestParam("userName") String userName,
@RequestParam("userDisplayName") String nickName, // @RequestParam("userDisplayName") String nickName,
@RequestParam("userEmail") String userEmail, // @RequestParam("userEmail") String userEmail,
@RequestParam("userPwd") String userPwd, // @RequestParam("userPwd") String userPwd,
HttpServletRequest request) { // HttpServletRequest request) {
try { // try {
if (StrUtil.equals("true", OPTIONS.get("is_install"))) { // if (StrUtil.equals("true", OPTIONS.get("is_install"))) {
return new JsonResult(0, "该博客已初始化,不能再次安装!"); // return new JsonResult(0, "该博客已初始化,不能再次安装!");
} // }
// Create new user // // Create new user
final User user = new User(); // final User user = new User();
user.setUsername(userName); // user.setUsername(userName);
user.setNickname(StrUtil.isBlank(nickName) ? userName : nickName); // user.setNickname(StrUtil.isBlank(nickName) ? userName : nickName);
user.setEmail(userEmail); // user.setEmail(userEmail);
user.setPassword(SecureUtil.md5(userPwd)); // user.setPassword(SecureUtil.md5(userPwd));
userService.create(user); // userService.create(user);
//
//默认分类 // //默认分类
Category category = new Category(); // Category category = new Category();
category.setName("未分类"); // category.setName("未分类");
category.setSlugName("default"); // category.setSlugName("default");
category.setDescription("未分类"); // category.setDescription("未分类");
category = categoryService.create(category); // category = categoryService.create(category);
//
//第一篇文章 // //第一篇文章
final Post post = new Post(); // final Post post = new Post();
final List<Category> categories = new ArrayList<>(1); // final List<Category> categories = new ArrayList<>(1);
categories.add(category); // categories.add(category);
post.setTitle("Hello Halo!"); // post.setTitle("Hello Halo!");
post.setOriginalContent("# Hello Halo!\n" + // post.setOriginalContent("# Hello Halo!\n" +
"欢迎使用Halo进行创作删除这篇文章后赶紧开始吧。"); // "欢迎使用Halo进行创作删除这篇文章后赶紧开始吧。");
post.setFormatContent(MarkdownUtils.renderMarkdown(post.getOriginalContent())); // post.setFormatContent(MarkdownUtils.renderMarkdown(post.getOriginalContent()));
post.setSummary("欢迎使用Halo进行创作删除这篇文章后赶紧开始吧。"); // post.setSummary("欢迎使用Halo进行创作删除这篇文章后赶紧开始吧。");
post.setStatus(PostStatus.PUBLISHED); // post.setStatus(PostStatus.PUBLISHED);
post.setUrl("hello-halo"); // post.setUrl("hello-halo");
post.setDisallowComment(true); // post.setDisallowComment(true);
post.setThumbnail("/static/halo-frontend/images/thumbnail/thumbnail-" + RandomUtil.randomInt(1, 11) + ".jpg"); // post.setThumbnail("/static/halo-frontend/images/thumbnail/thumbnail-" + RandomUtil.randomInt(1, 11) + ".jpg");
postService.create(post); // postService.create(post);
//
//第一个评论 // //第一个评论
final Comment comment = new Comment(); // final Comment comment = new Comment();
comment.setAuthor("ruibaby"); // comment.setAuthor("ruibaby");
comment.setEmail("i@ryanc.cc"); // comment.setEmail("i@ryanc.cc");
comment.setAuthorUrl("https://ryanc.cc"); // comment.setAuthorUrl("https://ryanc.cc");
comment.setIpAddress("127.0.0.1"); // comment.setIpAddress("127.0.0.1");
comment.setGavatarMd5(SecureUtil.md5("i@ryanc.cc")); // comment.setGavatarMd5(SecureUtil.md5("i@ryanc.cc"));
comment.setContent("欢迎,欢迎!"); // comment.setContent("欢迎,欢迎!");
comment.setStatus(CommentStatus.PUBLISHED); // comment.setStatus(CommentStatus.PUBLISHED);
comment.setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36"); // comment.setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36");
comment.setIsAdmin(false); // comment.setIsAdmin(false);
commentService.create(comment); // commentService.create(comment);
//
final Map<BlogProperties, Object> options = new HashMap<>(); // final Map<BlogProperties, Object> options = new HashMap<>();
// options.put(BlogProperties.IS_INSTALL, TrueFalseEnum.TRUE.getDesc()); //// options.put(BlogProperties.IS_INSTALL, TrueFalseEnum.TRUE.getDesc());
// options.put(BlogProperties.BLOG_LOCALE, blogLocale); //// options.put(BlogProperties.BLOG_LOCALE, blogLocale);
// options.put(BlogProperties.BLOG_TITLE, blogTitle); //// options.put(BlogProperties.BLOG_TITLE, blogTitle);
// options.put(BlogProperties.BLOG_URL, blogUrl); //// options.put(BlogProperties.BLOG_URL, blogUrl);
// options.put(BlogProperties.THEME, "anatole"); //// options.put(BlogProperties.THEME, "anatole");
// options.put(BlogProperties.BLOG_START, DateUtil.format(DateUtil.date(), "yyyy-MM-dd")); //// options.put(BlogProperties.BLOG_START, DateUtil.format(DateUtil.date(), "yyyy-MM-dd"));
// options.put(BlogProperties.SMTP_EMAIL_ENABLE, TrueFalseEnum.FALSE.getDesc()); //// options.put(BlogProperties.SMTP_EMAIL_ENABLE, TrueFalseEnum.FALSE.getDesc());
// options.put(BlogProperties.NEW_COMMENT_NOTICE, TrueFalseEnum.FALSE.getDesc()); //// options.put(BlogProperties.NEW_COMMENT_NOTICE, TrueFalseEnum.FALSE.getDesc());
// options.put(BlogProperties.COMMENT_PASS_NOTICE, TrueFalseEnum.FALSE.getDesc()); //// options.put(BlogProperties.COMMENT_PASS_NOTICE, TrueFalseEnum.FALSE.getDesc());
// options.put(BlogProperties.COMMENT_REPLY_NOTICE, TrueFalseEnum.FALSE.getDesc()); //// options.put(BlogProperties.COMMENT_REPLY_NOTICE, TrueFalseEnum.FALSE.getDesc());
// options.put(BlogProperties.ATTACH_LOC, AttachLocationEnum.SERVER.getDesc()); //// options.put(BlogProperties.ATTACH_LOC, AttachLocationEnum.SERVER.getDesc());
// optionService.saveOptions(options); //// optionService.saveOptions(options);
//
//更新日志 // //更新日志
// logsService.save(LogsRecord.INSTALL, "安装成功欢迎使用Halo。", request); //// logsService.save(LogsRecord.INSTALL, "安装成功欢迎使用Halo。", request);
//
final Menu menuIndex = new Menu(); // final Menu menuIndex = new Menu();
menuIndex.setName("首页"); // menuIndex.setName("首页");
menuIndex.setUrl("/"); // menuIndex.setUrl("/");
menuIndex.setSort(1); // menuIndex.setSort(1);
menuService.create(menuIndex); // menuService.create(menuIndex);
//
final Menu menuArchive = new Menu(); // final Menu menuArchive = new Menu();
menuArchive.setName("归档"); // menuArchive.setName("归档");
menuArchive.setUrl("/archives"); // menuArchive.setUrl("/archives");
menuArchive.setSort(2); // menuArchive.setSort(2);
menuService.create(menuArchive); // menuService.create(menuArchive);
//
OPTIONS.clear(); // OPTIONS.clear();
OPTIONS.putAll(optionService.listOptions()); // OPTIONS.putAll(optionService.listOptions());
configuration.setSharedVariable("options", OPTIONS); // configuration.setSharedVariable("options", OPTIONS);
// configuration.setSharedVariable("user", userService.findUser()); //// configuration.setSharedVariable("user", userService.findUser());
} catch (Exception e) { // } catch (Exception e) {
log.error(e.getMessage()); // log.error(e.getMessage());
return new JsonResult(0, e.getMessage()); // return new JsonResult(0, e.getMessage());
} // }
return new JsonResult(1, "安装成功!"); // return new JsonResult(1, "安装成功!");
} // }
} }

View File

@ -1,10 +0,0 @@
package cc.ryanc.halo.utils;
/**
* Reflection utils test.
*
* @author johnniang
*/
public class ReflectionUtilsTest {
}