From bd8ec510a1c1d8ae474bceee7837311f9de05bdb Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Thu, 27 Jun 2013 09:26:35 -0400
Subject: [PATCH] Improve RPC documentation

---
 src/main/java/com/gitblit/GitBlit.java |   84 ++++++++++++++++++++++++++++++++++++++----
 1 files changed, 76 insertions(+), 8 deletions(-)

diff --git a/src/main/java/com/gitblit/GitBlit.java b/src/main/java/com/gitblit/GitBlit.java
index ef73978..bcfebb5 100644
--- a/src/main/java/com/gitblit/GitBlit.java
+++ b/src/main/java/com/gitblit/GitBlit.java
@@ -102,6 +102,7 @@
 import com.gitblit.models.GitClientApplication;
 import com.gitblit.models.Metric;
 import com.gitblit.models.ProjectModel;
+import com.gitblit.models.RefModel;
 import com.gitblit.models.RegistrantAccessPermission;
 import com.gitblit.models.RepositoryModel;
 import com.gitblit.models.RepositoryUrl;
@@ -114,6 +115,7 @@
 import com.gitblit.utils.ArrayUtils;
 import com.gitblit.utils.Base64;
 import com.gitblit.utils.ByteFormat;
+import com.gitblit.utils.CommitCache;
 import com.gitblit.utils.ContainerUtils;
 import com.gitblit.utils.DeepCopier;
 import com.gitblit.utils.FederationUtils;
@@ -509,7 +511,7 @@
 		if (user == null) {
 			user = UserModel.ANONYMOUS;
 		}
-		String username = UserModel.ANONYMOUS.equals(user) ? "" : user.username;
+		String username = encodeUsername(UserModel.ANONYMOUS.equals(user) ? "" : user.username);
 
 		List<RepositoryUrl> list = new ArrayList<RepositoryUrl>();
 		// http/https url
@@ -735,6 +737,7 @@
 			// can not authenticate empty username
 			return null;
 		}
+		String usernameDecoded = decodeUsername(username);
 		String pw = new String(password);
 		if (StringUtils.isEmpty(pw)) {
 			// can not authenticate empty password
@@ -743,7 +746,7 @@
 
 		// check to see if this is the federation user
 		if (canFederate()) {
-			if (username.equalsIgnoreCase(Constants.FEDERATION_USER)) {
+			if (usernameDecoded.equalsIgnoreCase(Constants.FEDERATION_USER)) {
 				List<String> tokens = getFederationTokens();
 				if (tokens.contains(pw)) {
 					// the federation user is an administrator
@@ -758,7 +761,7 @@
 		if (userService == null) {
 			return null;
 		}
-		return userService.authenticate(username, password);
+		return userService.authenticate(usernameDecoded, password);
 	}
 
 	/**
@@ -929,7 +932,10 @@
 		if (userService == null) {
 			return;
 		}
-		if (userService.supportsCookies()) {
+		GitBlitWebSession session = GitBlitWebSession.get();
+		boolean standardLogin = session.authenticationType.isStandard();
+
+		if (userService.supportsCookies() && standardLogin) {
 			Cookie userCookie;
 			if (user == null) {
 				// clear cookie for logout
@@ -964,6 +970,26 @@
 	}
 
 	/**
+	 * Encode the username for user in an url.
+	 * 
+	 * @param name
+	 * @return the encoded name
+	 */
+	protected String encodeUsername(String name) {
+		return name.replace("@", "%40").replace(" ", "%20").replace("\\", "%5C");	
+	}
+
+	/**
+	 * Decode a username from an encoded url.
+	 * 
+	 * @param name
+	 * @return the decoded name
+	 */
+	protected String decodeUsername(String name) {
+		return name.replace("%40", "@").replace("%20", " ").replace("%5C", "\\");
+	}
+	
+	/**
 	 * Returns the list of all users available to the login service.
 	 * 
 	 * @see IUserService.getAllUsernames()
@@ -996,7 +1022,8 @@
 		if (StringUtils.isEmpty(username)) {
 			return false;
 		}
-		return userService.deleteUser(username);
+		String usernameDecoded = decodeUsername(username);
+		return userService.deleteUser(usernameDecoded);
 	}
 
 	/**
@@ -1010,7 +1037,8 @@
 		if (StringUtils.isEmpty(username)) {
 			return null;
 		}
-		UserModel user = userService.getUserModel(username);		
+		String usernameDecoded = decodeUsername(username);
+		UserModel user = userService.getUserModel(usernameDecoded);		
 		return user;
 	}
 	
@@ -1447,7 +1475,10 @@
 		}
 		
 		// return sorted copy of cached list
-		List<String> list = new ArrayList<String>(repositoryListCache.keySet());		
+		List<String> list = new ArrayList<String>();
+		for (RepositoryModel model : repositoryListCache.values()) {
+			list.add(model.name);
+		}
 		StringUtils.sortRepositorynames(list);
 		return list;
 	}
@@ -3375,7 +3406,8 @@
 		configureJGit();
 		configureFanout();
 		configureGitDaemon();
-		
+		configureCommitCache();
+
 		ContainerUtils.CVE_2007_0450.test();
 	}
 	
@@ -3485,6 +3517,42 @@
 		}
 	}
 	
+	protected void configureCommitCache() {
+		int daysToCache = settings.getInteger(Keys.web.activityCacheDays, 14);
+		if (daysToCache <= 0) {
+			logger.info("commit cache disabled");
+		} else {
+			long start = System.nanoTime();
+			long repoCount = 0;
+			long commitCount = 0;
+			logger.info(MessageFormat.format("preparing {0} day commit cache. please wait...", daysToCache));
+			CommitCache.instance().setCacheDays(daysToCache);
+			Date cutoff = CommitCache.instance().getCutoffDate();
+			for (String repositoryName : getRepositoryList()) {
+				RepositoryModel model = getRepositoryModel(repositoryName);
+				if (model.hasCommits && model.lastChange.after(cutoff)) {
+					repoCount++;
+					Repository repository = getRepository(repositoryName);
+					for (RefModel ref : JGitUtils.getLocalBranches(repository, true, -1)) {
+						if (!ref.getDate().after(cutoff)) {
+							// branch not recently updated
+							continue;
+						}
+						List<?> commits = CommitCache.instance().getCommits(repositoryName, repository, ref.getName());
+						if (commits.size() > 0) {
+							logger.info(MessageFormat.format("  cached {0} commits for {1}:{2}",
+									commits.size(), repositoryName, ref.getName()));
+							commitCount += commits.size();
+						}
+					}
+					repository.close();
+				}
+			}
+			logger.info(MessageFormat.format("built {0} day commit cache of {1} commits across {2} repositories in {3} msecs",
+					daysToCache, commitCount, repoCount, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start)));
+		}
+	}
+	
 	protected final Logger getLogger() {
 		return logger;
 	}

--
Gitblit v1.9.1