com.blade.Blade.of() - java examples

Here are the examples of the java api com.blade.Blade.of() taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

8 Examples 7

19 View Complete Implementation : WebSocketDemo.java
Copyright Apache License 2.0
Author : lets-blade
public static void main(String[] args) {
    Blade.of().get("/hello", ctx -> ctx.text("get route")).post("/post", ctx -> ctx.text(ctx.request().query("param", "null"))).webSocket("/websocket", new WebSocketHandler() {

        @Override
        public void onConnect(WebSocketContext ctx) {
            System.out.println(ctx.session().uuid() + ":open");
            ctx.message(ctx.session().uuid() + ":open");
        }

        @Override
        public void onText(WebSocketContext ctx) {
            if ("close".equals(ctx.message())) {
                ctx.disconnect();
            } else {
                System.out.println(ctx.session().uuid() + ":" + ctx.message());
                ctx.message(ctx.message());
            }
        }

        @Override
        public void onDisConnect(WebSocketContext ctx) {
            System.out.println(ctx.session().uuid() + ":close:" + ctx.session());
        }
    }).start(WebSocketDemo.clreplaced);
}

19 View Complete Implementation : Application.java
Copyright BSD 3-Clause "New" or "Revised" License
Author : TechEmpower
public static void main(String[] args) {
    Blade.of().get("/json", ctx -> ctx.json(new Message()).contentType(JSON_CONTENT_TYPE).header(SERVER_HEADER, SERVER_VALUE)).get("/plaintext", ctx -> ctx.body(PLAINTEXT).contentType("text/plain").header(SERVER_HEADER, SERVER_VALUE)).get("/db", Application::db).get("/queries", Application::queries).get("/updates", Application::updates).get("/fortunes", Application::fortunes).disableSession().start(Application.clreplaced, args);
}

16 View Complete Implementation : Application.java
Copyright MIT License
Author : biezhi
public static void main(String[] args) {
    CsrfOption csrfOption = CsrfOption.builder().build();
    csrfOption.getUrlExclusions().add("/callback/youzan");
    csrfOption.getUrlExclusions().add("/callback/payjs");
    Blade.of().use(new CsrfMiddleware(csrfOption)).use(new XssMiddleware()).start(Application.clreplaced, args);
}

16 View Complete Implementation : EventManagerTest.java
Copyright Apache License 2.0
Author : lets-blade
@Test
public void testManager() {
    EventManager eventManager = new EventManager();
    eventManager.addEventListener(EventType.SERVER_STARTED, b -> System.out.println("server started"));
    eventManager.fireEvent(EventType.SERVER_STARTED, new Event().attribute("blade", Blade.of()));
}

16 View Complete Implementation : ServerTest.java
Copyright Apache License 2.0
Author : lets-blade
@Test
public void testStart() throws Exception {
    NettyServer nettyServer = new NettyServer();
    nettyServer.start(Blade.of().listen(10087));
    nettyServer.stop();
}

14 View Complete Implementation : Hello.java
Copyright Apache License 2.0
Author : lets-blade
public static void main(String[] args) {
    Blade.of().get("/", ctx -> {
        String[] chars = new String[] { "Here a special char \" that not escaped", "And Another \\ char" };
        ctx.json(chars);
    }).get("/user/aa", ctx -> ctx.render("upload.html")).get("/up", ctx -> ctx.render("upload.html")).get("/d1", ctx -> {
        File file = new File("/Users/biezhi/Pictures/rand/003.jpg");
        ctx.response().contentType("image/jpeg");
        ctx.response().header("Content-Disposition", "attachment; filename=003.jpg");
        ctx.response().body(ByteBody.of(file));
    }).get("/d2", ctx -> {
        File file = new File("/Users/biezhi/Pictures/rand/003.jpg");
        try (FileInputStream inputStream = new FileInputStream(file)) {
            ctx.response().contentType("image/jpef");
            ctx.response().header("Content-Disposition", "attachment; filename=m1.png");
            ctx.response().body(StreamBody.of(inputStream));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }).get("/d3", ctx -> {
        String str = "hello world";
        ctx.response().contentType("text/html");
        ctx.response().body(ByteBody.of(str.getBytes()));
    }).get("/error", ctx -> {
        int a = 1 / 0;
        ctx.text("ok");
    }).get("/hello", ctx -> ctx.body(hello)).get("/error", ctx -> {
        int a = 1 / 0;
        ctx.text("Hello World.");
    }).post("/hello", ctx -> ctx.text("Hello World.")).put("/body", ctx -> {
        ctx.text(ctx.bodyToString());
    }).put("/hello", ctx -> ctx.text("Hello World.")).delete("/hello", ctx -> ctx.text("Hello World.")).get("/download", ctx -> {
        try {
            ctx.response().download("hello.txt", new File("/Users/biezhi/workspace/projects/java/blade/src/test/resources/static/a.txt"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }).get("/rand", ctx -> {
        try {
            int timeout = ctx.fromInt("timeout", new Random().nextInt(1000));
            TimeUnit.SECONDS.sleep(timeout);
            ctx.text("sleep " + timeout + "s");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }).before("/user/*", ctx -> {
        System.out.println("before: " + ctx.uri());
        ctx.text("Hello World");
        ctx.abort();
    }).enableCors(true).event(EventType.ENVIRONMENT_CHANGED, new ConfigChanged()).event(EventType.SESSION_DESTROY, e -> {
        System.out.println("session 失效了");
    }).start(Hello.clreplaced, args);
}

7 View Complete Implementation : BasicAuthMiddlewareTest.java
Copyright Apache License 2.0
Author : lets-blade
@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.clreplaced.getMethod("handle", RouteContext.clreplaced)).targetType(AuthHandler.clreplaced).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);
    replacedertTrue(flag);
}

7 View Complete Implementation : BasicAuthMiddlewareTest.java
Copyright Apache License 2.0
Author : lets-blade
@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.clreplaced.getMethod("handle", RouteContext.clreplaced)).targetType(AuthHandler.clreplaced).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);
    replacedertFalse(flag);
}