org.takes.facets.forward.TkForward Java Examples

The following examples show how to use org.takes.facets.forward.TkForward. 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: PsBasicTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * PsBasic can request authentication.
 * @throws Exception If some problem inside
 */
@Test
public void requestAuthentication() throws Exception {
    final Take take = new TkForward(
        new TkAuth(
            new TkSecure(
                new TkText("secured area...")
            ),
            new PsBasic(
                "the realm 5",
                new PsBasic.Default("bob pwd88 urn:users:bob")
            )
        )
    );
    new Assertion<>(
        "Response with 401 Unauthorized status",
        new RsPrint(
            take.act(new RqFake())
        ),
        new TextHasString("HTTP/1.1 401 Unauthorized\r\n")
    ).affirm();
}
 
Example #2
Source File: TkApp.java    From jpeek with MIT License 4 votes vote down vote up
/**
 * Ctor.
 * @param home Home directory
 * @return The take
 * @throws IOException If fails
 */
private static Take make(final Path home) throws IOException {
    final Futures futures = new Futures(
        new Reports(home)
    );
    final BiFunc<String, String, Func<String, Response>> reports =
        new AsyncReports(
            // @checkstyle MagicNumber (1 line)
            new StickyFutures(futures, 100)
        );
    return new TkSslOnly(
        new TkFallback(
            new TkForward(
                new TkFork(
                    new FkRegex("/", new TkIndex()),
                    new FkRegex("/robots.txt", new TkText("")),
                    new FkRegex("/mistakes", new TkMistakes()),
                    new FkRegex(
                        "/flush",
                        (Take) req -> new RsText(
                            String.format("%d flushed", new Results().flush())
                        )
                    ),
                    new FkRegex(
                        "/upload",
                        (Take) req -> new RsPage(req, "upload")
                    ),
                    new FkRegex("/do-upload", new TkUpload(reports)),
                    new FkRegex("/all", new TkAll()),
                    new FkRegex("/queue", new TkQueue(futures)),
                    new FkRegex(
                        ".+\\.xsl",
                        new TkWithType(
                            new TkClasspath(),
                            "text/xsl"
                        )
                    ),
                    new FkRegex(
                        "/jpeek\\.css",
                        new TkWithType(
                            new TkText(
                                new TextOf(
                                    new ResourceOf("org/jpeek/jpeek.css")
                                ).asString()
                            ),
                            "text/css"
                        )
                    ),
                    new FkRegex(
                        "/([^/]+)/([^/]+)(.*)",
                        new TkReport(reports, new Results())
                    )
                )
            ),
            new FbChain(
                new FbStatus(
                    HttpURLConnection.HTTP_NOT_FOUND,
                    (Fallback) req -> new Opt.Single<>(
                        new RsWithStatus(
                            new RsText(req.throwable().getMessage()),
                            req.code()
                        )
                    )
                ),
                req -> {
                    Sentry.capture(req.throwable());
                    return new Opt.Single<>(
                        new RsWithStatus(
                            new RsText(
                                new TextOf(req.throwable()).asString()
                            ),
                            HttpURLConnection.HTTP_INTERNAL_ERROR
                        )
                    );
                }
            )
        )
    );
}