From d8aa8d69ba21d7846a72d51810a9f89f032a5ecb Mon Sep 17 00:00:00 2001 From: James Moger <james.moger@gitblit.com> Date: Wed, 15 May 2013 09:57:58 -0400 Subject: [PATCH] Updated Tower icon per fournova's request --- src/main/java/com/gitblit/GitBlit.java | 223 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 184 insertions(+), 39 deletions(-) diff --git a/src/main/java/com/gitblit/GitBlit.java b/src/main/java/com/gitblit/GitBlit.java index 42a1434..93293d8 100644 --- a/src/main/java/com/gitblit/GitBlit.java +++ b/src/main/java/com/gitblit/GitBlit.java @@ -18,9 +18,13 @@ import java.io.BufferedReader; import java.io.File; import java.io.FileFilter; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.OutputStream; import java.lang.reflect.Field; import java.lang.reflect.Type; import java.net.URI; @@ -91,15 +95,16 @@ import com.gitblit.fanout.FanoutService; import com.gitblit.fanout.FanoutSocketService; import com.gitblit.git.GitDaemon; -import com.gitblit.models.GitClientApplication; import com.gitblit.models.FederationModel; import com.gitblit.models.FederationProposal; import com.gitblit.models.FederationSet; import com.gitblit.models.ForkModel; +import com.gitblit.models.GitClientApplication; import com.gitblit.models.Metric; import com.gitblit.models.ProjectModel; import com.gitblit.models.RegistrantAccessPermission; import com.gitblit.models.RepositoryModel; +import com.gitblit.models.RepositoryUrl; import com.gitblit.models.SearchResult; import com.gitblit.models.ServerSettings; import com.gitblit.models.ServerStatus; @@ -155,8 +160,7 @@ private final List<FederationModel> federationRegistrations = Collections .synchronizedList(new ArrayList<FederationModel>()); - private final List<GitClientApplication> clientApplications = Collections - .synchronizedList(new ArrayList<GitClientApplication>()); + private final ObjectCache<Collection<GitClientApplication>> clientApplications = new ObjectCache<Collection<GitClientApplication>>(); private final Map<String, FederationModel> federationPullResults = new ConcurrentHashMap<String, FederationModel>(); @@ -201,7 +205,7 @@ private FanoutService fanoutService; private GitDaemon gitDaemon; - + public GitBlit() { if (gitblit == null) { // set the static singleton reference @@ -460,44 +464,150 @@ serverStatus.heapFree = Runtime.getRuntime().freeMemory(); return serverStatus; } - + /** - * Returns the list of non-Gitblit clone urls. This allows Gitblit to - * advertise alternative urls for Git client repository access. + * Returns a list of repository URLs and the user access permission. * - * @param repositoryName - * @param userName - * @return list of non-gitblit clone urls + * @param request + * @param user + * @param repository + * @return a list of repository urls */ - public List<String> getOtherCloneUrls(String repositoryName, String username) { - List<String> cloneUrls = new ArrayList<String>(); - for (String url : settings.getStrings(Keys.web.otherUrls)) { - cloneUrls.add(MessageFormat.format(url, repositoryName, username)); + public List<RepositoryUrl> getRepositoryUrls(HttpServletRequest request, UserModel user, RepositoryModel repository) { + if (user == null) { + user = UserModel.ANONYMOUS; } - return cloneUrls; + String username = UserModel.ANONYMOUS.equals(user) ? "" : user.username; + + List<RepositoryUrl> list = new ArrayList<RepositoryUrl>(); + // http/https url + if (settings.getBoolean(Keys.git.enableGitServlet, true)) { + AccessPermission permission = user.getRepositoryPermission(repository).permission; + if (permission.exceeds(AccessPermission.NONE)) { + list.add(new RepositoryUrl(getRepositoryUrl(request, username, repository), permission)); + } + } + + // git daemon url + String gitDaemonUrl = getGitDaemonUrl(request, user, repository); + if (!StringUtils.isEmpty(gitDaemonUrl)) { + AccessPermission permission = getGitDaemonAccessPermission(user, repository); + if (permission.exceeds(AccessPermission.NONE)) { + list.add(new RepositoryUrl(gitDaemonUrl, permission)); + } + } + + // add all other urls + // {0} = repository + // {1} = username + for (String url : settings.getStrings(Keys.web.otherUrls)) { + if (url.contains("{1}")) { + // external url requires username, only add url IF we have one + if(!StringUtils.isEmpty(username)) { + list.add(new RepositoryUrl(MessageFormat.format(url, repository.name, username), null)); + } + } else { + // external url does not require username + list.add(new RepositoryUrl(MessageFormat.format(url, repository.name), null)); + } + } + return list; } + protected String getRepositoryUrl(HttpServletRequest request, String username, RepositoryModel repository) { + StringBuilder sb = new StringBuilder(); + sb.append(HttpUtils.getGitblitURL(request)); + sb.append(Constants.GIT_PATH); + sb.append(repository.name); + + // inject username into repository url if authentication is required + if (repository.accessRestriction.exceeds(AccessRestrictionType.NONE) + && !StringUtils.isEmpty(username)) { + sb.insert(sb.indexOf("://") + 3, username + "@"); + } + return sb.toString(); + } + + protected String getGitDaemonUrl(HttpServletRequest request, UserModel user, RepositoryModel repository) { + if (gitDaemon != null) { + String bindInterface = settings.getString(Keys.git.daemonBindInterface, "localhost"); + if (bindInterface.equals("localhost") + && (!request.getServerName().equals("localhost") && !request.getServerName().equals("127.0.0.1"))) { + // git daemon is bound to localhost and the request is from elsewhere + return null; + } + if (user.canClone(repository)) { + String servername = request.getServerName(); + String url = gitDaemon.formatUrl(servername, repository.name); + return url; + } + } + return null; + } + + protected AccessPermission getGitDaemonAccessPermission(UserModel user, RepositoryModel repository) { + if (gitDaemon != null && user.canClone(repository)) { + AccessPermission gitDaemonPermission = user.getRepositoryPermission(repository).permission; + if (gitDaemonPermission.atLeast(AccessPermission.CLONE)) { + if (repository.accessRestriction.atLeast(AccessRestrictionType.CLONE)) { + // can not authenticate clone via anonymous git protocol + gitDaemonPermission = AccessPermission.NONE; + } else if (repository.accessRestriction.atLeast(AccessRestrictionType.PUSH)) { + // can not authenticate push via anonymous git protocol + gitDaemonPermission = AccessPermission.CLONE; + } else { + // normal user permission + } + } + return gitDaemonPermission; + } + return AccessPermission.NONE; + } + /** * Returns the list of custom client applications to be used for the * repository url panel; * - * @return a list of client applications + * @return a collection of client applications */ - public List<GitClientApplication> getClientApplications() { - if (clientApplications.isEmpty()) { + public Collection<GitClientApplication> getClientApplications() { + // prefer user definitions, if they exist + File userDefs = new File(baseFolder, "clientapps.json"); + if (userDefs.exists()) { + Date lastModified = new Date(userDefs.lastModified()); + if (clientApplications.hasCurrent("user", lastModified)) { + return clientApplications.getObject("user"); + } else { + // (re)load user definitions + try { + InputStream is = new FileInputStream(userDefs); + Collection<GitClientApplication> clients = readClientApplications(is); + is.close(); + if (clients != null) { + clientApplications.updateObject("user", lastModified, clients); + return clients; + } + } catch (IOException e) { + logger.error("Failed to deserialize " + userDefs.getAbsolutePath(), e); + } + } + } + + // no user definitions, use system definitions + if (!clientApplications.hasCurrent("system", new Date(0))) { try { InputStream is = getClass().getResourceAsStream("/clientapps.json"); Collection<GitClientApplication> clients = readClientApplications(is); is.close(); if (clients != null) { - clientApplications.clear(); - clientApplications.addAll(clients); + clientApplications.updateObject("system", new Date(0), clients); } } catch (IOException e) { logger.error("Failed to deserialize clientapps.json resource!", e); } } - return clientApplications; + + return clientApplications.getObject("system"); } private Collection<GitClientApplication> readClientApplications(InputStream is) { @@ -3283,8 +3393,8 @@ } protected void configureGitDaemon() { - String bindInterface = settings.getString(Keys.git.daemonBindInterface, "localhost"); int port = settings.getInteger(Keys.git.daemonPort, 0); + String bindInterface = settings.getString(Keys.git.daemonBindInterface, "localhost"); if (port > 0) { try { gitDaemon = new GitDaemon(bindInterface, port, getRepositoriesFolder()); @@ -3328,7 +3438,8 @@ // Gitblit is running in a servlet container ServletContext context = contextEvent.getServletContext(); WebXmlSettings webxmlSettings = new WebXmlSettings(context); - File contextFolder = new File(context.getRealPath("/")); + String contextRealPath = context.getRealPath("/"); + File contextFolder = (contextRealPath != null) ? new File(contextRealPath) : null; String openShift = System.getenv("OPENSHIFT_DATA_DIR"); if (!StringUtils.isEmpty(openShift)) { @@ -3360,27 +3471,29 @@ configureContext(webxmlSettings, base, true); } else { // Gitblit is running in a standard servlet container - logger.info("WAR contextFolder is " + contextFolder.getAbsolutePath()); + logger.info("WAR contextFolder is " + ((contextFolder != null) ? contextFolder.getAbsolutePath() : "<empty>")); String path = webxmlSettings.getString(Constants.baseFolder, Constants.contextFolder$ + "/WEB-INF/data"); - File base = com.gitblit.utils.FileUtils.resolveParameter(Constants.contextFolder$, contextFolder, path); - base.mkdirs(); - // try to copy the data folder contents to the baseFolder - File localSettings = new File(base, "gitblit.properties"); - if (!localSettings.exists()) { - File contextData = new File(contextFolder, "/WEB-INF/data"); - if (!base.equals(contextData)) { - try { - com.gitblit.utils.FileUtils.copy(base, contextData.listFiles()); - } catch (IOException e) { - logger.error(MessageFormat.format( - "Failed to copy included data from {0} to {1}", - contextData, base)); - } - } + if (path.contains(Constants.contextFolder$) && contextFolder == null) { + // warn about null contextFolder (issue-199) + logger.error(""); + logger.error(MessageFormat.format("\"{0}\" depends on \"{1}\" but \"{2}\" is returning NULL for \"{1}\"!", + Constants.baseFolder, Constants.contextFolder$, context.getServerInfo())); + logger.error(MessageFormat.format("Please specify a non-parameterized path for <context-param> {0} in web.xml!!", Constants.baseFolder)); + logger.error(MessageFormat.format("OR configure your servlet container to specify a \"{0}\" parameter in the context configuration!!", Constants.baseFolder)); + logger.error(""); } + File base = com.gitblit.utils.FileUtils.resolveParameter(Constants.contextFolder$, contextFolder, path); + base.mkdirs(); + + // try to extract the data folder resource to the baseFolder + File localSettings = new File(base, "gitblit.properties"); + if (!localSettings.exists()) { + extractResources(context, "/WEB-INF/data/", base); + } + // delegate all config to baseFolder/gitblit.properties file FileSettings settings = new FileSettings(localSettings.getAbsolutePath()); configureContext(settings, base, true); @@ -3390,6 +3503,38 @@ settingsModel = loadSettingModels(); serverStatus.servletContainer = servletContext.getServerInfo(); } + + protected void extractResources(ServletContext context, String path, File toDir) { + for (String resource : context.getResourcePaths(path)) { + // extract the resource to the directory if it does not exist + File f = new File(toDir, resource.substring(path.length())); + if (!f.exists()) { + try { + if (resource.charAt(resource.length() - 1) == '/') { + // directory + f.mkdirs(); + extractResources(context, resource, f); + } else { + // file + f.getParentFile().mkdirs(); + InputStream is = context.getResourceAsStream(resource); + OutputStream os = new FileOutputStream(f); + byte [] buffer = new byte[4096]; + int len = 0; + while ((len = is.read(buffer)) > -1) { + os.write(buffer, 0, len); + } + is.close(); + os.close(); + } + } catch (FileNotFoundException e) { + logger.error("Failed to find resource \"" + resource + "\"", e); + } catch (IOException e) { + logger.error("Failed to copy resource \"" + resource + "\" to " + f, e); + } + } + } + } /** * Gitblit is being shutdown either because the servlet container is -- Gitblit v1.9.1