Search in sources :

Example 6 with FreeMarkerRenderer

use of org.b3log.latke.servlet.renderer.freemarker.FreeMarkerRenderer in project solo by b3log.

the class ArticleProcessor method showArticle.

/**
     * Shows an article with the specified context.
     *
     * @param context the specified context
     * @param request the specified HTTP servlet request
     * @param response the specified HTTP servlet response
     * @throws IOException io exception
     */
@RequestProcessing(value = "/article", method = HTTPRequestMethod.GET)
public void showArticle(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws IOException {
    // See PermalinkFilter#dispatchToArticleOrPageProcessor()
    final JSONObject article = (JSONObject) request.getAttribute(Article.ARTICLE);
    if (null == article) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    final String articleId = article.optString(Keys.OBJECT_ID);
    LOGGER.log(Level.DEBUG, "Article[id={0}]", articleId);
    final AbstractFreeMarkerRenderer renderer = new FreeMarkerRenderer();
    context.setRenderer(renderer);
    renderer.setTemplateName("article.ftl");
    try {
        final JSONObject preference = preferenceQueryService.getPreference();
        final boolean allowVisitDraftViaPermalink = preference.getBoolean(Option.ID_C_ALLOW_VISIT_DRAFT_VIA_PERMALINK);
        if (!article.optBoolean(Article.ARTICLE_IS_PUBLISHED) && !allowVisitDraftViaPermalink) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        LOGGER.log(Level.TRACE, "Article[title={0}]", article.getString(Article.ARTICLE_TITLE));
        articleQueryService.markdown(article);
        // For <meta name="description" content="${article.articleAbstract}"/>
        final String metaDescription = Jsoup.parse(article.optString(Article.ARTICLE_ABSTRACT)).text();
        article.put(Article.ARTICLE_ABSTRACT, metaDescription);
        if (preference.getBoolean(Option.ID_C_ENABLE_ARTICLE_UPDATE_HINT)) {
            article.put(Common.HAS_UPDATED, articleQueryService.hasUpdated(article));
        } else {
            article.put(Common.HAS_UPDATED, false);
        }
        final JSONObject author = articleQueryService.getAuthor(article);
        final String authorName = author.getString(User.USER_NAME);
        article.put(Common.AUTHOR_NAME, authorName);
        final String authorId = author.getString(Keys.OBJECT_ID);
        article.put(Common.AUTHOR_ID, authorId);
        article.put(Common.AUTHOR_ROLE, author.getString(User.USER_ROLE));
        final String userAvatar = author.optString(UserExt.USER_AVATAR);
        if (!Strings.isEmptyOrNull(userAvatar)) {
            article.put(Common.AUTHOR_THUMBNAIL_URL, userAvatar);
        } else {
            final String thumbnailURL = Thumbnails.getGravatarURL(author.optString(User.USER_EMAIL), "128");
            article.put(Common.AUTHOR_THUMBNAIL_URL, thumbnailURL);
        }
        final Map<String, Object> dataModel = renderer.getDataModel();
        prepareShowArticle(preference, dataModel, article);
        filler.fillBlogHeader(request, response, dataModel, preference);
        filler.fillBlogFooter(request, dataModel, preference);
        filler.fillSide(request, dataModel, preference);
        Skins.fillLangs(preference.optString(Option.ID_C_LOCALE_STRING), (String) request.getAttribute(Keys.TEMAPLTE_DIR_NAME), dataModel);
        if (!Requests.hasBeenServed(request, response)) {
            articleMgmtService.incViewCount(articleId);
        }
        statisticMgmtService.incBlogViewCount(request, response);
        // Fire [Before Render Article] event
        final JSONObject eventData = new JSONObject();
        eventData.put(Article.ARTICLE, article);
        try {
            eventManager.fireEventSynchronously(new Event<JSONObject>(EventTypes.BEFORE_RENDER_ARTICLE, eventData));
        } catch (final EventException e) {
            LOGGER.log(Level.ERROR, "Fires [" + EventTypes.BEFORE_RENDER_ARTICLE + "] event failed", e);
        }
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        try {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        } catch (final IOException ex) {
            LOGGER.error(ex.getMessage());
        }
    }
}
Also used : JSONObject(org.json.JSONObject) EventException(org.b3log.latke.event.EventException) JSONObject(org.json.JSONObject) FreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.FreeMarkerRenderer) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) IOException(java.io.IOException) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) EventException(org.b3log.latke.event.EventException) ServiceException(org.b3log.latke.service.ServiceException) JSONException(org.json.JSONException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 7 with FreeMarkerRenderer

use of org.b3log.latke.servlet.renderer.freemarker.FreeMarkerRenderer in project solo by b3log.

the class IndexProcessor method showIndex.

/**
     * Shows index with the specified context.
     *
     * @param context the specified context
     * @param request the specified HTTP servlet request
     * @param response the specified HTTP servlet response
     */
@RequestProcessing(value = { "/\\d*", "" }, uriPatternsMode = URIPatternMode.REGEX, method = HTTPRequestMethod.GET)
public void showIndex(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) {
    final AbstractFreeMarkerRenderer renderer = new FreeMarkerRenderer();
    context.setRenderer(renderer);
    renderer.setTemplateName("index.ftl");
    final Map<String, Object> dataModel = renderer.getDataModel();
    final String requestURI = request.getRequestURI();
    try {
        final int currentPageNum = getCurrentPageNum(requestURI);
        final JSONObject preference = preferenceQueryService.getPreference();
        // https://github.com/b3log/solo/issues/12060
        String specifiedSkin = Skins.getSkinDirName(request);
        if (null != specifiedSkin) {
            if ("default".equals(specifiedSkin)) {
                specifiedSkin = preference.optString(Option.ID_C_SKIN_DIR_NAME);
            }
        } else {
            specifiedSkin = preference.optString(Option.ID_C_SKIN_DIR_NAME);
        }
        Templates.MAIN_CFG.setServletContextForTemplateLoading(SoloServletListener.getServletContext(), "/skins/" + specifiedSkin);
        request.setAttribute(Keys.TEMAPLTE_DIR_NAME, specifiedSkin);
        Skins.fillLangs(preference.optString(Option.ID_C_LOCALE_STRING), (String) request.getAttribute(Keys.TEMAPLTE_DIR_NAME), dataModel);
        filler.fillIndexArticles(request, dataModel, currentPageNum, preference);
        filler.fillSide(request, dataModel, preference);
        filler.fillBlogHeader(request, response, dataModel, preference);
        filler.fillBlogFooter(request, dataModel, preference);
        dataModel.put(Pagination.PAGINATION_CURRENT_PAGE_NUM, currentPageNum);
        final int previousPageNum = currentPageNum > 1 ? currentPageNum - 1 : 0;
        dataModel.put(Pagination.PAGINATION_PREVIOUS_PAGE_NUM, previousPageNum);
        final Integer pageCount = (Integer) dataModel.get(Pagination.PAGINATION_PAGE_COUNT);
        final int nextPageNum = currentPageNum + 1 > pageCount ? pageCount : currentPageNum + 1;
        dataModel.put(Pagination.PAGINATION_NEXT_PAGE_NUM, nextPageNum);
        dataModel.put(Common.PATH, "");
        statisticMgmtService.incBlogViewCount(request, response);
        // https://github.com/b3log/solo/issues/12060
        final Cookie cookie = new Cookie(Skin.SKIN, specifiedSkin);
        cookie.setPath("/");
        response.addCookie(cookie);
    } catch (final ServiceException e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        try {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        } catch (final IOException ex) {
            LOGGER.error(ex.getMessage());
        }
    }
}
Also used : Cookie(javax.servlet.http.Cookie) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) JSONObject(org.json.JSONObject) FreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.FreeMarkerRenderer) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) IOException(java.io.IOException) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Aggregations

IOException (java.io.IOException)7 RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)7 AbstractFreeMarkerRenderer (org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer)7 FreeMarkerRenderer (org.b3log.latke.servlet.renderer.freemarker.FreeMarkerRenderer)7 JSONObject (org.json.JSONObject)7 ServiceException (org.b3log.latke.service.ServiceException)5 JSONException (org.json.JSONException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 EventException (org.b3log.latke.event.EventException)2 Template (freemarker.template.Template)1 Cookie (javax.servlet.http.Cookie)1