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);
}
}
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);
}
}
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();
}
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();
}
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);
}
}
Aggregations