Using Context.headerAsClass() method, we can validate the request headers.
Example
Integer expireAfter = ctx
.headerAsClass("expire_after_in_minutes", Integer.class)
.check(time -> time <= 10 && time > 5, "Expiry time should be between 5 and 10 inclusive")
.getOrDefault(8);
Find the below working application.
Step 1: Create new maven project ‘javalin-validate-request-header’.
Step 2: Update pom.xml with maven dependenices.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample.app</groupId>
<artifactId>javalin-validate-request-header</artifactId>
<version>1.0.0</version>
<properties>
<packaging>jar</packaging>
<jdk.version>17</jdk.version>
<release.version>17</release.version>
</properties>
<dependencies>
<dependency>
<groupId>io.javalin</groupId>
<artifactId>javalin</artifactId>
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.14.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.sample.app.App</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Step 3: Define JsonUtil class.
JsonUtil.java
package com.sample.app.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonUtil {
public static String getJson(Object obj) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(obj);
}
}
Step 4: Define main application class.
App.java
package com.sample.app;
import java.util.HashMap;
import java.util.Map;
import com.sample.app.util.JsonUtil;
import io.javalin.Javalin;
import io.javalin.http.HttpStatus;
public class App {
public static void main(String[] args) {
Javalin javalin = Javalin.create();
javalin.get("/", (ctx) -> {
ctx.result("Welcome to Javalin programming!!!!");
});
javalin.get("/headers-demo", (ctx) -> {
Integer expireAfter = ctx.headerAsClass("expire_after_in_minutes", Integer.class)
.check(time -> time <= 10 && time > 5, "Expiry time should be between 5 and 10 inclusive")
.getOrDefault(8);
Map<String, Object> map = new HashMap<>();
map.put("expire_after_in_minutes", expireAfter);
String respJson = JsonUtil.getJson(map);
ctx.res().setHeader("Content-Type", "application/json");
ctx.result(respJson).status(HttpStatus.OK);
});
javalin.start(8080);
}
}
Total project structure looks like below.
Build the project
Navigate to the folder, where pom.xml is located and execute the command ‘mvn package’.
Upon successful command execution, you can see a ‘javalin-validate-request-header-1.0.0-jar-with-dependencies.jar’ file.
$ls ./target/
archive-tmp
classes
generated-sources
generated-test-sources
javalin-validate-request-header-1.0.0-jar-with-dependencies.jar
javalin-validate-request-header-1.0.0.jar
maven-archiver
maven-status
test-classes
Run the application
Execute below command to run the application.
java -jar ./target/javalin-validate-request-header-1.0.0-jar-with-dependencies.jar
$java -jar ./target/javalin-validate-request-header-1.0.0-jar-with-dependencies.jar
[main] INFO io.javalin.Javalin - Starting Javalin ...
[main] INFO org.eclipse.jetty.server.Server - jetty-11.0.13; built: 2022-12-07T20:47:15.149Z; git: a04bd1ccf844cf9bebc12129335d7493111cbff6; jvm 17.0.5+9-LTS-191
[main] INFO org.eclipse.jetty.server.session.DefaultSessionIdManager - Session workerName=node0
[main] INFO org.eclipse.jetty.server.handler.ContextHandler - Started i.j.j.@30ee2816{/,null,AVAILABLE}
[main] INFO org.eclipse.jetty.server.AbstractConnector - Started ServerConnector@26a7b76d{HTTP/1.1, (http/1.1)}{0.0.0.0:8080}
[main] INFO org.eclipse.jetty.server.Server - Started Server@1fc2b765{STARTING}[11.0.13,sto=0] @381ms
[main] INFO io.javalin.Javalin -
__ ___ ______
/ /___ __ ______ _/ (_)___ / ____/
__ / / __ `/ | / / __ `/ / / __ \ /___ \
/ /_/ / /_/ /| |/ / /_/ / / / / / / ____/ /
\____/\__,_/ |___/\__,_/_/_/_/ /_/ /_____/
https://javalin.io/documentation
[main] INFO io.javalin.Javalin - Listening on http://localhost:8080/
[main] INFO io.javalin.Javalin - You are running Javalin 5.3.2 (released January 22, 2023).
[main] INFO io.javalin.Javalin - Javalin started in 256ms \o/
Test the apis
Example 1: with invalid request header value.
curl --location --request GET 'http://localhost:8080/headers-demo' \
--header 'expire_after_in_minutes: 3'
$curl --location --request GET 'http://localhost:8080/headers-demo' \
> --header 'expire_after_in_minutes: 3'
{"expire_after_in_minutes":[{"message":"Expiry time should be between 5 and 10 inclusive","args":{},"value":3}]}
Example 2: with valid request header value.
curl --location --request GET 'http://localhost:8080/headers-demo' \
--header 'expire_after_in_minutes: 9'
$curl --location --request GET 'http://localhost:8080/headers-demo' \
> --header 'expire_after_in_minutes: 9'
{"expire_after_in_minutes":9}
Example 3: By not passing the request header.
$curl --location --request GET 'http://localhost:8080/headers-demo'
{"expire_after_in_minutes":8}
You can download this application from this link.
Previous Next Home
No comments:
Post a Comment