You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GitblitReceivePackFactory.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.git;
  17. import javax.servlet.http.HttpServletRequest;
  18. import org.eclipse.jgit.lib.PersonIdent;
  19. import org.eclipse.jgit.lib.Repository;
  20. import org.eclipse.jgit.transport.ReceivePack;
  21. import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
  22. import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
  23. import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;
  26. import com.gitblit.GitBlit;
  27. import com.gitblit.Keys;
  28. import com.gitblit.models.RepositoryModel;
  29. import com.gitblit.models.UserModel;
  30. import com.gitblit.utils.HttpUtils;
  31. import com.gitblit.utils.StringUtils;
  32. /**
  33. * The receive pack factory creates the receive pack which processes pushes.
  34. *
  35. * @author James Moger
  36. *
  37. * @param <X> the connection type
  38. */
  39. public class GitblitReceivePackFactory<X> implements ReceivePackFactory<X> {
  40. protected final Logger logger = LoggerFactory.getLogger(GitblitReceivePackFactory.class);
  41. @Override
  42. public ReceivePack create(X req, Repository db)
  43. throws ServiceNotEnabledException, ServiceNotAuthorizedException {
  44. UserModel user = UserModel.ANONYMOUS;
  45. String repositoryName = "";
  46. String origin = "";
  47. String gitblitUrl = "";
  48. String repositoryUrl = "";
  49. int timeout = 0;
  50. if (req instanceof HttpServletRequest) {
  51. // http/https request may or may not be authenticated
  52. HttpServletRequest request = (HttpServletRequest) req;
  53. repositoryName = request.getAttribute("gitblitRepositoryName").toString();
  54. origin = request.getRemoteHost();
  55. gitblitUrl = HttpUtils.getGitblitURL(request);
  56. repositoryUrl = request.getRequestURI();
  57. // determine pushing user
  58. String username = request.getRemoteUser();
  59. if (!StringUtils.isEmpty(username)) {
  60. UserModel u = GitBlit.self().getUserModel(username);
  61. if (u != null) {
  62. user = u;
  63. }
  64. }
  65. } else if (req instanceof GitDaemonClient) {
  66. // git daemon request is always anonymous
  67. GitDaemonClient client = (GitDaemonClient) req;
  68. repositoryName = client.getRepositoryName();
  69. origin = client.getRemoteAddress().getHostAddress();
  70. // set timeout from Git daemon
  71. timeout = client.getDaemon().getTimeout();
  72. }
  73. boolean allowAnonymousPushes = GitBlit.getBoolean(Keys.git.allowAnonymousPushes, true);
  74. if (!allowAnonymousPushes && UserModel.ANONYMOUS.equals(user)) {
  75. // prohibit anonymous pushes
  76. throw new ServiceNotEnabledException();
  77. }
  78. final RepositoryModel repository = GitBlit.self().getRepositoryModel(repositoryName);
  79. final GitblitReceivePack rp = new GitblitReceivePack(db, repository, user);
  80. rp.setGitblitUrl(gitblitUrl);
  81. rp.setRepositoryUrl(repositoryUrl);
  82. rp.setRefLogIdent(new PersonIdent(user.username, user.username + "@" + origin));
  83. rp.setTimeout(timeout);
  84. return rp;
  85. }
  86. }