Skip to content

Bridje HTTP

gilbertovento edited this page Jul 25, 2017 · 18 revisions

Introduction

Bridje HTTP is a small lightweight http server based on netty.

    <dependencies>
        ....
        <dependency>
            <groupId>org.bridje</groupId>
            <artifactId>bridje-http</artifactId>
            <version>${bridje.version}</version>
        </dependency>
        ....
    </dependencies>

Bridlets

Bridje HTTP will take care of the HTTP protocol but your application must handle the requests. The request are handled by a group of Bridje-IoC Components that implements the HttpBridlet interface. They receive a HttpServerContext object with holds the data for the current request and returns a boolean value indicating when ever the request was handled or not.

@Component
@Priority(1)
class HelloHandler implements HttpBridlet
{
    @Override
    public boolean handle(HttpServerContext context) throws IOException
    {
        HttpServerResponse resp = context.getResponse();
        resp.setStatusCode(200);
        try (OutputStreamWriter writer = new OutputStreamWriter(resp.getOutputStream()))
        {
            writer.append("<h1>Hello</h1>");
            writer.flush();
        }
        return true;            
    }
}

The previous example shows how you can create a bridlet for your application, the handle method will be called by Bridje HTTP every time a new HTTP request hits the server. The bridlet will be instantiated only once and this object will handle all the request so you must take care of the multi-thread environment in wich it will run.

Bridlet Chain

Bridlets can be chained together using Bridje-IoC @InjectNext annotation.

@Component
@Priority(1)
class MyBridlet1 implements HttpBridlet
{
    @InjectNext
    private HttpBridlet nextHandler;

    @Override
    public boolean handle(HttpServerContext context) throws IOException
    {
        if(nextHandler != null)
        {
            return nextHandler.handle(context);
        }
        else
        {
            // no next bridlet found, the request must be handled by this object or it must return false.
        }
        return true;            
    }
}

@Component
@Priority(2)
class MyBridlet2 implements HttpBridlet
{
    @Override
    public boolean handle(HttpServerContext context) throws IOException
    {
        //output some results
        HttpServerResponse resp = context.getResponse();
        resp.setStatusCode(200);
        try (OutputStreamWriter writer = new OutputStreamWriter(resp.getOutputStream()))
        {
            writer.append("<h1>Hello</h1>");
            writer.flush();
        }
        return true;            
    }
}

In this example MyBridlet1 has a priority of 1 and MyBridlet2 has priority of 2 so what this means is that Brije IoC will inject MyBridlet1 in the internal RootBridlet component provided by Bridje HTTP, and MyBridlet2 will be injected into MyBridlet1, so all the requests passed to this application will take this route.

HTTP Request: RootHandler (Priority Integer.MIN_VALUE) -> MyBridlet1 (Priority 1) -> MyBridlet2 (Priority 2)

You can add as many bridlets as you wish, but take care of the @Priority annotation, the value you put in it will decide the order in witch the bridlets will be chained. To avoid two bridlets having the same priority, witch can cause that one of then is no injected, we advise you put spread the numbers, (ex: 10, 20, 30). The priority value can be any int number.

HttpServerContext

All bridlets receive a HttpServerContext object, This object holds all the data for the current request, the HttpServerContext object is created for each request. The framework will provide two objects that you can use to control the request, the HttpServerRequest and HttpServerResponse. You can obtain this objects from the context like this:

    public boolean handle(HttpServerContext context) throws IOException
    {
        HttpServerRequest resp = context.getRequest();
        HttpServerResponse resp = context.getResponse();
    }
  • HttpServerRequest holds the request headers and body and provides useful methods to access all the HTTP request data.
  • HttpServerResponse holds the data for the HTTP response, you can use this object to set the response headers and render the body of the response.

You can put more information in the HttpServerContext by using the set method like this:

    public boolean handle(HttpServerContext context) throws IOException
    {
        MyData myData = new MyData();
        context.set(MyData.class, myData);
    }

The the same bridlet or any other bridlet in the chain can use the get method to retrieve the MyData instance.

    public boolean handle(HttpServerContext context) throws IOException
    {
        MyData myData = context.get(MyData.class);
    }

The get method will return null if no instance of the class was put in the context previously.

Configuration

To configure Bridje HTTP you can create an http.xml file in the etc virtual folder the file.

<?xml version="1.0" encoding="UTF-8"?>
<http:http
    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
    xmlns:http='http://www.bridje.org/schemas/http-server'
    xsi:schemaLocation='http://www.bridje.org/schemas/http-server http://repo1.maven.org/maven2/org/bridje/bridje-http/0.2.2/bridje-http-0.2.2-config.xsd'>
    <!-- The server name, returned in the Server header -->
    <http:name>My Server</http:name>
    <!-- HTTP server port -->
    <http:port>8080</http:port>
    <!-- Listen on all ips -->
    <http:listen></http:listen>
</http:http>

The framework specs that the /etc/http.xml virtual file exists. You can mount any folder to the etc virtual folder at runtime.

new VFile("/etc").mount(new FileSource("path/to/my/config/folder"));

If the file does not exists the HTTP server will take the default configuration and run on the 8080 port listening to all ips.

Clone this wiki locally