Search in sources :

Example 41 with JSONRenderer

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

the class PageConsole method getPages.

/**
     * Gets pages by the specified request.
     * 
     * <p>
     * Renders the response with a json object, for example,
     * <pre>
     * {
     *     "pagination": {
     *         "paginationPageCount": 100,
     *         "paginationPageNums": [1, 2, 3, 4, 5]
     *     },
     *     "pages": [{
     *         "oId": "",
     *         "pageTitle": "",
     *         "pageCommentCount": int,
     *         "pageOrder": int,
     *         "pagePermalink": ""
     *      }, ....]
     *     "sc": "GET_PAGES_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
     * @see Requests#PAGINATION_PATH_PATTERN
     */
@RequestProcessing(value = "/console/pages/*/*/*", /* Requests.PAGINATION_PATH_PATTERN */
method = HTTPRequestMethod.GET)
public void getPages(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 requestURI = request.getRequestURI();
        final String path = requestURI.substring((Latkes.getContextPath() + "/console/pages/").length());
        final JSONObject requestJSONObject = Requests.buildPaginationRequest(path);
        final JSONObject result = pageQueryService.getPages(requestJSONObject);
        final JSONArray pages = result.optJSONArray(Page.PAGES);
        // Site-internal URLs process
        for (int i = 0; i < pages.length(); i++) {
            final JSONObject page = pages.getJSONObject(i);
            if ("page".equals(page.optString(Page.PAGE_TYPE))) {
                final String permalink = page.optString(Page.PAGE_PERMALINK);
                page.put(Page.PAGE_PERMALINK, Latkes.getServePath() + permalink);
            }
        }
        result.put(Keys.STATUS_CODE, true);
        renderer.setJSONObject(result);
    } 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("getFailLabel"));
    }
}
Also used : JSONRenderer(org.b3log.latke.servlet.renderer.JSONRenderer) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ServiceException(org.b3log.latke.service.ServiceException) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 42 with JSONRenderer

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

the class PluginConsole method toSetting.

/**
     * get the info of the specified pluginoId,just fot the plugin-setting.
     * @param request the specified http servlet request
     * @param response the specified http servlet response
     * @param context the specified http request context
     * @param renderer the specified {@link ConsoleRenderer}
     * @throws Exception exception
     */
@RequestProcessing(value = "/console/plugin/toSetting", method = HTTPRequestMethod.POST)
public void toSetting(final HttpServletRequest request, final HttpServletResponse response, final HTTPRequestContext context, final ConsoleRenderer renderer) throws Exception {
    context.setRenderer(renderer);
    try {
        final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, response);
        final String pluginId = requestJSONObject.getString(Keys.OBJECT_ID);
        final String setting = pluginQueryService.getPluginSetting(pluginId);
        renderer.setTemplateName("admin-plugin-setting.ftl");
        final Map<String, Object> dataModel = renderer.getDataModel();
        Keys.fillRuntime(dataModel);
        dataModel.put(Plugin.PLUGIN_SETTING, setting);
        dataModel.put(Keys.OBJECT_ID, pluginId);
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        final JSONObject jsonObject = QueryResults.defaultResult();
        final JSONRenderer jsonRenderer = new JSONRenderer();
        jsonRenderer.setJSONObject(jsonObject);
        jsonObject.put(Keys.MSG, langPropsService.get("getFailLabel"));
    }
}
Also used : JSONObject(org.json.JSONObject) JSONRenderer(org.b3log.latke.servlet.renderer.JSONRenderer) JSONObject(org.json.JSONObject) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 43 with JSONRenderer

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

the class PluginConsole method getPlugins.

/**
     * Gets plugins by the specified request.
     * 
     * <p>
     * The request URI contains the pagination arguments. For example, the 
     * request URI is /console/plugins/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>
     * {
     *     "sc": boolean,
     *     "pagination": {
     *         "paginationPageCount": 100,
     *         "paginationPageNums": [1, 2, 3, 4, 5]
     *     },
     *     "plugins": [{
     *         "name": "",
     *         "version": "",
     *         "author": "",
     *         "status": "", // Enumeration name of {@link org.b3log.latke.plugin.PluginStatus}
     *      }, ....]
     * }
     * </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
     * @see Requests#PAGINATION_PATH_PATTERN
     */
