protected void handleValidators(ValidatorException validatorException, Request request, Response response) { var code = Optional.ofNullable(validatorException.getCode()).orElse(500); if (request.isAjax() || request.contentType().toLowerCase().contains("json")) { response.json(RestResponse.fail(code, validatorException.getMessage())); } else { this.handleException(validatorException, request, response); } }
protected void handleValidators(ValidatorException validatorException, Request request, Response response) { var code = Optional.ofNullable(validatorException.getCode()).orElse(500); if (request.isAjax() || request.contentType().toLowerCase().contains("json")) { response.json(RestResponse.fail(code, validatorException.getMessage())); } else { this.handleException(validatorException, request, response); } }
response.json(RestResponse.fail(NotFoundException.STATUS, "Not Found [" + request.uri() + "]")); } else { var page404 = Optional.ofNullable(blade.environment().get(ENV_KEY_PAGE_404, null)); response.json(RestResponse.fail(MethodNotAllowedException.STATUS, e.getMessage())); } else { response.text(e.getMessage());
response.json(RestResponse.fail(NotFoundException.STATUS, "Not Found [" + request.uri() + "]")); } else { var page404 = Optional.ofNullable(blade.environment().get(ENV_KEY_PAGE_404, null)); response.json(RestResponse.fail(MethodNotAllowedException.STATUS, e.getMessage())); } else { response.text(e.getMessage());
public RestResponse asResponse(){ return RestResponse.fail(this.getCode(), this.getMsg()); } }
@Route(value = "save", method = HttpMethod.POST) @JSON public RestResponse saveTpl(@Param String fileName, @Param String content) { if (StringKit.isBlank(fileName)) { return RestResponse.fail("缺少参数,请重试"); } String themePath = Const.CLASSPATH + File.separatorChar + "templates" + File.separatorChar + "themes" + File.separatorChar + Commons.site_theme(); String filePath = themePath + File.separatorChar + fileName; try { if (Files.exists(Paths.get(filePath))) { byte[] rf_wiki_byte = content.getBytes("UTF-8"); Files.write(Paths.get(filePath), rf_wiki_byte); } else { Files.createFile(Paths.get(filePath)); byte[] rf_wiki_byte = content.getBytes("UTF-8"); Files.write(Paths.get(filePath), rf_wiki_byte); } return RestResponse.ok(); } catch (Exception e) { log.error("写入文件失败", e); return RestResponse.fail("写入文件失败: " + e.getMessage()); } }
protected void handleValidators(ValidatorException validatorException, Request request, Response response) { var code = Optional.ofNullable(validatorException.getCode()).orElse(500); if (request.isAjax() || request.contentType().toLowerCase().contains("json")) { response.json(RestResponse.fail(code, validatorException.getMessage())); } else { this.handleException(validatorException, request, response); } }
/** * 创建支付订单 * * @param order * @param request * @return */ @PostRoute("/pay_order") @JSON public RestResponse<PayOrderResponse> createOrder(Order order, Request request) { try { Validator.orderParam(order); if (null == request.header("") || null == request.userAgent()) { return RestResponse.fail("非法请求"); } return RestResponse.ok(makeMoneyService.createOrder(order)); } catch (ValidatorException e) { return RestResponse.fail(e.getMessage()); } catch (Exception e) { log.error("支付出现异常", e); return RestResponse.fail(e.getMessage()); } }
@Override public void handle(Exception e) { if (e instanceof ValidateException) { ValidateException validateException = (ValidateException) e; String msg = validateException.getErrMsg(); WebContext.response().json(RestResponse.fail(msg)); } else { super.handle(e); } }
@Route(value = "modify", method = HttpMethod.POST) @JSON public RestResponse modifyArticle(@Valid Contents contents) { if (null == contents || null == contents.getCid()) { return RestResponse.fail("缺少参数,请重试"); } try { Integer cid = contents.getCid(); contents.setType(Types.PAGE); contentsService.updateArticle(contents); return RestResponse.ok(cid); } catch (Exception e) { String msg = "页面编辑失败"; if (e instanceof TipException) { msg = e.getMessage(); } else { log.error(msg, e); } return RestResponse.fail(msg); } }
@Route(value = "delete") @JSON public RestResponse delete(@Param int mid) { try { metasService.delete(mid); siteService.cleanCache(Types.C_STATISTICS); } catch (Exception e) { String msg = "删除失败"; if (e instanceof TipException) { msg = e.getMessage(); } else { log.error(msg, e); } return RestResponse.fail(msg); } return RestResponse.ok(); }
@Route(value = "save", method = HttpMethod.POST) @JSON public RestResponse saveCategory(@Param String cname, @Param Integer mid) { try { metasService.saveMeta(Types.CATEGORY, cname, mid); siteService.cleanCache(Types.C_STATISTICS); } catch (Exception e) { String msg = "分类保存失败"; if (e instanceof TipException) { msg = e.getMessage(); } else { log.error(msg, e); } return RestResponse.fail(msg); } return RestResponse.ok(); }
@PostRoute("password") @JSON public RestResponse updatePassword(@Param PasswordParam passwordParam) { Option option = select().from(Option.class) .where(Option::getKey, Constant.SAFE_PASSWORD).one(); String base64 = Base64.getEncoder().encodeToString(option.getValue().getBytes()); boolean success = PasswordKit.checkPassword(passwordParam.getOldPass(), new String(Base64.getDecoder().decode(base64))); if (!success) { return RestResponse.fail("旧密码输入错误, 无法修改!"); } String pass = PasswordKit.hashPassword(passwordParam.getNewPass()); update().from(Option.class) .set(Option::getValue, pass) .where(Option::getKey, Constant.SAFE_PASSWORD).execute(); return RestResponse.ok(); }
@PostRoute("login") @JSON public RestResponse doLogin(@BodyParam LoginParam loginParam, Session session) { Validator.authParam(loginParam.getUsername(), loginParam.getPassword()); long username = select().from(Option.class) .where(Option::getKey, Constant.SAFE_USERNAME) .and(Option::getValue, loginParam.getUsername()) .count(); if (username == 0) { return RestResponse.fail("要饭先把名字输对,O不OK?"); } Option option = select().from(Option.class) .where(Option::getKey, Constant.SAFE_PASSWORD).one(); String base64 = Base64.getEncoder().encodeToString(option.getValue().getBytes()); boolean success = PasswordKit.checkPassword(loginParam.getPassword(), new String(Base64.getDecoder().decode(base64))); if (success) { session.attribute(Constant.LOGIN_SESSION_KEY, username); return RestResponse.ok(); } return RestResponse.fail("密码都输不对的人没资格要饭 (σ`д′)σ"); }
@Route(value = "delete") @JSON public RestResponse delete(@Param int cid, Request request) { try { contentsService.delete(cid); siteService.cleanCache(Types.C_STATISTICS); new Logs(LogActions.DEL_PAGE, cid + "", request.address(), this.getUid()).save(); } catch (Exception e) { String msg = "页面删除失败"; if (e instanceof TipException) { msg = e.getMessage(); } else { log.error(msg, e); } return RestResponse.fail(msg); } return RestResponse.ok(); } }
@PostRoute(value = "status") @JSON public RestResponse delete(@Param Integer coid, @Param String status) { try { Comments comments = new Comments(); comments.setCoid(coid); comments.setStatus(status); comments.update(); siteService.cleanCache(Types.C_STATISTICS); } catch (Exception e) { String msg = "操作失败"; if (e instanceof TipException) { msg = e.getMessage(); } else { log.error(msg, e); } return RestResponse.fail(msg); } return RestResponse.ok(); }
@Route(value = "publish", method = HttpMethod.POST) @JSON public RestResponse publishPage(@Valid Contents contents) { Users users = this.user(); contents.setType(Types.PAGE); contents.setAllowPing(true); contents.setAuthorId(users.getUid()); try { contentsService.publish(contents); siteService.cleanCache(Types.C_STATISTICS); } catch (Exception e) { String msg = "页面发布失败"; if (e instanceof TipException) { msg = e.getMessage(); } else { log.error(msg, e); } return RestResponse.fail(msg); } return RestResponse.ok(); }
/** * 保存系统设置 */ @Route(value = "setting", method = HttpMethod.POST) @JSON public RestResponse saveSetting(@Param String site_theme, Request request) { try { Map<String, List<String>> querys = request.parameters(); optionsService.saveOptions(querys); Environment config = Environment.of(optionsService.getOptions()); TaleConst.OPTIONS = config; new Logs(LogActions.SYS_SETTING, JsonKit.toString(querys), request.address(), this.getUid()).save(); return RestResponse.ok(); } catch (Exception e) { String msg = "保存设置失败"; if (e instanceof TipException) { msg = e.getMessage(); } else { log.error(msg, e); } return RestResponse.fail(msg); } }
@PostRoute("install") @JSON public RestResponse doInstall(@BodyParam InstallParam installParam) throws IOException { if (Utils.isInstall()) { return RestResponse.fail("请勿重复安装!"); } log.info("安装参数: {}", installParam); Validator.installParam(installParam); delete().from(Option.class).execute(); optionService.updatePayConfig(installParam); optionService.save(Constant.TODAY_COUNT, "0"); optionService.save(Constant.TOTAL_COUNT, "0"); optionService.save(Constant.TODAY_AMOUNT, "0"); optionService.save(Constant.TOTAL_AMOUNT, "0"); optionService.save(Constant.PAGE_SIZE, "10"); optionService.save(Constant.COMMENT_MIN_SIZE, "4"); optionService.save(Constant.COMMENT_MAX_SIZE, "500"); optionService.save(Constant.AMOUNT_MIN, "0.1"); optionService.save(Constant.AMOUNT_MAX, "1000"); Bootstrap.refreshConfig(); Bootstrap.payApi = PayApi.getPayApi(installParam.getPlatform()); File lock = new File(Utils.CLASSPATH + "install.lock"); lock.createNewFile(); return RestResponse.ok(); }