Intro to Takes

This commit is contained in:
Anshul BANSAL 2020-02-21 11:00:23 +02:00
parent acd7d764e2
commit 15ab6e7a57
9 changed files with 278 additions and 46 deletions

View File

@ -78,6 +78,28 @@
<artifactId>takes</artifactId> <artifactId>takes</artifactId>
<version>1.19</version> <version>1.19</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1</version>
</dependency>
</dependencies> </dependencies>
<repositories> <repositories>
@ -87,10 +109,29 @@
</repository> </repository>
</repositories> </repositories>
<build>
<resources>
<resource>
<directory>src/main/webapp</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<profiles> <profiles>
<profile> <profile>
<id>hit-refresh</id> <id>hit-refresh</id>
<build> <build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.codehaus.mojo</groupId> <groupId>org.codehaus.mojo</groupId>

View File

@ -1,26 +1,103 @@
package com.baeldung.takes; package com.baeldung.takes;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import org.takes.Request;
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.Fork;
import org.takes.facets.fork.TkFork;
import org.takes.http.Exit; import org.takes.http.Exit;
import org.takes.http.FtBasic; import org.takes.http.FtBasic;
import org.takes.http.FtCli; import org.takes.misc.Opt;
import org.takes.tk.TkClasspath; import org.takes.rq.RqHref;
import org.takes.tk.TkWrap; import org.takes.rs.RsText;
import org.takes.Take; import org.takes.rs.RsWithBody;
import org.takes.facets.fork.FkRegex; import org.takes.rs.RsWithStatus;
import org.takes.facets.fork.TkFork; import org.takes.rs.RsWithType;
import org.takes.tk.TkSlf4j;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
public final class TakesApp { public final class TakesApp {
// public static void main(final String... args) throws Exception {
// new FtBasic(new TkFork(
// new FkRegex("/", "Hello, World!"),
// new FkRegex("/index", new TakesIndex()),
// new FkRegex("/contact", new TakesContact())
// ),6060).start(Exit.NEVER);
// }
public static void main(final String... args) throws IOException, SQLException {
new FtBasic(
new TkFallback(
new TkSlf4j(
new TkFork(
new FkRegex("/", "Hello, World!"),
new FkRegex("/index", new TakesIndex()),
new FkRegex("/contact", new TakesContact()),
new FkRegex("\\A/createUser", new TakesCreateUser(TakesApp.dbConnection())),
new FkRegex("\\A/readUser", new TakesReadUser(TakesApp.dbConnection()))
)
),
new FbChain(
new FbStatus(404, new RsText("sorry, page is absent")),
new FbStatus(405, new RsText("this method is not allowed here")),
new Fallback() {
@Override
public Opt<Response> route(final RqFallback req) {
return new Opt.Single<Response>(
new RsText(req.throwable().getMessage())
);
}
})
),
6060
).start(Exit.NEVER);
}
// public static void main(final String... args) throws Exception { // public static void main(final String... args) throws IOException {
// new FtBasic(new TkFork(new FkRegex("/", "hello, world... this is coming from Takes!")), 6060).start(Exit.NEVER); //
// } // new FtBasic(
// new TkFork(
// new Fork() {
// @Override
// public Opt<Response> route(Request req) throws Exception {
// if (new RqHref.Base(req).href().path().equals("/entry")) {
// return new Opt.Single<Response>(new RsWithStatus(new RsWithType(new RsWithBody("hello this is for request"), "text/html"), 200));
// } else {
// return new Opt.Single<Response>(null);
// }
// }
// }
// ),6060).start(Exit.NEVER);
//
//
// }
public static void main(final String... args) throws Exception { private static Connection dbConnection() throws SQLException {
new FtBasic(new TakesIndex(), 6060).start(Exit.NEVER); HikariConfig config = new HikariConfig();
}
config.setJdbcUrl("jdbc:h2:mem:devDB;DB_CLOSE_DELAY=-1;INIT=runscript from 'classpath:/db.sql'");
config.setUsername("sa");
config.setPassword("");
@SuppressWarnings("resource")
HikariDataSource ds = new HikariDataSource(config);
return ds.getConnection();
}
} }

View File

@ -0,0 +1,26 @@
package com.baeldung.takes;
import java.io.IOException;
import java.sql.SQLException;
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, SQLException {
return new RsWithStatus(new RsWithType(new RsWithBody("<html>"
+ "<head>"
+ "<title>Takes Application - Contact</title></head>"
+ "<body style='color:green'>"
+ "Contact us at @baeldung.com"
+ "</body></html>"), "text/html"), 200);
}
}

