Intercepting HTTP requests in Ballerina

Vinod Kavinda
Ballerina Swan Lake Tech Blog
3 min readNov 14, 2018

--

Source:http://labs.codernity.com/codernitydb-http/

In the microservices world, you may have several to hundreds of services. There can be requirements that you need to intercept incoming and outgoing messages. In most use cases these requirements are common for several services. When you have a large number of services, it is not convenient to implement that logic in all the services. This is where an interceptor is useful.

Some of the use cases of interceptors are,

  • Doing security validations.
  • Writing to a common log system.
  • Impose common request/response validations on services.
  • Detecting attacks.
  • For analytics requirements.

As you know Ballerina is a programming language for integration. So it has first class support for network programming. Ballerina allows you to write HTTP services in no time. You can easily intercept HTTP messages to a ballerina service by using the http:Filter. It is an object type with two methods filterRequest() and filterResponse() that returns boolean.

The HTTP:Filter object

All you need to do is to write your own filter object that is structurally similar to the HTTP:Filter object. Then include an instance of your filter in the filters list of the service endpoint.

Specifying a filter in a endpoint

That’s it, your filter will be invoked upon receiving a request and before sending a response back.

Within the interceptor you can,

  • Read the Http Request or Response information.
  • Share information between filters by adding attributes to the FilterContext.
  • By returning false, request response processing can be halted and send a 500 internal server error response to the client.

Let’s look at a sample http intercepter for a hello world service using the filter.

Intercepting hello world service

I hope the code is self explanatory. We have defined a object called LogFilter. Note that it has same structure of the Filter object above. In the log filter, within the filterRequest(), we have logged the request information. Then an attribute is added to the FilterContext. So that it can be read by other filters if there is any. Within the filterResponse() we have added a new header called filtered.

Then we have defined the filter and added it in the endpoint definition.

Let’s start our hello world service by running,

ballerina run http-intercepter.bal

It will start our service on port 5389. Now open another console and send following curl command. Here -i is for displaying response headers.

curl -i localhost:5389/hello/sayHello

In the terminal the ballerina service is running, you will see the logs printed by the request filter.

Info log from the filter

In the response to the curl request you will notice a new header called filtered is added by our response filter.

Response for the curl request

That’s all about writing a http intercepter for ballerina services. All the above sample codes are based on Ballerina v0.983.0

Happy dancing with Ballerina!

--

--

Vinod Kavinda
Ballerina Swan Lake Tech Blog

Engineer @WSO2 | Developing the brilliant @ballerinalang | Trying to learn the art of writing here!