BAEL_3760-Introduction-to-Takes (#8767)
* Initial commit * Intro to Takes * Takes - code * Code changes * Takes - unit and integration test * Takes - hit-refresh feature * Takes - renamed hit-refresh to reload * Takes - review changes
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.takes;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.takes.Response;
|
||||
import org.takes.facets.fallback.Fallback;
|
||||
import org.takes.facets.fallback.FbChain;
|
||||
import org.takes.facets.fallback.FbStatus;
|
||||
import org.takes.facets.fallback.RqFallback;
|
||||
import org.takes.facets.fallback.TkFallback;
|
||||
import org.takes.facets.fork.FkRegex;
|
||||
import org.takes.facets.fork.TkFork;
|
||||
import org.takes.http.Exit;
|
||||
import org.takes.http.FtBasic;
|
||||
import org.takes.misc.Opt;
|
||||
import org.takes.rs.RsText;
|
||||
|
||||
public final class TakesApp {
|
||||
|
||||
public static void main(final String... args) throws IOException, SQLException {
|
||||
new FtBasic(
|
||||
new TkFallback(
|
||||
new TkFork(
|
||||
new FkRegex("/", new TakesHelloWorld()),
|
||||
new FkRegex("/index", new TakesIndex()),
|
||||
new FkRegex("/contact", new TakesContact())
|
||||
),
|
||||
new FbChain(
|
||||
new FbStatus(404, new RsText("Page Not Found")),
|
||||
new FbStatus(405, new RsText("Method Not Allowed")),
|
||||
new Fallback() {
|
||||
@Override
|
||||
public Opt<Response> route(final RqFallback req) {
|
||||
return new Opt.Single<Response>(new RsText(req.throwable().getMessage()));
|
||||
}
|
||||
})
|
||||
), 6060
|
||||
).start(Exit.NEVER);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.takes;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.takes.Request;
|
||||
import org.takes.Response;
|
||||
import org.takes.Take;
|
||||
import org.takes.rs.RsWithBody;
|
||||
import org.takes.rs.RsWithStatus;
|
||||
import org.takes.rs.RsWithType;
|
||||
|
||||
public final class TakesContact implements Take {
|
||||
|
||||
@Override
|
||||
public Response act(Request req) throws IOException {
|
||||
return new RsWithStatus(
|
||||
new RsWithType(
|
||||
new RsWithBody("Contact us at https://www.baeldung.com"),
|
||||
"text/html"), 200);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.takes;
|
||||
|
||||
import org.takes.Request;
|
||||
import org.takes.Response;
|
||||
import org.takes.Take;
|
||||
import org.takes.rs.RsText;
|
||||
|
||||
public class TakesHelloWorld implements Take {
|
||||
|
||||
@Override
|
||||
public Response act(final Request request) {
|
||||
return new RsText("Hello, world!");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.takes;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.takes.Request;
|
||||
import org.takes.Response;
|
||||
import org.takes.Take;
|
||||
import org.takes.rq.form.RqFormSmart;
|
||||
import org.takes.rs.RsHtml;
|
||||
import org.takes.rs.RsVelocity;
|
||||
|
||||
public final class TakesIndex implements Take {
|
||||
|
||||
@Override
|
||||
public Response act(final Request req) throws IOException {
|
||||
RqFormSmart form = new RqFormSmart(req);
|
||||
String username = form.single("username");
|
||||
return new RsHtml(
|
||||
new RsVelocity(this.getClass().getResource("/templates/index.vm"),
|
||||
new RsVelocity.Pair("username", username))
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user