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:
parent
505a0c9547
commit
a6c090f66b
|
@ -83,7 +83,6 @@
|
|||
<artifactId>moshi-adapters</artifactId>
|
||||
<version>${moshi.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.jcabi</groupId>
|
||||
<artifactId>jcabi-aspects</artifactId>
|
||||
|
@ -95,6 +94,16 @@
|
|||
<version>${aspectjrt.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.takes</groupId>
|
||||
<artifactId>takes</artifactId>
|
||||
<version>${takes.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.velocity</groupId>
|
||||
<artifactId>velocity-engine-core</artifactId>
|
||||
<version>${velocity-engine-core.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
|
@ -132,8 +141,55 @@
|
|||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/webapp</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>reload</id>
|
||||
<build>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
<resource>
|
||||
<directory>src/main/webapp</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
<version>${exec-maven-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>start-server</id>
|
||||
<phase>pre-integration-test</phase>
|
||||
<goals>
|
||||
<goal>java</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<mainClass>com.baeldung.takes.TakesApp</mainClass>
|
||||
<cleanupDaemonThreads>false</cleanupDaemonThreads>
|
||||
<arguments>
|
||||
<argument>--port=${port}</argument>
|
||||
</arguments>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
<jcommander.version>1.78</jcommander.version>
|
||||
<lombok.version>1.18.6</lombok.version>
|
||||
|
@ -151,5 +207,9 @@
|
|||
<jcabi-maven-plugin.version>0.14.1</jcabi-maven-plugin.version>
|
||||
<aspectjtools.version>1.9.2</aspectjtools.version>
|
||||
<aspectjweaver.version>1.9.2</aspectjweaver.version>
|
||||
|
||||
<takes.version>1.19</takes.version>
|
||||
<velocity-engine-core.version>2.2</velocity-engine-core.version>
|
||||
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
|
@ -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))
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>Index</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Takes Web Application</h1>
|
||||
<h2>Welcome, ${username}</h2>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.takes;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.junit.Test;
|
||||
import org.takes.http.FtRemote;
|
||||
|
||||
public class TakesAppIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenTake_whenRunRemoteServer_thenRespond() throws Exception {
|
||||
new FtRemote(new TakesContact()).exec(
|
||||
new FtRemote.Script() {
|
||||
@Override
|
||||
public void exec(final URI home) throws IOException {
|
||||
HttpClient client = HttpClientBuilder.create().build();
|
||||
HttpResponse response = client.execute(new HttpGet(home));
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
HttpEntity entity = response.getEntity();
|
||||
String result = EntityUtils.toString(entity);
|
||||
|
||||
assertEquals(200, statusCode);
|
||||
assertEquals("Contact us at https://www.baeldung.com", result);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.takes;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.takes.rq.RqFake;
|
||||
import org.takes.rs.RsPrint;
|
||||
|
||||
public class TakesContactUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenTake_whenInvokeActMethod_thenRespond() throws Exception {
|
||||
final String resp = new RsPrint(new TakesContact().act(new RqFake())).printBody();
|
||||
assertEquals("Contact us at https://www.baeldung.com", resp);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue