Search in sources :

Example 1 with Request

use of com.blade.mvc.http.Request in project tale by otale.

the class Theme method articles.

/**
 * 分页
 *
 * @param limit
 * @return
 */
public static Page<Contents> articles(int limit) {
    Request request = WebContext.request();
    Integer page = request.attribute("page_num");
    page = null == page ? request.queryInt("page", 1) : page;
    page = page < 0 || page > TaleConst.MAX_PAGE ? 1 : page;
    Page<Contents> articles = select().from(Contents.class).where(Contents::getType, Types.ARTICLE).and("status", Types.PUBLISH).order(Contents::getCreated, OrderBy.DESC).page(page, limit);
    request.attribute("articles", articles);
    if (page > 1) {
        WebContext.request().attribute("title", "第" + page + "页");
    }
    request.attribute("is_home", true);
    request.attribute("page_prefix", "/page");
    return articles;
}
Also used : Contents(com.tale.model.entity.Contents) Request(com.blade.mvc.http.Request)

Example 2 with Request

use of com.blade.mvc.http.Request in project blade by biezhi.

the class RouteActionArguments method getQueryParam.

private static Object getQueryParam(ParamStruct paramStruct) {
    Param param = paramStruct.param;
    String paramName = paramStruct.paramName;
    Type argType = paramStruct.argType;
    Request request = paramStruct.request;
    if (null == param) {
        return null;
    }
    String name = StringKit.isBlank(param.name()) ? paramName : param.name();
    if (ReflectKit.isBasicType(argType) || argType.equals(Date.class) || argType.equals(BigDecimal.class) || argType.equals(LocalDate.class) || argType.equals(LocalDateTime.class) || (argType instanceof Class && ((Class) argType).isEnum())) {
        String value = request.query(name).orElseGet(() -> getDefaultValue(param.defaultValue(), argType));
        return ReflectKit.convert(argType, value);
    } else {
        if (ParameterizedType.class.isInstance(argType)) {
            List<String> values = request.parameters().get(param.name());
            return getParameterizedTypeValues(values, argType);
        }
        return parseModel(ReflectKit.typeToClass(argType), request, param.name());
    }
}
Also used : Request(com.blade.mvc.http.Request) LocalDate(java.time.LocalDate) LocalDate(java.time.LocalDate)

Example 3 with Request

use of com.blade.mvc.http.Request in project blade by biezhi.

the class RouteActionArguments method getBodyParam.

private static Object getBodyParam(ParamStruct paramStruct) {
    Type argType = paramStruct.argType;
    Request request = paramStruct.request;
    if (ReflectKit.isPrimitive(argType)) {
        return ReflectKit.convert(argType, request.bodyToString());
    } else {
        String json = request.bodyToString();
        return StringKit.isNotEmpty(json) ? JsonKit.formJson(json, argType) : null;
    }
}
Also used : Request(com.blade.mvc.http.Request)

Example 4 with Request

use of com.blade.mvc.http.Request in project blade by biezhi.

the class RouteActionArguments method getPathParam.

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);
}
Also used : Request(com.blade.mvc.http.Request)

Example 5 with Request

use of com.blade.mvc.http.Request in project blade by biezhi.

the class RouteActionArguments method getCookie.

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);
}
Also used : Request(com.blade.mvc.http.Request)

Aggregations

Request (com.blade.mvc.http.Request)29 HttpRequest (com.blade.mvc.http.HttpRequest)18 Test (org.junit.Test)18 HashMap (java.util.HashMap)7 CaseInsensitiveHashMap (com.blade.kit.CaseInsensitiveHashMap)5 WebContext (com.blade.mvc.WebContext)3 Response (com.blade.mvc.http.Response)3 RouteContext (com.blade.mvc.RouteContext)2 AuthOption (com.blade.security.web.auth.AuthOption)2 BasicAuthMiddleware (com.blade.security.web.auth.BasicAuthMiddleware)2 ForbiddenException (com.blade.exception.ForbiddenException)1 NotFoundException (com.blade.exception.NotFoundException)1 TemplateException (com.blade.exception.TemplateException)1 Session (com.blade.mvc.http.Session)1 FileItem (com.blade.mvc.multipart.FileItem)1 Contents (com.tale.model.entity.Contents)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 DefaultFileRegion (io.netty.channel.DefaultFileRegion)1 SslHandler (io.netty.handler.ssl.SslHandler)1