PageRenderTime 71ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/b3log-solo/core/src/main/java/org/b3log/solo/event/rhythm/ArticleSender.java

http://b3log-solo.googlecode.com/
Java | 140 lines | 86 code | 14 blank | 40 comment | 4 complexity | 911988ea140e773d47579372bb11aa26 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1
  1. /*
  2. * Copyright (c) 2009, 2010, 2011, 2012, B3log Team
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.b3log.solo.event.rhythm;
  17. import java.net.MalformedURLException;
  18. import java.net.URL;
  19. import java.util.Date;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import org.b3log.latke.Keys;
  23. import org.b3log.latke.event.AbstractEventListener;
  24. import org.b3log.latke.event.Event;
  25. import org.b3log.latke.event.EventException;
  26. import org.b3log.latke.servlet.HTTPRequestMethod;
  27. import org.b3log.latke.urlfetch.HTTPRequest;
  28. import org.b3log.latke.urlfetch.URLFetchService;
  29. import org.b3log.latke.urlfetch.URLFetchServiceFactory;
  30. import org.b3log.solo.SoloServletListener;
  31. import org.b3log.solo.event.EventTypes;
  32. import org.b3log.solo.model.Article;
  33. import org.b3log.solo.model.Common;
  34. import org.b3log.solo.model.Preference;
  35. import org.b3log.solo.service.PreferenceQueryService;
  36. import org.json.JSONObject;
  37. /**
  38. * This listener is responsible for sending article to B3log Rhythm.
  39. *
  40. * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
  41. * @version 1.0.1.9, May 4, 2012
  42. * @since 0.3.1
  43. */
  44. public final class ArticleSender extends AbstractEventListener<JSONObject> {
  45. /**
  46. * Logger.
  47. */
  48. private static final Logger LOGGER = Logger.getLogger(ArticleSender.class.getName());
  49. /**
  50. * URL fetch service.
  51. */
  52. private final URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();
  53. /**
  54. * Preference query service.
  55. */
  56. private PreferenceQueryService preferenceQueryService = PreferenceQueryService.getInstance();
  57. /**
  58. * URL of adding article to Rhythm.
  59. */
  60. private static final URL ADD_ARTICLE_URL;
  61. static {
  62. try {
  63. ADD_ARTICLE_URL = new URL(SoloServletListener.B3LOG_RHYTHM_ADDRESS + "/add-article.do");
  64. } catch (final MalformedURLException e) {
  65. LOGGER.log(Level.SEVERE, "Creates remote service address[rhythm add article] error!");
  66. throw new IllegalStateException(e);
  67. }
  68. }
  69. @Override
  70. public void action(final Event<JSONObject> event) throws EventException {
  71. final JSONObject data = event.getData();
  72. LOGGER.log(Level.FINER, "Processing an event[type={0}, data={1}] in listener[className={2}]",
  73. new Object[]{event.getType(), data, ArticleSender.class.getName()});
  74. try {
  75. final JSONObject originalArticle = data.getJSONObject(Article.ARTICLE);
  76. if (!originalArticle.getBoolean(Article.ARTICLE_IS_PUBLISHED)) {
  77. LOGGER.log(Level.FINER, "Ignores post article[title={0}] to Rhythm", originalArticle.getString(Article.ARTICLE_TITLE));
  78. return;
  79. }
  80. final JSONObject preference = preferenceQueryService.getPreference();
  81. if (null == preference) {
  82. throw new EventException("Not found preference");
  83. }
  84. final String blogHost = preference.getString(Preference.BLOG_HOST).toLowerCase();
  85. if (blogHost.contains("localhost")) {
  86. LOGGER.log(Level.INFO, "Blog Solo runs on local server, so should not send this article[id={0}, title={1}] to Rhythm",
  87. new Object[]{originalArticle.getString(Keys.OBJECT_ID), originalArticle.getString(Article.ARTICLE_TITLE)});
  88. return;
  89. }
  90. final HTTPRequest httpRequest = new HTTPRequest();
  91. httpRequest.setURL(ADD_ARTICLE_URL);
  92. httpRequest.setRequestMethod(HTTPRequestMethod.POST);
  93. final JSONObject requestJSONObject = new JSONObject();
  94. final JSONObject article = new JSONObject();
  95. article.put(Keys.OBJECT_ID, originalArticle.getString(Keys.OBJECT_ID));
  96. article.put(Article.ARTICLE_TITLE, originalArticle.getString(Article.ARTICLE_TITLE));
  97. article.put(Article.ARTICLE_PERMALINK, originalArticle.getString(Article.ARTICLE_PERMALINK));
  98. article.put(Article.ARTICLE_TAGS_REF, originalArticle.getString(Article.ARTICLE_TAGS_REF));
  99. article.put(Article.ARTICLE_AUTHOR_EMAIL, originalArticle.getString(Article.ARTICLE_AUTHOR_EMAIL));
  100. article.put(Article.ARTICLE_CONTENT, originalArticle.getString(Article.ARTICLE_CONTENT));
  101. article.put(Article.ARTICLE_CREATE_DATE, ((Date) originalArticle.get(Article.ARTICLE_CREATE_DATE)).getTime());
  102. article.put(Common.POST_TO_COMMUNITY, originalArticle.getBoolean(Common.POST_TO_COMMUNITY));
  103. // Removes this property avoid to persist
  104. originalArticle.remove(Common.POST_TO_COMMUNITY);
  105. requestJSONObject.put(Article.ARTICLE, article);
  106. requestJSONObject.put(Common.BLOG_VERSION, SoloServletListener.VERSION);
  107. requestJSONObject.put(Common.BLOG, "B3log Solo");
  108. requestJSONObject.put(Preference.BLOG_TITLE, preference.getString(Preference.BLOG_TITLE));
  109. requestJSONObject.put(Preference.BLOG_HOST, blogHost);
  110. httpRequest.setPayload(requestJSONObject.toString().getBytes("UTF-8"));
  111. urlFetchService.fetchAsync(httpRequest);
  112. } catch (final Exception e) {
  113. LOGGER.log(Level.SEVERE, "Sends an article to Rhythm error: {0}", e.getMessage());
  114. }
  115. LOGGER.log(Level.FINER, "Sent an article to Rhythm");
  116. }
  117. /**
  118. * Gets the event type {@linkplain EventTypes#ADD_ARTICLE}.
  119. *
  120. * @return event type
  121. */
  122. @Override
  123. public String getEventType() {
  124. return EventTypes.ADD_ARTICLE;
  125. }
  126. }