Java Code Examples for com.blade.kit.StringKit#isNotBlank()

The following examples show how to use com.blade.kit.StringKit#isNotBlank() . 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: WechatApp.java    From squirrelAI with Apache License 2.0 6 votes vote down vote up
/**
 * 获取用户的备注名称
 *
 * @param id
 * @return
 */
public String getUserRemarkName(String id) {
    String name = "微信群消息";//一般值的是群聊消息
    for (int i = 0, len = this.MemberList.size(); i < len; i++) {
        JSONObject member = (JSONObject) this.MemberList.get(i);
        if (member.getString("UserName").equals(id)) {
            if (StringKit.isNotBlank(member.getString("RemarkName"))) {
                name = member.getString("RemarkName");
            } else {
                name = member.getString("NickName");
            }
            return name;
        }
    }
    return name;
}
 
Example 2
Source File: Theme.java    From tale with MIT License 6 votes vote down vote up
/**
 * 显示文章缩略图,顺序为:文章第一张图 -> 随机获取
 *
 * @return
 */
public static String show_thumb(Contents contents) {
    if (null == contents) {
        return "";
    }
    if (StringKit.isNotBlank(contents.getThumbImg())) {
        return contents.getThumbImg();
    }
    String content = article(contents.getContent());
    String img     = Commons.show_thumb(content);
    if (StringKit.isNotBlank(img)) {
        return img;
    }
    int cid  = contents.getCid();
    int size = cid % 20;
    size = size == 0 ? 1 : size;
    return "/templates/themes/default/static/img/rand/" + size + ".jpg";
}
 
Example 3
Source File: MetasService.java    From tale with MIT License 5 votes vote down vote up
/**
 * 保存多个项目
 *
 * @param cid   文章id
 * @param names 类型名称列表
 * @param type  类型,tag or category
 */
public void saveMetas(Integer cid, String names, String type) {
    if (null == cid) {
        throw new TipException("项目关联id不能为空");
    }
    if (StringKit.isNotBlank(names) && StringKit.isNotBlank(type)) {
        String[] nameArr = names.split(",");
        for (String name : nameArr) {
            this.saveOrUpdate(cid, name, type);
        }
    }
}
 
Example 4
Source File: WechatApp.java    From squirrelAI with Apache License 2.0 5 votes vote down vote up
/**
 * 获取UUID:uuid是唯一的识别码
 */
public String getUUID() {

    HttpRequest request =
            HttpRequest.get(WechatInterface.UUID_URL, true, "appid", "wx782c26e4c19acffb", "fun", "new", "lang", "zh_CN", "_", DateKit.getCurrentUnixTime());
    // LOGGER.info 记录信息
    LOGGER.info("[*] " + request);

    String res = request.body();
    //res 为UUID
    request.disconnect();

    //如果返回的uuid信息不等于空
    if (StringKit.isNotBlank(res)) {
        //将登陆信息赋值给code
        String code = Matchers.match("window.QRLogin.code = (\\d+);", res);
        //如果登陆信息不等于空,那么微信登陆成功,否则反馈错误状态码
        if (null != code) {
            if (code.equals("200")) {
                this.Uuid = Matchers.match("window.QRLogin.uuid = \"(.*)\";", res);
                return this.Uuid;
            } else {
                LOGGER.info("[*] 错误的状态码: %s"+ code);
            }
        }
    }
    return null;
}
 
Example 5
Source File: Ason.java    From blade with Apache License 2.0 5 votes vote down vote up
public Long getLong(String key) {
    String val = getString(key);
    if (StringKit.isNotBlank(val)) {
        return Long.parseLong(val);
    }
    return null;
}
 
Example 6
Source File: Ason.java    From blade with Apache License 2.0 5 votes vote down vote up
public Float getFloat(String key) {
    String val = getString(key);
    if (StringKit.isNotBlank(val)) {
        return Float.parseFloat(val);
    }
    return null;
}
 
Example 7
Source File: MetasService.java    From tale with MIT License 5 votes vote down vote up
/**
 * 查询项目映射
 *
 * @param type 类型,tag or category
 */
public Map<String, List<Contents>> getMetaMapping(String type) {
    if (StringKit.isNotBlank(type)) {
        List<Metas> metas = getMetas(type);
        if (null != metas) {
            return metas.stream().collect(Collectors.toMap(Metas::getName, this::getMetaContents));
        }
    }
    return new HashMap<>();
}
 
