Search in sources :

Example 16 with RequestProcessing

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

the class PageProcessor method showPage.

/**
     * Shows page with the specified context.
     *
     * @param context the specified context
     */
@RequestProcessing(value = "/page", method = HTTPRequestMethod.GET)
public void showPage(final HTTPRequestContext context) {
    final AbstractFreeMarkerRenderer renderer = new FreeMarkerRenderer();
    context.setRenderer(renderer);
    renderer.setTemplateName("page.ftl");
    final Map<String, Object> dataModel = renderer.getDataModel();
    final HttpServletRequest request = context.getRequest();
    final HttpServletResponse response = context.getResponse();
    try {
        final JSONObject preference = preferenceQueryService.getPreference();
        if (null == preference) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        Skins.fillLangs(preference.getString(Option.ID_C_LOCALE_STRING), (String) request.getAttribute(Keys.TEMAPLTE_DIR_NAME), dataModel);
        // See PermalinkFilter#dispatchToArticleOrPageProcessor()
        final JSONObject page = (JSONObject) request.getAttribute(Page.PAGE);
        if (null == page) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        final String pageId = page.getString(Keys.OBJECT_ID);
        page.put(Common.COMMENTABLE, preference.getBoolean(Option.ID_C_COMMENTABLE) && page.getBoolean(Page.PAGE_COMMENTABLE));
        page.put(Common.PERMALINK, page.getString(Page.PAGE_PERMALINK));
        dataModel.put(Page.PAGE, page);
        final List<JSONObject> comments = commentQueryService.getComments(pageId);
        dataModel.put(Page.PAGE_COMMENTS_REF, comments);
        // Markdown
        if ("CodeMirror-Markdown".equals(page.optString(Page.PAGE_EDITOR_TYPE))) {
            Stopwatchs.start("Markdown Page[id=" + page.optString(Keys.OBJECT_ID) + "]");
            String content = page.optString(Page.PAGE_CONTENT);
            content = Emotions.convert(content);
            content = Markdowns.toHTML(content);
            page.put(Page.PAGE_CONTENT, content);
            Stopwatchs.end();
        }
        filler.fillSide(request, dataModel, preference);
        filler.fillBlogHeader(request, response, dataModel, preference);
        filler.fillBlogFooter(request, dataModel, preference);
        statisticMgmtService.incBlogViewCount(request, response);
    } 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 : HttpServletRequest(javax.servlet.http.HttpServletRequest) JSONObject(org.json.JSONObject) HttpServletResponse(javax.servlet.http.HttpServletResponse) 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) IOException(java.io.IOException) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 17 with RequestProcessing

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

the class RepairProcessor method removeUnusedArticleProperties.

/**
     * Removes unused properties of each article.
     *
     * @param context the specified context
     */
