Search in sources :

Example 66 with JSONRenderer

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

the class ArticleProcessor method getAuthorsArticlesByPage.

/**
     * Gets author articles paged with the specified context.
     *
     * @param context the specified context
     * @param request the specified request
     */
@RequestProcessing(value = "/articles/authors/\\d+/\\d+", uriPatternsMode = URIPatternMode.REGEX, method = HTTPRequestMethod.GET)
public void getAuthorsArticlesByPage(final HTTPRequestContext context, final HttpServletRequest request) {
    final JSONObject jsonObject = new JSONObject();
    final String authorId = getAuthorsArticlesPagedAuthorId(request.getRequestURI());
    final int currentPageNum = getAuthorsArticlesPagedCurrentPageNum(request.getRequestURI());
    Stopwatchs.start("Get Author-Articles Paged[authorId=" + authorId + ", pageNum=" + currentPageNum + ']');
    try {
        jsonObject.put(Keys.STATUS_CODE, true);
        final JSONObject preference = preferenceQueryService.getPreference();
        final int pageSize = preference.getInt(Option.ID_C_ARTICLE_LIST_DISPLAY_COUNT);
        final JSONObject authorRet = userQueryService.getUser(authorId);
        if (null == authorRet) {
            context.getResponse().sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        final JSONObject author = authorRet.getJSONObject(User.USER);
        final String authorEmail = author.optString(User.USER_EMAIL);
        final List<JSONObject> articles = articleQueryService.getArticlesByAuthorEmail(authorEmail, currentPageNum, pageSize);
        if (!articles.isEmpty()) {
            filler.setArticlesExProperties(request, articles, author, preference);
        }
        final int articleCount = author.getInt(UserExt.USER_PUBLISHED_ARTICLE_COUNT);
        final int pageCount = (int) Math.ceil((double) articleCount / (double) pageSize);
        final JSONObject result = new JSONObject();
        final JSONObject pagination = new JSONObject();
        pagination.put(Pagination.PAGINATION_PAGE_COUNT, pageCount);
        result.put(Pagination.PAGINATION, pagination);
        result.put(Article.ARTICLES, articles);
        jsonObject.put(Keys.RESULTS, result);
    } catch (final Exception e) {
        jsonObject.put(Keys.STATUS_CODE, false);
        LOGGER.log(Level.ERROR, "Gets article paged failed", e);
    } finally {
        Stopwatchs.end();
    }
    final JSONRenderer renderer = new JSONRenderer();
    context.setRenderer(renderer);
    renderer.setJSONObject(jsonObject);
}
Also used : JSONObject(org.json.JSONObject) JSONRenderer(org.b3log.latke.servlet.renderer.JSONRenderer) 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 67 with JSONRenderer

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

the class ArticleProcessor method getTagArticlesByPage.

/**
     * Gets tag articles paged with the specified context.
     *
     * @param context the specified context
     * @param request the specified request
     */
@RequestProcessing(value = "/articles/tags/.+/\\d+", uriPatternsMode = URIPatternMode.REGEX, method = HTTPRequestMethod.GET)
public void getTagArticlesByPage(final HTTPRequestContext context, final HttpServletRequest request) {
    final JSONObject jsonObject = new JSONObject();
    String tagTitle = getTagArticlesPagedTag(request.getRequestURI());
    try {
        tagTitle = URLDecoder.decode(tagTitle, "UTF-8");
    } catch (final UnsupportedEncodingException e) {
        LOGGER.log(Level.ERROR, "Gets tag title failed[requestURI=" + request.getRequestURI() + ']', e);
        tagTitle = "";
    }
    final int currentPageNum = getTagArticlesPagedCurrentPageNum(request.getRequestURI());
    Stopwatchs.start("Get Tag-Articles Paged[tagTitle=" + tagTitle + ", pageNum=" + currentPageNum + ']');
    try {
        jsonObject.put(Keys.STATUS_CODE, true);
        final JSONObject preference = preferenceQueryService.getPreference();
        final int pageSize = preference.getInt(Option.ID_C_ARTICLE_LIST_DISPLAY_COUNT);
        final JSONObject tagQueryResult = tagQueryService.getTagByTitle(tagTitle);
        if (null == tagQueryResult) {
            throw new Exception("Can not foud tag[title=" + tagTitle + "]");
        }
        final JSONObject tag = tagQueryResult.getJSONObject(Tag.TAG);
        final String tagId = tag.getString(Keys.OBJECT_ID);
        final List<JSONObject> articles = articleQueryService.getArticlesByTag(tagId, currentPageNum, pageSize);
        final int tagArticleCount = tag.getInt(Tag.TAG_PUBLISHED_REFERENCE_COUNT);
        final int pageCount = (int) Math.ceil((double) tagArticleCount / (double) pageSize);
        final boolean hasMultipleUsers = userQueryService.hasMultipleUsers();
        if (hasMultipleUsers) {
            filler.setArticlesExProperties(request, articles, preference);
        } else if (!articles.isEmpty()) {
            final JSONObject author = articleQueryService.getAuthor(articles.get(0));
            filler.setArticlesExProperties(request, articles, author, preference);
        }
        Collections.sort(articles, Comparators.ARTICLE_CREATE_DATE_COMPARATOR);
        final JSONObject result = new JSONObject();
        final JSONObject pagination = new JSONObject();
        pagination.put(Pagination.PAGINATION_PAGE_COUNT, pageCount);
        result.put(Pagination.PAGINATION, pagination);
        result.put(Article.ARTICLES, articles);
        jsonObject.put(Keys.RESULTS, result);
    } catch (final Exception e) {
        jsonObject.put(Keys.STATUS_CODE, false);
        LOGGER.log(Level.ERROR, "Gets article paged failed", e);
    } finally {
        Stopwatchs.end();
    }
    final JSONRenderer renderer = new JSONRenderer();
    context.setRenderer(renderer);
    renderer.setJSONObject(jsonObject);
}
Also used : JSONObject(org.json.JSONObject) JSONRenderer(org.b3log.latke.servlet.renderer.JSONRenderer) UnsupportedEncodingException(java.io.UnsupportedEncodingException) 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)

Aggregations

RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)67 JSONRenderer (org.b3log.latke.servlet.renderer.JSONRenderer)67 JSONObject (org.json.JSONObject)67 ServiceException (org.b3log.latke.service.ServiceException)38 IOException (java.io.IOException)12 JSONException (org.json.JSONException)7 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 MalformedURLException (java.net.MalformedURLException)4 EventException (org.b3log.latke.event.EventException)4 JSONArray (org.json.JSONArray)4 RepositoryException (org.b3log.latke.repository.RepositoryException)3 Template (freemarker.template.Template)2 StringWriter (java.io.StringWriter)2 URL (java.net.URL)2 HashMap (java.util.HashMap)2 ServletException (javax.servlet.ServletException)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 HttpSession (javax.servlet.http.HttpSession)2 HTTPRequest (org.b3log.latke.urlfetch.HTTPRequest)2