do not break if userprofile store can not be found

This commit is contained in:
Jörg Prante 2024-06-05 14:34:54 +02:00
parent 7d8205dd7a
commit f0d743642a
2 changed files with 15 additions and 17 deletions

View file

@ -1,3 +1,3 @@
group = org.xbib group = org.xbib
name = net-http name = net-http
version = 4.7.0 version = 4.7.1

View file

@ -108,16 +108,7 @@ public class IncomingContextHandler implements HttpHandler {
// important // important
context.getAttributes().put("session", session); context.getAttributes().put("session", session);
if (userProfile == null) { if (userProfile == null) {
try {
userProfile = recoverUserProfile(context, session, payload); userProfile = recoverUserProfile(context, session, payload);
if (userProfile != null) {
logger.log(Level.FINE, "user profile recovered");
} else {
logger.log(Level.FINE, "no user profile recovered");
}
} catch (IOException e) {
logger.log(Level.FINE, "unable to recover new user profile: " + e.getMessage(), e);
}
} }
// important // important
context.getAttributes().put("userprofile", userProfile); context.getAttributes().put("userprofile", userProfile);
@ -178,25 +169,32 @@ public class IncomingContextHandler implements HttpHandler {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected UserProfile recoverUserProfile(HttpRouterContext context, protected UserProfile recoverUserProfile(HttpRouterContext context,
Session session, Session session,
Map<String, Object> payload) Map<String, Object> payload) {
throws IOException {
// already in context from previous handler? // already in context from previous handler?
UserProfile userProfile = context.getAttributes().get(UserProfile.class, "userprofile"); UserProfile userProfile = context.getAttributes().get(UserProfile.class, "userprofile");
if (userProfile != null) { if (userProfile != null) {
try {
return userProfileCodec.read(userProfile.getUserId()); return userProfileCodec.read(userProfile.getUserId());
} catch (IOException e) {
// ignore I/O error, leads to empty user profile
}
} }
// initialize // initialize
Map<String, Object> map = null; Map<String, Object> map = null;
// from session? // is user profile present in session?
if (payload == null && session != null) { if (payload == null && session != null) {
// session has stored user profile? this is important for spanning HTTP request/response like in HTTP POST/FORWARD/GET // session has stored user profile? this is important for spanning HTTP request/response like in HTTP POST/FORWARD/GET
map = (Map<String, Object>) session.get("userprofile"); map = (Map<String, Object>) session.get("userprofile");
} else if (payload != null) { } else if (payload != null) {
// otherwise from cookie? // is a mini user profile otherwise present in our cookie?
map = (Map<String, Object>) payload.get("map"); map = (Map<String, Object>) payload.get("map");
} }
if (map != null && map.containsKey(UserProfile.USER_ID)) { if (map != null && map.containsKey(UserProfile.USER_ID)) {
try {
return userProfileCodec.read(BaseUserProfile.fromMap(map).getUserId()); return userProfileCodec.read(BaseUserProfile.fromMap(map).getUserId());
} catch (IOException e) {
// ignore I/O error, leads to incomplete user profile
}
} else { } else {
logger.log(Level.FINE, "unable to find info for initialize user profile"); logger.log(Level.FINE, "unable to find info for initialize user profile");
} }