Commit 51d67ec7 authored by Liang Ding's avatar Liang Ding

🎨 clean code

parent 661b9e68
...@@ -44,7 +44,7 @@ import javax.servlet.http.HttpServletResponse; ...@@ -44,7 +44,7 @@ import javax.servlet.http.HttpServletResponse;
* Preference console request processing. * Preference console request processing.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.2.0.9, Nov 15, 2016 * @version 1.2.0.10, May 2, 2017
* @since 0.4.0 * @since 0.4.0
*/ */
@RequestProcessor @RequestProcessor
...@@ -53,7 +53,12 @@ public class PreferenceConsole { ...@@ -53,7 +53,12 @@ public class PreferenceConsole {
/** /**
* Logger. * Logger.
*/ */
private static final Logger LOGGER = Logger.getLogger(PreferenceConsole.class.getName()); private static final Logger LOGGER = Logger.getLogger(PreferenceConsole.class);
/**
* Preference URI prefix.
*/
private static final String PREFERENCE_URI_PREFIX = "/console/preference/";
/** /**
* Preference query service. * Preference query service.
...@@ -91,14 +96,8 @@ public class PreferenceConsole { ...@@ -91,14 +96,8 @@ public class PreferenceConsole {
@Inject @Inject
private LangPropsService langPropsService; private LangPropsService langPropsService;
/**
* Preference URI prefix.
*/
private static final String PREFERENCE_URI_PREFIX = "/console/preference/";
/** /**
* Gets reply template. * Gets reply template.
*
* <p> * <p>
* Renders the response with a json object, for example, * Renders the response with a json object, for example,
* <pre> * <pre>
...@@ -120,31 +119,27 @@ public class PreferenceConsole { ...@@ -120,31 +119,27 @@ public class PreferenceConsole {
@RequestProcessing(value = "/console/reply/notification/template", method = HTTPRequestMethod.GET) @RequestProcessing(value = "/console/reply/notification/template", method = HTTPRequestMethod.GET)
public void getReplyNotificationTemplate(final HttpServletRequest request, public void getReplyNotificationTemplate(final HttpServletRequest request,
final HttpServletResponse response, final HttpServletResponse response,
final HTTPRequestContext context) final HTTPRequestContext context) throws Exception {
throws Exception {
if (!userQueryService.isLoggedIn(request, response)) { if (!userQueryService.isLoggedIn(request, response)) {
response.sendError(HttpServletResponse.SC_FORBIDDEN); response.sendError(HttpServletResponse.SC_FORBIDDEN);
return; return;
} }
final JSONRenderer renderer = new JSONRenderer(); final JSONRenderer renderer = new JSONRenderer();
context.setRenderer(renderer); context.setRenderer(renderer);
try { try {
final JSONObject replyNotificationTemplate = preferenceQueryService.getReplyNotificationTemplate(); final JSONObject replyNotificationTemplate = preferenceQueryService.getReplyNotificationTemplate();
final JSONObject ret = new JSONObject(); final JSONObject ret = new JSONObject();
renderer.setJSONObject(ret); renderer.setJSONObject(ret);
ret.put("replyNotificationTemplate", replyNotificationTemplate); ret.put("replyNotificationTemplate", replyNotificationTemplate);
ret.put(Keys.STATUS_CODE, true); ret.put(Keys.STATUS_CODE, true);
} catch (final Exception e) { } catch (final Exception e) {
LOGGER.log(Level.ERROR, e.getMessage(), e); LOGGER.log(Level.ERROR, e.getMessage(), e);
final JSONObject jsonObject = QueryResults.defaultResult(); final JSONObject jsonObject = QueryResults.defaultResult();
renderer.setJSONObject(jsonObject); renderer.setJSONObject(jsonObject);
jsonObject.put(Keys.MSG, langPropsService.get("getFailLabel")); jsonObject.put(Keys.MSG, langPropsService.get("getFailLabel"));
} }
...@@ -153,15 +148,11 @@ public class PreferenceConsole { ...@@ -153,15 +148,11 @@ public class PreferenceConsole {
/** /**
* Updates reply template. * Updates reply template.
* *
* @param request the specified http servlet request, for example, <pre> * @param request the specified http servlet request, for example,
* {
* "replyNotificationTemplate": { * "replyNotificationTemplate": {
* "subject": "", * "subject": "",
* "body": "" * "body": ""
* } * }
* }
* </pre>
*
* @param response the specified http servlet response * @param response the specified http servlet response
* @param context the specified http request context * @param context the specified http request context
* @throws Exception exception * @throws Exception exception
...@@ -169,35 +160,28 @@ public class PreferenceConsole { ...@@ -169,35 +160,28 @@ public class PreferenceConsole {
@RequestProcessing(value = "/console/reply/notification/template", method = HTTPRequestMethod.PUT) @RequestProcessing(value = "/console/reply/notification/template", method = HTTPRequestMethod.PUT)
public void updateReplyNotificationTemplate(final HttpServletRequest request, public void updateReplyNotificationTemplate(final HttpServletRequest request,
final HttpServletResponse response, final HttpServletResponse response,
final HTTPRequestContext context) final HTTPRequestContext context) throws Exception {
throws Exception {
if (!userQueryService.isLoggedIn(request, response)) { if (!userQueryService.isLoggedIn(request, response)) {
response.sendError(HttpServletResponse.SC_FORBIDDEN); response.sendError(HttpServletResponse.SC_FORBIDDEN);
return; return;
} }
final JSONRenderer renderer = new JSONRenderer(); final JSONRenderer renderer = new JSONRenderer();
context.setRenderer(renderer); context.setRenderer(renderer);
try { try {
final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, response); final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, response);
final JSONObject replyNotificationTemplate = requestJSONObject.getJSONObject("replyNotificationTemplate"); final JSONObject replyNotificationTemplate = requestJSONObject.getJSONObject("replyNotificationTemplate");
preferenceMgmtService.updateReplyNotificationTemplate(replyNotificationTemplate); preferenceMgmtService.updateReplyNotificationTemplate(replyNotificationTemplate);
final JSONObject ret = new JSONObject(); final JSONObject ret = new JSONObject();
ret.put(Keys.STATUS_CODE, true); ret.put(Keys.STATUS_CODE, true);
ret.put(Keys.MSG, langPropsService.get("updateSuccLabel")); ret.put(Keys.MSG, langPropsService.get("updateSuccLabel"));
renderer.setJSONObject(ret); renderer.setJSONObject(ret);
} catch (final Exception e) { } catch (final Exception e) {
LOGGER.log(Level.ERROR, e.getMessage(), e); LOGGER.log(Level.ERROR, e.getMessage(), e);
final JSONObject jsonObject = QueryResults.defaultResult(); final JSONObject jsonObject = QueryResults.defaultResult();
renderer.setJSONObject(jsonObject); renderer.setJSONObject(jsonObject);
jsonObject.put(Keys.MSG, langPropsService.get("updateFailLabel")); jsonObject.put(Keys.MSG, langPropsService.get("updateFailLabel"));
} }
...@@ -205,7 +189,6 @@ public class PreferenceConsole { ...@@ -205,7 +189,6 @@ public class PreferenceConsole {
/** /**
* Gets signs. * Gets signs.
*
* <p> * <p>
* Renders the response with a json object, for example, * Renders the response with a json object, for example,
* <pre> * <pre>
...@@ -229,18 +212,16 @@ public class PreferenceConsole { ...@@ -229,18 +212,16 @@ public class PreferenceConsole {
throws Exception { throws Exception {
if (!userQueryService.isLoggedIn(request, response)) { if (!userQueryService.isLoggedIn(request, response)) {
response.sendError(HttpServletResponse.SC_FORBIDDEN); response.sendError(HttpServletResponse.SC_FORBIDDEN);
return; return;
} }
final JSONRenderer renderer = new JSONRenderer(); final JSONRenderer renderer = new JSONRenderer();
context.setRenderer(renderer); context.setRenderer(renderer);
try { try {
final JSONObject preference = preferenceQueryService.getPreference(); final JSONObject preference = preferenceQueryService.getPreference();
final JSONArray signs = new JSONArray(); final JSONArray signs = new JSONArray();
final JSONArray allSigns final JSONArray allSigns
= // includes the empty sign(id=0) = // includes the empty sign(id=0)
new JSONArray(preference.getString(Option.ID_C_SIGNS)); new JSONArray(preference.getString(Option.ID_C_SIGNS));
...@@ -250,16 +231,13 @@ public class PreferenceConsole { ...@@ -250,16 +231,13 @@ public class PreferenceConsole {
} }
final JSONObject ret = new JSONObject(); final JSONObject ret = new JSONObject();
renderer.setJSONObject(ret); renderer.setJSONObject(ret);
ret.put(Sign.SIGNS, signs); ret.put(Sign.SIGNS, signs);
ret.put(Keys.STATUS_CODE, true); ret.put(Keys.STATUS_CODE, true);
} catch (final Exception e) { } catch (final Exception e) {
LOGGER.log(Level.ERROR, e.getMessage(), e); LOGGER.log(Level.ERROR, e.getMessage(), e);
final JSONObject jsonObject = QueryResults.defaultResult(); final JSONObject jsonObject = QueryResults.defaultResult();
renderer.setJSONObject(jsonObject); renderer.setJSONObject(jsonObject);
jsonObject.put(Keys.MSG, langPropsService.get("getFailLabel")); jsonObject.put(Keys.MSG, langPropsService.get("getFailLabel"));
} }
...@@ -267,7 +245,6 @@ public class PreferenceConsole { ...@@ -267,7 +245,6 @@ public class PreferenceConsole {
/** /**
* Gets preference. * Gets preference.
*
* <p> * <p>
* Renders the response with a json object, for example, * Renders the response with a json object, for example,
* <pre> * <pre>
...@@ -330,12 +307,10 @@ public class PreferenceConsole { ...@@ -330,12 +307,10 @@ public class PreferenceConsole {
} }
final JSONRenderer renderer = new JSONRenderer(); final JSONRenderer renderer = new JSONRenderer();
context.setRenderer(renderer); context.setRenderer(renderer);
try { try {
final JSONObject preference = preferenceQueryService.getPreference(); final JSONObject preference = preferenceQueryService.getPreference();
if (null == preference) { if (null == preference) {
renderer.setJSONObject(QueryResults.defaultResult()); renderer.setJSONObject(QueryResults.defaultResult());
...@@ -350,7 +325,6 @@ public class PreferenceConsole { ...@@ -350,7 +325,6 @@ public class PreferenceConsole {
preference.put(Option.ID_C_FOOTER_CONTENT, footerContent); preference.put(Option.ID_C_FOOTER_CONTENT, footerContent);
final JSONObject ret = new JSONObject(); final JSONObject ret = new JSONObject();
renderer.setJSONObject(ret); renderer.setJSONObject(ret);
ret.put(Option.CATEGORY_C_PREFERENCE, preference); ret.put(Option.CATEGORY_C_PREFERENCE, preference);
ret.put(Keys.STATUS_CODE, true); ret.put(Keys.STATUS_CODE, true);
...@@ -358,7 +332,6 @@ public class PreferenceConsole { ...@@ -358,7 +332,6 @@ public class PreferenceConsole {
LOGGER.log(Level.ERROR, e.getMessage(), e); LOGGER.log(Level.ERROR, e.getMessage(), e);
final JSONObject jsonObject = QueryResults.defaultResult(); final JSONObject jsonObject = QueryResults.defaultResult();
renderer.setJSONObject(jsonObject); renderer.setJSONObject(jsonObject);
jsonObject.put(Keys.MSG, langPropsService.get("getFailLabel")); jsonObject.put(Keys.MSG, langPropsService.get("getFailLabel"));
} }
...@@ -367,8 +340,7 @@ public class PreferenceConsole { ...@@ -367,8 +340,7 @@ public class PreferenceConsole {
/** /**
* Updates the preference by the specified request. * Updates the preference by the specified request.
* *
* @param request the specified http servlet request, for example, <pre> * @param request the specified http servlet request, for example,
* {
* "preference": { * "preference": {
* "mostViewArticleDisplayCount": int, * "mostViewArticleDisplayCount": int,
* "recentCommentDisplayCount": int, * "recentCommentDisplayCount": int,
...@@ -401,9 +373,6 @@ public class PreferenceConsole { ...@@ -401,9 +373,6 @@ public class PreferenceConsole {
* "feedOutputMode: "", * "feedOutputMode: "",
* "feedOutputCnt": int * "feedOutputCnt": int
* } * }
* }, see {@link org.b3log.solo.model.Preference} for more details
* </pre>
*
* @param response the specified http servlet response * @param response the specified http servlet response
* @param context the specified http request context * @param context the specified http request context
* @throws Exception exception * @throws Exception exception
...@@ -417,18 +386,13 @@ public class PreferenceConsole { ...@@ -417,18 +386,13 @@ public class PreferenceConsole {
} }
final JSONRenderer renderer = new JSONRenderer(); final JSONRenderer renderer = new JSONRenderer();
context.setRenderer(renderer); context.setRenderer(renderer);
try { try {
final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, response); final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, response);
final JSONObject preference = requestJSONObject.getJSONObject(Option.CATEGORY_C_PREFERENCE); final JSONObject preference = requestJSONObject.getJSONObject(Option.CATEGORY_C_PREFERENCE);
final JSONObject ret = new JSONObject(); final JSONObject ret = new JSONObject();
renderer.setJSONObject(ret); renderer.setJSONObject(ret);
if (isInvalid(preference, ret)) { if (isInvalid(preference, ret)) {
return; return;
} }
...@@ -445,7 +409,6 @@ public class PreferenceConsole { ...@@ -445,7 +409,6 @@ public class PreferenceConsole {
LOGGER.log(Level.ERROR, e.getMessage(), e); LOGGER.log(Level.ERROR, e.getMessage(), e);
final JSONObject jsonObject = QueryResults.defaultResult(); final JSONObject jsonObject = QueryResults.defaultResult();
renderer.setJSONObject(jsonObject); renderer.setJSONObject(jsonObject);
jsonObject.put(Keys.MSG, e.getMessage()); jsonObject.put(Keys.MSG, e.getMessage());
} }
...@@ -453,7 +416,6 @@ public class PreferenceConsole { ...@@ -453,7 +416,6 @@ public class PreferenceConsole {
/** /**
* Gets Qiniu preference. * Gets Qiniu preference.
*
* <p> * <p>
* Renders the response with a json object, for example, * Renders the response with a json object, for example,
* <pre> * <pre>
...@@ -486,7 +448,6 @@ public class PreferenceConsole { ...@@ -486,7 +448,6 @@ public class PreferenceConsole {
try { try {
final JSONObject qiniu = optionQueryService.getOptions(Option.CATEGORY_C_QINIU); final JSONObject qiniu = optionQueryService.getOptions(Option.CATEGORY_C_QINIU);
if (null == qiniu) { if (null == qiniu) {
renderer.setJSONObject(QueryResults.defaultResult()); renderer.setJSONObject(QueryResults.defaultResult());
...@@ -494,7 +455,6 @@ public class PreferenceConsole { ...@@ -494,7 +455,6 @@ public class PreferenceConsole {
} }
final JSONObject ret = new JSONObject(); final JSONObject ret = new JSONObject();
renderer.setJSONObject(ret); renderer.setJSONObject(ret);
ret.put(Option.CATEGORY_C_QINIU, qiniu); ret.put(Option.CATEGORY_C_QINIU, qiniu);
ret.put(Keys.STATUS_CODE, true); ret.put(Keys.STATUS_CODE, true);
...@@ -502,7 +462,6 @@ public class PreferenceConsole { ...@@ -502,7 +462,6 @@ public class PreferenceConsole {
LOGGER.log(Level.ERROR, e.getMessage(), e); LOGGER.log(Level.ERROR, e.getMessage(), e);
final JSONObject jsonObject = QueryResults.defaultResult(); final JSONObject jsonObject = QueryResults.defaultResult();
renderer.setJSONObject(jsonObject); renderer.setJSONObject(jsonObject);
jsonObject.put(Keys.MSG, langPropsService.get("getFailLabel")); jsonObject.put(Keys.MSG, langPropsService.get("getFailLabel"));
} }
...@@ -511,15 +470,11 @@ public class PreferenceConsole { ...@@ -511,15 +470,11 @@ public class PreferenceConsole {
/** /**
* Updates the Qiniu preference by the specified request. * Updates the Qiniu preference by the specified request.
* *
* @param request the specified http servlet request, for example, <pre> * @param request the specified http servlet request, for example,
* {
* "qiniuAccessKey": "", * "qiniuAccessKey": "",
* "qiniuSecretKey": "", * "qiniuSecretKey": "",
* "qiniuDomain": "", * "qiniuDomain": "",
* "qiniuBucket": "" * "qiniuBucket": ""
* }, see {@link org.b3log.solo.model.Option} for more details
* </pre>
*
* @param response the specified http servlet response * @param response the specified http servlet response
* @param context the specified http request context * @param context the specified http request context
* @throws Exception exception * @throws Exception exception
...@@ -596,11 +551,9 @@ public class PreferenceConsole { ...@@ -596,11 +551,9 @@ public class PreferenceConsole {
responseObject.put(Keys.STATUS_CODE, false); responseObject.put(Keys.STATUS_CODE, false);
final StringBuilder errMsgBuilder = new StringBuilder('[' + langPropsService.get("paramSettingsLabel")); final StringBuilder errMsgBuilder = new StringBuilder('[' + langPropsService.get("paramSettingsLabel"));
errMsgBuilder.append(" - "); errMsgBuilder.append(" - ");
String input = preference.optString(Option.ID_C_EXTERNAL_RELEVANT_ARTICLES_DISPLAY_CNT); String input = preference.optString(Option.ID_C_EXTERNAL_RELEVANT_ARTICLES_DISPLAY_CNT);
if (!isNonNegativeInteger(input)) { if (!isNonNegativeInteger(input)) {
errMsgBuilder.append(langPropsService.get("externalRelevantArticlesDisplayCntLabel")).append("] ").append( errMsgBuilder.append(langPropsService.get("externalRelevantArticlesDisplayCntLabel")).append("] ").append(
langPropsService.get("nonNegativeIntegerOnlyLabel")); langPropsService.get("nonNegativeIntegerOnlyLabel"));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment