if (domain == null) {
return null;
}
// Tr to get pers db from domain
WGDatabase persDB = null;
try {
persDB = _core.openPersonalisationDB(domain, session);
}
catch (WGAPIException e1) {
_core.getLog().error("Error opening personalisation db for domain '" + domain + "'", e1);
throw e1;
}
// Try to use content database as pers db or use an external self
// personalisation db
if (persDB == null) {
if (database.hasFeature(WGDatabase.FEATURE_SELF_PERSONALIZABLE)) {
persDB = database;
}
else {
persDB = (WGDatabase) database.getAttribute(WGACore.DBATTRIB_EXTERNAL_SELF_PERSONALISATION_DB);
if (persDB != null) {
if (!persDB.isSessionOpen()) {
persDB.openSession();
}
}
else {
return null;
}
}
}
// Try to retrieve previously stored user profile in session
String wgpid = (String) session.getAttribute(SESSION_PROFILENAME_INDIVIDUALDB + database.getDbReference());
if (wgpid != null) {
WGUserProfile profile = persDB.getUserProfile(wgpid);
if (profile != null) {
TMLUserProfile tmlProfile = new TMLUserProfile(profile, getCore());
request.setAttribute(WGACore.ATTRIB_PROFILE + database.getDbReference(), tmlProfile);
return tmlProfile;
}
}
// Fetch user profile using database's persmode. If not available we
// take the default value for the option
String persModeStr = (String) _core.readPublisherOptionOrDefault(database, WGACore.DBATTRIB_PERSMODE);
Integer persMode;
if (persModeStr != null) {
persMode = Integer.valueOf(persModeStr);
}
// Backup for sure, which should not happen as the option is defined for
// all databases
else {
persMode = Constants.PERSMODE_AUTO;
}
// If in Mode AUTO, the User Profile is retrieved automatically
// (otherwise it is set in the login process)
WGUserProfile userProfile = null;
boolean created = false;
if (persMode == Constants.PERSMODE_AUTO) {
boolean secureMode = database.getBooleanAttribute(WGACore.DBATTRIB_SECURE_APP, false);
String cookieName = COOKIE_WGPID;
if (secureMode) {
cookieName = COOKIE_SECURE_WGPID;
}
wgpid = this.findCookie(request.getCookies(), cookieName);
if (wgpid != null) {
userProfile = persDB.getUserProfile(wgpid);
}
if (userProfile == null) {
userProfile = createUserProfile((String) request.getHeader("User-Agent"), persDB, wgpid, Constants.PERSMODE_AUTO);
created = true;
}
if (userProfile != null && !userProfile.isDummy()) {
Cookie wgpidCookie = new Cookie(cookieName, userProfile.getName());
wgpidCookie.setPath("/");
wgpidCookie.setSecure(secureMode);
wgpidCookie.setMaxAge(60 * 60 * 24 * 365);
response.addCookie(wgpidCookie);
}
}
else if (persMode == Constants.PERSMODE_LOGIN) {
if (database.getSessionContext().isAnonymous()) {
// no profile for anonymous
return null;
}
String userName = database.getSessionContext().getUser();
userProfile = persDB.getUserProfile(userName);
if (userProfile == null) {
userProfile = createUserProfile((String) request.getHeader("User-Agent"), persDB, userName, Constants.PERSMODE_LOGIN);
created = true;
}
}