WLRepositoryResolver.java 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package uk.ac.ic.wlgitbridge.git;
  2. import org.eclipse.jgit.api.AddCommand;
  3. import org.eclipse.jgit.api.CommitCommand;
  4. import org.eclipse.jgit.api.Git;
  5. import org.eclipse.jgit.api.errors.GitAPIException;
  6. import org.eclipse.jgit.errors.RepositoryNotFoundException;
  7. import org.eclipse.jgit.lib.Repository;
  8. import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
  9. import org.eclipse.jgit.transport.ServiceMayNotContinueException;
  10. import org.eclipse.jgit.transport.resolver.RepositoryResolver;
  11. import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
  12. import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
  13. import uk.ac.ic.wlgitbridge.git.exception.InvalidRootDirectoryPathException;
  14. import javax.servlet.http.HttpServletRequest;
  15. import java.io.File;
  16. import java.io.IOException;
  17. /**
  18. * Created by Winston on 02/11/14.
  19. */
  20. public class WLRepositoryResolver implements RepositoryResolver<HttpServletRequest> {
  21. private File rootGitDirectory;
  22. public WLRepositoryResolver(String rootGitDirectoryPath) throws InvalidRootDirectoryPathException {
  23. initRootGitDirectory(rootGitDirectoryPath);
  24. }
  25. @Override
  26. public Repository open(HttpServletRequest httpServletRequest, String name) throws RepositoryNotFoundException, ServiceNotAuthorizedException, ServiceNotEnabledException, ServiceMayNotContinueException {
  27. File repositoryRoot = new File(rootGitDirectory.getAbsolutePath(), name);
  28. Repository repository = null;
  29. try {
  30. repository = new FileRepositoryBuilder().setWorkTree(repositoryRoot).build();
  31. } catch (IOException e) {
  32. throw new RepositoryNotFoundException(name);
  33. }
  34. if (!repository.getObjectDatabase().exists()) {
  35. throw new RepositoryNotFoundException(name);
  36. }
  37. return repository;
  38. }
  39. private void initRootGitDirectory(String rootGitDirectoryPath) throws InvalidRootDirectoryPathException {
  40. rootGitDirectory = new File(rootGitDirectoryPath);
  41. /* throws SecurityException */
  42. rootGitDirectory.mkdirs();
  43. rootGitDirectory.getAbsolutePath();
  44. if (!rootGitDirectory.isDirectory()) {
  45. throw new InvalidRootDirectoryPathException();
  46. }
  47. }
  48. }