James Moger
2014-03-18 41a7e46579d12e36c583aa8c2418e49c3f4c29a4
commit | author | age
75bca8 1 /*
JM 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
18 import javax.servlet.http.HttpServletRequest;
19
20 import org.eclipse.jgit.lib.PersonIdent;
21 import org.eclipse.jgit.lib.Repository;
22 import org.eclipse.jgit.transport.ReceivePack;
23 import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
24 import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
25 import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
db4f6b 29 import com.gitblit.IStoredSettings;
629806 30 import com.gitblit.Keys;
3a9e76 31 import com.gitblit.manager.IGitblit;
75bca8 32 import com.gitblit.models.RepositoryModel;
JM 33 import com.gitblit.models.UserModel;
31f477 34 import com.gitblit.transport.git.GitDaemonClient;
a8dd37 35 import com.gitblit.transport.ssh.SshDaemonClient;
75bca8 36 import com.gitblit.utils.HttpUtils;
234933 37 import com.gitblit.utils.StringUtils;
75bca8 38
JM 39 /**
234933 40  * The receive pack factory creates the receive pack which processes pushes.
JM 41  *
75bca8 42  * @author James Moger
JM 43  *
44  * @param <X> the connection type
45  */
46 public class GitblitReceivePackFactory<X> implements ReceivePackFactory<X> {
47
48     protected final Logger logger = LoggerFactory.getLogger(GitblitReceivePackFactory.class);
234933 49
cacf8b 50     private final IStoredSettings settings;
JM 51
3a9e76 52     private final IGitblit gitblit;
cacf8b 53
3a9e76 54     public GitblitReceivePackFactory(IGitblit gitblit) {
cacf8b 55         super();
325396 56         this.settings = gitblit.getSettings();
JM 57         this.gitblit = gitblit;
cacf8b 58     }
JM 59
75bca8 60     @Override
JM 61     public ReceivePack create(X req, Repository db)
62             throws ServiceNotEnabledException, ServiceNotAuthorizedException {
db4f6b 63
75bca8 64         UserModel user = UserModel.ANONYMOUS;
40aa84 65         String repositoryName = "";
75bca8 66         String origin = "";
JM 67         String gitblitUrl = "";
68         int timeout = 0;
234933 69
75bca8 70         if (req instanceof HttpServletRequest) {
234933 71             // http/https request may or may not be authenticated
4ccdfe 72             HttpServletRequest client = (HttpServletRequest) req;
JM 73             repositoryName = client.getAttribute("gitblitRepositoryName").toString();
74             origin = client.getRemoteHost();
75             gitblitUrl = HttpUtils.getGitblitURL(client);
75bca8 76
JM 77             // determine pushing user
4ccdfe 78             String username = client.getRemoteUser();
234933 79             if (!StringUtils.isEmpty(username)) {
325396 80                 UserModel u = gitblit.getUserModel(username);
234933 81                 if (u != null) {
JM 82                     user = u;
75bca8 83                 }
JM 84             }
40aa84 85         } else if (req instanceof GitDaemonClient) {
234933 86             // git daemon request is always anonymous
40aa84 87             GitDaemonClient client = (GitDaemonClient) req;
JM 88             repositoryName = client.getRepositoryName();
75bca8 89             origin = client.getRemoteAddress().getHostAddress();
234933 90
75bca8 91             // set timeout from Git daemon
JM 92             timeout = client.getDaemon().getTimeout();
a8dd37 93         } else if (req instanceof SshDaemonClient) {
e3b636 94             // SSH request is always authenticated
a8dd37 95             SshDaemonClient client = (SshDaemonClient) req;
4ccdfe 96             repositoryName = client.getRepositoryName();
JM 97             origin = client.getRemoteAddress().toString();
a8dd37 98             user = client.getUser();
75bca8 99         }
JM 100
db4f6b 101         boolean allowAnonymousPushes = settings.getBoolean(Keys.git.allowAnonymousPushes, false);
234933 102         if (!allowAnonymousPushes && UserModel.ANONYMOUS.equals(user)) {
JM 103             // prohibit anonymous pushes
104             throw new ServiceNotEnabledException();
105         }
106
14cbbe 107         String url = settings.getString(Keys.web.canonicalUrl, null);
JM 108         if (StringUtils.isEmpty(url)) {
109             url = gitblitUrl;
110         }
5e3521 111
325396 112         final RepositoryModel repository = gitblit.getRepositoryModel(repositoryName);
234933 113
5e3521 114         // Determine which receive pack to use for pushes
JM 115         final GitblitReceivePack rp;
116         if (gitblit.getTicketService().isAcceptingNewPatchsets(repository)) {
117             rp = new PatchsetReceivePack(gitblit, db, repository, user);
118         } else {
119             rp = new GitblitReceivePack(gitblit, db, repository, user);
120         }
121
14cbbe 122         rp.setGitblitUrl(url);
75bca8 123         rp.setRefLogIdent(new PersonIdent(user.username, user.username + "@" + origin));
JM 124         rp.setTimeout(timeout);
125
126         return rp;
127     }
234933 128 }