Java Code Examples for com.blade.mvc.http.Response#cookie()

The following examples show how to use com.blade.mvc.http.Response#cookie() . 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: TaleUtils.java    From tale with MIT License 5 votes vote down vote up
/**
     * 设置记住密码cookie
     *
     * @param response
     * @param uid
     */
    public static void setCookie(Response response, Integer uid) {
        try {
            HASH_PREFIX[0] = uid;
            String val = HASH_IDS.encode(HASH_PREFIX);
            HASH_PREFIX[0] = -1;
//            String  val   = new String(EncrypKit.encryptAES(uid.toString().getBytes(), TaleConst.AES_SALT.getBytes()));
            boolean isSSL = Commons.site_url().startsWith("https");
            response.cookie("/", TaleConst.USER_IN_COOKIE, val, ONE_MONTH, isSSL);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
Example 2
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);
    }
}