View File

@ -0,0 +1,49 @@
package com.baeldung.takes;
import java.io.IOException;
import java.net.URI;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.takes.Request;
import org.takes.Response;
import org.takes.Take;
import org.takes.misc.Href;
import org.takes.rq.RqForm;
import org.takes.rq.RqHref;
import org.takes.rq.form.RqFormSmart;
import org.takes.rs.RsHtml;
import org.takes.rs.RsVelocity;
public final class TakesCreateUser implements Take {
public static Connection con;
TakesCreateUser(Connection connection) {
con = connection;
}
@Override
public Response act(final Request req) throws IOException, SQLException {
RqForm form = new RqFormSmart(req);
Iterable<String> idParam = form.param("id");
Iterable<String> userParam = form.param("user");
int id = Integer.parseInt(idParam.iterator().next());
String user = userParam.iterator().next();
final String INSERT_SQL_QUERY = "insert into take_user values ("+id+", "+"'"+user+"')";
try (PreparedStatement pst = con.prepareStatement(INSERT_SQL_QUERY)) {
System.out.println(pst.executeUpdate());
} catch (SQLException e) {
e.printStackTrace();
}
return new RsHtml(new RsVelocity(this.getClass().getResource("/templates/index.vm") ,new RsVelocity.Pair("userName", "Anshul")));
}
}

View File

@ -1,25 +1,20 @@
package com.baeldung.takes; package com.baeldung.takes;
import java.io.IOException; import java.io.IOException;
import java.sql.SQLException;
import org.takes.Request; import org.takes.Request;
import org.takes.Response; import org.takes.Response;
import org.takes.Take; import org.takes.Take;
import org.takes.rs.RsHtml;
import org.takes.rs.RsVelocity; import org.takes.rs.RsVelocity;
import org.takes.rs.RsWithBody;
import org.takes.rs.RsWithStatus;
import org.takes.rs.RsWithType;
public final class TakesIndex implements Take { public final class TakesIndex implements Take {
// @Override
// public Response act(Request req) throws IOException {
// return new RsWithStatus(new RsWithType(new RsWithBody("<html>Hello, world!</html>"), "text/html"), 200);
// }
@Override @Override
public Response act(Request req) { public Response act(final Request req) throws IOException, SQLException {
return new RsVelocity("Hello, ${name}", new RsVelocity.Pair("name", "Jeffrey"));
return new RsHtml(new RsVelocity(this.getClass().getResource("/templates/index.vm") ,new RsVelocity.Pair("userName", "Anshul")));
} }
} }

View File

@ -0,0 +1,51 @@
package com.baeldung.takes;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.json.Json;
import javax.json.JsonStructure;
import org.takes.Request;
import org.takes.Response;
import org.takes.Take;
import org.takes.misc.Href;
import org.takes.misc.VerboseIterable;
import org.takes.rq.RqHref;
import org.takes.rs.RsJson;
public final class TakesReadUser implements Take {
public static Connection con;
TakesReadUser(Connection connection) {
con = connection;
}
@Override
public Response act(final Request req) throws IOException, SQLException {
Href href = new RqHref.Base(req).href();
Iterable<String> ids = href.param("id");
int id = Integer.parseInt((String) ids.iterator().next());
final String SELECT_SQL_QUERY = "select id, user from take_user where id = " + id;
JsonStructure json = null;
try (PreparedStatement pst = con.prepareStatement(SELECT_SQL_QUERY); ResultSet rs = pst.executeQuery();) {
while (rs.next()) {
json = Json.createObjectBuilder()
.add("id", rs.getInt("id"))
.add("user", rs.getString("user"))
.build();
}
} catch (SQLException e) {
e.printStackTrace();
}
return new RsJson(json);
}
}

View File

@ -1,23 +0,0 @@
package com.baeldung.takes;
import javax.servlet.ServletContext;
import org.takes.Request;
import org.takes.Response;
import org.takes.Take;
import org.takes.rs.RsText;
public final class TakesServletApp implements Take {
private final ServletContext ctx;
public TakesServletApp(final ServletContext context) {
this.ctx = context;
}
@Override
public Response act(final Request req) {
return new RsText("Hello servlet!");
}
}

View File

@ -0,0 +1,7 @@
drop table if exists take_user;
create table take_user(
id numeric,
user varchar(255),
constraint pk_take_user primary key (id)
);

View File

@ -0,0 +1,9 @@
<html>
<head>
<title>Index</title>
</head>
<body>
<h1>Takes Web Application</h1>
<h2>Welcome, ${userName}</h2>
</body>
</html>