Search in sources :

Example 11 with Session

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

the class GetMaxInactiveInterval 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.getMaxInactiveInterval()));
        } else {
            throw new IllegalStateException("Failed to get max inactive interval: 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)

Example 12 with Session

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

the class GetSession method execute.

@Override
public void execute(Context context) {
    try {
        BStruct requestStruct = ((BStruct) context.getRefArgument(0));
        // TODO check below line
        HTTPCarbonMessage httpCarbonMessage = HttpUtil.getCarbonMsg(requestStruct, null);
        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) {
                // Return null as the session is invalidated.
                logger.info("Failed to get session: session is not available");
                context.setReturnValues();
                return;
            }
            // path Validity check
            if (session != null && session.getPath().equals(path)) {
                session.setNew(false);
                session.setAccessed();
            } else {
                throw new BallerinaException("Failed to get session: " + path + " is not an allowed path");
            }
        } else {
            // Return null as the session cookie is not available.
            logger.info("Failed to get session: session cookie is not available");
            context.setReturnValues();
            return;
        }
        httpCarbonMessage.setProperty(HttpConstants.HTTP_SESSION, session);
        httpCarbonMessage.removeHeader(HttpConstants.COOKIE_HEADER);
        context.setReturnValues(HttpUtil.createSessionStruct(context, session));
    } 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 13 with Session

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

the class RemoveAttribute method execute.

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

Example 14 with Session

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

the class SetAttribute method execute.

@Override
public void execute(Context context) throws IllegalFormatException {
    try {
        BStruct sessionStruct = ((BStruct) context.getRefArgument(0));
        String attributeKey = context.getStringArgument(0);
        BValue attributeValue = context.getRefArgument(1);
        Session session = (Session) sessionStruct.getNativeData(HttpConstants.HTTP_SESSION);
        if (attributeKey == null || attributeValue == null) {
            throw new NullPointerException("Failed to set attribute: Attribute key: " + attributeKey + "Attribute Value: " + attributeValue);
        }
        if (session != null && session.isValid()) {
            session.setAttribute(attributeKey, attributeValue);
        } else {
            throw new IllegalStateException("Failed to set attribute: No such session in progress");
        }
    } catch (IllegalStateException e) {
        throw new BallerinaException(e.getMessage(), e);
    }
    context.setReturnValues();
}
Also used : BStruct(org.ballerinalang.model.values.BStruct) BValue(org.ballerinalang.model.values.BValue) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) Session(org.ballerinalang.net.http.session.Session)

Example 15 with Session

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

the class GetAttribute method execute.

@Override
public void execute(Context context) {
    try {
        BStruct sessionStruct = ((BStruct) context.getRefArgument(0));
        String attributeKey = context.getStringArgument(0);
        Session session = (Session) sessionStruct.getNativeData(HttpConstants.HTTP_SESSION);
        if (session != null && session.isValid()) {
            context.setReturnValues(session.getAttributeValue(attributeKey));
        } else {
            throw new IllegalStateException("Failed to get attribute: 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)

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