com.blade.mvc.hook.WebHook Java Examples

The following examples show how to use com.blade.mvc.hook.WebHook. 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 Project: blade   Author: lets-blade   File: RouteMethodHandler.java    License: Apache License 2.0 5 votes vote down vote up
private boolean invokeMiddleware(List<Route> middleware, RouteContext context) throws BladeException {
    if (BladeKit.isEmpty(middleware)) {
        return true;
    }
    for (Route route : middleware) {
        WebHook webHook = (WebHook) WebContext.blade().ioc().getBean(route.getTargetType());
        boolean flag    = webHook.before(context);
        if (!flag) return false;
    }
    return true;
}
 
Example #2
Source Project: blade   Author: lets-blade   File: Blade.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * The use of multiple middleware, if any
 *
 * @param middleware middleware object array
 * @return blade
 */
public Blade use(@NonNull WebHook... middleware) {
    if (BladeKit.isEmpty(middleware)) {
        return this;
    }
    this.middleware.addAll(Arrays.asList(middleware));
    for (var webHook : middleware) {
        this.register(webHook);
    }
    return this;
}
 
Example #3
Source Project: blade   Author: lets-blade   File: MiddlewareTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testMiddleware() throws Exception {
    WebHook middleware = mock(WebHook.class);
    Signature signature = mock(Signature.class);

    middleware.before(signature.routeContext());
    middleware.after(signature.routeContext());

    verify(middleware).before(signature.routeContext());
    verify(middleware).after(signature.routeContext());
}
 
Example #4
Source Project: blade   Author: lets-blade   File: RouteBuilderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateRouteBuilder() throws Exception {
    RouteMatcher routeMatcher = new RouteMatcher();
    RouteBuilder routeBuilder = new RouteBuilder(routeMatcher);
    routeBuilder.addRouter(DemoController.class, new DemoController());
    routeBuilder.addWebHook(WebHook.class, "/*");

    routeMatcher.register();

    Route route = routeMatcher.lookupRoute("GET", "/");
    Assert.assertNotNull(route);
    Assert.assertEquals("GET\t/", route.toString());
}
 
Example #5
Source Project: blade   Author: lets-blade   File: BladeTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testUse() {
    Blade         blade      = Blade.of().use(new CsrfMiddleware());
    List<WebHook> middleware = blade.middleware();
    Assert.assertNotNull(middleware);
    assertEquals(1, middleware.size());
}
 
Example #6
Source Project: blade   Author: lets-blade   File: RouteMatcher.java    License: Apache License 2.0 4 votes vote down vote up
public void initMiddleware(List<WebHook> hooks) {
    this.middleware = hooks.stream().map(webHook -> {
        Method method = ReflectKit.getMethod(WebHook.class, "before", Signature.class);
        return new Route(HttpMethod.BEFORE, "/.*", webHook, WebHook.class, method);
    }).collect(Collectors.toList());
}
 
Example #7
Source Project: blade   Author: lets-blade   File: NettyServer.java    License: Apache License 2.0 4 votes vote down vote up
private void parseAndCreate(Class<?> clazz) {
    if (null != clazz.getAnnotation(Bean.class) || null != clazz.getAnnotation(Value.class)) {
        blade.register(clazz);
    }
    if (null != clazz.getAnnotation(Path.class)) {
        if (null == blade.getBean(clazz)) {
            blade.register(clazz);
        }
        Object controller = blade.getBean(clazz);
        routeBuilder.addRouter(clazz, controller);
    }
    if (null != clazz.getAnnotation(Configuration.class) && clazz.getMethods().length > 0) {
        Object config = ReflectKit.newInstance(clazz);
        Arrays.stream(clazz.getMethods())
                .filter(m -> m.getAnnotation(Bean.class) != null)
                .forEach(n -> {
                    try {
                        blade.register(n.invoke(config));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
    }
    if (ReflectKit.hasInterface(clazz, WebHook.class) && null != clazz.getAnnotation(Bean.class)) {
        URLPattern URLPattern = clazz.getAnnotation(URLPattern.class);
        if (null == URLPattern) {
            routeBuilder.addWebHook(clazz, "/.*");
        } else {
            Stream.of(URLPattern.values())
                    .forEach(pattern -> routeBuilder.addWebHook(clazz, pattern));
        }
    }

    if (ReflectKit.hasInterface(clazz, BladeLoader.class) && null != clazz.getAnnotation(Bean.class)) {
        this.loaders.add((BladeLoader) blade.getBean(clazz));
    }
    if (ReflectKit.hasInterface(clazz, BeanProcessor.class) && null != clazz.getAnnotation(Bean.class)) {
        this.processors.add((BeanProcessor) blade.getBean(clazz));
    }
    if (isExceptionHandler(clazz)) {
        ExceptionHandler exceptionHandler = (ExceptionHandler) blade.getBean(clazz);
        blade.exceptionHandler(exceptionHandler);
    }
    WebSocket webSocket;
    if (null != (webSocket = clazz.getAnnotation(WebSocket.class))) {
        if (null == blade.getBean(clazz)) {
            blade.register(clazz);
        }
        if (ReflectKit.hasInterface(clazz, WebSocketHandler.class)) {
            blade.webSocket(webSocket.value(), (WebSocketHandler) blade.getBean(clazz));
        } else {
            WebSocketHandlerWrapper wrapper = blade.getBean(WebSocketHandlerWrapper.class);
            if (wrapper == null) {
                wrapper = WebSocketHandlerWrapper.init(blade);
                blade.register(wrapper);
            }
            blade.webSocket(webSocket.value(), wrapper);
            wrapper.wrapHandler(webSocket.value(), clazz);
        }

    }
}
 
Example #8
Source Project: blade   Author: lets-blade   File: Blade.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Get middleware list
 *
 * @return return middleware list
 */
public List<WebHook> middleware() {
    return this.middleware;
}