Java Code Examples for com.blade.mvc.WebContext#set()

The following examples show how to use com.blade.mvc.WebContext#set() . 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: HttpServerHandler.java    From blade with Apache License 2.0 5 votes vote down vote up
private FullHttpResponse buildResponse(WebContext webContext) {
    WebContext.set(webContext);
    return routeHandler.handleResponse(
            webContext.getRequest(), webContext.getResponse(),
            webContext.getChannelHandlerContext()
    );
}
 
Example 2
Source File: BasicAuthMiddlewareTest.java    From blade with Apache License 2.0 5 votes vote down vote up
@Test
public void testAuthSuccess() throws Exception {

    Request mockRequest = mockHttpRequest("GET");

    WebContext.init(Blade.of(), "/");

    Map<String, String> headers = new HashMap<>();
    headers.put("Authorization", "Basic YWRtaW46MTIzNDU2");

    when(mockRequest.parameters()).thenReturn(new HashMap<>());
    when(mockRequest.headers()).thenReturn(headers);

    Request  request  = new HttpRequest(mockRequest);
    Response response = mockHttpResponse(200);

    RouteContext context = new RouteContext(request, response);
    context.initRoute(Route.builder()
            .action(AuthHandler.class.getMethod("handle", RouteContext.class))
            .targetType(AuthHandler.class)
            .target(new AuthHandler()).build());

    WebContext.set(new WebContext(request, response, null));

    AuthOption authOption = AuthOption.builder().build();
    authOption.addUser("admin", "123456");

    BasicAuthMiddleware basicAuthMiddleware = new BasicAuthMiddleware(authOption);
    boolean             flag                = basicAuthMiddleware.before(context);
    assertTrue(flag);
}
 
Example 3
Source File: BasicAuthMiddlewareTest.java    From blade with Apache License 2.0 5 votes vote down vote up
@Test
public void testAuthFail() throws Exception {
    Request mockRequest = mockHttpRequest("GET");

    WebContext.init(Blade.of(), "/");

    Map<String, String> headers = new HashMap<>();
    headers.put("Authorization", "Basic YmxhZGU6YmxhZGUyMg==");

    when(mockRequest.parameters()).thenReturn(new HashMap<>());
    when(mockRequest.headers()).thenReturn(headers);

    Request  request  = new HttpRequest(mockRequest);
    Response response = mockHttpResponse(200);

    RouteContext context = new RouteContext(request, response);

    context.initRoute(Route.builder()
            .action(AuthHandler.class.getMethod("handle", RouteContext.class))
            .targetType(AuthHandler.class)
            .target(new AuthHandler()).build());

    WebContext.set(new WebContext(request, response, null));

    AuthOption authOption = AuthOption.builder().build();
    authOption.addUser("admin", "123456");

    BasicAuthMiddleware basicAuthMiddleware = new BasicAuthMiddleware(authOption);
    boolean             flag                = basicAuthMiddleware.before(context);
    assertFalse(flag);
}
 
Example 4
Source File: ExceptionHandlerTest.java    From blade with Apache License 2.0 5 votes vote down vote up
@Before
public void before() {
    request = mock(Request.class);
    when(request.header("Accept")).thenReturn("text/html");
    response = mock(Response.class);

    WebContext.init(Blade.me(), "/");
    WebContext.set(new WebContext(request, response, null));
}
 
Example 5
Source File: HttpServerHandler.java    From blade with Apache License 2.0 4 votes vote down vote up
private WebContext executeLogic(WebContext webContext) {
    try {
        WebContext.set(webContext);
        Request request = webContext.getRequest();
        String method = request.method();
        String uri = request.uri();
        Instant start = null;

        if (ALLOW_COST && !PERFORMANCE) {
            start = Instant.now();
        }

        if (isStaticFile(method, uri)) {
            staticFileHandler.handle(webContext);
        } else {
            if (HttpMethod.OPTIONS.name().equals(method) && null != WebContext.blade().corsMiddleware()) {
                WebContext.blade().corsMiddleware().handle(new RouteContext(webContext.getRequest(), webContext.getResponse()));
            } else {
                Route route = routeMatcher.lookupRoute(method, uri);
                if (null != route) {
                    webContext.setRoute(route);
                } else {
                    throw new NotFoundException(uri);
                }
                routeHandler.handle(webContext);
            }

            if (PERFORMANCE) {
                return webContext;
            }

            if (ALLOW_COST) {
                long cost = log200AndCost(log, start, BladeCache.getPaddingMethod(method), uri);
                request.attribute(REQUEST_COST_TIME, cost);
            } else {
                log200(log, BladeCache.getPaddingMethod(method), uri);
            }
        }
        return webContext;
    } catch (Exception e) {
        throw BladeException.wrapper(e);
    }
}