Search in sources :

Example 86 with RequestProcessing

use of org.b3log.latke.servlet.annotation.RequestProcessing in project solo by b3log.

the class ArticleGenerator method genArticles.

/**
     * Generates some dummy articles with the specified context.
     * 
     * @param context the specified context
     * @param request the specified request
     * @param response the specified response
     * @throws IOException io exception 
     */
@RequestProcessing(value = "/dev/articles/gen/*", method = HTTPRequestMethod.GET)
public void genArticles(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws IOException {
    if (RuntimeMode.DEVELOPMENT != Latkes.getRuntimeMode()) {
        LOGGER.log(Level.WARN, "Article generation just for development mode, " + "current runtime mode is [{0}]", Latkes.getRuntimeMode());
        response.sendRedirect(Latkes.getServePath());
        return;
    }
    Stopwatchs.start("Gen Articles");
    final String requestURI = request.getRequestURI();
    final int num = Integer.valueOf(requestURI.substring((Latkes.getContextPath() + "/dev/articles/gen/").length()));
    try {
        final JSONObject admin = userQueryService.getAdmin();
        final String authorEmail = admin.optString(User.USER_EMAIL);
        for (int i = 0; i < num; i++) {
            final JSONObject article = new JSONObject();
            article.put(Article.ARTICLE_TITLE, "article title" + i);
            article.put(Article.ARTICLE_ABSTRACT, "article" + i + " abstract");
            final int deviationTag = 3;
            article.put(Article.ARTICLE_TAGS_REF, "taga,tagb,tag" + i % deviationTag);
            article.put(Article.ARTICLE_AUTHOR_EMAIL, authorEmail);
            article.put(Article.ARTICLE_COMMENT_COUNT, 0);
            article.put(Article.ARTICLE_VIEW_COUNT, 0);
            article.put(Article.ARTICLE_CONTENT, "article content");
            article.put(Article.ARTICLE_PERMALINK, "article" + i + " permalink");
            article.put(Article.ARTICLE_HAD_BEEN_PUBLISHED, true);
            article.put(Article.ARTICLE_IS_PUBLISHED, true);
            article.put(Article.ARTICLE_PUT_TOP, false);
            final int deviationBase = 5;
            final int deviationDay = -(Integer.valueOf(String.valueOf(i).substring(0, 1)) % deviationBase);
            final Date date = DateUtils.addMonths(new Date(), deviationDay);
            article.put(Article.ARTICLE_CREATE_DATE, date);
            article.put(Article.ARTICLE_UPDATE_DATE, date);
            article.put(Article.ARTICLE_RANDOM_DOUBLE, Math.random());
            article.put(Article.ARTICLE_COMMENTABLE, true);
            article.put(Article.ARTICLE_VIEW_PWD, "");
            article.put(Article.ARTICLE_SIGN_ID, "1");
            articleMgmtService.addArticle(new JSONObject().put(Article.ARTICLE, article));
        }
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
    }
    Stopwatchs.end();
    response.sendRedirect(Latkes.getServePath());
}
Also used : JSONObject(org.json.JSONObject) Date(java.util.Date) IOException(java.io.IOException) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 87 with RequestProcessing

use of org.b3log.latke.servlet.annotation.RequestProcessing in project solo by b3log.

the class ArticleProcessor method showArticlePwdForm.

/**
     * Shows the article view password form.
     *
     * @param context the specified context
     * @param request the specified HTTP servlet request
     * @param response the specified HTTP servlet response
     * @throws Exception exception
     */
@RequestProcessing(value = "/console/article-pwd", method = HTTPRequestMethod.GET)
public void showArticlePwdForm(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    final String articleId = request.getParameter("articleId");
    if (Strings.isEmptyOrNull(articleId)) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    final JSONObject article = articleQueryService.getArticleById(articleId);
    if (null == article) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    final AbstractFreeMarkerRenderer renderer = new ConsoleRenderer();
    context.setRenderer(renderer);
    renderer.setTemplateName("article-pwd.ftl");
    final Map<String, Object> dataModel = renderer.getDataModel();
    dataModel.put("articleId", articleId);
    dataModel.put("articlePermalink", article.optString(Article.ARTICLE_PERMALINK));
    dataModel.put("articleTitle", article.optString(Article.ARTICLE_TITLE));
    dataModel.put("articleAbstract", article.optString(Article.ARTICLE_ABSTRACT));
    final String msg = request.getParameter(Keys.MSG);
    if (!Strings.isEmptyOrNull(msg)) {
        dataModel.put(Keys.MSG, langPropsService.get("passwordNotMatchLabel"));
    }
    final Map<String, String> langs = langPropsService.getAll(Latkes.getLocale());
    dataModel.putAll(langs);
    final JSONObject preference = preferenceQueryService.getPreference();
    dataModel.put(Option.ID_C_BLOG_TITLE, preference.getString(Option.ID_C_BLOG_TITLE));
    dataModel.put(Common.VERSION, SoloServletListener.VERSION);
    dataModel.put(Common.STATIC_RESOURCE_VERSION, Latkes.getStaticResourceVersion());
    dataModel.put(Common.YEAR, String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
    Keys.fillRuntime(dataModel);
    filler.fillMinified(dataModel);
}
Also used : JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) ConsoleRenderer(org.b3log.solo.processor.renderer.ConsoleRenderer) AbstractFreeMarkerRenderer(org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 88 with RequestProcessing

use of org.b3log.latke.servlet.annotation.RequestProcessing 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 89 with RequestProcessing

use of org.b3log.latke.servlet.annotation.RequestProcessing in project solo by b3log.

the class BlogProcessor method getInterestTags.

/**
     * Gets interest tags (top 10 and bottom 10).
     *
     * <pre>
     * {
     *     "data": ["tag1", "tag2", ....]
     * }
     * </pre>
     *
     * @param context the specified context
     * @param request the specified HTTP servlet request
     * @param response the specified HTTP servlet response
     * @throws Exception io exception
     */
@RequestProcessing(value = "/blog/interest-tags", method = HTTPRequestMethod.GET)
public void getInterestTags(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    final JSONRenderer renderer = new JSONRenderer();
    context.setRenderer(renderer);
    final JSONObject ret = new JSONObject();
    renderer.setJSONObject(ret);
    final Set<String> tagTitles = new HashSet<>();
    final List<JSONObject> topTags = tagQueryService.getTopTags(10);
    for (final JSONObject topTag : topTags) {
        tagTitles.add(topTag.optString(Tag.TAG_TITLE));
    }
    final List<JSONObject> bottomTags = tagQueryService.getBottomTags(10);
    for (final JSONObject bottomTag : bottomTags) {
        tagTitles.add(bottomTag.optString(Tag.TAG_TITLE));
    }
    ret.put("data", tagTitles);
}
Also used : JSONRenderer(org.b3log.latke.servlet.renderer.JSONRenderer) JSONObject(org.json.JSONObject) HashSet(java.util.HashSet) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 90 with RequestProcessing

use of org.b3log.latke.servlet.annotation.RequestProcessing in project solo by b3log.

the class BlogProcessor method getArticlesTags.

/**
     * Gets tags of all articles.
     *
     * <pre>
     * {
     *     "data": [
     *         ["tag1", "tag2", ....], // tags of one article
     *         ["tagX", "tagY", ....], // tags of another article
     *         ....
     *     ]
     * }
     * </pre>
     *
     * @param context the specified context
     * @param request the specified HTTP servlet request
     * @param response the specified HTTP servlet response
     * @throws Exception io exception
     */
@RequestProcessing(value = "/blog/articles-tags", method = HTTPRequestMethod.GET)
public void getArticlesTags(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    final String pwd = request.getParameter("pwd");
    if (Strings.isEmptyOrNull(pwd)) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    final JSONObject admin = userQueryService.getAdmin();
    if (!MD5.hash(pwd).equals(admin.getString(User.USER_PASSWORD))) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    final JSONObject requestJSONObject = new JSONObject();
    requestJSONObject.put(Pagination.PAGINATION_CURRENT_PAGE_NUM, 1);
    requestJSONObject.put(Pagination.PAGINATION_PAGE_SIZE, Integer.MAX_VALUE);
    requestJSONObject.put(Pagination.PAGINATION_WINDOW_SIZE, Integer.MAX_VALUE);
    requestJSONObject.put(Article.ARTICLE_IS_PUBLISHED, true);
    final JSONArray excludes = new JSONArray();
    excludes.put(Article.ARTICLE_CONTENT);
    excludes.put(Article.ARTICLE_UPDATE_DATE);
    excludes.put(Article.ARTICLE_CREATE_DATE);
    excludes.put(Article.ARTICLE_AUTHOR_EMAIL);
    excludes.put(Article.ARTICLE_HAD_BEEN_PUBLISHED);
    excludes.put(Article.ARTICLE_IS_PUBLISHED);
    excludes.put(Article.ARTICLE_RANDOM_DOUBLE);
    requestJSONObject.put(Keys.EXCLUDES, excludes);
    final JSONObject result = articleQueryService.getArticles(requestJSONObject);
    final JSONArray articles = result.optJSONArray(Article.ARTICLES);
    final JSONRenderer renderer = new JSONRenderer();
    context.setRenderer(renderer);
    final JSONObject ret = new JSONObject();
    renderer.setJSONObject(ret);
    final JSONArray data = new JSONArray();
    ret.put("data", data);
    for (int i = 0; i < articles.length(); i++) {
        final JSONObject article = articles.optJSONObject(i);
        final String tagString = article.optString(Article.ARTICLE_TAGS_REF);
        final JSONArray tagArray = new JSONArray();
        data.put(tagArray);
        final String[] tags = tagString.split(",");
        for (final String tag : tags) {
            final String trim = tag.trim();
            if (!Strings.isEmptyOrNull(trim)) {
                tagArray.put(tag);
            }
        }
    }
}
Also used : JSONObject(org.json.JSONObject) JSONRenderer(org.b3log.latke.servlet.renderer.JSONRenderer) JSONArray(org.json.JSONArray) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Aggregations

RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)104 JSONObject (org.json.JSONObject)94 JSONRenderer (org.b3log.latke.servlet.renderer.JSONRenderer)67 ServiceException (org.b3log.latke.service.ServiceException)51 IOException (java.io.IOException)35 AbstractFreeMarkerRenderer (org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer)14 HttpServletRequest (javax.servlet.http.HttpServletRequest)13 JSONException (org.json.JSONException)12 JSONArray (org.json.JSONArray)11 HttpServletResponse (javax.servlet.http.HttpServletResponse)9 EventException (org.b3log.latke.event.EventException)9 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7 TextHTMLRenderer (org.b3log.latke.servlet.renderer.TextHTMLRenderer)7 FreeMarkerRenderer (org.b3log.latke.servlet.renderer.freemarker.FreeMarkerRenderer)7 ConsoleRenderer (org.b3log.solo.processor.renderer.ConsoleRenderer)7 Date (java.util.Date)6 ExecutionException (java.util.concurrent.ExecutionException)6 ArrayList (java.util.ArrayList)5 MalformedURLException (java.net.MalformedURLException)4 HttpSession (javax.servlet.http.HttpSession)4