Java Code Examples for com.blade.mvc.ui.RestResponse#fail()

The following examples show how to use com.blade.mvc.ui.RestResponse#fail() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PageController.java    From tale with MIT License 6 votes vote down vote up
@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);
    }
}
 
Example 2
Source File: ParameterInjectionExampleController.java    From tutorials with MIT License 6 votes vote down vote up
@PostRoute("/params-file") // DO NOT USE A SLASH WITHIN THE ROUTE OR IT WILL BREAK (?)
@JSON
public RestResponse<?> fileParam(@MultipartParam FileItem fileItem) throws Exception {
    try {
        byte[] fileContent = fileItem.getData();

        log.debug("Saving the uploaded file");
        java.nio.file.Path tempFile = Files.createTempFile("baeldung_tempfiles", ".tmp");
        Files.write(tempFile, fileContent, StandardOpenOption.WRITE);

        return RestResponse.ok();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        return RestResponse.fail(e.getMessage());
    }
}
 
Example 3
Source File: IndexController.java    From tale with MIT License 6 votes vote down vote up
/**
 * 系统备份
 *
 * @return
 */
@Route(value = "backup", method = HttpMethod.POST)
@JSON
public RestResponse backup(@Param String bk_type, @Param String bk_path,
                           Request request) {
    if (StringKit.isBlank(bk_type)) {
        return RestResponse.fail("请确认信息输入完整");
    }

    try {
        BackResponse backResponse = siteService.backup(bk_type, bk_path, "yyyyMMddHHmm");
        new Logs(LogActions.SYS_BACKUP, null, request.address(), this.getUid()).save();
        return RestResponse.ok(backResponse);
    } catch (Exception e) {
        String msg = "备份失败";
        if (e instanceof TipException) {
            msg = e.getMessage();
        } else {
            log.error(msg, e);
        }
        return RestResponse.fail(msg);
    }
}
 
Example 4
Source File: IndexController.java    From tale with MIT License 6 votes vote down vote up
/**
 * 保存系统设置
 */
@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);
    }
}
 
Example 5
Source File: ArticleController.java    From tale with MIT License 6 votes vote down vote up
/**
 * 删除文章操作
 *
 * @param cid
 * @param request
 * @return
 */
@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_ARTICLE, 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();
}
 
Example 6
Source File: ArticleController.java    From tale with MIT License 6 votes vote down vote up
/**
 * 修改文章操作
 *
 * @return
 */
@PostRoute(value = "modify")
@JSON
public RestResponse modifyArticle(@Valid Contents contents) {
    try {
        if (null == contents || null == contents.getCid()) {
            return RestResponse.fail("缺少参数,请重试");
        }
        Integer cid = contents.getCid();
        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);
    }
}
 
Example 7
Source File: ArticleController.java    From tale with MIT License 6 votes vote down vote up
/**
 * 发布文章操作
 *
 * @return
 */
@PostRoute(value = "publish")
@JSON
public RestResponse publishArticle(@Valid Contents contents) {
    Users users = this.user();
    contents.setType(Types.ARTICLE);
    contents.setAuthorId(users.getUid());
    if (StringKit.isBlank(contents.getCategories())) {
        contents.setCategories("默认");
    }

    try {
        Integer cid = contentsService.publish(contents);
        siteService.cleanCache(Types.C_STATISTICS);
        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);
    }
}
 
Example 8
Source File: CommentController.java    From tale with MIT License 6 votes vote down vote up
@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();
}
 
Example 9
Source File: CommentController.java    From tale with MIT License 6 votes vote down vote up
/**
 * 删除一条评论
 *
 * @param coid
 * @return
 */
@PostRoute(value = "delete")
@JSON
public RestResponse delete(@Param Integer coid) {
    try {
        Comments comments = commentsService.byId(coid);
        if (null == comments) {
            return RestResponse.fail("不存在该评论");
        }
        commentsService.delete(coid, comments.getCid());
        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();
}
 
Example 10
Source File: CategoryController.java    From tale with MIT License 6 votes vote down vote up
@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();
}
 
Example 11
Source File: CategoryController.java    From tale with MIT License 6 votes vote down vote up
@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();
}
 
Example 12
Source File: PageController.java    From tale with MIT License 6 votes vote down vote up
@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();
}
 
Example 13
Source File: PageController.java    From tale with MIT License 6 votes vote down vote up
@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();
}
 
