import ballerina/mime;
import ballerina/http;
endpoint http:Listener cbrEP {
    port:9090
};
endpoint http:Client locationEP {
    targets:[{url: "http://www.mocky.io"}]
};@http:ServiceConfig { basePath:"/cbr" }
service<http:Service> contentBasedRouting bind cbrEP {
    @http:ResourceConfig {
        methods:["POST"],
        path:"/route"
    }
    cbrResource (endpoint outboundEP, http:Request req) {
        var jsonMsg = req.getJsonPayload();        match jsonMsg {
            json msg => {
                string nameString;
                nameString = check <string>msg["name"];
                (http:Response|http:HttpConnectorError|()) clientResponse;                if (nameString == "sanFrancisco") {
                    clientResponse = locationEP -> post("/v2/594e018c1100002811d6d39a", new);
                } else {
                    clientResponse = locationEP -> post("/v2/594e026c1100004011d6d39c", new);
                }
                match clientResponse {
                    http:Response respone => {
                        _ = outboundEP -> respond(respone);
                    }
                    http:HttpConnectorError conError => {
                        http:HttpConnectorError err = {};
                        http:Response res = new;
                        res.statusCode = 500;
                        res.setStringPayload(err.message);
                        _ = outboundEP -> respond(res);
                    }
                    () => {
                    }
                }
            }
            http:PayloadError err => {
                http:Response res = new;
                res.statusCode = 500;
                res.setStringPayload(err.message);
                _ = outboundEP -> respond(res);
            }
        }
    }
}

Content Based Routing

The Content-Based Router service reads the content of a request and routes it to a specific recipient based on the content.

import ballerina/mime;
import ballerina/http;
endpoint http:Listener cbrEP {
    port:9090
};

Define the attributes associated with the service endpoint here.

endpoint http:Client locationEP {
    targets:[{url: "http://www.mocky.io"}]
};

Define the attributes associated with the client endpoint here.

@http:ServiceConfig { basePath:"/cbr" }
service<http:Service> contentBasedRouting bind cbrEP {
    @http:ResourceConfig {
        methods:["POST"],
        path:"/route"
    }
    cbrResource (endpoint outboundEP, http:Request req) {

Use the @http:POST{} annotation to declare the HTTP method.

        var jsonMsg = req.getJsonPayload();

Get the JSON payload from the request message.

        match jsonMsg {
            json msg => {
                string nameString;
                nameString = check <string>msg["name"];
                (http:Response|http:HttpConnectorError|()) clientResponse;

Get the string value relevant to the key ‘name’.

                if (nameString == "sanFrancisco") {
                    clientResponse = locationEP -> post("/v2/594e018c1100002811d6d39a", new);
                } else {
                    clientResponse = locationEP -> post("/v2/594e026c1100004011d6d39c", new);
                }

Here, ‘post’ represents the POST action of the HTTP client connector. This routes the payload to the relevant service when the server accepts the enclosed entity.

                match clientResponse {
                    http:Response respone => {
                        _ = outboundEP -> respond(respone);
                    }
                    http:HttpConnectorError conError => {
                        http:HttpConnectorError err = {};
                        http:Response res = new;
                        res.statusCode = 500;
                        res.setStringPayload(err.message);
                        _ = outboundEP -> respond(res);
                    }
                    () => {
                    }
                }
            }
            http:PayloadError err => {
                http:Response res = new;
                res.statusCode = 500;
                res.setStringPayload(err.message);
                _ = outboundEP -> respond(res);
            }
        }
    }
}

Use the native function ‘respond’ to send the client response back to the caller.

$ ballerina run content-based-routing.bal
ballerina: initiating service(s) in 'content-based-routing.bal'
ballerina: started HTTP/WS server connector 0.0.0.0:9090
$ curl -v http://localhost:9090/cbr/route -d '{"name" : "sanFrancisco"}' -H "Content-Type:application/json"
{
    "name": "San Francisco Test Station,USA",
    "longitude": -122.43,
    "latitude": 37.76,
    "altitude": 150,
    "rank": 1
}
$ curl -v http://localhost:9090/cbr/route -d '{"name" : "london"}' -H "Content-Type:application/json"
{
    "name": "London Test Station,England",
    "longitude": -156.49,
    "latitude": 57.76,
    "altitude": 430,
    "rank": 5
}