Search in sources :

Example 1 with Entry

use of org.b3log.solo.model.feed.atom.Entry in project solo by b3log.

the class FeedProcessor method getEntryForArticle.

private Entry getEntryForArticle(final List<JSONObject> articles, final boolean hasMultipleUsers, String authorName, final boolean isFullContent, int i) throws org.json.JSONException, org.b3log.latke.service.ServiceException {
    final JSONObject article = articles.get(i);
    final Entry ret = new Entry();
    final String title = StringEscapeUtils.escapeXml(article.getString(Article.ARTICLE_TITLE));
    ret.setTitle(title);
    final String summary = isFullContent ? StringEscapeUtils.escapeXml(article.getString(Article.ARTICLE_CONTENT)) : StringEscapeUtils.escapeXml(article.optString(Article.ARTICLE_ABSTRACT));
    ret.setSummary(summary);
    final Date updated = (Date) article.get(Article.ARTICLE_UPDATE_DATE);
    ret.setUpdated(updated);
    final String link = Latkes.getServePath() + article.getString(Article.ARTICLE_PERMALINK);
    ret.setLink(link);
    ret.setId(link);
    if (hasMultipleUsers) {
        authorName = StringEscapeUtils.escapeXml(articleQueryService.getAuthor(article).getString(User.USER_NAME));
    }
    ret.setAuthor(authorName);
    final String tagsString = article.getString(Article.ARTICLE_TAGS_REF);
    final String[] tagStrings = tagsString.split(",");
    for (final String tagString : tagStrings) {
        final Category catetory = new Category();
        ret.addCatetory(catetory);
        catetory.setTerm(tagString);
    }
    return ret;
}
Also used : Entry(org.b3log.solo.model.feed.atom.Entry) Category(org.b3log.solo.model.feed.atom.Category) JSONObject(org.json.JSONObject) Date(java.util.Date)

Example 2 with Entry

use of org.b3log.solo.model.feed.atom.Entry in project solo by b3log.

the class FeedProcessor method getEntry.

private Entry getEntry(final boolean hasMultipleUsers, String authorName, final JSONArray articles, final boolean isFullContent, int i) throws org.json.JSONException, org.b3log.latke.service.ServiceException {
    final JSONObject article = articles.getJSONObject(i);
    final Entry ret = new Entry();
    final String title = StringEscapeUtils.escapeXml(article.getString(Article.ARTICLE_TITLE));
    ret.setTitle(title);
    final String summary = isFullContent ? StringEscapeUtils.escapeXml(article.getString(Article.ARTICLE_CONTENT)) : StringEscapeUtils.escapeXml(article.optString(Article.ARTICLE_ABSTRACT));
    ret.setSummary(summary);
    final Date updated = (Date) article.get(Article.ARTICLE_UPDATE_DATE);
    ret.setUpdated(updated);
    final String link = Latkes.getServePath() + article.getString(Article.ARTICLE_PERMALINK);
    ret.setLink(link);
    ret.setId(link);
    if (hasMultipleUsers) {
        authorName = StringEscapeUtils.escapeXml(articleQueryService.getAuthor(article).getString(User.USER_NAME));
    }
    ret.setAuthor(authorName);
    final String tagsString = article.getString(Article.ARTICLE_TAGS_REF);
    final String[] tagStrings = tagsString.split(",");
    for (final String tagString : tagStrings) {
        final Category catetory = new Category();
        ret.addCatetory(catetory);
        final String tag = tagString;
        catetory.setTerm(tag);
    }
    return ret;
}
Also used : Entry(org.b3log.solo.model.feed.atom.Entry) Category(org.b3log.solo.model.feed.atom.Category) JSONObject(org.json.JSONObject) Date(java.util.Date)

Example 3 with Entry

use of org.b3log.solo.model.feed.atom.Entry in project solo by b3log.

the class FeedProcessor method tagArticlesAtom.

/**
     * Tag articles Atom output.
     *
     * @param context the specified context
     * @throws IOException io exception
     */
