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>
...@@ -112,39 +111,35 @@ public class PreferenceConsole { ...@@ -112,39 +111,35 @@ public class PreferenceConsole {
* </pre> * </pre>
* </p> * </p>
* *
* @param request the specified http servlet request * @param request the specified http servlet request
* @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
*/ */
@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,51 +148,40 @@ public class PreferenceConsole { ...@@ -153,51 +148,40 @@ 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
*/ */
@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>
...@@ -219,9 +202,9 @@ public class PreferenceConsole { ...@@ -219,9 +202,9 @@ public class PreferenceConsole {
* </pre> * </pre>
* </p> * </p>
* *
* @param request the specified http servlet request * @param request the specified http servlet request
* @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
*/ */
@RequestProcessing(value = "/console/signs/", method = HTTPRequestMethod.GET) @RequestProcessing(value = "/console/signs/", method = HTTPRequestMethod.GET)
...@@ -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>
...@@ -316,9 +293,9 @@ public class PreferenceConsole { ...@@ -316,9 +293,9 @@ public class PreferenceConsole {
* </pre> * </pre>
* </p> * </p>
* *
* @param request the specified http servlet request * @param request the specified http servlet request
* @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
*/ */
@RequestProcessing(value = PREFERENCE_URI_PREFIX, method = HTTPRequestMethod.GET) @RequestProcessing(value = PREFERENCE_URI_PREFIX, method = HTTPRequestMethod.GET)
...@@ -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,45 +340,41 @@ public class PreferenceConsole { ...@@ -367,45 +340,41 @@ 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, * "mostUsedTagDisplayCount": int,
* "mostUsedTagDisplayCount": int, * "articleListDisplayCount": int,
* "articleListDisplayCount": int, * "articleListPaginationWindowSize": int,
* "articleListPaginationWindowSize": int, * "mostCommentArticleDisplayCount": int,
* "mostCommentArticleDisplayCount": int, * "externalRelevantArticlesDisplayCount": int,
* "externalRelevantArticlesDisplayCount": int, * "relevantArticlesDisplayCount": int,
* "relevantArticlesDisplayCount": int, * "randomArticlesDisplayCount": int,
* "randomArticlesDisplayCount": int, * "blogTitle": "",
* "blogTitle": "", * "blogSubtitle": "",
* "blogSubtitle": "", * "skinDirName": "",
* "skinDirName": "", * "localeString": "",
* "localeString": "", * "timeZoneId": "",
* "timeZoneId": "", * "noticeBoard": "",
* "noticeBoard": "", * "footerContent": "",
* "footerContent": "", * "htmlHead": "",
* "htmlHead": "", * "metaKeywords": "",
* "metaKeywords": "", * "metaDescription": "",
* "metaDescription": "", * "enableArticleUpdateHint": boolean,
* "enableArticleUpdateHint": boolean, * "signs": [{
* "signs": [{ * "oId": "",
* "oId": "", * "signHTML": ""
* "signHTML": "" * }, ...],
* }, ...], * "allowVisitDraftViaPermalink": boolean,
* "allowVisitDraftViaPermalink": boolean, * "allowRegister": boolean,
* "allowRegister": boolean, * "articleListStyle": "",
* "articleListStyle": "", * "commentable": boolean,
* "commentable": boolean, * "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
*/ */
@RequestProcessing(value = PREFERENCE_URI_PREFIX, method = HTTPRequestMethod.PUT) @RequestProcessing(value = PREFERENCE_URI_PREFIX, method = HTTPRequestMethod.PUT)
...@@ -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>
...@@ -467,14 +429,14 @@ public class PreferenceConsole { ...@@ -467,14 +429,14 @@ public class PreferenceConsole {
* </pre> * </pre>
* </p> * </p>
* *
* @param request the specified http servlet request * @param request the specified http servlet request
* @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
*/ */
@RequestProcessing(value = PREFERENCE_URI_PREFIX + "qiniu", method = HTTPRequestMethod.GET) @RequestProcessing(value = PREFERENCE_URI_PREFIX + "qiniu", method = HTTPRequestMethod.GET)
public void getQiniuPreference(final HttpServletRequest request, final HttpServletResponse response, public void getQiniuPreference(final HttpServletRequest request, final HttpServletResponse response,
final HTTPRequestContext context) throws Exception { final HTTPRequestContext context) throws Exception {
if (!userQueryService.isAdminLoggedIn(request)) { if (!userQueryService.isAdminLoggedIn(request)) {
response.sendError(HttpServletResponse.SC_FORBIDDEN); response.sendError(HttpServletResponse.SC_FORBIDDEN);
...@@ -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,22 +470,18 @@ public class PreferenceConsole { ...@@ -511,22 +470,18 @@ 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
*/ */
@RequestProcessing(value = PREFERENCE_URI_PREFIX + "qiniu", method = HTTPRequestMethod.PUT) @RequestProcessing(value = PREFERENCE_URI_PREFIX + "qiniu", method = HTTPRequestMethod.PUT)
public void updateQiniu(final HttpServletRequest request, final HttpServletResponse response, public void updateQiniu(final HttpServletRequest request, final HttpServletResponse response,
final HTTPRequestContext context) throws Exception { final HTTPRequestContext context) throws Exception {
if (!userQueryService.isAdminLoggedIn(request)) { if (!userQueryService.isAdminLoggedIn(request)) {
response.sendError(HttpServletResponse.SC_FORBIDDEN); response.sendError(HttpServletResponse.SC_FORBIDDEN);
...@@ -588,7 +543,7 @@ public class PreferenceConsole { ...@@ -588,7 +543,7 @@ public class PreferenceConsole {
/** /**
* Checks whether the specified preference is invalid and sets the specified response object. * Checks whether the specified preference is invalid and sets the specified response object.
* *
* @param preference the specified preference * @param preference the specified preference
* @param responseObject the specified response object * @param responseObject the specified response object
* @return {@code true} if the specified preference is invalid, returns {@code false} otherwise * @return {@code true} if the specified preference is invalid, returns {@code false} otherwise
*/ */
...@@ -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