Commit a2102828 authored by Liang Ding's avatar Liang Ding

Merge remote-tracking branch 'refs/remotes/origin/2.2.0-dev'

parents 786c176b b73917dc
......@@ -43,14 +43,13 @@ import java.util.Date;
/**
* This listener is responsible for sending article to B3log Rhythm.
*
* <p>
* The B3log Rhythm article update interface: http://rhythm.b3log.org/article (POST).
* </p>
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @author ArmstrongCN
* @version 1.0.2.8, Nov 20, 2015
* @version 1.0.2.9, Jun 26, 2017
* @since 0.3.1
*/
public final class ArticleSender extends AbstractEventListener<JSONObject> {
......@@ -58,12 +57,7 @@ public final class ArticleSender extends AbstractEventListener<JSONObject> {
/**
* Logger.
*/
private static final Logger LOGGER = Logger.getLogger(ArticleSender.class.getName());
/**
* URL fetch service.
*/
private final URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();
private static final Logger LOGGER = Logger.getLogger(ArticleSender.class);
/**
* URL of adding article to Rhythm.
......@@ -79,6 +73,11 @@ public final class ArticleSender extends AbstractEventListener<JSONObject> {
}
}
/**
* URL fetch service.
*/
private final URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();
@Override
public void action(final Event<JSONObject> event) throws EventException {
final JSONObject data = event.getData();
......@@ -87,7 +86,6 @@ public final class ArticleSender extends AbstractEventListener<JSONObject> {
event.getType(), data, ArticleSender.class.getName());
try {
final JSONObject originalArticle = data.getJSONObject(Article.ARTICLE);
if (!originalArticle.getBoolean(Article.ARTICLE_IS_PUBLISHED)) {
LOGGER.log(Level.DEBUG, "Ignores post article[title={0}] to Rhythm", originalArticle.getString(Article.ARTICLE_TITLE));
......@@ -98,7 +96,6 @@ public final class ArticleSender extends AbstractEventListener<JSONObject> {
final PreferenceQueryService preferenceQueryService = beanManager.getReference(PreferenceQueryService.class);
final JSONObject preference = preferenceQueryService.getPreference();
if (null == preference) {
throw new EventException("Not found preference");
}
......@@ -108,7 +105,7 @@ public final class ArticleSender extends AbstractEventListener<JSONObject> {
}
if (Latkes.getServePath().contains("localhost")) {
LOGGER.log(Level.INFO, "Solo runs on local server, so should not send this article[id={0}, title={1}] to Rhythm",
LOGGER.log(Level.TRACE, "Solo runs on local server, so should not send this article[id={0}, title={1}] to Rhythm",
originalArticle.getString(Keys.OBJECT_ID), originalArticle.getString(Article.ARTICLE_TITLE));
return;
}
......
......@@ -43,13 +43,12 @@ import java.util.Date;
/**
* This listener is responsible for updating article to B3log Rhythm.
*
* <p>
* The B3log Rhythm article update interface: http://rhythm.b3log.org/article (PUT).
* </p>
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.2, Nov 20, 2015
* @version 1.0.0.3, Jun 26, 2017
* @since 0.6.0
*/
public final class ArticleUpdater extends AbstractEventListener<JSONObject> {
......@@ -57,12 +56,7 @@ public final class ArticleUpdater extends AbstractEventListener<JSONObject> {
/**
* Logger.
*/
private static final Logger LOGGER = Logger.getLogger(ArticleUpdater.class.getName());
/**
* URL fetch service.
*/
private final URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();
private static final Logger LOGGER = Logger.getLogger(ArticleUpdater.class);
/**
* URL of updating article to Rhythm.
......@@ -78,6 +72,11 @@ public final class ArticleUpdater extends AbstractEventListener<JSONObject> {
}
}
/**
* URL fetch service.
*/
private final URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();
@Override
public void action(final Event<JSONObject> event) throws EventException {
final JSONObject data = event.getData();
......@@ -86,7 +85,6 @@ public final class ArticleUpdater extends AbstractEventListener<JSONObject> {
event.getType(), data, ArticleUpdater.class.getName());
try {
final JSONObject originalArticle = data.getJSONObject(Article.ARTICLE);
if (!originalArticle.getBoolean(Article.ARTICLE_IS_PUBLISHED)) {
LOGGER.log(Level.DEBUG, "Ignores post article[title={0}] to Rhythm", originalArticle.getString(Article.ARTICLE_TITLE));
......@@ -97,7 +95,6 @@ public final class ArticleUpdater extends AbstractEventListener<JSONObject> {
final PreferenceQueryService preferenceQueryService = beanManager.getReference(PreferenceQueryService.class);
final JSONObject preference = preferenceQueryService.getPreference();
if (null == preference) {
throw new EventException("Not found preference");
}
......@@ -107,7 +104,7 @@ public final class ArticleUpdater extends AbstractEventListener<JSONObject> {
}
if (Latkes.getServePath().contains("localhost")) {
LOGGER.log(Level.INFO, "Solo runs on local server, so should not send this article[id={0}, title={1}] to Rhythm",
LOGGER.log(Level.TRACE, "Solo runs on local server, so should not send this article[id={0}, title={1}] to Rhythm",
originalArticle.getString(Keys.OBJECT_ID), originalArticle.getString(Article.ARTICLE_TITLE));
return;
}
......
......@@ -8,6 +8,7 @@ import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger;
import org.b3log.latke.model.User;
import org.b3log.latke.service.annotation.Service;
import org.b3log.latke.util.Strings;
import org.b3log.solo.SoloServletListener;
import org.b3log.solo.model.Article;
import org.json.JSONObject;
......@@ -30,7 +31,7 @@ public class ImportService {
/**
* Logger.
*/
private static final Logger LOGGER = Logger.getLogger(UpgradeService.class);
private static final Logger LOGGER = Logger.getLogger(ImportService.class);
/**
* Default tag.
......@@ -50,45 +51,66 @@ public class ImportService {
private UserQueryService userQueryService;
public void importMarkdowns() {
final ServletContext servletContext = SoloServletListener.getServletContext();
final String markdownsPath = servletContext.getRealPath("markdowns");
LOGGER.debug("Import directory [" + markdownsPath + "]");
new Thread(() -> {
final ServletContext servletContext = SoloServletListener.getServletContext();
final String markdownsPath = servletContext.getRealPath("markdowns");
LOGGER.debug("Import directory [" + markdownsPath + "]");
JSONObject admin;
try {
admin = userQueryService.getAdmin();
} catch (final Exception e) {
return;
}
if (null == admin) { // Not init yet
return;
}
final String adminEmail = admin.optString(User.USER_EMAIL);
JSONObject admin;
try {
admin = userQueryService.getAdmin();
} catch (final Exception e) {
return;
}
final Collection<File> mds = FileUtils.listFiles(new File(markdownsPath), new String[]{"md"}, true);
for (final File md : mds) {
final String fileName = md.getName();
if (StringUtils.equalsIgnoreCase(fileName, "README.md")) {
continue;
if (null == admin) { // Not init yet
return;
}
try {
final String fileContent = FileUtils.readFileToString(md, "UTF-8");
final JSONObject article = parseArticle(fileName, fileContent);
article.put(Article.ARTICLE_AUTHOR_EMAIL, adminEmail);
final String adminEmail = admin.optString(User.USER_EMAIL);
int succCnt = 0, failCnt = 0;
final Set<String> failSet = new TreeSet<>();
final Collection<File> mds = FileUtils.listFiles(new File(markdownsPath), new String[]{"md"}, true);
for (final File md : mds) {
final String fileName = md.getName();
if (StringUtils.equalsIgnoreCase(fileName, "README.md")) {
continue;
}
try {
final String fileContent = FileUtils.readFileToString(md, "UTF-8");
final JSONObject article = parseArticle(fileName, fileContent);
article.put(Article.ARTICLE_AUTHOR_EMAIL, adminEmail);
final JSONObject request = new JSONObject();
request.put(Article.ARTICLE, article);
final String id = articleMgmtService.addArticle(request);
FileUtils.moveFile(md, new File(md.getPath() + "." + id));
LOGGER.info("Imported article [" + article.optString(Article.ARTICLE_TITLE) + "]");
succCnt++;
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Import file [" + fileName + "] failed", e);
failCnt++;
failSet.add(fileName);
}
}
final JSONObject request = new JSONObject();
request.put(Article.ARTICLE, article);
final StringBuilder logBuilder = new StringBuilder();
logBuilder.append("[").append(succCnt).append("] imported, [").append(failCnt).append("] failed");
if (failCnt > 0) {
logBuilder.append(": ").append(Strings.LINE_SEPARATOR);
final String id = articleMgmtService.addArticle(request);
FileUtils.moveFile(md, new File(md.getPath() + "." + id));
LOGGER.info("Imported article [" + article.optString(Article.ARTICLE_TITLE) + "]");
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Import file [" + fileName + "] failed", e);
for (final String fail : failSet) {
logBuilder.append(" ").append(fail).append(Strings.LINE_SEPARATOR);
}
} else {
logBuilder.append(" :p");
}
}
LOGGER.info(logBuilder.toString());
}).start();
}
private JSONObject parseArticle(final String fileName, final String fileContent) {
......
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