| | |
| | | import java.text.MessageFormat;
|
| | | import java.util.ArrayList;
|
| | | import java.util.Collections;
|
| | | import java.util.HashMap;
|
| | | import java.util.List;
|
| | | import java.util.Map;
|
| | | import java.util.Map.Entry;
|
| | |
|
| | | import javax.servlet.ServletContextEvent;
|
| | | import javax.servlet.ServletContextListener;
|
| | | import javax.servlet.http.Cookie;
|
| | |
|
| | | import org.apache.wicket.protocol.http.WebResponse;
|
| | | import org.eclipse.jgit.errors.RepositoryNotFoundException;
|
| | | import org.eclipse.jgit.lib.Repository;
|
| | | import org.eclipse.jgit.lib.StoredConfig;
|
| | |
| | |
|
| | | public class GitBlit implements ServletContextListener {
|
| | |
|
| | | private static final GitBlit GITBLIT;
|
| | | private static GitBlit gitblit;
|
| | |
|
| | | private final Logger logger = LoggerFactory.getLogger(GitBlit.class);
|
| | |
|
| | |
| | |
|
| | | private File repositoriesFolder;
|
| | |
|
| | | private boolean exportAll;
|
| | | private boolean exportAll = true;
|
| | |
|
| | | private ILoginService loginService;
|
| | | private IUserService userService;
|
| | |
|
| | | private IStoredSettings storedSettings;
|
| | |
|
| | | static {
|
| | | GITBLIT = new GitBlit();
|
| | | }
|
| | |
|
| | | private GitBlit() {
|
| | | public GitBlit() {
|
| | | if (gitblit == null) {
|
| | | // Singleton reference when running in standard servlet container
|
| | | gitblit = this;
|
| | | }
|
| | | }
|
| | |
|
| | | public static GitBlit self() {
|
| | | return GITBLIT;
|
| | | if (gitblit == null) {
|
| | | gitblit = new GitBlit();
|
| | | }
|
| | | return gitblit;
|
| | | }
|
| | |
|
| | | public static boolean getBoolean(String key, boolean defaultValue) {
|
| | | return GITBLIT.storedSettings.getBoolean(key, defaultValue);
|
| | | return self().storedSettings.getBoolean(key, defaultValue);
|
| | | }
|
| | |
|
| | | public static int getInteger(String key, int defaultValue) {
|
| | | return GITBLIT.storedSettings.getInteger(key, defaultValue);
|
| | | return self().storedSettings.getInteger(key, defaultValue);
|
| | | }
|
| | |
|
| | | public static String getString(String key, String defaultValue) {
|
| | | return GITBLIT.storedSettings.getString(key, defaultValue);
|
| | | return self().storedSettings.getString(key, defaultValue);
|
| | | }
|
| | |
|
| | | public static List<String> getStrings(String key) {
|
| | | return GITBLIT.storedSettings.getStrings(key);
|
| | | return self().storedSettings.getStrings(key);
|
| | | }
|
| | |
|
| | | public static List<String> getAllKeys(String startingWith) {
|
| | | return GITBLIT.storedSettings.getAllKeys(startingWith);
|
| | | return self().storedSettings.getAllKeys(startingWith);
|
| | | }
|
| | |
|
| | | public boolean isDebugMode() {
|
| | | return storedSettings.getBoolean(Keys.web.debugMode, false);
|
| | | public static boolean isDebugMode() {
|
| | | return self().storedSettings.getBoolean(Keys.web.debugMode, false);
|
| | | }
|
| | |
|
| | | public List<String> getOtherCloneUrls(String repositoryName) {
|
| | |
| | | return cloneUrls;
|
| | | }
|
| | |
|
| | | public void setLoginService(ILoginService loginService) {
|
| | | this.loginService = loginService;
|
| | | public void setUserService(IUserService userService) {
|
| | | logger.info("Setting up user service " + userService.toString());
|
| | | this.userService = userService;
|
| | | }
|
| | |
|
| | | public UserModel authenticate(String username, char[] password) {
|
| | | if (loginService == null) {
|
| | | if (userService == null) {
|
| | | return null;
|
| | | }
|
| | | return loginService.authenticate(username, password);
|
| | | return userService.authenticate(username, password);
|
| | | }
|
| | |
|
| | | public UserModel authenticate(Cookie[] cookies) {
|
| | | if (userService == null) {
|
| | | return null;
|
| | | }
|
| | | if (userService.supportsCookies()) {
|
| | | if (cookies != null && cookies.length > 0) {
|
| | | for (Cookie cookie : cookies) {
|
| | | if (cookie.getName().equals(Constants.NAME)) {
|
| | | String value = cookie.getValue();
|
| | | return userService.authenticate(value.toCharArray());
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | public void setCookie(WebResponse response, UserModel user) {
|
| | | if (userService == null) {
|
| | | return;
|
| | | }
|
| | | if (userService.supportsCookies()) {
|
| | | Cookie userCookie;
|
| | | if (user == null) {
|
| | | // clear cookie for logout
|
| | | userCookie = new Cookie(Constants.NAME, "");
|
| | | } else {
|
| | | // set cookie for login
|
| | | char[] cookie = userService.getCookie(user);
|
| | | userCookie = new Cookie(Constants.NAME, new String(cookie));
|
| | | userCookie.setMaxAge(Integer.MAX_VALUE);
|
| | | }
|
| | | userCookie.setPath("/");
|
| | | response.addCookie(userCookie);
|
| | | }
|
| | | }
|
| | |
|
| | | public List<String> getAllUsernames() {
|
| | | List<String> names = new ArrayList<String>(loginService.getAllUsernames());
|
| | | List<String> names = new ArrayList<String>(userService.getAllUsernames());
|
| | | Collections.sort(names);
|
| | | return names;
|
| | | }
|
| | |
|
| | | public boolean deleteUser(String username) {
|
| | | return loginService.deleteUser(username);
|
| | | return userService.deleteUser(username);
|
| | | }
|
| | |
|
| | | public UserModel getUserModel(String username) {
|
| | | UserModel user = loginService.getUserModel(username);
|
| | | UserModel user = userService.getUserModel(username);
|
| | | return user;
|
| | | }
|
| | |
|
| | | public List<String> getRepositoryUsers(RepositoryModel repository) {
|
| | | return loginService.getUsernamesForRole(repository.name);
|
| | | return userService.getUsernamesForRepository(repository.name);
|
| | | }
|
| | |
|
| | | public boolean setRepositoryUsers(RepositoryModel repository, List<String> repositoryUsers) {
|
| | | return loginService.setUsernamesForRole(repository.name, repositoryUsers);
|
| | | return userService.setUsernamesForRepository(repository.name, repositoryUsers);
|
| | | }
|
| | |
|
| | | public void editUserModel(String username, UserModel user, boolean isCreate)
|
| | | throws GitBlitException {
|
| | | if (!loginService.updateUserModel(username, user)) {
|
| | | if (!userService.updateUserModel(username, user)) {
|
| | | throw new GitBlitException(isCreate ? "Failed to add user!" : "Failed to update user!");
|
| | | }
|
| | | }
|
| | |
|
| | | public List<String> getRepositoryList() {
|
| | | return JGitUtils.getRepositoryList(repositoriesFolder, exportAll,
|
| | | storedSettings.getBoolean(Keys.git.nestedRepositories, true));
|
| | | storedSettings.getBoolean(Keys.git.searchRepositoriesSubfolders, true));
|
| | | }
|
| | |
|
| | | public Repository getRepository(String repositoryName) {
|
| | |
| | |
|
| | | public RepositoryModel getRepositoryModel(UserModel user, String repositoryName) {
|
| | | RepositoryModel model = getRepositoryModel(repositoryName);
|
| | | if (model == null) {
|
| | | return null;
|
| | | }
|
| | | if (model.accessRestriction.atLeast(AccessRestrictionType.VIEW)) {
|
| | | if (user != null && user.canAccessRepository(model.name)) {
|
| | | return model;
|
| | |
| | | Repository r = null;
|
| | | if (isCreate) {
|
| | | // ensure created repository name ends with .git
|
| | | if (!repository.name.endsWith(org.eclipse.jgit.lib.Constants.DOT_GIT_EXT)) {
|
| | | if (!repository.name.toLowerCase().endsWith(org.eclipse.jgit.lib.Constants.DOT_GIT_EXT)) {
|
| | | repository.name += org.eclipse.jgit.lib.Constants.DOT_GIT_EXT;
|
| | | }
|
| | | if (new File(repositoriesFolder, repository.name).exists()) {
|
| | |
| | | }
|
| | | // create repository
|
| | | logger.info("create repository " + repository.name);
|
| | | r = JGitUtils.createRepository(repositoriesFolder, repository.name, true);
|
| | | r = JGitUtils.createRepository(repositoriesFolder, repository.name);
|
| | | } else {
|
| | | // rename repository
|
| | | if (!repositoryName.equalsIgnoreCase(repository.name)) {
|
| | |
| | | repository.name));
|
| | | }
|
| | | // rename the roles
|
| | | if (!loginService.renameRole(repositoryName, repository.name)) {
|
| | | if (!userService.renameRepositoryRole(repositoryName, repository.name)) {
|
| | | throw new GitBlitException(MessageFormat.format(
|
| | | "Failed to rename repository permissions ''{0}'' to ''{1}''.",
|
| | | repositoryName, repository.name));
|
| | |
| | | File folder = new File(repositoriesFolder, repositoryName);
|
| | | if (folder.exists() && folder.isDirectory()) {
|
| | | FileUtils.delete(folder, FileUtils.RECURSIVE);
|
| | | if (loginService.deleteRole(repositoryName)) {
|
| | | if (userService.deleteRepositoryRole(repositoryName)) {
|
| | | return true;
|
| | | }
|
| | | }
|
| | |
| | | return false;
|
| | | }
|
| | |
|
| | | public boolean renameRepository(RepositoryModel model, String newName) {
|
| | | File folder = new File(repositoriesFolder, model.name);
|
| | | if (folder.exists() && folder.isDirectory()) {
|
| | | File newFolder = new File(repositoriesFolder, newName);
|
| | | if (folder.renameTo(newFolder)) {
|
| | | return loginService.renameRole(model.name, newName);
|
| | | public String processCommitMessage(String repositoryName, String text) {
|
| | | String html = StringUtils.breakLinesForHtml(text);
|
| | | Map<String, String> map = new HashMap<String, String>();
|
| | | // global regex keys
|
| | | if (storedSettings.getBoolean(Keys.regex.global, false)) {
|
| | | for (String key : storedSettings.getAllKeys(Keys.regex.global)) {
|
| | | if (!key.equals(Keys.regex.global)) {
|
| | | String subKey = key.substring(key.lastIndexOf('.') + 1);
|
| | | map.put(subKey, storedSettings.getString(key, ""));
|
| | | }
|
| | | }
|
| | | }
|
| | | return false;
|
| | |
|
| | | // repository-specific regex keys
|
| | | List<String> keys = storedSettings.getAllKeys(Keys.regex._ROOT + "."
|
| | | + repositoryName.toLowerCase());
|
| | | for (String key : keys) {
|
| | | String subKey = key.substring(key.lastIndexOf('.') + 1);
|
| | | map.put(subKey, storedSettings.getString(key, ""));
|
| | | }
|
| | |
|
| | | for (Entry<String, String> entry : map.entrySet()) {
|
| | | String definition = entry.getValue().trim();
|
| | | String[] chunks = definition.split("!!!");
|
| | | if (chunks.length == 2) {
|
| | | html = html.replaceAll(chunks[0], chunks[1]);
|
| | | } else {
|
| | | logger.warn(entry.getKey()
|
| | | + " improperly formatted. Use !!! to separate match from replacement: "
|
| | | + definition);
|
| | | }
|
| | | }
|
| | | return html;
|
| | | }
|
| | |
|
| | | public void configureContext(IStoredSettings settings) {
|
| | | logger.info("Reading configuration from " + settings.toString());
|
| | | this.storedSettings = settings;
|
| | | repositoriesFolder = new File(settings.getString(Keys.git.repositoriesFolder, "repos"));
|
| | | exportAll = settings.getBoolean(Keys.git.exportAll, true);
|
| | | repositoriesFolder = new File(settings.getString(Keys.git.repositoriesFolder, "git"));
|
| | | logger.info("Git repositories folder " + repositoriesFolder.getAbsolutePath());
|
| | | repositoryResolver = new FileResolver<Void>(repositoriesFolder, exportAll);
|
| | | String realm = settings.getString(Keys.realm.userService, "users.properties");
|
| | | IUserService loginService = null;
|
| | | try {
|
| | | // Check to see if this "file" is a login service class
|
| | | Class<?> realmClass = Class.forName(realm);
|
| | | if (IUserService.class.isAssignableFrom(realmClass)) {
|
| | | loginService = (IUserService) realmClass.newInstance();
|
| | | }
|
| | | } catch (Throwable t) {
|
| | | // Not a login service class OR other issue
|
| | | // Use default file login service
|
| | | File realmFile = new File(realm);
|
| | | if (!realmFile.exists()) {
|
| | | try {
|
| | | realmFile.createNewFile();
|
| | | } catch (IOException x) {
|
| | | logger.error(
|
| | | MessageFormat.format("COULD NOT CREATE REALM FILE {0}!", realmFile), x);
|
| | | }
|
| | | }
|
| | | loginService = new FileUserService(realmFile);
|
| | | }
|
| | | setUserService(loginService);
|
| | | }
|
| | |
|
| | | @Override
|
| | | public void contextInitialized(ServletContextEvent contextEvent) {
|
| | | if (storedSettings == null) {
|
| | | // for running gitblit as a traditional webapp in a servlet
|
| | | // container
|
| | | WebXmlSettings webxmlSettings = new WebXmlSettings(contextEvent.getServletContext());
|
| | | configureContext(webxmlSettings);
|
| | | }
|
| | |
| | |
|
| | | @Override
|
| | | public void contextDestroyed(ServletContextEvent contextEvent) {
|
| | | logger.info("GitBlit context destroyed by servlet container.");
|
| | | logger.info("Gitblit context destroyed by servlet container.");
|
| | | }
|
| | | }
|