Search in sources :

Example 11 with LatkeBeanManager

use of org.b3log.latke.ioc.LatkeBeanManager in project solo by b3log.

the class ArticleUpdater method action.

@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, 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));
            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(UPDATE_ARTICLE_URL);
        httpRequest.setRequestMethod(HTTPRequestMethod.PUT);
        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.getServerHost() + ":" + Latkes.getServerPort());
        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");
}
Also used : PreferenceQueryService(org.b3log.solo.service.PreferenceQueryService) HTTPRequest(org.b3log.latke.urlfetch.HTTPRequest) JSONObject(org.json.JSONObject) EventException(org.b3log.latke.event.EventException) MalformedURLException(java.net.MalformedURLException) EventException(org.b3log.latke.event.EventException) LatkeBeanManager(org.b3log.latke.ioc.LatkeBeanManager)

Example 12 with LatkeBeanManager

use of org.b3log.latke.ioc.LatkeBeanManager in project solo by b3log.

the class CommentSender method action.

@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 originalComment = data.getJSONObject(Comment.COMMENT);
        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 (Latkes.getServePath().contains("localhost")) {
            LOGGER.log(Level.TRACE, "Solo runs on local server, so should not send this comment[id={0}] to Symphony", new Object[] { originalComment.getString(Keys.OBJECT_ID) });
            return;
        }
        final HTTPRequest httpRequest = new HTTPRequest();
        httpRequest.setURL(ADD_COMMENT_URL);
        httpRequest.setRequestMethod(HTTPRequestMethod.POST);
        final JSONObject requestJSONObject = new JSONObject();
        final JSONObject comment = new JSONObject();
        comment.put("commentId", originalComment.optString(Keys.OBJECT_ID));
        comment.put("commentAuthorName", originalComment.getString(Comment.COMMENT_NAME));
        comment.put("commentAuthorEmail", originalComment.getString(Comment.COMMENT_EMAIL));
        comment.put(Comment.COMMENT_CONTENT, originalComment.getString(Comment.COMMENT_CONTENT));
        comment.put("articleId", originalComment.getString(Comment.COMMENT_ON_ID));
        requestJSONObject.put(Comment.COMMENT, comment);
        requestJSONObject.put("clientVersion", SoloServletListener.VERSION);
        requestJSONObject.put("clientRuntimeEnv", Latkes.getRuntimeEnv().name());
        requestJSONObject.put("clientName", "B3log Solo");
        requestJSONObject.put("clientHost", Latkes.getServerHost() + ":" + Latkes.getServerPort());
        requestJSONObject.put("clientAdminEmail", preference.optString(Option.ID_C_ADMIN_EMAIL));
        requestJSONObject.put("userB3Key", preference.optString(Option.ID_C_KEY_OF_SOLO));
        httpRequest.setPayload(requestJSONObject.toString().getBytes("UTF-8"));
        urlFetchService.fetchAsync(httpRequest);
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Sends a comment to Symphony error: {0}", e.getMessage());
    }
    LOGGER.log(Level.DEBUG, "Sent a comment to Symphony");
}
Also used : PreferenceQueryService(org.b3log.solo.service.PreferenceQueryService) HTTPRequest(org.b3log.latke.urlfetch.HTTPRequest) JSONObject(org.json.JSONObject) EventException(org.b3log.latke.event.EventException) ArticleSender(org.b3log.solo.event.rhythm.ArticleSender) MalformedURLException(java.net.MalformedURLException) EventException(org.b3log.latke.event.EventException) LatkeBeanManager(org.b3log.latke.ioc.LatkeBeanManager)

Example 13 with LatkeBeanManager

use of org.b3log.latke.ioc.LatkeBeanManager in project solo by b3log.

the class AuthFilter method doFilter.

/**
     * If the specified request is NOT made by an authenticated user, sends 
     * error 403.
     *
     * @param request the specified request
     * @param response the specified response
     * @param chain filter chain
     * @throws IOException io exception
     * @throws ServletException servlet exception
     */
