use of com.willshex.blogwt.server.service.session.ISessionService in project blogwt by billy1380.
the class LoginActionHandler method handle.
/* (non-Javadoc)
*
* @see
* com.willshex.gson.web.service.server.ActionHandler#handle(com.willshex.
* gson.web.service.shared.Request,
* com.willshex.gson.web.service.shared.Response) */
@Override
protected void handle(LoginRequest input, LoginResponse output) throws Exception {
input = ApiValidator.request(input, LoginRequest.class);
input.accessCode = ApiValidator.accessCode(input.accessCode, "input.accessCode");
boolean foundToken = false;
if (input.session != null && input.session.id != null) {
foundToken = true;
}
if (!foundToken) {
IUserService userService = UserServiceProvider.provide();
User user = null;
if (input.username != null) {
user = userService.getLoginUser(input.username, input.password);
if (user == null)
throw new AuthenticationException(input.username);
}
if (user == null && input.email != null) {
user = userService.getEmailLoginUser(input.email, input.password);
if (user == null)
throw new AuthenticationException(input.email);
}
if (user == null)
throw new AuthenticationException("Either username or email addressed cannot be null");
ISessionService sessionService = SessionServiceProvider.provide();
if (LOG.isLoggable(Level.FINER)) {
LOG.finer("Getting user session");
}
output.session = sessionService.getUserSession(user);
if (output.session == null) {
if (LOG.isLoggable(Level.FINER)) {
LOG.finer("Existing session not found, creating new session");
}
output.session = sessionService.createUserSession(user, input.longTerm);
UserServiceProvider.provide().updateUserIdLastLoggedIn(user.id);
} else {
if (input.longTerm != null) {
output.session.longTerm(input.longTerm);
}
output.session = SessionServiceProvider.provide().extendSession(output.session);
output.session.user = user;
}
} else {
output.session = SessionValidator.lookupCheckAndExtend(input.session, "input.session");
}
if (output.session.user.roleKeys != null) {
output.session.user.roles = PersistenceHelper.batchLookup(RoleServiceProvider.provide(), output.session.user.roleKeys);
}
if (output.session.user.permissionKeys != null) {
output.session.user.permissions = PersistenceHelper.batchLookup(PermissionServiceProvider.provide(), output.session.user.permissionKeys);
}
}
use of com.willshex.blogwt.server.service.session.ISessionService in project blogwt by billy1380.
the class ServletHelper method session.
public static Session session(HttpServletRequest request) {
ISessionService sessionService = SessionServiceProvider.provide();
Cookie[] cookies = request.getCookies();
String sessionId = null;
Session userSession = null;
if (cookies != null) {
for (Cookie currentCookie : cookies) {
if ("session.id".equals(currentCookie.getName())) {
sessionId = currentCookie.getValue();
break;
}
}
if (sessionId != null) {
userSession = sessionService.getSession(Long.valueOf(sessionId));
if (userSession != null) {
try {
userSession = SessionValidator.lookupCheckAndExtend(userSession, "session");
UserHelper.stripPassword(userSession.user);
UserHelper.populateRolesAndPermissionsFromKeys(userSession.user);
} catch (ServiceException e) {
userSession = null;
}
}
}
}
return userSession;
}
Aggregations