]> source.dussan.org Git - jgit.git/blob
d362a96bec50f6075fc5e9a4d9470fcadb3cd9d8
[jgit.git] /
1 /*
2  * Copyright (C) 2009-2010, Google Inc. and others
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Distribution License v. 1.0 which is available at
6  * https://www.eclipse.org/org/documents/edl-v10.php.
7  *
8  * SPDX-License-Identifier: BSD-3-Clause
9  */
10
11 package org.eclipse.jgit.http.server.resolver;
12
13 import java.util.Arrays;
14
15 import javax.servlet.http.HttpServletRequest;
16
17 import org.eclipse.jgit.lib.Config;
18 import org.eclipse.jgit.lib.Repository;
19 import org.eclipse.jgit.transport.UploadPack;
20 import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
21 import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
22 import org.eclipse.jgit.transport.resolver.UploadPackFactory;
23
24 /**
25  * Create and configure {@link org.eclipse.jgit.transport.UploadPack} service
26  * instance.
27  * <p>
28  * Reading by upload-pack is permitted unless {@code http.uploadpack} is
29  * explicitly set to false.
30  */
31 public class DefaultUploadPackFactory implements
32                 UploadPackFactory<HttpServletRequest> {
33         private static class ServiceConfig {
34                 final boolean enabled;
35
36                 ServiceConfig(Config cfg) {
37                         enabled = cfg.getBoolean("http", "uploadpack", true);
38                 }
39         }
40
41         /** {@inheritDoc} */
42         @Override
43         public UploadPack create(HttpServletRequest req, Repository db)
44                         throws ServiceNotEnabledException, ServiceNotAuthorizedException {
45                 if (db.getConfig().get(ServiceConfig::new).enabled) {
46                         UploadPack up = new UploadPack(db);
47                         String header = req.getHeader("Git-Protocol"); //$NON-NLS-1$
48                         if (header != null) {
49                                 String[] params = header.split(":"); //$NON-NLS-1$
50                                 up.setExtraParameters(Arrays.asList(params));
51                         }
52                         return up;
53                 }
54                 throw new ServiceNotEnabledException();
55         }
56 }