@Override public void action(final Event<JSONObject> event) throws EventException { final JSONObject data = event.getData(); LOGGER.log( Level.DEBUG, "Processing an event[type={0}, data={1}] in listener[className={2}]", new Object[] {event.getType(), data, CommentSender.class.getName()}); try { if (data.optBoolean(Common.FROM_CLIENT)) { return; } final JSONObject originalArticle = data.getJSONObject(Article.ARTICLE); if (!originalArticle.optBoolean(Article.ARTICLE_SYNC_TO_CLIENT)) { return; } final LatkeBeanManager beanManager = Lifecycle.getBeanManager(); final UserQueryService userQueryService = beanManager.getReference(UserQueryService.class); final String authorId = originalArticle.optString(Article.ARTICLE_AUTHOR_ID); final JSONObject author = userQueryService.getUser(authorId); final String clientURL = author.optString(UserExt.USER_B3_CLIENT_ADD_COMMENT_URL); if (Strings.isEmptyOrNull(clientURL)) { return; } final JSONObject originalComment = data.getJSONObject(Comment.COMMENT); final String commenterId = originalComment.optString(Comment.COMMENT_AUTHOR_ID); final JSONObject commenter = userQueryService.getUser(commenterId); final HTTPRequest httpRequest = new HTTPRequest(); httpRequest.setURL(new URL(clientURL)); httpRequest.setRequestMethod(HTTPRequestMethod.PUT); final JSONObject requestJSONObject = new JSONObject(); final JSONObject comment = new JSONObject( originalComment, new String[] {Comment.COMMENT_AUTHOR_EMAIL, Comment.COMMENT_CONTENT, Keys.OBJECT_ID}); comment.put(Comment.COMMENT_T_AUTHOR_NAME, commenter.optString(User.USER_NAME)); comment.put(UserExt.USER_B3_KEY, author.optString(UserExt.USER_B3_KEY)); comment.put(Comment.COMMENT_T_AUTHOR_URL, commenter.optString(User.USER_URL)); comment.put( Comment.COMMENT_ON_ARTICLE_ID, originalArticle.optString(Article.ARTICLE_CLIENT_ARTICLE_ID)); comment.put(Comment.COMMENT_T_SYMPHONY_ID, originalArticle.optString(Keys.OBJECT_ID)); requestJSONObject.put(Comment.COMMENT, comment); httpRequest.setPayload(requestJSONObject.toString().getBytes("UTF-8")); urlFetchService.fetchAsync(httpRequest); } catch (final Exception e) { LOGGER.log(Level.ERROR, "Sends a comment to client error: {0}", e.getMessage()); } LOGGER.log(Level.DEBUG, "Sent a comment to client"); }
@Override public HTTPResponse fetch(final HTTPRequest request) throws IOException { final HTTPRequestMethod requestMethod = request.getRequestMethod(); if (requestMethod == null) { throw new IOException("RequestMethod for URLFetch should not be null"); } return UrlFetchHandlerFactory.getFetchHandler(requestMethod).doFetch(request); }
@Override public void action(final Event<JSONObject> event) throws EventException { final JSONObject data = event.getData(); LOGGER.log( Level.DEBUG, "Processing an event[type={0}, data={1}] in listener[className={2}]", new Object[] {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)); return; } final LatkeBeanManager beanManager = Lifecycle.getBeanManager(); final PreferenceQueryService preferenceQueryService = beanManager.getReference(PreferenceQueryService.class); final JSONObject preference = preferenceQueryService.getPreference(); if (null == preference) { throw new EventException("Not found preference"); } if (!Strings.isEmptyOrNull(originalArticle.optString(Article.ARTICLE_VIEW_PWD))) { return; } 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", new Object[] { originalArticle.getString(Keys.OBJECT_ID), originalArticle.getString(Article.ARTICLE_TITLE) }); return; } final HTTPRequest httpRequest = new HTTPRequest(); httpRequest.setURL(ADD_ARTICLE_URL); httpRequest.setRequestMethod(HTTPRequestMethod.POST); final JSONObject requestJSONObject = new JSONObject(); final JSONObject article = new JSONObject(); article.put(Keys.OBJECT_ID, originalArticle.getString(Keys.OBJECT_ID)); article.put(Article.ARTICLE_TITLE, originalArticle.getString(Article.ARTICLE_TITLE)); article.put(Article.ARTICLE_PERMALINK, originalArticle.getString(Article.ARTICLE_PERMALINK)); article.put(Article.ARTICLE_TAGS_REF, originalArticle.getString(Article.ARTICLE_TAGS_REF)); article.put( Article.ARTICLE_AUTHOR_EMAIL, originalArticle.getString(Article.ARTICLE_AUTHOR_EMAIL)); article.put(Article.ARTICLE_CONTENT, originalArticle.getString(Article.ARTICLE_CONTENT)); article.put( Article.ARTICLE_CREATE_DATE, ((Date) originalArticle.get(Article.ARTICLE_CREATE_DATE)).getTime()); article.put(Common.POST_TO_COMMUNITY, originalArticle.getBoolean(Common.POST_TO_COMMUNITY)); // Removes this property avoid to persist originalArticle.remove(Common.POST_TO_COMMUNITY); requestJSONObject.put(Article.ARTICLE, article); requestJSONObject.put(Common.BLOG_VERSION, SoloServletListener.VERSION); requestJSONObject.put(Common.BLOG, "B3log Solo"); requestJSONObject.put(Option.ID_C_BLOG_TITLE, preference.getString(Option.ID_C_BLOG_TITLE)); requestJSONObject.put("blogHost", Latkes.getServePath()); requestJSONObject.put("userB3Key", preference.optString(Option.ID_C_KEY_OF_SOLO)); requestJSONObject.put("clientAdminEmail", preference.optString(Option.ID_C_ADMIN_EMAIL)); requestJSONObject.put("clientRuntimeEnv", Latkes.getRuntimeEnv().name()); httpRequest.setPayload(requestJSONObject.toString().getBytes("UTF-8")); urlFetchService.fetchAsync(httpRequest); } catch (final Exception e) { LOGGER.log(Level.ERROR, "Sends an article to Rhythm error: {0}", e.getMessage()); } LOGGER.log(Level.DEBUG, "Sent an article to Rhythm"); }