Search in sources :

Example 36 with ServiceException

use of org.b3log.latke.service.ServiceException in project solo by b3log.

the class UserConsole method getUsers.

/**
     * Gets users by the specified request json object.
     * <p>
     * The request URI contains the pagination arguments. For example, the request URI is /console/users/1/10/20, means
     * the current page is 1, the page size is 10, and the window size is 20.
     * </p>
     *
     * <p>
     * Renders the response with a json object, for example,
     * <pre>
     * {
     *     "pagination": {
     *         "paginationPageCount": 100,
     *         "paginationPageNums": [1, 2, 3, 4, 5]
     *     },
     *     "users": [{
     *         "oId": "",
     *         "userName": "",
     *         "userEmail": "",
     *         "userPassword": "",
     *         "roleName": ""
     *      }, ....]
     *     "sc": "GET_USERS_SUCC"
     * }
     * </pre>
     * </p>
     *
     * @param request the specified http servlet request
     * @param response the specified http servlet response
     * @param context the specified http request context
     * @throws Exception exception
     */
@RequestProcessing(value = "/console/users/*/*/*", /* Requests.PAGINATION_PATH_PATTERN */
method = HTTPRequestMethod.GET)
public void getUsers(final HttpServletRequest request, final HttpServletResponse response, final HTTPRequestContext context) throws Exception {
    final JSONRenderer renderer = new JSONRenderer();
    context.setRenderer(renderer);
    if (!userQueryService.isAdminLoggedIn(request)) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    try {
        final String requestURI = request.getRequestURI();
        final String path = requestURI.substring((Latkes.getContextPath() + "/console/users/").length());
        final JSONObject requestJSONObject = Requests.buildPaginationRequest(path);
        final JSONObject result = userQueryService.getUsers(requestJSONObject);
        result.put(Keys.STATUS_CODE, true);
        renderer.setJSONObject(result);
    } catch (final ServiceException e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        final JSONObject jsonObject = QueryResults.defaultResult();
        renderer.setJSONObject(jsonObject);
        jsonObject.put(Keys.MSG, langPropsService.get("getFailLabel"));
    }
}
Also used : JSONRenderer(org.b3log.latke.servlet.renderer.JSONRenderer) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 37 with ServiceException

use of org.b3log.latke.service.ServiceException in project solo by b3log.

the class UserConsole method changeUserRole.

/**
     * Change a user role.
     *
     * <p>
     * Renders the response with a json object, for example,
     * <pre>
     * {
     *     "sc": boolean,
     *     "msg": ""
     * }
     * </pre>
     * </p>
     *
     * @param request the specified http servlet request
     * @param response the specified http servlet response
     * @param context the specified http request context
     * @throws Exception exception
     */
@RequestProcessing(value = "/console/changeRole/*", method = HTTPRequestMethod.GET)
public void changeUserRole(final HttpServletRequest request, final HttpServletResponse response, final HTTPRequestContext context) throws Exception {
    if (!userQueryService.isAdminLoggedIn(request)) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    final JSONRenderer renderer = new JSONRenderer();
    context.setRenderer(renderer);
    final JSONObject jsonObject = new JSONObject();
    renderer.setJSONObject(jsonObject);
    try {
        final String userId = request.getRequestURI().substring((Latkes.getContextPath() + "/console/changeRole/").length());
        userMgmtService.changeRole(userId);
        jsonObject.put(Keys.STATUS_CODE, true);
        jsonObject.put(Keys.MSG, langPropsService.get("updateSuccLabel"));
    } catch (final ServiceException e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        jsonObject.put(Keys.STATUS_CODE, false);
        jsonObject.put(Keys.MSG, langPropsService.get("removeFailLabel"));
    }
}
Also used : JSONRenderer(org.b3log.latke.servlet.renderer.JSONRenderer) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 38 with ServiceException

use of org.b3log.latke.service.ServiceException in project solo by b3log.

the class Filler method fillSide.

/**
     * Fills side.ftl.
     *
     * @param request the specified HTTP servlet request
     * @param dataModel data model
     * @param preference the specified preference
     * @throws ServiceException service exception
     */
public void fillSide(final HttpServletRequest request, final Map<String, Object> dataModel, final JSONObject preference) throws ServiceException {
    Stopwatchs.start("Fill Side");
    try {
        LOGGER.debug("Filling side....");
        Template template = Templates.getTemplate((String) request.getAttribute(Keys.TEMAPLTE_DIR_NAME), "side.ftl");
        if (null == template) {
            LOGGER.debug("The skin dose not contain [side.ftl] template");
            template = Templates.getTemplate((String) request.getAttribute(Keys.TEMAPLTE_DIR_NAME), "index.ftl");
            if (null == template) {
                LOGGER.debug("The skin dose not contain [index.ftl] template");
                return;
            }
        }
        dataModel.put("fillTagArticles", fillTagArticles);
        if (Templates.hasExpression(template, "<#list recentArticles as article>")) {
            fillRecentArticles(dataModel, preference);
        }
        if (Templates.hasExpression(template, "<#list links as link>")) {
            fillLinks(dataModel);
        }
        if (Templates.hasExpression(template, "<#list recentComments as comment>")) {
            fillRecentComments(dataModel, preference);
        }
        if (Templates.hasExpression(template, "<#list mostUsedTags as tag>")) {
            fillMostUsedTags(dataModel, preference);
        }
        if (Templates.hasExpression(template, "<#list mostCommentArticles as article>")) {
            fillMostCommentArticles(dataModel, preference);
        }
        if (Templates.hasExpression(template, "<#list mostViewCountArticles as article>")) {
            fillMostViewCountArticles(dataModel, preference);
        }
        if (Templates.hasExpression(template, "<#list archiveDates as archiveDate>")) {
            fillArchiveDates(dataModel, preference);
        }
    } catch (final ServiceException e) {
        LOGGER.log(Level.ERROR, "Fills side failed", e);
        throw new ServiceException(e);
    } finally {
        Stopwatchs.end();
    }
}
Also used : ServiceException(org.b3log.latke.service.ServiceException) Template(freemarker.template.Template)