@RequestProcessing(value = "/fix/normalization/articles/properties", method = HTTPRequestMethod.POST)
public void removeUnusedArticleProperties(final HTTPRequestContext context) {
    LOGGER.log(Level.INFO, "Processes remove unused article properties");
    final TextHTMLRenderer renderer = new TextHTMLRenderer();
    context.setRenderer(renderer);
    Transaction transaction = null;
    try {
        final JSONArray articles = articleRepository.get(new Query()).getJSONArray(Keys.RESULTS);
        if (articles.length() <= 0) {
            renderer.setContent("No unused article properties");
            return;
        }
        transaction = articleRepository.beginTransaction();
        final Set<String> keyNames = Repositories.getKeyNames(Article.ARTICLE);
        for (int i = 0; i < articles.length(); i++) {
            final JSONObject article = articles.getJSONObject(i);
            final JSONArray names = article.names();
            final Set<String> nameSet = CollectionUtils.<String>jsonArrayToSet(names);
            if (nameSet.removeAll(keyNames)) {
                for (final String unusedName : nameSet) {
                    article.remove(unusedName);
                }
                articleRepository.update(article.getString(Keys.OBJECT_ID), article);
                LOGGER.log(Level.INFO, "Found an article[id={0}] exists unused properties[{1}]", new Object[] { article.getString(Keys.OBJECT_ID), nameSet });
            }
        }
        transaction.commit();
    } catch (final Exception e) {
        if (null != transaction && transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        renderer.setContent("Removes unused article properties failed, error msg[" + e.getMessage() + "]");
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) TextHTMLRenderer(org.b3log.latke.servlet.renderer.TextHTMLRenderer) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 18 with RequestProcessing

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

the class RepairProcessor method removeAllDataPOST.

/**
     * Removes all data.
     *
     * @param context the specified context
     */
@RequestProcessing(value = "/rm-all-data.do", method = HTTPRequestMethod.POST)
public void removeAllDataPOST(final HTTPRequestContext context) {
    LOGGER.info("Removing all data....");
    boolean succeed = false;
    try {
        remove(beanManager.getReference(ArchiveDateArticleRepositoryImpl.class));
        remove(beanManager.getReference(ArchiveDateRepositoryImpl.class));
        remove(beanManager.getReference(ArticleRepositoryImpl.class));
        remove(beanManager.getReference(CommentRepositoryImpl.class));
        remove(beanManager.getReference(LinkRepositoryImpl.class));
        remove(beanManager.getReference(OptionRepositoryImpl.class));
        remove(beanManager.getReference(PageRepositoryImpl.class));
        remove(beanManager.getReference(PluginRepositoryImpl.class));
        remove(beanManager.getReference(StatisticRepositoryImpl.class));
        remove(beanManager.getReference(TagArticleRepositoryImpl.class));
        remove(beanManager.getReference(TagRepositoryImpl.class));
        remove(beanManager.getReference(UserRepositoryImpl.class));
        succeed = true;
    } catch (final Exception e) {
        LOGGER.log(Level.WARN, "Removed partial data only", e);
    }
    final StringBuilder htmlBuilder = new StringBuilder();
    htmlBuilder.append("<html><head><title>Result</title></head><body>");
    try {
        final TextHTMLRenderer renderer = new TextHTMLRenderer();
        context.setRenderer(renderer);
        if (succeed) {
            htmlBuilder.append("Removed all data!");
        } else {
            htmlBuilder.append("Refresh this page and run this remover again.");
        }
        htmlBuilder.append("</body></html>");
        renderer.setContent(htmlBuilder.toString());
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        try {
            context.getResponse().sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
        } catch (final IOException ex) {
            throw new RuntimeException(ex);
        }
    }
    LOGGER.info("Removed all data....");
}
Also used : TagRepositoryImpl(org.b3log.solo.repository.impl.TagRepositoryImpl) PageRepositoryImpl(org.b3log.solo.repository.impl.PageRepositoryImpl) UserRepositoryImpl(org.b3log.solo.repository.impl.UserRepositoryImpl) IOException(java.io.IOException) ArchiveDateArticleRepositoryImpl(org.b3log.solo.repository.impl.ArchiveDateArticleRepositoryImpl) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) PluginRepositoryImpl(org.b3log.solo.repository.impl.PluginRepositoryImpl) CommentRepositoryImpl(org.b3log.solo.repository.impl.CommentRepositoryImpl) TagArticleRepositoryImpl(org.b3log.solo.repository.impl.TagArticleRepositoryImpl) ArchiveDateRepositoryImpl(org.b3log.solo.repository.impl.ArchiveDateRepositoryImpl) OptionRepositoryImpl(org.b3log.solo.repository.impl.OptionRepositoryImpl) ArticleRepositoryImpl(org.b3log.solo.repository.impl.ArticleRepositoryImpl) TagArticleRepositoryImpl(org.b3log.solo.repository.impl.TagArticleRepositoryImpl) ArchiveDateArticleRepositoryImpl(org.b3log.solo.repository.impl.ArchiveDateArticleRepositoryImpl) StatisticRepositoryImpl(org.b3log.solo.repository.impl.StatisticRepositoryImpl) TextHTMLRenderer(org.b3log.latke.servlet.renderer.TextHTMLRenderer) LinkRepositoryImpl(org.b3log.solo.repository.impl.LinkRepositoryImpl) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 19 with RequestProcessing

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

the class RepairProcessor method repairTagArticleCounter.

/**
     * Repairs tag article counter.
     *
     * @param context the specified context
     */
@RequestProcessing(value = "/fix/tag-article-counter-repair.do", method = HTTPRequestMethod.GET)
@Transactional
public void repairTagArticleCounter(final HTTPRequestContext context) {
    final TextHTMLRenderer renderer = new TextHTMLRenderer();
    context.setRenderer(renderer);
    try {
        final JSONObject result = tagRepository.get(new Query());
        final JSONArray tagArray = result.getJSONArray(Keys.RESULTS);
        final List<JSONObject> tags = CollectionUtils.jsonArrayToList(tagArray);
        for (final JSONObject tag : tags) {
            final String tagId = tag.getString(Keys.OBJECT_ID);
            final JSONObject tagArticleResult = tagArticleRepository.getByTagId(tagId, 1, Integer.MAX_VALUE);
            final JSONArray tagArticles = tagArticleResult.getJSONArray(Keys.RESULTS);
            final int tagRefCnt = tagArticles.length();
            int publishedTagRefCnt = 0;
            for (int i = 0; i < tagRefCnt; i++) {
                final JSONObject tagArticle = tagArticles.getJSONObject(i);
                final String articleId = tagArticle.getString(Article.ARTICLE + "_" + Keys.OBJECT_ID);
                final JSONObject article = articleRepository.get(articleId);
                if (null == article) {
                    tagArticleRepository.remove(tagArticle.optString(Keys.OBJECT_ID));
                    continue;
                }
                if (article.getBoolean(Article.ARTICLE_IS_PUBLISHED)) {
                    publishedTagRefCnt++;
                }
            }
            tag.put(Tag.TAG_REFERENCE_COUNT, tagRefCnt);
            tag.put(Tag.TAG_PUBLISHED_REFERENCE_COUNT, publishedTagRefCnt);
            tagRepository.update(tagId, tag);
            LOGGER.log(Level.INFO, "Repaired tag[title={0}, refCnt={1}, publishedTagRefCnt={2}]", new Object[] { tag.getString(Tag.TAG_TITLE), tagRefCnt, publishedTagRefCnt });
        }
        renderer.setContent("Repair sucessfully!");
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        renderer.setContent("Repairs failed, error msg[" + e.getMessage() + "]");
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) TextHTMLRenderer(org.b3log.latke.servlet.renderer.TextHTMLRenderer) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing) Transactional(org.b3log.latke.repository.annotation.Transactional)

Example 20 with RequestProcessing

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

the class SitemapProcessor method sitemap.

/**
     * Returns the sitemap.
     * 
     * @param context the specified context
     */
@RequestProcessing(value = "/sitemap.xml", method = HTTPRequestMethod.GET)
public void sitemap(final HTTPRequestContext context) {
    final TextXMLRenderer renderer = new TextXMLRenderer();
    context.setRenderer(renderer);
    final Sitemap sitemap = new Sitemap();
    try {
        addArticles(sitemap);
        addNavigations(sitemap);
        addTags(sitemap);
        addArchives(sitemap);
        LOGGER.log(Level.INFO, "Generating sitemap....");
        final String content = sitemap.toString();
        LOGGER.log(Level.INFO, "Generated sitemap");
        renderer.setContent(content);
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Get blog article feed error", e);
        try {
            context.getResponse().sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
        } catch (final IOException ex) {
            throw new RuntimeException(ex);
        }
    }
}
Also used : Sitemap(org.b3log.solo.model.sitemap.Sitemap) TextXMLRenderer(org.b3log.latke.servlet.renderer.TextXMLRenderer) IOException(java.io.IOException) IOException(java.io.IOException) 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