@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
    final HttpServletResponse httpServletResponse = (HttpServletResponse) response;
    final HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    final LatkeBeanManager beanManager = Lifecycle.getBeanManager();
    final UserMgmtService userMgmtService = beanManager.getReference(UserMgmtService.class);
    final UserQueryService userQueryService = beanManager.getReference(UserQueryService.class);
    try {
        userMgmtService.tryLogInWithCookie(httpServletRequest, httpServletResponse);
        final JSONObject currentUser = userQueryService.getCurrentUser(httpServletRequest);
        if (null == currentUser) {
            LOGGER.warn("The request has been forbidden");
            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN);
            return;
        }
        final String userRole = currentUser.optString(User.USER_ROLE);
        if (Role.VISITOR_ROLE.equals(userRole)) {
            LOGGER.warn("The request [Visitor] has been forbidden");
            httpServletResponse.sendError(HttpServletResponse.SC_FORBIDDEN);
            return;
        }
        chain.doFilter(request, response);
    } catch (final IOException e) {
        LOGGER.log(Level.ERROR, "Auth filter failed", e);
        httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) JSONObject(org.json.JSONObject) HttpServletResponse(javax.servlet.http.HttpServletResponse) UserQueryService(org.b3log.solo.service.UserQueryService) IOException(java.io.IOException) UserMgmtService(org.b3log.solo.service.UserMgmtService) LatkeBeanManager(org.b3log.latke.ioc.LatkeBeanManager)

Example 14 with LatkeBeanManager

use of org.b3log.latke.ioc.LatkeBeanManager in project solo by b3log.

the class PermalinkFilter method doFilter.

/**
     * Tries to dispatch request to article processor.
     *
     * @param request the specified request
     * @param response the specified response
     * @param chain filter chain
     * @throws IOException io exception
     * @throws ServletException servlet exception
     */
@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
    final HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    final HttpServletResponse httpServletResponse = (HttpServletResponse) response;
    final String requestURI = httpServletRequest.getRequestURI();
    LOGGER.log(Level.DEBUG, "Request URI[{0}]", requestURI);
    final String contextPath = Latkes.getContextPath();
    final String permalink = StringUtils.substringAfter(requestURI, contextPath);
    if (PermalinkQueryService.invalidPermalinkFormat(permalink)) {
        LOGGER.log(Level.DEBUG, "Skip filter request[URI={0}]", permalink);
        chain.doFilter(request, response);
        return;
    }
    JSONObject article;
    JSONObject page = null;
    final LatkeBeanManager beanManager = Lifecycle.getBeanManager();
    try {
        final ArticleRepository articleRepository = beanManager.getReference(ArticleRepositoryImpl.class);
        article = articleRepository.getByPermalink(permalink);
        if (null == article) {
            final PageRepository pageRepository = beanManager.getReference(PageRepositoryImpl.class);
            page = pageRepository.getByPermalink(permalink);
        }
        if (null == page && null == article) {
            LOGGER.log(Level.DEBUG, "Not found article/page with permalink[{0}]", permalink);
            chain.doFilter(request, response);
            return;
        }
    } catch (final RepositoryException e) {
        LOGGER.log(Level.ERROR, "Processes article permalink filter failed", e);
        httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    // If requests an article and the article need view passowrd, sends redirect to the password form
    final ArticleQueryService articleQueryService = beanManager.getReference(ArticleQueryService.class);
    if (null != article && articleQueryService.needViewPwd(httpServletRequest, article)) {
        try {
            httpServletResponse.sendRedirect(Latkes.getServePath() + "/console/article-pwd?articleId=" + article.optString(Keys.OBJECT_ID));
            return;
        } catch (final Exception e) {
            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
    }
    dispatchToArticleOrPageProcessor(request, response, article, page);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) JSONObject(org.json.JSONObject) PageRepository(org.b3log.solo.repository.PageRepository) ArticleQueryService(org.b3log.solo.service.ArticleQueryService) HttpServletResponse(javax.servlet.http.HttpServletResponse) ArticleRepository(org.b3log.solo.repository.ArticleRepository) RepositoryException(org.b3log.latke.repository.RepositoryException) ServletException(javax.servlet.ServletException) RepositoryException(org.b3log.latke.repository.RepositoryException) IOException(java.io.IOException) LatkeBeanManager(org.b3log.latke.ioc.LatkeBeanManager)

Aggregations

LatkeBeanManager (org.b3log.latke.ioc.LatkeBeanManager)14 JSONObject (org.json.JSONObject)10 EventException (org.b3log.latke.event.EventException)8 PreferenceQueryService (org.b3log.solo.service.PreferenceQueryService)7 IOException (java.io.IOException)5 HTTPRequest (org.b3log.latke.urlfetch.HTTPRequest)5 MalformedURLException (java.net.MalformedURLException)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 URL (java.net.URL)2 ServletException (javax.servlet.ServletException)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Message (org.b3log.latke.mail.MailService.Message)2 RepositoryException (org.b3log.latke.repository.RepositoryException)2 ServiceException (org.b3log.latke.service.ServiceException)2 CommentRepository (org.b3log.solo.repository.CommentRepository)2 UserQueryService (org.b3log.solo.service.UserQueryService)2 InputStream (java.io.InputStream)1 Properties (java.util.Properties)1 ServletContext (javax.servlet.ServletContext)1 Cookie (javax.servlet.http.Cookie)1