code: public static Session getSession( HttpServletRequest request,
                                        String repositoryName,
                                        String workspaceName ) throws RepositoryException {
          // Go through all the RepositoryFactory instances and try to create one ...
          Repository repository = getRepository(repositoryName);
  
          // If there's no authenticated user, try an anonymous login
          if (request == null || request.getUserPrincipal() == null) {
              return repository.login(workspaceName);
          }
  
          return repository.login(new ServletCredentials(request), workspaceName);
      }
docs: Get a JCR Session for the named workspace in the named repository, using the supplied HTTP servlet request for
  authentication information.
  
  @param request the servlet request; may not be null or unauthenticated
  @param repositoryName the name of the repository in which the session is created
  @param workspaceName the name of the workspace to which the session should be connected
  @return an active session with the given workspace in the named repository
  @throws RepositoryException if the named repository does not exist or there was a problem obtaining the named repository