import ballerina/http;
import ballerina/io;endpoint http:Listener sessionTestEP {
    port:9090
};@http:ServiceConfig { basePath:"/sessionTest" }
service<http:Service> sessionTest bind sessionTestEP {    string key = "status";
    @http:ResourceConfig {
        methods:["GET"],
        path:"/sayHello"
    }
    sayHello (endpoint outboundEP, http:Request req) {
        http:Session session = req.createSessionIfAbsent();
        string result;
        if (session.isNew()) {
            result = "Say hello to a new session";
        } else {
            result = "Say hello to an existing session";
        }
        session.setAttribute(key, "Session sample");
        http:Response res = new;
        res.setStringPayload(result);
        _ = outboundEP -> respond(res);
    }    @http:ResourceConfig {
        methods:["GET"],
        path:"/doTask"
    }
    doTask (endpoint outboundEP, http:Request req) {
        http:Session session = req.getSession();
        string attributeValue;
        if (session != null) {
            attributeValue = <string>session.getAttribute(key);
        } else {
            attributeValue = "Session unavailable";
        }
        http:Response res = new;
        res.setStringPayload(attributeValue);
        _ = outboundEP -> respond(res);
    }    @http:ResourceConfig {
        methods:["GET"],
        path:"/sayBye"
    }
    sayBye (endpoint outboundEP, http:Request req) {
        http:Session session = req.getSession();
        http:Response res = new;
        if (session != null) {
            string id = session.getId();
            session.invalidate();
            res.setStringPayload("Session: " + id + " invalidated");
        } else {
            res.setStringPayload("Session unavailable");
        }
        _ = outboundEP -> respond(res);
    }
}

HTTP Sessions

This example demonstrates the HTTP Session support in Ballerina. More session related functions are described in https://ballerinalang.org/docs/api/ under http package.

import ballerina/http;
import ballerina/io;
endpoint http:Listener sessionTestEP {
    port:9090
};
@http:ServiceConfig { basePath:"/sessionTest" }
service<http:Service> sessionTest bind sessionTestEP {
    string key = "status";
    @http:ResourceConfig {
        methods:["GET"],
        path:"/sayHello"
    }
    sayHello (endpoint outboundEP, http:Request req) {
        http:Session session = req.createSessionIfAbsent();
        string result;

createSessionIfAbsent() function returns an existing session for a valid session id, otherwise it returns a new session.

        if (session.isNew()) {
            result = "Say hello to a new session";
        } else {
            result = "Say hello to an existing session";
        }

Session status(new or already existing) is informed by isNew() as boolean value.

        session.setAttribute(key, "Session sample");
        http:Response res = new;
        res.setStringPayload(result);
        _ = outboundEP -> respond(res);
    }

Binds a string attribute to this session with a key(string).

    @http:ResourceConfig {
        methods:["GET"],
        path:"/doTask"
    }
    doTask (endpoint outboundEP, http:Request req) {
        http:Session session = req.getSession();
        string attributeValue;
        if (session != null) {

getSession() returns an existing session for a valid session id. otherwise null.

            attributeValue = <string>session.getAttribute(key);
        } else {
            attributeValue = "Session unavailable";
        }
        http:Response res = new;
        res.setStringPayload(attributeValue);
        _ = outboundEP -> respond(res);
    }

Returns the object bound with the specified key.

    @http:ResourceConfig {
        methods:["GET"],
        path:"/sayBye"
    }
    sayBye (endpoint outboundEP, http:Request req) {
        http:Session session = req.getSession();
        http:Response res = new;
        if (session != null) {
            string id = session.getId();

Returns session id.

            session.invalidate();
            res.setStringPayload("Session: " + id + " invalidated");
        } else {
            res.setStringPayload("Session unavailable");
        }
        _ = outboundEP -> respond(res);
    }
}

Invalidates this session.

$ ballerina run http-sessions.bal
ballerina: initiating service(s) in 'http-sessions.bal'
ballerina: started HTTP/WS server connector 0.0.0.0:9090
$ curl -v http://localhost:9090/sessionTest/sayHello
< HTTP/1.1 200 OK
< Content-Type: text/plain
< Set-Cookie: BSESSIONID=D844B4AAC699........E4E249BE5F21; Path=/session;
< Content-Length: 26
Say hello to a new session
$ curl -v http://localhost:9090/sessionTest/doTask -H "Cookie: BSESSIONID=..(use given BSESSIONID).."
Session sample

Copy the BSESSIONID of session cookie in above response. Now invoke doTask resource as follows.

$ curl -v http://localhost:9090/sessionTest/sayBye -H "Cookie: BSESSIONID=..(use same BSESSIONID).."
Session: ..(same BSESSIONID).. invalidated

Finally invoke sayBye using same BSESSIONID.