View Javadoc
1   /*
2    * Copyright (C) 2008-2009, Google Inc.
3    * Copyright (C) 2009, Robin Rosenberg <robin.rosenberg@dewire.com> and others
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
10   */
11  
12  package org.eclipse.jgit.transport;
13  
14  import java.io.IOException;
15  import java.util.Collection;
16  
17  import org.eclipse.jgit.annotations.Nullable;
18  import org.eclipse.jgit.lib.Config;
19  import org.eclipse.jgit.lib.Config.SectionParser;
20  import org.eclipse.jgit.lib.Repository;
21  import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
22  import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
23  
24  /**
25   * A service exposed by {@link org.eclipse.jgit.transport.Daemon} over anonymous
26   * <code>git://</code>.
27   */
28  public abstract class DaemonService {
29  	private final String command;
30  
31  	private final SectionParser<ServiceConfig> configKey;
32  
33  	private boolean enabled;
34  
35  	private boolean overridable;
36  
37  	DaemonService(String cmdName, String cfgName) {
38  		command = cmdName.startsWith("git-") ? cmdName : "git-" + cmdName; //$NON-NLS-1$ //$NON-NLS-2$
39  		configKey = cfg -> new ServiceConfig(DaemonService.this, cfg, cfgName);
40  		overridable = true;
41  	}
42  
43  	private static class ServiceConfig {
44  		final boolean enabled;
45  
46  		ServiceConfig(final DaemonService service, final Config cfg,
47  				final String name) {
48  			enabled = cfg.getBoolean("daemon", name, service.isEnabled()); //$NON-NLS-1$
49  		}
50  	}
51  
52  	/**
53  	 * Whether this service is enabled for invocation.
54  	 *
55  	 * @return whether this service is enabled for invocation.
56  	 */
57  	public boolean isEnabled() {
58  		return enabled;
59  	}
60  
61  	/**
62  	 * Set if it is allowed to use this service
63  	 *
64  	 * @param on
65  	 *            {@code true} to allow this service to be used; {@code false}
66  	 *            to deny it.
67  	 */
68  	public void setEnabled(boolean on) {
69  		enabled = on;
70  	}
71  
72  	/** @return can this service be configured in the repository config file? */
73  	/**
74  	 * Whether this service can be configured in the repository config file
75  	 *
76  	 * @return whether this service can be configured in the repository config
77  	 *         file
78  	 */
79  	public boolean isOverridable() {
80  		return overridable;
81  	}
82  
83  	/**
84  	 * Whether to permit repositories to override this service's enabled state
85  	 * with the <code>daemon.servicename</code> config setting.
86  	 *
87  	 * @param on
88  	 *            {@code true} to permit repositories to override this service's
89  	 *            enabled state with the <code>daemon.servicename</code> config
90  	 *            setting.
91  	 */
92  	public void setOverridable(boolean on) {
93  		overridable = on;
94  	}
95  
96  	/**
97  	 * Get name of the command requested by clients.
98  	 *
99  	 * @return name of the command requested by clients.
100 	 */
101 	public String getCommandName() {
102 		return command;
103 	}
104 
105 	/**
106 	 * Determine if this service can handle the requested command.
107 	 *
108 	 * @param commandLine
109 	 *            input line from the client.
110 	 * @return true if this command can accept the given command line.
111 	 */
112 	public boolean handles(String commandLine) {
113 		return command.length() + 1 < commandLine.length()
114 				&& commandLine.charAt(command.length()) == ' '
115 				&& commandLine.startsWith(command);
116 	}
117 
118 	void execute(DaemonClient client, String commandLine,
119 			@Nullable Collection<String> extraParameters)
120 			throws IOException, ServiceNotEnabledException,
121 			ServiceNotAuthorizedException {
122 		final String name = commandLine.substring(command.length() + 1);
123 		try (Repository db = client.getDaemon().openRepository(client, name)) {
124 			if (isEnabledFor(db)) {
125 				execute(client, db, extraParameters);
126 			}
127 		} catch (ServiceMayNotContinueException e) {
128 			// An error when opening the repo means the client is expecting a ref
129 			// advertisement, so use that style of error.
130 			PacketLineOut pktOut = new PacketLineOut(client.getOutputStream());
131 			pktOut.writeString("ERR " + e.getMessage() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
132 		}
133 	}
134 
135 	private boolean isEnabledFor(Repository db) {
136 		if (isOverridable())
137 			return db.getConfig().get(configKey).enabled;
138 		return isEnabled();
139 	}
140 
141 	abstract void execute(DaemonClient client, Repository db,
142 			@Nullable Collection<String> extraParameters)
143 			throws IOException, ServiceNotEnabledException,
144 			ServiceNotAuthorizedException;
145 }