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"));
}
}
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"));
}
}
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"));
}
}
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);
}
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());
}
}
Aggregations