@RequestProcessing(value = { "/tag-articles-feed.do" }, method = { HTTPRequestMethod.GET, HTTPRequestMethod.HEAD })
public void tagArticlesAtom(final HTTPRequestContext context) throws IOException {
    final AtomRenderer renderer = new AtomRenderer();
    context.setRenderer(renderer);
    final HttpServletRequest request = context.getRequest();
    final HttpServletResponse response = context.getResponse();
    final String queryString = request.getQueryString();
    if (Strings.isEmptyOrNull(queryString)) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    final String oIdMap = queryString.split("&")[0];
    final String tagId = oIdMap.split("=")[1];
    final Feed feed = new Feed();
    try {
        final JSONObject tag = tagRepository.get(tagId);
        if (null == tag) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        final String tagTitle = tag.getString(Tag.TAG_TITLE);
        final JSONObject preference = preferenceQueryService.getPreference();
        if (null == preference) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        final String blogTitle = preference.getString(Option.ID_C_BLOG_TITLE);
        final String blogSubtitle = preference.getString(Option.ID_C_BLOG_SUBTITLE) + ", " + tagTitle;
        final int outputCnt = preference.getInt(Option.ID_C_FEED_OUTPUT_CNT);
        feed.setTitle(StringEscapeUtils.escapeXml(blogTitle));
        feed.setSubtitle(StringEscapeUtils.escapeXml(blogSubtitle));
        feed.setUpdated(new Date());
        feed.setAuthor(StringEscapeUtils.escapeXml(blogTitle));
        feed.setLink(Latkes.getServePath() + "/tag-articles-feed.do");
        feed.setId(Latkes.getServePath() + "/");
        final JSONObject tagArticleResult = tagArticleRepository.getByTagId(tagId, 1, outputCnt);
        final JSONArray tagArticleRelations = tagArticleResult.getJSONArray(Keys.RESULTS);
        if (0 == tagArticleRelations.length()) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        final List<JSONObject> articles = new ArrayList<JSONObject>();
        for (int i = 0; i < tagArticleRelations.length(); i++) {
            final JSONObject tagArticleRelation = tagArticleRelations.getJSONObject(i);
            final String articleId = tagArticleRelation.getString(Article.ARTICLE + "_" + Keys.OBJECT_ID);
            final JSONObject article = articleRepository.get(articleId);
            if (// Skips the unpublished article
            article.getBoolean(Article.ARTICLE_IS_PUBLISHED) && Strings.isEmptyOrNull(article.optString(Article.ARTICLE_VIEW_PWD))) {
                // Skips article with password
                articles.add(article);
            }
        }
        final boolean hasMultipleUsers = userQueryService.hasMultipleUsers();
        String authorName = "";
        if (!hasMultipleUsers && !articles.isEmpty()) {
            authorName = articleQueryService.getAuthor(articles.get(0)).getString(User.USER_NAME);
        }
        final boolean isFullContent = "fullContent".equals(preference.getString(Option.ID_C_FEED_OUTPUT_MODE));
        for (int i = 0; i < articles.size(); i++) {
            Entry entry = getEntryForArticle(articles, hasMultipleUsers, authorName, isFullContent, i);
            feed.addEntry(entry);
        }
        renderer.setContent(feed.toString());
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Get tag article feed error", e);
        try {
            context.getResponse().sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
        } catch (final IOException ex) {
            throw new RuntimeException(ex);
        }
    }
}
Also used : AtomRenderer(org.b3log.latke.servlet.renderer.AtomRenderer) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Date(java.util.Date) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) Entry(org.b3log.solo.model.feed.atom.Entry) JSONObject(org.json.JSONObject) Feed(org.b3log.solo.model.feed.atom.Feed) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 4 with Entry

use of org.b3log.solo.model.feed.atom.Entry in project solo by b3log.

the class FeedProcessor method blogArticlesAtom.

/**
     * Blog articles Atom output.
     *
     * @param context the specified context
     */