Example 14
Source File: AttachController.java    From tale with MIT License 5 votes vote down vote up
@Route(value = "delete")
@JSON
public RestResponse delete(@Param Integer id, Request request) {
    try {
        Attach attach = new Attach().find(id);
        if (null == attach) {
            return RestResponse.fail("不存在该附件");
        }
        String fkey = attach.getFkey();
        siteService.cleanCache(Types.C_STATISTICS);
        String             filePath = CLASSPATH.substring(0, CLASSPATH.length() - 1) + fkey;
        java.nio.file.Path path     = Paths.get(filePath);
        log.info("Delete attach: [{}]", filePath);
        if (Files.exists(path)) {
            Files.delete(path);
        }
        attach.delete(id);
        new Logs(LogActions.DEL_ATTACH, fkey, 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();
}
 
Example 15
Source File: ThemeController.java    From tale with MIT License 5 votes vote down vote up
/**
 * 保存主题配置项
 *
 * @param request
 * @return
 */
@PostRoute(value = "setting")
@JSON
public RestResponse saveSetting(Request request) {
    try {
        Map<String, List<String>> query = request.parameters();

        // theme_milk_options => {  }
        String currentTheme = Commons.site_theme();
        String key          = "theme_" + currentTheme + "_options";

        Map<String, String> options = new HashMap<>();
        query.forEach((k, v) -> options.put(k, v.get(0)));

        optionsService.saveOption(key, JsonKit.toString(options));

        TaleConst.OPTIONS = Environment.of(optionsService.getOptions());
        new Logs(LogActions.THEME_SETTING, JsonKit.toString(query), 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);
    }
}
 
Example 16
Source File: IndexController.java    From tale with MIT License 5 votes vote down vote up
/**
 * 修改密码
 */
@Route(value = "password", method = HttpMethod.POST)
@JSON
public RestResponse upPwd(@Param String old_password, @Param String password, Request request) {
    Users users = this.user();
    if (StringKit.isBlank(old_password) || StringKit.isBlank(password)) {
        return RestResponse.fail("请确认信息输入完整");
    }

    if (!users.getPassword().equals(EncryptKit.md5(users.getUsername() + old_password))) {
        return RestResponse.fail("旧密码错误");
    }
    if (password.length() < 6 || password.length() > 14) {
        return RestResponse.fail("请输入6-14位密码");
    }

    try {
        Users  temp = new Users();
        String pwd  = EncryptKit.md5(users.getUsername() + password);
        temp.setPassword(pwd);
        temp.update(users.getUid());
        new Logs(LogActions.UP_PWD, null, 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);
    }
}
 
Example 17
Source File: CommentController.java    From tale with MIT License 4 votes vote down vote up
@PostRoute(value = "")
@JSON
public RestResponse reply(@Param Integer coid, @Param String content, Request request) {
    if (null == coid || StringKit.isBlank(content)) {
        return RestResponse.fail("请输入完整后评论");
    }

    if (content.length() > 2000) {
        return RestResponse.fail("请输入2000个字符以内的回复");
    }
    Comments c = commentsService.byId(coid);
    if (null == c) {
        return RestResponse.fail("不存在该评论");
    }
    Users users = this.user();
    content = TaleUtils.cleanXSS(content);
    content = EmojiParser.parseToAliases(content);

    Comments comments = new Comments();
    comments.setAuthor(users.getUsername());
    comments.setAuthor_id(users.getUid());
    comments.setCid(c.getCid());
    comments.setIp(request.address());
    comments.setUrl(users.getHome_url());
    comments.setContent(content);
    if (StringKit.isNotBlank(users.getEmail())) {
        comments.setMail(users.getEmail());
    } else {
        comments.setMail("[email protected]");
    }
    comments.setParent(coid);
    try {
        commentsService.saveComment(comments);
        siteService.cleanCache(Types.C_STATISTICS);
        return RestResponse.ok();
    } catch (Exception e) {
        String msg = "回复失败";
        if (e instanceof TipException) {
            msg = e.getMessage();
        } else {
            log.error(msg, e);
        }
        return RestResponse.fail(msg);
    }
}
 
Example 18
Source File: AuthController.java    From tale with MIT License 4 votes vote down vote up
@Route(value = "login", method = HttpMethod.POST)
@JSON
public RestResponse doLogin(LoginParam loginParam, Request request,
                            Session session, Response response) {

    Integer error_count = cache.get("login_error_count");
    try {
        error_count = null == error_count ? 0 : error_count;
        if (null != error_count && error_count > 3) {
            return RestResponse.fail("您输入密码已经错误超过3次,请10分钟后尝试");
        }

        long count = new Users().where("username", loginParam.getUsername()).count();
        if (count < 1) {
            return RestResponse.fail("不存在该用户");
        }
        String pwd = EncryptKit.md5(loginParam.getUsername(), loginParam.getPassword());

        Users user = new Users().where("username", loginParam.getUsername()).and("password", pwd).find();
        if (null == user) {
            return RestResponse.fail("用户名或密码错误");
        }
        session.attribute(TaleConst.LOGIN_SESSION_KEY, user);
        if (StringKit.isNotBlank(loginParam.getRemeberMe())) {
            TaleUtils.setCookie(response, user.getUid());
        }

        Users temp = new Users();
        temp.setLogged(DateKit.nowUnix());
        temp.update(user.getUid());
        log.info("登录成功:{}", loginParam.getUsername());
        cache.set("login_error_count", 0);

        new Logs(LogActions.LOGIN, loginParam.getUsername(), request.address(), user.getUid()).save();
    } catch (Exception e) {
        error_count += 1;
        cache.set("login_error_count", error_count, 10 * 60);
        String msg = "登录失败";
        if (e instanceof TipException) {
            msg = e.getMessage();
        } else {
            log.error(msg, e);
        }
        return RestResponse.fail(msg);
    }
    return RestResponse.ok();
}
 
Example 19
Source File: IndexController.java    From tale with MIT License 4 votes vote down vote up
/**
 * 评论操作
 */
@CsrfToken(valid = true)
@PostRoute(value = "comment")
@JSON
public RestResponse comment(Request request, Response response,
                            @HeaderParam String Referer, @Valid Comments comments) {

    if (StringKit.isBlank(Referer)) {
        return RestResponse.fail(ErrorCode.BAD_REQUEST);
    }

    if (!Referer.startsWith(Commons.site_url())) {
        return RestResponse.fail("非法评论来源");
    }

    String  val   = request.address() + ":" + comments.getCid();
    Integer count = cache.hget(Types.COMMENTS_FREQUENCY, val);
    if (null != count && count > 0) {
        return RestResponse.fail("您发表评论太快了,请过会再试");
    }

    comments.setAuthor(TaleUtils.cleanXSS(comments.getAuthor()));
    comments.setContent(TaleUtils.cleanXSS(comments.getContent()));

    comments.setAuthor(EmojiParser.parseToAliases(comments.getAuthor()));
    comments.setContent(EmojiParser.parseToAliases(comments.getContent()));
    comments.setIp(request.address());
    comments.setParent(comments.getCoid());

    try {
        commentsService.saveComment(comments);
        response.cookie("tale_remember_author", URLEncoder.encode(comments.getAuthor(), "UTF-8"), 7 * 24 * 60 * 60);
        response.cookie("tale_remember_mail", URLEncoder.encode(comments.getMail(), "UTF-8"), 7 * 24 * 60 * 60);
        if (StringKit.isNotBlank(comments.getUrl())) {
            response.cookie("tale_remember_url", URLEncoder.encode(comments.getUrl(), "UTF-8"), 7 * 24 * 60 * 60);
        }

        // 设置对每个文章30秒可以评论一次
        cache.hset(Types.COMMENTS_FREQUENCY, val, 1, 30);
        siteService.cleanCache(Types.C_STATISTICS);

        return RestResponse.ok();
    } catch (Exception e) {
        String msg = "评论发布失败";
        if (e instanceof TipException) {
            msg = e.getMessage();
        } else {
            log.error(msg, e);
        }
        return RestResponse.fail(msg);
    }
}
 
Example 20
Source File: InstallController.java    From tale with MIT License 4 votes vote down vote up
@Route(value = "/", method = HttpMethod.POST)
@JSON
public RestResponse doInstall(@Param String site_title, @Param String site_url,
                              @Param String admin_user, @Param String admin_email,
                              @Param String admin_pwd) {
    if (Files.exists(Paths.get(AttachController.CLASSPATH + "install.lock"))
            && TaleConst.OPTIONS.getInt("allow_install", 0) != 1) {
        return RestResponse.fail("请勿重复安装");
    }
    try {
        if (StringKit.isBlank(site_title) ||
                StringKit.isBlank(site_url) ||
                StringKit.isBlank(admin_user) ||
                StringKit.isBlank(admin_pwd)) {
            return RestResponse.fail("请确认网站信息输入完整");
        }

        if (admin_pwd.length() < 6 || admin_pwd.length() > 14) {
            return RestResponse.fail("请输入6-14位密码");
        }

        if (StringKit.isNotBlank(admin_email) && !TaleUtils.isEmail(admin_email)) {
            return RestResponse.fail("邮箱格式不正确");
        }

        Users temp = new Users();
        temp.setUsername(admin_user);
        temp.setPassword(admin_pwd);
        temp.setEmail(admin_email);

        siteService.initSite(temp);

        if (site_url.endsWith("/")) {
            site_url = site_url.substring(0, site_url.length() - 1);
        }
        if (!site_url.startsWith("http")) {
            site_url = "http://".concat(site_url);
        }
        optionsService.saveOption("site_title", site_title);
        optionsService.saveOption("site_url", site_url);

        TaleConst.OPTIONS = Environment.of(optionsService.getOptions());
    } catch (Exception e) {
        String msg = "安装失败";
        if (e instanceof TipException) {
            msg = e.getMessage();
        } else {
            log.error(msg, e);
        }
        return RestResponse.fail(msg);
    }
    return RestResponse.ok();
}