-
Notifications
You must be signed in to change notification settings - Fork 13
Routing with Undertow
Undertow is the Kikaha's HTTP engine. Behind the scenes all method mapped through the uRouting API will be delegated to an undertow's HttpHandler implementation. Developers who are familiar with this API could use Kikaha to auto-deploy its HttpHandler's routes.
import io.undertow.server.*;
import io.undertow.utils.*;
import kikaha.core.api.*;
import javax.inject.*;
@Singleton
@WebResource( value="/hello/world", method="GET" )
public class HelloWorldResource implements HttpHandler {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
exchange.getResponseSender().send("Hello World");
}
}
The above sample code produces the same result as the example code found at the undertow.io documentation page. The kikaha.core.modules.http.WebResource
maps the resource to a endpoint location and HTTP method. But, unlike what happens with regular uRouting methods (and classes), this implementation needs to be managed.
All information available during a HTTP request could be retrieved by the HttpServerExchange object, exposed by the Undertow's HttpHandler interface. It is also an object which you can send response back to the HTTP client. Designed to be flexible, it a low-level API which could also make code a little verbose. Kikaha provide an out-of-box abstraction that a few convenient methods that avoid developers to write verbose code. The following sample code show how is possible to use this simplified API.
import kikaha.urouting.*;
import javax.inject.*;
import io.undertow.server.*;
@Singleton
public class MySampleRedirectHttpHandler implements HttpHandler {
@Inject UndertowHelper requestHelper;
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
// A wrapper for HttpServerExchange
SimpleExchange simplified = requestHelper.simplify( exchange );
String newLocation = simplified.getRelativePath() + "/" + simplified.getQueryParam( "id", Long.class );
simplified.sendResponse( DefaultResponse.seeOther( newLocation ) );
}
}
In order to use the Simplified API, you should import the
kikaha-urouting
module on your project.
WELCOME
About
Kikaha philosophy
GETTING STARTED
Getting started in 1 minute
Creating a Kikaha maven project
Architecture overview
TUTORIALS
Logging configuration
Configuring the server
Creating your first HTTP route
Kikaha's command line interface
Configuring your favorite IDE
Wro4j Integration
CircleCI Integration
CORE FEATURES
HTTP and HTTPS
Routing static assets
Dependency injection
Authentication and authorization
Smart routes
ESSENTIAL MODULES
μRouting API
WebSocket Routing
Database Connection Pool
JSON with Jackson
Protobuf
Mustache Templates
Rocker Templates
BCrypt
CLOUD MODULES
Overview of Cloud Modules
Consul.io
Codahale's Metrics
Auth0 Single Sign-On
μWorkers - Actor-like API
Hazelcast
AWS-RELATED MODULES
Overview of AWS-Related Modules
Deploying Applications on AWS
AWS IAM Credentials
AWS EC2
AWS SQS queues
AWS CloudWatch metrics
AWS Application Load Balancer
AWS Lambda functions
AWS X-Ray
ADVANCED TOPICS
Creating custom modules
Routing with Undertow's API
Creating custom cloud modules