private LoginStatus tryLogin(HttpServletRequest httpRequest,String service,boolean setCookie,HttpServletResponse servletResponse,boolean tokenOnly) throws UnsupportedEncodingException, IOException {
  String newToken=null;
  String userName=null;
  _log.debug("Logging in");
  UsernamePasswordCredentials credentials=tokenOnly ? null : getCredentials(httpRequest);
  if (credentials == null) {
    StorageOSUser user=getUserFromContext();
    if (user != null) {
      newToken=user.getToken();
      userName=user.getName();
      _log.debug("Logged in with user from context");
    }
  }
 else {
    StorageOSUserDAO user=authenticateUser(credentials);
    if (user != null) {
      validateLocalUserExpiration(credentials);
      newToken=_tokenManager.getToken(user);
      if (newToken == null) {
        _log.error("Could not generate token for user: {}",user.getUserName());
        throw new IllegalStateException(MessageFormat.format("Could not generate token for user: {}",user.getUserName()));
      }
      userName=user.getUserName();
      auditOp(URI.create(user.getTenantId()),URI.create(user.getUserName()),OperationTypeEnum.AUTHENTICATION,true,null,credentials.getUserName());
    }
 else {
      auditOp(null,null,OperationTypeEnum.AUTHENTICATION,false,null,credentials.getUserName());
    }
  }
  return new LoginStatus(userName,newToken,null != credentials);
}

private LoginStatus tryLogin ( HttpServletRequest httpRequest , String service , boolean setCookie , HttpServletResponse servletResponse , boolean tokenOnly ) throws UnsupportedEncodingException , IOException { String newToken = null ; String userName = null ; _log . debug ( "Logging in" ) ; UsernamePasswordCredentials credentials = tokenOnly ? null : getCredentials ( httpRequest ) ; if ( credentials == null ) { StorageOSUser user = getUserFromContext ( ) ; if ( user != null ) { newToken = user . getToken ( ) ; userName = user . getName ( ) ; _log . debug ( "Logged in with user from context" ) ; } } else { StorageOSUserDAO user = authenticateUser ( credentials ) ; if ( user != null ) { validateLocalUserExpiration ( credentials ) ; newToken = _tokenManager . getToken ( user ) ; if ( newToken == null ) { _log . error ( "Could not generate token for user: {}" , user . getUserName ( ) ) ; throw new IllegalStateException ( MessageFormat . format ( "Could not generate token for user: {}" , user . getUserName ( ) ) ) ; } userName = user . getUserName ( ) ; auditOp ( URI . create ( user . getTenantId ( ) ) , URI . create ( user . getUserName ( ) ) , OperationTypeEnum . AUTHENTICATION , true , null , credentials . getUserName ( ) ) ; } else { auditOp ( null , null , OperationTypeEnum . AUTHENTICATION , false , null , credentials . getUserName ( ) ) ; } } return new LoginStatus ( userName , newToken , null != credentials ) ; }
see if the user is already logged in or try to login the user if credentials were supplied . return authentication status