Example 39 with ServiceException

use of org.b3log.latke.service.ServiceException in project solo by b3log.

the class ArticleConsole method getArticle.

/**
     * Gets an article by the specified request json object.
     *
     * <p>
     * Renders the response with a json object, for example,
     * <pre>
     * {
     *     "oId": "",
     *     "articleTitle": "",
     *     "articleAbstract": "",
     *     "articleContent": "",
     *     "articlePermalink": "",
     *     "articleHadBeenPublished": boolean,
     *     "articleTags": [{
     *         "oId": "",
     *         "tagTitle": ""
     *     }, ....],
     *     "articleSignId": "",
     *     "signs": [{
     *         "oId": "",
     *         "signHTML": ""
     *     }, ....]
     *     "sc": "GET_ARTICLE_SUCC"
     * }
     * </pre>
     * </p>
     *
     * @param request the specified http servlet request
     * @param response the specified http servlet response
     * @param context the specified http request context
     * @throws Exception exception
     */
@RequestProcessing(value = "/console/article/*", method = HTTPRequestMethod.GET)
public void getArticle(final HttpServletRequest request, final HttpServletResponse response, final HTTPRequestContext context) throws Exception {
    if (!userQueryService.isLoggedIn(request, response)) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    final JSONRenderer renderer = new JSONRenderer();
    context.setRenderer(renderer);
    try {
        final String articleId = request.getRequestURI().substring((Latkes.getContextPath() + "/console/article/").length());
        final JSONObject result = articleQueryService.getArticle(articleId);
        result.put(Keys.STATUS_CODE, true);
        renderer.setJSONObject(result);
    } catch (final ServiceException e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        final JSONObject jsonObject = QueryResults.defaultResult();
        renderer.setJSONObject(jsonObject);
        jsonObject.put(Keys.MSG, langPropsService.get("getFailLabel"));
    }
}
Also used : JSONRenderer(org.b3log.latke.servlet.renderer.JSONRenderer) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 40 with ServiceException

use of org.b3log.latke.service.ServiceException in project solo by b3log.

the class Filler method fillRecentArticles.

/**
     * Fills post articles recently.
     *
     * @param dataModel data model
     * @param preference the specified preference
     * @throws ServiceException service exception
     */
public void fillRecentArticles(final Map<String, Object> dataModel, final JSONObject preference) throws ServiceException {
    Stopwatchs.start("Fill Recent Articles");
    try {
        final int recentArticleDisplayCnt = preference.getInt(Option.ID_C_RECENT_ARTICLE_DISPLAY_CNT);
        final List<JSONObject> recentArticles = articleRepository.getRecentArticles(recentArticleDisplayCnt);
        dataModel.put(Common.RECENT_ARTICLES, recentArticles);
    } catch (final JSONException e) {
        LOGGER.log(Level.ERROR, "Fills recent articles failed", e);
        throw new ServiceException(e);
    } catch (final RepositoryException e) {
        LOGGER.log(Level.ERROR, "Fills recent articles failed", e);
        throw new ServiceException(e);
    } finally {
        Stopwatchs.end();
    }
}
Also used : JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) JSONException(org.json.JSONException)

Aggregations

ServiceException (org.b3log.latke.service.ServiceException)102 JSONObject (org.json.JSONObject)91 JSONException (org.json.JSONException)45 RepositoryException (org.b3log.latke.repository.RepositoryException)34 Transaction (org.b3log.latke.repository.Transaction)31 RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)23 JSONArray (org.json.JSONArray)17 JSONRenderer (org.b3log.latke.servlet.renderer.JSONRenderer)16 EventException (org.b3log.latke.event.EventException)14 IOException (java.io.IOException)11 ParseException (java.text.ParseException)10 ArrayList (java.util.ArrayList)8 Query (org.b3log.latke.repository.Query)8 Date (java.util.Date)7 AbstractFreeMarkerRenderer (org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer)6 Template (freemarker.template.Template)3 FreeMarkerRenderer (org.b3log.latke.servlet.renderer.freemarker.FreeMarkerRenderer)3 ArchiveDate (org.b3log.solo.model.ArchiveDate)3 URL (java.net.URL)2 HashMap (java.util.HashMap)2