Search in sources :

Example 36 with JSONRenderer

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

the class UserConsole method updateUser.

/**
     * Updates a user by the specified request.
     *
     * <p>
     * Renders the response with a json object, for example,
     * <pre>
     * {
     *     "sc": boolean,
     *     "msg": ""
     * }
     * </pre>
     * </p>
     *
     * @param request the specified http servlet request, for example,      <pre>
     * {
     *     "oId": "",
     *     "userName": "",
     *     "userEmail": "",
     *     "userPassword": "", // Unhashed
     *     "userRole": "", // optional
     *     "userURL": "", // optional
     *     "userAvatar": "" // optional
     * }
     * </pre>
     *
     * @param context the specified http request context
     * @param response the specified http servlet response
     * @throws Exception exception
     */
@RequestProcessing(value = "/console/user/", method = HTTPRequestMethod.PUT)
public void updateUser(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 ret = new JSONObject();
    try {
        final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, response);
        userMgmtService.updateUser(requestJSONObject);
        ret.put(Keys.STATUS_CODE, true);
        ret.put(Keys.MSG, langPropsService.get("updateSuccLabel"));
        renderer.setJSONObject(ret);
    } catch (final ServiceException e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        final JSONObject jsonObject = QueryResults.defaultResult();
        renderer.setJSONObject(jsonObject);
        jsonObject.put(Keys.MSG, e.getMessage());
    }
}
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 JSONRenderer

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

the class ArticleConsole method removeArticle.

/**
     * Removes an article by the specified request.
     *
     * <p>
     * Renders the response with a json object, for example,
     * <pre>
     * {
     *     "sc": boolean,
     *     "msg": ""
     * }
     * </pre>
     * </p>
     *
     * @param context the specified http request context
     * @param request the specified http servlet request
     * @param response the specified http servlet response
     * @param articleId the specified article id
     * @throws Exception exception
     */
@RequestProcessing(value = "/console/article/{articleId}", method = HTTPRequestMethod.DELETE)
public void removeArticle(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response, final String articleId) throws Exception {
    if (!userQueryService.isLoggedIn(request, response)) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    final JSONRenderer renderer = new JSONRenderer();
    context.setRenderer(renderer);
    final JSONObject ret = new JSONObject();
    renderer.setJSONObject(ret);
    try {
        if (!articleQueryService.canAccessArticle(articleId, request)) {
            ret.put(Keys.STATUS_CODE, false);
            ret.put(Keys.MSG, langPropsService.get("forbiddenLabel"));
            return;
        }
        articleMgmtService.removeArticle(articleId);
        ret.put(Keys.STATUS_CODE, true);
        ret.put(Keys.MSG, langPropsService.get("removeSuccLabel"));
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        final JSONObject jsonObject = new JSONObject();
        renderer.setJSONObject(jsonObject);
        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 JSONRenderer

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

the class PageConsole method addPage.

/**
     * Adds a page with the specified request.
     * 
     * <p>
     * Renders the response with a json object, for example,
     * <pre>
     * {
     *     "sc": boolean,
     *     "oId": "", // Generated page id
     *     "msg": ""
     * }
     * </pre>
     * </p>
     * 
     * @param context the specified http request context
     * @param request the specified http servlet request, for example,
     * <pre>
     * {
     *     "page": {
     *         "pageTitle": "",
     *         "pageContent": "",
     *         "pagePermalink": "" // optional,
     *         "pageCommentable": boolean,
     *         "pageType": "",
     *         "pageOpenTarget": ""
     *     }
     * }, see {@link org.b3log.solo.model.Page} for more details
     * </pre>
     * @param response the specified http servlet response
     * @throws Exception exception
     */
@RequestProcessing(value = "/console/page/", method = HTTPRequestMethod.POST)
public void addPage(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    if (!userQueryService.isAdminLoggedIn(request)) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    final JSONRenderer renderer = new JSONRenderer();
    context.setRenderer(renderer);
    final JSONObject ret = new JSONObject();
    try {
        final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, response);
        final String pageId = pageMgmtService.addPage(requestJSONObject);
        ret.put(Keys.OBJECT_ID, pageId);
        ret.put(Keys.MSG, langPropsService.get("addSuccLabel"));
        ret.put(Keys.STATUS_CODE, true);
        renderer.setJSONObject(ret);
    } catch (final ServiceException e) {
        // May be permalink check exception
        LOGGER.log(Level.WARN, e.getMessage(), e);
        final JSONObject jsonObject = QueryResults.defaultResult();
        renderer.setJSONObject(jsonObject);
        jsonObject.put(Keys.MSG, e.getMessage());
    }
}
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 39 with JSONRenderer

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

the class PageConsole method changeOrder.

/**
     * Changes a page order by the specified page id and direction.
     * 
     * <p>
     * Renders the response with a json object, for example,
     * <pre>
     * {
     *     "sc": boolean,
     *     "msg": ""
     * }
     * </pre>
     * </p>
     *
     * @param request the specified http servlet request, for example,
     * <pre>
     * {
     *     "oId": "",
     *     "direction": "" // "up"/"down"
     * }
     * </pre>
     * @param response the specified http servlet response
     * @param context the specified http request context
     * @throws Exception exception 
     */
@RequestProcessing(value = "/console/page/order/", method = HTTPRequestMethod.PUT)
public void changeOrder(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 ret = new JSONObject();
    try {
        final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, response);
        final String linkId = requestJSONObject.getString(Keys.OBJECT_ID);
        final String direction = requestJSONObject.getString(Common.DIRECTION);
        pageMgmtService.changeOrder(linkId, direction);
        ret.put(Keys.STATUS_CODE, true);
        ret.put(Keys.MSG, langPropsService.get("updateSuccLabel"));
        renderer.setJSONObject(ret);
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        final JSONObject jsonObject = QueryResults.defaultResult();
        renderer.setJSONObject(jsonObject);
        jsonObject.put(Keys.MSG, langPropsService.get("updateFailLabel"));
    }
}
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 JSONRenderer

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

the class PageConsole method updatePage.

/**
     * Updates a page by the specified request.
     * 
     * <p>
     * Renders the response with a json object, for example,
     * <pre>
     * {
     *     "sc": boolean,
     *     "msg": ""
     * }
     * </pre>
     * </p>
     *
     * @param request the specified http servlet request, for example,
     * <pre>
     * {
     *     "page": {
     *         "oId": "",
     *         "pageTitle": "",
     *         "pageContent": "",
     *         "pageOrder": int,
     *         "pageCommentCount": int,
     *         "pagePermalink": "",
     *         "pageCommentable": boolean,
     *         "pageType": "",
     *         "pageOpenTarget": ""
     *     }
     * }, see {@link org.b3log.solo.model.Page} for more details
     * </pre>
     * @param response the specified http servlet response
     * @param context the specified http request context
     * @throws Exception exception
     */
@RequestProcessing(value = "/console/page/", method = HTTPRequestMethod.PUT)
public void updatePage(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 ret = new JSONObject();
    try {
        final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, response);
        pageMgmtService.updatePage(requestJSONObject);
        ret.put(Keys.STATUS_CODE, true);
        ret.put(Keys.MSG, langPropsService.get("updateSuccLabel"));
        renderer.setJSONObject(ret);
    } catch (final ServiceException e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        final JSONObject jsonObject = QueryResults.defaultResult();
        renderer.setJSONObject(jsonObject);
        jsonObject.put(Keys.MSG, e.getMessage());
    }
}
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)

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