Search in sources :

Example 6 with Session

use of org.ballerinalang.net.http.session.Session in project ballerina by ballerina-lang.

the class CreateSessionIfAbsent method execute.

@Override
public void execute(Context context) {
    try {
        BStruct requestStruct = ((BStruct) context.getRefArgument(0));
        // TODO check below line
        HTTPCarbonMessage httpCarbonMessage = HttpUtil.getCarbonMsg(requestStruct, HttpUtil.createHttpCarbonMessage(true));
        String cookieHeader = httpCarbonMessage.getHeader(HttpConstants.COOKIE_HEADER);
        String path = (String) httpCarbonMessage.getProperty(HttpConstants.BASE_PATH);
        Session session = (Session) httpCarbonMessage.getProperty(HttpConstants.HTTP_SESSION);
        if (cookieHeader != null) {
            try {
                String sessionId = HttpUtil.getSessionID(cookieHeader);
                // return value from cached session
                if (session != null && sessionId.equals(session.getId())) {
                    session = session.setAccessed();
                    context.setReturnValues(HttpUtil.createSessionStruct(context, session));
                    return;
                }
                session = SessionManager.getInstance().getHTTPSession(sessionId);
            } catch (NoSuchElementException e) {
                // ignore throwable
                logger.info("Failed to get session: Incorrect Session cookie");
            }
            if (session == null) {
                session = SessionManager.getInstance().createHTTPSession(path);
            } else if (session != null && session.getPath().equals(path)) {
                // path validity check
                session.setNew(false);
                session.setAccessed();
            } else {
                throw new BallerinaException("Failed to get session: " + path + " is not an allowed path");
            }
        } else {
            // cached session will return of this function is called twice.
            if (session != null) {
                session = session.setAccessed();
                context.setReturnValues(HttpUtil.createSessionStruct(context, session));
                return;
            }
            // create session since request doesn't have a cookie
            session = SessionManager.getInstance().createHTTPSession(path);
        }
        httpCarbonMessage.setProperty(HttpConstants.HTTP_SESSION, session);
        httpCarbonMessage.removeHeader(HttpConstants.COOKIE_HEADER);
        context.setReturnValues(HttpUtil.createSessionStruct(context, session));
        return;
    } catch (IllegalStateException e) {
        throw new BallerinaException(e.getMessage(), e);
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) NoSuchElementException(java.util.NoSuchElementException) Session(org.ballerinalang.net.http.session.Session)

Example 7 with Session

use of org.ballerinalang.net.http.session.Session in project ballerina by ballerina-lang.

the class HttpUtil method addHTTPSessionAndCorsHeaders.

public static void addHTTPSessionAndCorsHeaders(Context context, HTTPCarbonMessage requestMsg, HTTPCarbonMessage responseMsg) {
    Session session = (Session) requestMsg.getProperty(HttpConstants.HTTP_SESSION);
    if (session != null) {
        boolean isSecureRequest = false;
        AnnAttachmentInfo configAnn = context.getServiceInfo().getAnnotationAttachmentInfo(HttpConstants.PROTOCOL_PACKAGE_HTTP, HttpConstants.ANN_NAME_CONFIG);
        if (configAnn != null) {
            AnnAttributeValue httpsPortAttrVal = configAnn.getAttributeValue(HttpConstants.ANN_CONFIG_ATTR_HTTPS_PORT);
            if (httpsPortAttrVal != null) {
                Integer listenerPort = (Integer) requestMsg.getProperty(HttpConstants.LISTENER_PORT);
                if (listenerPort != null && httpsPortAttrVal.getIntValue() == listenerPort) {
                    isSecureRequest = true;
                }
            }
        }
        session.generateSessionHeader(responseMsg, isSecureRequest);
    }
    // Process CORS if exists.
    if (requestMsg.getHeader(HttpHeaderNames.ORIGIN.toString()) != null) {
        CorsHeaderGenerator.process(requestMsg, responseMsg, true);
    }
}
Also used : AnnAttachmentInfo(org.ballerinalang.util.codegen.AnnAttachmentInfo) AnnAttributeValue(org.ballerinalang.util.codegen.AnnAttributeValue) Session(org.ballerinalang.net.http.session.Session)

Example 8 with Session

use of org.ballerinalang.net.http.session.Session in project ballerina by ballerina-lang.

the class GetAttributes method execute.

@Override
public void execute(Context context) {
    try {
        BStruct sessionStruct = ((BStruct) context.getRefArgument(0));
        Session session = (Session) sessionStruct.getNativeData(HttpConstants.HTTP_SESSION);
        if (session != null && session.isValid()) {
            context.setReturnValues(session.getAttributes());
        } else {
            throw new IllegalStateException("Failed to get attribute map: No such session in progress");
        }
    } catch (IllegalStateException e) {
        throw new BallerinaException(e.getMessage(), e);
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) Session(org.ballerinalang.net.http.session.Session)

Example 9 with Session

use of org.ballerinalang.net.http.session.Session in project ballerina by ballerina-lang.

the class GetId method execute.

@Override
public void execute(Context context) {
    try {
        BStruct sessionStruct = ((BStruct) context.getRefArgument(0));
        Session session = (Session) sessionStruct.getNativeData(HttpConstants.HTTP_SESSION);
        if (session != null && session.isValid()) {
            context.setReturnValues(new BString(session.getId()));
        } else {
            throw new IllegalStateException("Failed to get session id: No such session in progress");
        }
    } catch (IllegalStateException e) {
        throw new BallerinaException(e.getMessage(), e);
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BString(org.ballerinalang.model.values.BString) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) Session(org.ballerinalang.net.http.session.Session)

Example 10 with Session

use of org.ballerinalang.net.http.session.Session in project ballerina by ballerina-lang.

the class GetLastAccessedTime method execute.

@Override
public void execute(Context context) {
    try {
        BStruct sessionStruct = ((BStruct) context.getRefArgument(0));
        Session session = (Session) sessionStruct.getNativeData(HttpConstants.HTTP_SESSION);
        if (session != null && session.isValid()) {
            context.setReturnValues(new BInteger(session.getLastAccessedTime()));
        } else {
            throw new IllegalStateException("Failed to get last accessed time: No such session in progress");
        }
    } catch (IllegalStateException e) {
        throw new BallerinaException(e.getMessage(), e);
    }
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BInteger(org.ballerinalang.model.values.BInteger) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) Session(org.ballerinalang.net.http.session.Session)

Aggregations

Session (org.ballerinalang.net.http.session.Session)15 BStruct (org.ballerinalang.model.values.BStruct)14 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)14 BInteger (org.ballerinalang.model.values.BInteger)3 NoSuchElementException (java.util.NoSuchElementException)2 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)2 BBoolean (org.ballerinalang.model.values.BBoolean)1 BString (org.ballerinalang.model.values.BString)1 BStringArray (org.ballerinalang.model.values.BStringArray)1 BValue (org.ballerinalang.model.values.BValue)1 AnnAttachmentInfo (org.ballerinalang.util.codegen.AnnAttachmentInfo)1 AnnAttributeValue (org.ballerinalang.util.codegen.AnnAttributeValue)1