Example 8
Source File: RouteActionArguments.java    From blade with Apache License 2.0 5 votes vote down vote up
public static <T> T parseModel(Class<T> argType, Request request, String name) {

        T obj = ReflectKit.newInstance(argType);

        List<Field> fields = ReflectKit.loopFields(argType);

        for (Field field : fields) {
            if ("serialVersionUID".equals(field.getName())) {
                continue;
            }
            Object value = null;

            Optional<String> fieldValue = request.query(field.getName());

            if (StringKit.isNotBlank(name)) {
                String fieldName = name + "[" + field.getName() + "]";
                fieldValue = request.query(fieldName);
            }

            if (fieldValue.isPresent() && StringKit.isNotBlank(fieldValue.get())) {
                value = ReflectKit.convert(field.getType(), fieldValue.get());
            }
            if (null != value) {
                ReflectKit.setFieldValue(field, obj, value);
            }
        }
        return obj;
    }
 
Example 9
Source File: TaleUtils.java    From tale with MIT License 5 votes vote down vote up
/**
 * 判断是否是合法路径
 *
 * @param slug
 * @return
 */
public static boolean isPath(String slug) {
    if (StringKit.isNotBlank(slug)) {
        if (slug.contains("/") || slug.contains(" ") || slug.contains(".")) {
            return false;
        }
        Matcher matcher = SLUG_REGEX.matcher(slug);
        return matcher.find();
    }
    return false;
}
 
Example 10
Source File: TaleUtils.java    From tale with MIT License 5 votes vote down vote up
/**
 * 提取html中的文字
 *
 * @param html
 * @return
 */
public static String htmlToText(String html) {
    if (StringKit.isNotBlank(html)) {
        return html.replaceAll("(?s)<[^>]*>(\\s*<[^>]*>)*", " ");
    }
    return "";
}
 
Example 11
Source File: Ason.java    From blade with Apache License 2.0 5 votes vote down vote up
public Boolean getBoolean(String key) {
    String val = getString(key);
    if (StringKit.isNotBlank(val)) {
        return Boolean.parseBoolean(val);
    }
    return Boolean.FALSE;
}
 
Example 12
Source File: Theme.java    From tale with MIT License 5 votes vote down vote up
/**
 * 显示文章内容,转换markdown为html
 *
 * @param value
 * @return
 */
public static String article(String value) {
    if (StringKit.isNotBlank(value)) {
        value = value.replace("<!--more-->", "\r\n");
        return TaleUtils.mdToHtml(value);
    }
    return "";
}
 
Example 13
Source File: Ason.java    From blade with Apache License 2.0 5 votes vote down vote up
public Double getDouble(String key) {
    String val = getString(key);
    if (StringKit.isNotBlank(val)) {
        return Double.parseDouble(val);
    }
    return null;
}
 
Example 14
Source File: MetasService.java    From tale with MIT License 5 votes vote down vote up
/**
 * 根据类型和名字查询项
 *
 * @param type 类型,tag or category
 * @param name 类型名
 */
public Metas getMeta(String type, String name) {
    if (StringKit.isNotBlank(type) && StringKit.isNotBlank(name)) {
        String sql = "select a.*, count(b.cid) as count from t_metas a left join `t_relationships` b on a.mid = b.mid " +
                "where a.type = ? and a.name = ? group by a.mid";

        return new Metas().query(sql, type, name);
    }
    return null;
}
 
Example 15
Source File: Ason.java    From blade with Apache License 2.0 5 votes vote down vote up
public Short getShort(String key) {
    String val = getString(key);
    if (StringKit.isNotBlank(val)) {
        return Short.parseShort(val);
    }
    return null;
}
 
Example 16
Source File: JetTag.java    From tale with MIT License 5 votes vote down vote up
public static void social(JetTagContext ctx, String name) throws IOException {
    String value = Commons.site_option("social_" + name);
    if (StringKit.isNotBlank(value)) {
        value = ctx.getBodyContent();
    }
    ctx.getWriter().print(value.toString());
}
 
Example 17
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 18
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 19
Source File: OptionsService.java    From tale with MIT License 4 votes vote down vote up
/**
 * 根据key删除配置项
 *
 * @param key 配置key
 */
public void deleteOption(String key) {
    if (StringKit.isNotBlank(key)) {
        new Options().like("name", key + "%").delete();
    }
}
 
Example 20
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);
    }
}