@RequestProcessing(value = "/console/plugins/*/*/*", /* Requests.PAGINATION_PATH_PATTERN */
method = HTTPRequestMethod.GET)
public void getPlugins(final HttpServletRequest request, final HttpServletResponse response, final HTTPRequestContext context) throws Exception {
    final JSONRenderer renderer = new JSONRenderer();
    context.setRenderer(renderer);
    try {
        final String requestURI = request.getRequestURI();
        final String path = requestURI.substring((Latkes.getContextPath() + "/console/plugins/").length());
        final JSONObject requestJSONObject = Requests.buildPaginationRequest(path);
        final JSONObject result = pluginQueryService.getPlugins(requestJSONObject);
        renderer.setJSONObject(result);
        result.put(Keys.STATUS_CODE, true);
    } 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("getFailLabel"));
    }
}
Also used : JSONRenderer(org.b3log.latke.servlet.renderer.JSONRenderer) JSONObject(org.json.JSONObject) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 44 with JSONRenderer

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

the class PluginConsole method setPluginStatus.

/**
     * Sets a plugin's status with the specified plugin id, status.
     * 
     * <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/plugin/status/", method = HTTPRequestMethod.PUT)
public void setPluginStatus(final HttpServletRequest request, final HttpServletResponse response, final HTTPRequestContext context) throws Exception {
    final JSONRenderer renderer = new JSONRenderer();
    context.setRenderer(renderer);
    final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, response);
    final String pluginId = requestJSONObject.getString(Keys.OBJECT_ID);
    final String status = requestJSONObject.getString(Plugin.PLUGIN_STATUS);
    final JSONObject result = pluginMgmtService.setPluginStatus(pluginId, status);
    renderer.setJSONObject(result);
}
Also used : JSONRenderer(org.b3log.latke.servlet.renderer.JSONRenderer) JSONObject(org.json.JSONObject) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 45 with JSONRenderer

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

the class PreferenceConsole method updateQiniu.

/**
     * Updates the Qiniu preference by the specified request.
     *
     * @param request the specified http servlet request, for example,      <pre>
     * {
     *     "qiniuAccessKey": "",
     *     "qiniuSecretKey": "",
     *     "qiniuDomain": "",
     *     "qiniuBucket": ""
     * }, see {@link org.b3log.solo.model.Option} for more details
     * </pre>
     *
     * @param response the specified http servlet response
     * @param context the specified http request context
     * @throws Exception exception
     */
@RequestProcessing(value = PREFERENCE_URI_PREFIX + "qiniu", method = HTTPRequestMethod.PUT)
public void updateQiniu(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);
    try {
        final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, response);
        final String accessKey = requestJSONObject.optString(Option.ID_C_QINIU_ACCESS_KEY).trim();
        final String secretKey = requestJSONObject.optString(Option.ID_C_QINIU_SECRET_KEY).trim();
        String domain = requestJSONObject.optString(Option.ID_C_QINIU_DOMAIN).trim();
        final String bucket = requestJSONObject.optString(Option.ID_C_QINIU_BUCKET).trim();
        final JSONObject ret = new JSONObject();
        renderer.setJSONObject(ret);
        if (StringUtils.isNotBlank(domain) && !StringUtils.endsWith(domain, "/")) {
            domain += "/";
        }
        final JSONObject accessKeyOpt = new JSONObject();
        accessKeyOpt.put(Keys.OBJECT_ID, Option.ID_C_QINIU_ACCESS_KEY);
        accessKeyOpt.put(Option.OPTION_CATEGORY, Option.CATEGORY_C_QINIU);
        accessKeyOpt.put(Option.OPTION_VALUE, accessKey);
        final JSONObject secretKeyOpt = new JSONObject();
        secretKeyOpt.put(Keys.OBJECT_ID, Option.ID_C_QINIU_SECRET_KEY);
        secretKeyOpt.put(Option.OPTION_CATEGORY, Option.CATEGORY_C_QINIU);
        secretKeyOpt.put(Option.OPTION_VALUE, secretKey);
        final JSONObject domainOpt = new JSONObject();
        domainOpt.put(Keys.OBJECT_ID, Option.ID_C_QINIU_DOMAIN);
        domainOpt.put(Option.OPTION_CATEGORY, Option.CATEGORY_C_QINIU);
        domainOpt.put(Option.OPTION_VALUE, domain);
        final JSONObject bucketOpt = new JSONObject();
        bucketOpt.put(Keys.OBJECT_ID, Option.ID_C_QINIU_BUCKET);
        bucketOpt.put(Option.OPTION_CATEGORY, Option.CATEGORY_C_QINIU);
        bucketOpt.put(Option.OPTION_VALUE, bucket);
        optionMgmtService.addOrUpdateOption(accessKeyOpt);
        optionMgmtService.addOrUpdateOption(secretKeyOpt);
        optionMgmtService.addOrUpdateOption(domainOpt);
        optionMgmtService.addOrUpdateOption(bucketOpt);
        ret.put(Keys.STATUS_CODE, true);
        ret.put(Keys.MSG, langPropsService.get("updateSuccLabel"));
    } 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