@RequestProcessing(value = { "/blog-articles-feed.do" }, method = { HTTPRequestMethod.GET, HTTPRequestMethod.HEAD })
public void blogArticlesAtom(final HTTPRequestContext context) {
    final AtomRenderer renderer = new AtomRenderer();
    context.setRenderer(renderer);
    final Feed feed = new Feed();
    try {
        final JSONObject preference = preferenceQueryService.getPreference();
        final String blogTitle = preference.getString(Option.ID_C_BLOG_TITLE);
        final String blogSubtitle = preference.getString(Option.ID_C_BLOG_SUBTITLE);
        final int outputCnt = preference.getInt(Option.ID_C_FEED_OUTPUT_CNT);
        feed.setTitle(StringEscapeUtils.escapeXml(blogTitle));
        feed.setSubtitle(StringEscapeUtils.escapeXml(blogSubtitle));
        feed.setUpdated(new Date());
        feed.setAuthor(StringEscapeUtils.escapeXml(blogTitle));
        feed.setLink(Latkes.getServePath() + "/blog-articles-feed.do");
        feed.setId(Latkes.getServePath() + "/");
        final List<Filter> filters = new ArrayList<Filter>();
        filters.add(new PropertyFilter(Article.ARTICLE_IS_PUBLISHED, FilterOperator.EQUAL, true));
        filters.add(new PropertyFilter(Article.ARTICLE_VIEW_PWD, FilterOperator.EQUAL, ""));
        final Query query = new Query().setCurrentPageNum(1).setPageSize(outputCnt).setFilter(new CompositeFilter(CompositeFilterOperator.AND, filters)).addSort(Article.ARTICLE_UPDATE_DATE, SortDirection.DESCENDING).setPageCount(1);
        final boolean hasMultipleUsers = userQueryService.hasMultipleUsers();
        String authorName = "";
        final JSONObject articleResult = articleRepository.get(query);
        final JSONArray articles = articleResult.getJSONArray(Keys.RESULTS);
        if (!hasMultipleUsers && 0 != articles.length()) {
            authorName = articleQueryService.getAuthor(articles.getJSONObject(0)).getString(User.USER_NAME);
        }
        final boolean isFullContent = "fullContent".equals(preference.getString(Option.ID_C_FEED_OUTPUT_MODE));
        for (int i = 0; i < articles.length(); i++) {
            Entry entry = getEntry(hasMultipleUsers, authorName, articles, isFullContent, i);
            feed.addEntry(entry);
        }
        renderer.setContent(feed.toString());
    } 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 : CompositeFilter(org.b3log.latke.repository.CompositeFilter) Query(org.b3log.latke.repository.Query) AtomRenderer(org.b3log.latke.servlet.renderer.AtomRenderer) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) IOException(java.io.IOException) Date(java.util.Date) IOException(java.io.IOException) Entry(org.b3log.solo.model.feed.atom.Entry) JSONObject(org.json.JSONObject) PropertyFilter(org.b3log.latke.repository.PropertyFilter) CompositeFilter(org.b3log.latke.repository.CompositeFilter) Filter(org.b3log.latke.repository.Filter) PropertyFilter(org.b3log.latke.repository.PropertyFilter) Feed(org.b3log.solo.model.feed.atom.Feed) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Aggregations

Date (java.util.Date)4 Entry (org.b3log.solo.model.feed.atom.Entry)4 JSONObject (org.json.JSONObject)4 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)2 AtomRenderer (org.b3log.latke.servlet.renderer.AtomRenderer)2 Category (org.b3log.solo.model.feed.atom.Category)2 Feed (org.b3log.solo.model.feed.atom.Feed)2 JSONArray (org.json.JSONArray)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 CompositeFilter (org.b3log.latke.repository.CompositeFilter)1 Filter (org.b3log.latke.repository.Filter)1 PropertyFilter (org.b3log.latke.repository.PropertyFilter)1 Query (org.b3log.latke.repository.Query)1