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