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

The following examples show how to use com.blade.kit.StringKit#isEmpty() . 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: Blade.java    From blade with Apache License 2.0 6 votes vote down vote up
/**
 * Get banner text
 *
 * @return return blade start banner text
 */
public String bannerText() {
    if (null != bannerText) return bannerText;
    String bannerPath = environment.get(ENV_KEY_BANNER_PATH, null);

    if (StringKit.isEmpty(bannerPath) || Files.notExists(Paths.get(bannerPath))) {
        return null;
    }

    try {
        BufferedReader bufferedReader = Files.newBufferedReader(Paths.get(bannerPath));
        bannerText = bufferedReader.lines().collect(Collectors.joining("\r\n"));
    } catch (Exception e) {
        log.error("Load Start Banner file error", e);
    }
    return bannerText;
}
 
Example 2
Source File: BasicAuthMiddleware.java    From blade with Apache License 2.0 5 votes vote down vote up
private String searchCredential(String authValue) {
    if (StringKit.isEmpty(authValue)) {
        return null;
    }
    return authPairs.stream()
            .filter(authPair -> authPair.getValue().equals(authValue))
            .map(AuthPair::getUser)
            .findFirst().orElse(null);
}
 
Example 3
Source File: CsrfMiddleware.java    From blade with Apache License 2.0 5 votes vote down vote up
@Override
public boolean before(RouteContext context) {
    if (csrfOption.isIgnoreMethod(context.method())) {
        if (csrfOption.isStartExclusion(context.uri())) {
            return true;
        }
        this.genToken(context);
        return true;
    }

    if (csrfOption.isExclusion(context.uri())) {
        return true;
    }

    String tokenUUID = context.session().attribute(sessionToken);
    if (StringKit.isEmpty(tokenUUID)) {
        csrfOption.getErrorHandler().accept(context);
        return false;
    }

    String token = csrfOption.getTokenGetter().apply(context.request());
    if (StringKit.isEmpty(token)) {
        csrfOption.getErrorHandler().accept(context);
        return false;
    }
    String hash = new String(Base64.getDecoder().decode(token));
    if (!PasswordKit.checkPassword(tokenUUID, hash)) {
        csrfOption.getErrorHandler().accept(context);
        return false;
    }

    return true;
}
 
Example 4
Source File: CsrfMiddleware.java    From blade with Apache License 2.0 5 votes vote down vote up
public String genToken(RouteContext context) {
    String tokenUUID = context.session().attribute(sessionToken);
    if (StringKit.isEmpty(tokenUUID)) {
        tokenUUID = UUID.UU64();
        context.session().attribute(sessionToken, tokenUUID);
    }
    String token = Base64.getEncoder().encodeToString(PasswordKit.hashPassword(tokenUUID).getBytes());
    context.attribute("_csrf_token", token);
    context.attribute("_csrf_token_input", "<input type='hidden' name='_token' value='" + token + "'/>");
    return token;
}
 
Example 5
Source File: RouteActionArguments.java    From blade with Apache License 2.0 5 votes vote down vote up
private static Object getCookie(ParamStruct paramStruct) throws BladeException {
    Type        argType     = paramStruct.argType;
    CookieParam cookieParam = paramStruct.cookieParam;
    String      paramName   = paramStruct.paramName;
    Request     request     = paramStruct.request;

    String cookieName = StringKit.isEmpty(cookieParam.value()) ? paramName : cookieParam.value();
    String val        = request.cookie(cookieName);
    if (null == val) {
        val = cookieParam.defaultValue();
    }
    return ReflectKit.convert(argType, val);
}
 
Example 6
Source File: RouteActionArguments.java    From blade with Apache License 2.0 5 votes vote down vote up
private static Object getHeader(ParamStruct paramStruct) throws BladeException {
    Type        argType     = paramStruct.argType;
    HeaderParam headerParam = paramStruct.headerParam;
    String      paramName   = paramStruct.paramName;
    Request     request     = paramStruct.request;

    String key = StringKit.isEmpty(headerParam.value()) ? paramName : headerParam.value();
    String val = request.header(key);
    if (StringKit.isBlank(val)) {
        val = headerParam.defaultValue();
    }
    return ReflectKit.convert(argType, val);
}
 
Example 7
Source File: RouteActionArguments.java    From blade with Apache License 2.0 5 votes vote down vote up
private static Object getPathParam(ParamStruct paramStruct) {
    Type      argType   = paramStruct.argType;
    PathParam pathParam = paramStruct.pathParam;
    String    paramName = paramStruct.paramName;
    Request   request   = paramStruct.request;

    String name = StringKit.isEmpty(pathParam.name()) ? paramName : pathParam.name();
    String val  = request.pathString(name);
    if (StringKit.isBlank(val)) {
        val = pathParam.defaultValue();
    }
    return ReflectKit.convert(argType, val);
}
 
Example 8
Source File: SessionHandler.java    From blade with Apache License 2.0 5 votes vote down vote up
private Session getSession(Request request) {
    String cookieHeader = request.cookie(WebContext.sessionKey());
    if (StringKit.isEmpty(cookieHeader)) {
        return null;
    }
    return sessionManager.getSession(cookieHeader);
}
 
Example 9
Source File: Response.java    From blade with Apache License 2.0 5 votes vote down vote up
/**
 * Render view, can be modified after WebHook
 *
 * @param view view page
 * @return Return Response
 */
default void render(String view) {
    if (StringKit.isEmpty(view)) {
        throw new BladeException(500, "Render view not empty.");
    }
    this.render(new ModelAndView(view));
}