Skip to content

Commit

Permalink
♻️ b3log#880
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Feb 27, 2019
1 parent 834c227 commit daea088
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 115 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/b3log/symphony/SymphonyServletListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void sessionDestroyed(final HttpSessionEvent httpSessionEvent) {
public void requestInitialized(final ServletRequestEvent servletRequestEvent) {
Locales.setLocale(Latkes.getLocale());

Sessions.setTemplateDir(Symphonys.get("skinDirName"));
Sessions.setTemplateDir(Symphonys.SKIN_DIR_NAME);
Sessions.setMobile(false);
Sessions.setAvatarViewMode(UserExt.USER_AVATAR_VIEW_MODE_C_ORIGINAL);

Expand Down Expand Up @@ -222,7 +222,7 @@ public void requestDestroyed(final ServletRequestEvent servletRequestEvent) {
if (null != isStaticObj && !(Boolean) isStaticObj) {
Stopwatchs.end();

final int threshold = Symphonys.getInt("performance.threshold");
final int threshold = Symphonys.PERFORMANCE_THRESHOLD;
if (0 < threshold) {
final long elapsed = Stopwatchs.getElapsed("Request initialized [" + request.getRequestURI() + "]");
if (elapsed >= threshold) {
Expand Down
11 changes: 3 additions & 8 deletions src/main/java/org/b3log/symphony/event/ArticleBaiduSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,13 @@ public class ArticleBaiduSender extends AbstractEventListener<JSONObject> {
*/
private static final Logger LOGGER = Logger.getLogger(ArticleBaiduSender.class);

/**
* Baidu data token.
*/
private static final String TOKEN = Symphonys.get("baidu.data.token");

/**
* Sends the specified URLs to Baidu.
*
* @param urls the specified URLs
*/
public static void sendToBaidu(final String... urls) {
if (Latkes.RuntimeMode.PRODUCTION != Latkes.getRuntimeMode() || StringUtils.isBlank(TOKEN)) {
if (Latkes.RuntimeMode.PRODUCTION != Latkes.getRuntimeMode() || StringUtils.isBlank(Symphonys.BAIDU_DATA_TOKEN)) {
return;
}

Expand All @@ -71,7 +66,7 @@ public static void sendToBaidu(final String... urls) {
Symphonys.EXECUTOR_SERVICE.submit(() -> {
try {
final String urlsStr = StringUtils.join(urls, "\n");
final HttpResponse response = HttpRequest.post("http://data.zz.baidu.com/urls?site=" + Latkes.getServerHost() + "&token=" + TOKEN).
final HttpResponse response = HttpRequest.post("http://data.zz.baidu.com/urls?site=" + Latkes.getServerHost() + "&token=" + Symphonys.BAIDU_DATA_TOKEN).
header(Common.USER_AGENT, "curl/7.12.1").
header("Host", "data.zz.baidu.com").
header("Content-Type", "text/plain").
Expand All @@ -89,7 +84,7 @@ public void action(final Event<JSONObject> event) {
final JSONObject data = event.getData();
LOGGER.log(Level.TRACE, "Processing an event [type={0}, data={1}]", event.getType(), data);

if (Latkes.RuntimeMode.PRODUCTION != Latkes.getRuntimeMode() || StringUtils.isBlank(TOKEN)) {
if (Latkes.RuntimeMode.PRODUCTION != Latkes.getRuntimeMode() || StringUtils.isBlank(Symphonys.BAIDU_DATA_TOKEN)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ public void action(final Event<JSONObject> event) {
return;
}

if (Symphonys.getBoolean("algolia.enabled")) {
if (Symphonys.ALGOLIA_ENABLED) {
searchMgmtService.updateAlgoliaDocument(JSONs.clone(article));
}

if (Symphonys.getBoolean("es.enabled")) {
if (Symphonys.ES_ENABLED) {
searchMgmtService.updateESDocument(JSONs.clone(article), Article.ARTICLE);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void showDomainArticles(final RequestContext context) {
final AbstractFreeMarkerRenderer renderer = new SkinRenderer(context, "domain-articles.ftl");
final Map<String, Object> dataModel = renderer.getDataModel();
final int pageNum = Paginator.getPage(request);
int pageSize = Symphonys.getInt("indexArticlesCnt");
int pageSize = Symphonys.ARTICLE_LIST_CNT;

final JSONObject user = Sessions.getUser();
if (null != user) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class Activity1A0001CollectValidation extends ProcessAdvice {

@Override
public void doAdvice(final RequestContext context) throws RequestProcessAdviceException {
if (Symphonys.getBoolean("activity1A0001Closed")) {
if (Symphonys.ACTIVITY_1A0001_CLOSED) {
throw new RequestProcessAdviceException(new JSONObject().put(Keys.MSG, langPropsService.get("activityClosedLabel")));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ public void initSym() {
LOGGER.info("Initialized admin user");

// Add tags
String tagTitle = Symphonys.get("systemAnnounce");
String tagTitle = Symphonys.SYS_ANNOUNCE_TAG;
String tagId = tagMgmtService.addTag(adminId, tagTitle);
JSONObject tag = tagRepository.get(tagId);
tag.put(Tag.TAG_URI, "announcement");
Expand Down
131 changes: 38 additions & 93 deletions src/main/java/org/b3log/symphony/util/Mails.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,61 +70,6 @@ public final class Mails {
*/
public static final String TEMPLATE_NAME_WEEKLY = "sym_weekly";

/**
* Mail channel.
*/
private static final String MAIL_CHANNEL = Symphonys.get("mail.channel");

/**
* SendCloud API user.
*/
private static final String SENDCLOUD_API_USER = Symphonys.get("mail.sendcloud.apiUser");

/**
* SendCloud API key.
*/
private static final String SENDCLOUD_API_KEY = Symphonys.get("mail.sendcloud.apiKey");

/**
* SendCloud from.
*/
private static final String SENDCLOUD_FROM = Symphonys.get("mail.sendcloud.from");

/**
* SendCloud batch API User.
*/
private static final String SENDCLOUD_BATCH_API_USER = Symphonys.get("mail.sendcloud.batch.apiUser");

/**
* SendCloud batch API key.
*/
private static final String SENDCLOUD_BATCH_API_KEY = Symphonys.get("mail.sendcloud.batch.apiKey");

/**
* SendCloud batch sender email.
*/
private static final String SENDCLOUD_BATCH_FROM = Symphonys.get("mail.sendcloud.batch.from");

/**
* Aliyun accesskey.
*/
private static final String ALIYUN_ACCESSKEY = Symphonys.get("mail.aliyun.accessKey");

/**
* Aliyun access secret.
*/
private static final String ALIYUN_ACCESSSECRET = Symphonys.get("mail.aliyun.accessSecret");

/**
* Aliyun from.
*/
private static final String ALIYUN_FROM = Symphonys.get("mail.aliyun.from");

/**
* Aliyun batch from.
*/
private static final String ALIYUN_BATCH_FROM = Symphonys.get("mail.aliyun.batch.from");

/**
* Template configuration.
*/
Expand All @@ -145,7 +90,7 @@ public final class Mails {
LOGGER.log(Level.ERROR, "Loads mail templates failed", e);
}

final String mailDomains = Symphonys.get("mail.channel.mailDomains");
final String mailDomains = Symphonys.MAIL_CHANNEL_MAIL_DOMAINS;
if (StringUtils.isNotBlank(mailDomains)) {
// aliyun:163.com,126.com;sendcloud:qq.com
final String[] channelMaps = StringUtils.split(mailDomains, ";");
Expand Down Expand Up @@ -179,14 +124,14 @@ private Mails() {
*/
public static void sendHTML(final String fromName, final String subject, final String toMail,
final String templateName, final Map<String, Object> dataModel) {
if ("sendcloud".equals(MAIL_CHANNEL)) {
if (StringUtils.isBlank(SENDCLOUD_API_USER) || StringUtils.isBlank(SENDCLOUD_API_KEY)) {
if ("sendcloud".equals(Symphonys.MAIL_CHANNEL)) {
if (StringUtils.isBlank(Symphonys.MAIL_SENDCLOUD_API_USER) || StringUtils.isBlank(Symphonys.MAIL_SENDCLOUD_API_KEY)) {
LOGGER.warn("Please configure [#### SendCloud Mail channel ####] section in symphony.properties for sending mail");

return;
}
} else if ("aliyun".equals(MAIL_CHANNEL)) {
if (StringUtils.isBlank(ALIYUN_ACCESSKEY) || StringUtils.isBlank(ALIYUN_ACCESSSECRET)) {
} else if ("aliyun".equals(Symphonys.MAIL_CHANNEL)) {
if (StringUtils.isBlank(Symphonys.MAIL_ALIYUN_AK) || StringUtils.isBlank(Symphonys.MAIL_ALIYUN_SK)) {
LOGGER.warn("Please configure [#### Aliyun Mail channel ####] section in symphony.properties for sending mail");

return;
Expand All @@ -211,10 +156,10 @@ public static void sendHTML(final String fromName, final String subject, final S
final String html = stringWriter.toString();

final String domain = StringUtils.substringAfter(toMail, "@");
final String channel = DOMAIN_CHANNEL.getOrDefault(domain, MAIL_CHANNEL);
final String channel = DOMAIN_CHANNEL.getOrDefault(domain, Symphonys.MAIL_CHANNEL);
switch (channel) {
case "aliyun":
aliSendHtml(ALIYUN_FROM, fromName, subject, toMail, html, ALIYUN_ACCESSKEY, ALIYUN_ACCESSSECRET);
aliSendHtml(Symphonys.MAIL_ALIYUN_FROM, fromName, subject, toMail, html, Symphonys.MAIL_ALIYUN_AK, Symphonys.MAIL_ALIYUN_SK);

return;
case "local":
Expand All @@ -223,9 +168,9 @@ public static void sendHTML(final String fromName, final String subject, final S
return;
case "sendcloud":
final Map<String, Object> formData = new HashMap<>();
formData.put("apiUser", SENDCLOUD_API_USER);
formData.put("apiKey", SENDCLOUD_API_KEY);
formData.put("from", SENDCLOUD_FROM);
formData.put("apiUser", Symphonys.MAIL_SENDCLOUD_API_USER);
formData.put("apiKey", Symphonys.MAIL_SENDCLOUD_BATCH_API_KEY);
formData.put("from", Symphonys.MAIL_SENDCLOUD_FROM);
formData.put("fromName", fromName);
formData.put("subject", subject);
formData.put("to", toMail);
Expand Down Expand Up @@ -257,14 +202,14 @@ public static void sendHTML(final String fromName, final String subject, final S
*/
public static void batchSendHTML(final String fromName, final String subject, final List<String> toMails,
final String templateName, final Map<String, Object> dataModel) {
if ("sendcloud".equals(MAIL_CHANNEL)) {
if (StringUtils.isBlank(SENDCLOUD_BATCH_API_USER) || StringUtils.isBlank(SENDCLOUD_BATCH_API_KEY)) {
if ("sendcloud".equals(Symphonys.MAIL_CHANNEL)) {
if (StringUtils.isBlank(Symphonys.MAIL_SENDCLOUD_BATCH_API_USER) || StringUtils.isBlank(Symphonys.MAIL_SENDCLOUD_BATCH_API_KEY)) {
LOGGER.warn("Please configure [#### SendCloud Mail channel ####] section in symphony.properties for sending mail");

return;
}
} else if ("aliyun".equals(MAIL_CHANNEL)) {
if (StringUtils.isBlank(ALIYUN_ACCESSKEY) || StringUtils.isBlank(ALIYUN_ACCESSSECRET)) {
} else if ("aliyun".equals(Symphonys.MAIL_CHANNEL)) {
if (StringUtils.isBlank(Symphonys.MAIL_ALIYUN_AK) || StringUtils.isBlank(Symphonys.MAIL_ALIYUN_SK)) {
LOGGER.warn("Please configure [#### Aliyun Mail channel ####] section in symphony.properties for sending mail");

return;
Expand All @@ -284,9 +229,9 @@ public static void batchSendHTML(final String fromName, final String subject, fi
try {
final Map<String, Object> formData = new HashMap<>();

formData.put("apiUser", SENDCLOUD_BATCH_API_USER);
formData.put("apiKey", SENDCLOUD_BATCH_API_KEY);
formData.put("from", SENDCLOUD_BATCH_FROM);
formData.put("apiUser", Symphonys.MAIL_SENDCLOUD_BATCH_API_USER);
formData.put("apiKey", Symphonys.MAIL_SENDCLOUD_BATCH_API_KEY);
formData.put("from", Symphonys.MAIL_SENDCLOUD_BATCH_FROM);
formData.put("fromName", fromName);
formData.put("subject", subject);
formData.put("templateInvokeName", templateName);
Expand All @@ -298,7 +243,7 @@ public static void batchSendHTML(final String fromName, final String subject, fi
final String html = stringWriter.toString();

// Creates or updates the SendCloud email template
if ("sendcloud".equals(MAIL_CHANNEL)) {
if ("sendcloud".equals(Symphonys.MAIL_CHANNEL)) {
refreshWeeklyTemplate(html);
}

Expand All @@ -312,12 +257,12 @@ public static void batchSendHTML(final String fromName, final String subject, fi
index++;

if (batch.size() > 99) {
if ("aliyun".equals(MAIL_CHANNEL)) {
if ("aliyun".equals(Symphonys.MAIL_CHANNEL)) {
final String toMail = getStringToMailByList(batch);
aliSendHtml(ALIYUN_BATCH_FROM, fromName, subject, toMail, html, ALIYUN_ACCESSKEY, ALIYUN_ACCESSSECRET);
aliSendHtml(Symphonys.MAIL_ALIYUN_BATCH_FROM, fromName, subject, toMail, html, Symphonys.MAIL_ALIYUN_AK, Symphonys.MAIL_ALIYUN_SK);

LOGGER.info("Sent [" + batch.size() + "] mails");
} else if ("local".equals(MAIL_CHANNEL.toLowerCase())) {
} else if ("local".equals(Symphonys.MAIL_CHANNEL.toLowerCase())) {
MailSender.getInstance().sendHTML(fromName, subject, batch, html);
} else {
try {
Expand All @@ -343,11 +288,11 @@ public static void batchSendHTML(final String fromName, final String subject, fi
}

if (!batch.isEmpty()) { // Process remains
if ("aliyun".equals(MAIL_CHANNEL)) {
if ("aliyun".equals(Symphonys.MAIL_CHANNEL)) {
final String toMail = getStringToMailByList(batch);
aliSendHtml(ALIYUN_BATCH_FROM, fromName, subject, toMail, html, ALIYUN_ACCESSKEY, ALIYUN_ACCESSSECRET);
aliSendHtml(Symphonys.MAIL_ALIYUN_BATCH_FROM, fromName, subject, toMail, html, Symphonys.MAIL_ALIYUN_AK, Symphonys.MAIL_ALIYUN_SK);
LOGGER.info("Sent [" + batch.size() + "] mails");
} else if ("local".equals(MAIL_CHANNEL.toLowerCase())) {
} else if ("local".equals(Symphonys.MAIL_CHANNEL.toLowerCase())) {
MailSender.getInstance().sendHTML(fromName, subject, batch, html);
} else {
try {
Expand Down Expand Up @@ -448,8 +393,8 @@ private static String getStringToMailByList(final List<String> toMails) {

private static void refreshWeeklyTemplate(final String html) {
final Map<String, Object> addData = new HashMap<>();
addData.put("apiUser", SENDCLOUD_BATCH_API_USER);
addData.put("apiKey", SENDCLOUD_BATCH_API_KEY);
addData.put("apiUser", Symphonys.MAIL_SENDCLOUD_BATCH_API_USER);
addData.put("apiKey", Symphonys.MAIL_SENDCLOUD_BATCH_API_KEY);
addData.put("invokeName", TEMPLATE_NAME_WEEKLY);
addData.put("name", "Weekly Newsletter");
addData.put("subject", "Weekly Newsletter");
Expand All @@ -460,8 +405,8 @@ private static void refreshWeeklyTemplate(final String html) {
connectionTimeout(5000).timeout(30000).send().close();

final Map<String, Object> updateData = new HashMap<>();
updateData.put("apiUser", SENDCLOUD_BATCH_API_USER);
updateData.put("apiKey", SENDCLOUD_BATCH_API_KEY);
updateData.put("apiUser", Symphonys.MAIL_SENDCLOUD_BATCH_API_USER);
updateData.put("apiKey", Symphonys.MAIL_SENDCLOUD_BATCH_API_KEY);
updateData.put("invokeName", TEMPLATE_NAME_WEEKLY);

updateData.put("html", html);
Expand All @@ -487,20 +432,20 @@ private static void refreshWeeklyTemplate(final String html) {
*/
final class MailSender implements java.io.Serializable {

public static final String sender = Symphonys.get("mail.local.smtp.sender");
public static final String username = Symphonys.get("mail.local.smtp.username");
public static final String password = Symphonys.get("mail.local.smtp.passsword");
public static final String sender = Symphonys.MAIL_LOCAL_SMTP_SENDER;
public static final String username = Symphonys.MAIL_LOCAL_SMTP_USERNAME;
public static final String password = Symphonys.MAIL_LOCAL_SMTP_PASSWORD;

private static final long serialVersionUID = -1000794424345267933L;
private static final String CHARSET = "text/html;charset=UTF-8";
private static final Logger LOGGER = Logger.getLogger(Markdowns.class);
private static final boolean is_debug = Boolean.valueOf(Symphonys.get("mail.local.isdebug"));
private static final String mail_transport_protocol = Symphonys.get("mail.local.transport.protocol");
private static final String mail_host = Symphonys.get("mail.local.host");
private static final String mail_port = Symphonys.get("mail.local.port");
private static final String mail_smtp_auth = Symphonys.get("mail.local.smtp.auth");
private static final String mail_smtp_ssl = Symphonys.get("mail.local.smtp.ssl");
private static final String mail_smtp_starttls_enable = Symphonys.get("mail.local.smtp.starttls.enable");
private static final boolean is_debug = Boolean.valueOf(Symphonys.MAIL_LOCAL_ISDEBUG);
private static final String mail_transport_protocol = Symphonys.MAIL_LOCAL_TRANSPORT_PROTOCOL;
private static final String mail_host = Symphonys.MAIL_LOCAL_HOST;
private static final String mail_port = Symphonys.MAIL_LOCAL_PORT;
private static final String mail_smtp_auth = Symphonys.MAIL_LOCAL_SMTP_AUTH;
private static final String mail_smtp_ssl = Symphonys.MAIL_LOCAL_SMTP_SSL;
private static final String mail_smtp_starttls_enable = Symphonys.MAIL_LOCAL_SMTP_STARTTLS;
private static MailSender mailSender;
private static Properties prop = new Properties();

Expand Down
9 changes: 2 additions & 7 deletions src/main/java/org/b3log/symphony/util/Markdowns.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,6 @@ public final class Markdowns {
*/
private static final Map<String, JSONObject> MD_CACHE = new ConcurrentHashMap<>();

/**
* Markdown to HTML timeout.
*/
private static final int MD_TIMEOUT = Symphonys.getInt("markdown.timeout");

/**
* Marked engine serve path.
*/
Expand Down Expand Up @@ -352,7 +347,7 @@ public void tail(org.jsoup.nodes.Node node, int depth) {
return;
}

if (StringUtils.startsWithAny(src, new String[]{Latkes.getServePath(), Symphonys.get("qiniu.domain")})
if (StringUtils.startsWithAny(src, new String[]{Latkes.getServePath(), Symphonys.UPLOAD_QINIU_DOMAIN})
|| StringUtils.endsWithIgnoreCase(src, ".mov")) {
return;
}
Expand All @@ -377,7 +372,7 @@ public void tail(org.jsoup.nodes.Node node, int depth) {
try {
final Future<String> future = pool.submit(call);

return future.get(MD_TIMEOUT, TimeUnit.MILLISECONDS);
return future.get(Symphonys.MARKDOWN_TIMEOUT, TimeUnit.MILLISECONDS);
} catch (final TimeoutException e) {
LOGGER.log(Level.ERROR, "Markdown timeout [md=" + StringUtils.substring(markdownText, 0, 256) + "]");
Callstacks.printCallstack(Level.ERROR, new String[]{"org.b3log"}, null);
Expand Down

0 comments on commit daea088

Please sign in to comment.