@RequestMapping(params = "method=changePassword", method = RequestMethod.GET) public String changePassword( HttpServletRequest request, HttpServletResponse response, @RequestParam("userName") String userName, @RequestParam("password") String password, ModelMap modelMap) { userName = userName.trim(); password = password.trim(); if (StringUtils.isBlank(userName) || DiamondUtils.hasInvalidChar(userName.trim())) { modelMap.addAttribute("message", "无效的用户名"); return listUser(request, response, modelMap); } if (StringUtils.isBlank(password) || DiamondUtils.hasInvalidChar(password.trim())) { modelMap.addAttribute("message", "无效的新密码"); return listUser(request, response, modelMap); } if (this.adminService.updatePassword(userName, password)) { modelMap.addAttribute("message", "更改成功,下次登录请用新密码!"); } else { modelMap.addAttribute("message", "更改失败!"); } return listUser(request, response, modelMap); }
@RequestMapping(params = "method=upload", method = RequestMethod.POST) public String upload( HttpServletRequest request, HttpServletResponse response, @RequestParam("dataId") String dataId, @RequestParam("group") String group, @RequestParam("contentFile") MultipartFile contentFile, ModelMap modelMap) { response.setCharacterEncoding(Constants.ENCODE); boolean checkSuccess = true; String errorMessage = "参数错误"; if (StringUtils.isBlank(dataId) || DiamondUtils.hasInvalidChar(dataId.trim())) { checkSuccess = false; errorMessage = "无效的DataId"; } if (StringUtils.isBlank(group) || DiamondUtils.hasInvalidChar(group.trim())) { checkSuccess = false; errorMessage = "无效的分组"; } String content = getContentFromFile(contentFile); if (StringUtils.isBlank(content)) { checkSuccess = false; errorMessage = "无效的内容"; } if (!checkSuccess) { modelMap.addAttribute("message", errorMessage); return "/admin/config/upload"; } this.configService.addConfigInfo(dataId, group, content); modelMap.addAttribute("message", "提交成功!"); return listConfig(request, response, dataId, group, 1, 20, modelMap); }
@RequestMapping(params = "method=addUser", method = RequestMethod.POST) public String addUser( HttpServletRequest request, HttpServletResponse response, @RequestParam("userName") String userName, @RequestParam("password") String password, ModelMap modelMap) { if (StringUtils.isBlank(userName) || DiamondUtils.hasInvalidChar(userName.trim())) { modelMap.addAttribute("message", "无效的用户名"); return listUser(request, response, modelMap); } if (StringUtils.isBlank(password) || DiamondUtils.hasInvalidChar(password.trim())) { modelMap.addAttribute("message", "无效的密码"); return "/admin/user/new"; } if (this.adminService.addUser(userName, password)) modelMap.addAttribute("message", "添加成功!"); else modelMap.addAttribute("message", "添加失败!"); return listUser(request, response, modelMap); }
@RequestMapping(params = "method=deleteUser", method = RequestMethod.GET) public String deleteUser( HttpServletRequest request, HttpServletResponse response, @RequestParam("userName") String userName, ModelMap modelMap) { if (StringUtils.isBlank(userName) || DiamondUtils.hasInvalidChar(userName.trim())) { modelMap.addAttribute("message", "无效的用户名"); return listUser(request, response, modelMap); } if (this.adminService.removeUser(userName)) { modelMap.addAttribute("message", "删除成功!"); } else { modelMap.addAttribute("message", "删除失败!"); } return listUser(request, response, modelMap); }
@RequestMapping(params = "method=batchAddOrUpdate", method = RequestMethod.POST) public String batchAddOrUpdate( HttpServletRequest request, HttpServletResponse response, @RequestParam("allDataIdAndContent") String allDataIdAndContent, @RequestParam("group") String group, ModelMap modelMap) { response.setCharacterEncoding(Constants.ENCODE); // 这里抛出的异常, 会产生一个500错误, 返回给sdk, sdk会将500错误记录到日志中 if (StringUtils.isBlank(allDataIdAndContent)) { throw new IllegalArgumentException("批量写, allDataIdAndContent不能为空"); } // group对批量操作的每一条数据都相同, 不需要在for循环里面进行判断 if (StringUtils.isBlank(group) || DiamondUtils.hasInvalidChar(group)) { throw new IllegalArgumentException("批量写, group不能为空或者包含非法字符"); } String[] dataIdAndContentArray = allDataIdAndContent.split(Constants.LINE_SEPARATOR); group = group.trim(); List<ConfigInfoEx> configInfoExList = new ArrayList<ConfigInfoEx>(); for (String dataIdAndContent : dataIdAndContentArray) { String dataId = dataIdAndContent.substring(0, dataIdAndContent.indexOf(Constants.WORD_SEPARATOR)); String content = dataIdAndContent.substring(dataIdAndContent.indexOf(Constants.WORD_SEPARATOR) + 1); ConfigInfoEx configInfoEx = new ConfigInfoEx(); configInfoEx.setDataId(dataId); configInfoEx.setGroup(group); configInfoEx.setContent(content); try { // 判断dataId是否包含非法字符 if (StringUtils.isBlank(dataId) || DiamondUtils.hasInvalidChar(dataId)) { // 这里抛出的异常, 会在下面catch, 然后设置状态, 保证一个dataId的异常不会影响其他dataId throw new IllegalArgumentException("批量写, dataId不能包含非法字符"); } // 判断内容是否为空 if (StringUtils.isBlank(content)) { throw new IllegalArgumentException("批量写, 内容不能为空"); } // 查询数据库 ConfigInfo configInfo = this.configService.findConfigInfo(dataId, group); if (configInfo == null) { // 数据不存在, 新增 this.configService.addConfigInfo(dataId, group, content); // 新增成功, 设置状态码 configInfoEx.setStatus(Constants.BATCH_ADD_SUCCESS); configInfoEx.setMessage("add success"); } else { // 数据存在, 更新 this.configService.updateConfigInfo(dataId, group, content); // 更新成功, 设置状态码 configInfoEx.setStatus(Constants.BATCH_UPDATE_SUCCESS); configInfoEx.setMessage("update success"); } } catch (Exception e) { log.error("批量写这条数据时出错, dataId=" + dataId + ",group=" + group + ",content=" + content, e); // 出现异常, 设置异常状态码 configInfoEx.setStatus(Constants.BATCH_OP_ERROR); configInfoEx.setMessage("batch write error: " + e.getMessage()); } configInfoExList.add(configInfoEx); } String json = null; try { json = JSONUtils.serializeObject(configInfoExList); } catch (Exception e) { log.error("批量写, 结果序列化出错, json=" + json, e); } modelMap.addAttribute("json", json); return "/admin/config/batch_result"; }