@Test public void returnsAnEmptyOptionalForABogusToken() { LoggedInUsers instance = userStoreWithUserA; String bogusToken = "bogus"; Optional<User> user = instance.userFor(bogusToken); assertThat(user, is(not(present()))); }
public synchronized Optional<User> userFor(String authHeader) { if (authHeader == null || authHeader.isEmpty()) { return Optional.empty(); } else { User local = users.getIfPresent(authHeader); if (local != null) { return Optional.of(local); } else { try { SecurityInformation securityInformation = authenticationHandler.getSecurityInformation(authHeader); Optional<User> userFromFile = userStore.userFor(securityInformation.getPersistentID()); if (userFromFile.isPresent()) { users.put(authHeader, userFromFile.get()); return userFromFile; } else { User nw = userStore.saveNew(securityInformation.getDisplayName(), securityInformation.getPersistentID()); users.put(authHeader, nw); return Optional.of(nw); } } catch (UnauthorizedException e) { LOG.warn("User is not retrievable", e); return Optional.empty(); } catch (IOException | AuthenticationUnavailableException e) { LOG.error("An exception is thrown while retrieving the user information.", e); return Optional.empty(); } } } }
LoggedInUsers { public synchronized Optional<User> userFor(String authHeader) { if (authHeader == null || authHeader.isEmpty()) { return Optional.empty(); } else { User local = users.getIfPresent(authHeader); if (local != null) { return Optional.of(local); } else { try { SecurityInformation securityInformation = authenticationHandler.getSecurityInformation(authHeader); Optional<User> userFromFile = userStore.userFor(securityInformation.getPersistentID()); if (userFromFile.isPresent()) { users.put(authHeader, userFromFile.get()); return userFromFile; } else { User nw = userStore.saveNew(securityInformation.getDisplayName(), securityInformation.getPersistentID()); users.put(authHeader, nw); return Optional.of(nw); } } catch (UnauthorizedException e) { LOG.warn("User is not retrievable", e); return Optional.empty(); } catch (IOException | AuthenticationUnavailableException e) { LOG.error("An exception is thrown while retrieving the user information.", e); return Optional.empty(); } } } } }
LoggedInUsers { public synchronized Optional<User> userFor(String authHeader) { if (authHeader == null || authHeader.isEmpty()) { return Optional.empty(); } else { User local = users.getIfPresent(authHeader); if (local != null) { return Optional.of(local); } else { try { SecurityInformation securityInformation = authenticationHandler.getSecurityInformation(authHeader); Optional<User> userFromFile = userStore.userFor(securityInformation.getPersistentID()); if (userFromFile.isPresent()) { users.put(authHeader, userFromFile.get()); return userFromFile; } else { User nw = userStore.saveNew(securityInformation.getDisplayName(), securityInformation.getPersistentID()); users.put(authHeader, nw); return Optional.of(nw); } } catch (UnauthorizedException e) { LOG.warn("User is not retrievable", e); return Optional.empty(); } catch (IOException | AuthenticationUnavailableException e) { LOG.error("An exception is thrown while retrieving the user information.", e); return Optional.empty(); } } } } LoggedInUsers(Authenticator authenticator, UserStore userStore,
                       Timeout inactivityTimeout, AuthenticationHandler authenticationHandler); }
LoggedInUsers { public synchronized Optional<User> userFor(String authHeader) { if (authHeader == null || authHeader.isEmpty()) { return Optional.empty(); } else { User local = users.getIfPresent(authHeader); if (local != null) { return Optional.of(local); } else { try { SecurityInformation securityInformation = authenticationHandler.getSecurityInformation(authHeader); Optional<User> userFromFile = userStore.userFor(securityInformation.getPersistentID()); if (userFromFile.isPresent()) { users.put(authHeader, userFromFile.get()); return userFromFile; } else { User nw = userStore.saveNew(securityInformation.getDisplayName(), securityInformation.getPersistentID()); users.put(authHeader, nw); return Optional.of(nw); } } catch (UnauthorizedException e) { LOG.warn("User is not retrievable", e); return Optional.empty(); } catch (IOException | AuthenticationUnavailableException e) { LOG.error("An exception is thrown while retrieving the user information.", e); return Optional.empty(); } } } } LoggedInUsers(Authenticator authenticator, UserStore userStore,
                       Timeout inactivityTimeout, AuthenticationHandler authenticationHandler); synchronized Optional<User> userFor(String authHeader); synchronized Optional<String> userTokenFor(String username, String password); }
LoggedInUsers { public synchronized Optional<User> userFor(String authHeader) { if (authHeader == null || authHeader.isEmpty()) { return Optional.empty(); } else { User local = users.getIfPresent(authHeader); if (local != null) { return Optional.of(local); } else { try { SecurityInformation securityInformation = authenticationHandler.getSecurityInformation(authHeader); Optional<User> userFromFile = userStore.userFor(securityInformation.getPersistentID()); if (userFromFile.isPresent()) { users.put(authHeader, userFromFile.get()); return userFromFile; } else { User nw = userStore.saveNew(securityInformation.getDisplayName(), securityInformation.getPersistentID()); users.put(authHeader, nw); return Optional.of(nw); } } catch (UnauthorizedException e) { LOG.warn("User is not retrievable", e); return Optional.empty(); } catch (IOException | AuthenticationUnavailableException e) { LOG.error("An exception is thrown while retrieving the user information.", e); return Optional.empty(); } } } } LoggedInUsers(Authenticator authenticator, UserStore userStore,
                       Timeout inactivityTimeout, AuthenticationHandler authenticationHandler); synchronized Optional<User> userFor(String authHeader); synchronized Optional<String> userTokenFor(String username, String password); static final Logger LOG; }