1 /*
2 * Copyright (C) 2011, Google Inc.
3 * and other copyright owners as documented in the project's IP log.
4 *
5 * This program and the accompanying materials are made available
6 * under the terms of the Eclipse Distribution License v1.0 which
7 * accompanies this distribution, is reproduced below, and is
8 * available at http://www.eclipse.org/org/documents/edl-v10.php
9 *
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials provided
22 * with the distribution.
23 *
24 * - Neither the name of the Eclipse Foundation, Inc. nor the
25 * names of its contributors may be used to endorse or promote
26 * products derived from this software without specific prior
27 * written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 */
43
44 package org.eclipse.jgit.transport;
45
46 import java.util.Collections;
47 import java.util.EnumSet;
48 import java.util.Set;
49
50 import org.eclipse.jgit.errors.NotSupportedException;
51 import org.eclipse.jgit.errors.TransportException;
52 import org.eclipse.jgit.internal.JGitText;
53 import org.eclipse.jgit.lib.Repository;
54
55 /**
56 * Describes a way to connect to another Git repository.
57 * <p>
58 * Implementations of this class are typically immutable singletons held by
59 * static class members, for example:
60 *
61 * <pre>
62 * package com.example.my_transport;
63 *
64 * class MyTransport extends Transport {
65 * public static final TransportProtocol PROTO = new TransportProtocol() {
66 * public String getName() {
67 * return "My Protocol";
68 * }
69 * };
70 * }
71 * </pre>
72 *
73 * <p>
74 * Applications may register additional protocols for use by JGit by calling
75 * {@link Transport#register(TransportProtocol)}. Because that API holds onto
76 * the protocol object by a WeakReference, applications must ensure their own
77 * ClassLoader retains the TransportProtocol for the life of the application.
78 * Using a static singleton pattern as above will ensure the protocol is valid
79 * so long as the ClassLoader that defines it remains valid.
80 * <p>
81 * Applications may automatically register additional protocols by filling in
82 * the names of their TransportProtocol defining classes using the services file
83 * {@code META-INF/services/org.eclipse.jgit.transport.Transport}. For each
84 * class name listed in the services file, any static fields of type
85 * {@code TransportProtocol} will be automatically registered. For the above
86 * example the string {@code com.example.my_transport.MyTransport} should be
87 * listed in the file, as that is the name of the class that defines the static
88 * PROTO singleton.
89 */
90 public abstract class TransportProtocol {
91 /** Fields within a {@link URIish} that a transport uses. */
92 public static enum URIishField {
93 /** the user field */
94 USER,
95 /** the pass (aka password) field */
96 PASS,
97 /** the host field */
98 HOST,
99 /** the port field */
100 PORT,
101 /** the path field */
102 PATH,
103 }
104
105 /** @return text name of the protocol suitable for display to a user. */
106 public abstract String getName();
107
108 /** @return immutable set of schemes supported by this protocol. */
109 public Set<String> getSchemes() {
110 return Collections.emptySet();
111 }
112
113 /** @return immutable set of URIishFields that must be filled in. */
114 public Set<URIishField> getRequiredFields() {
115 return Collections.unmodifiableSet(EnumSet.of(URIishField.PATH));
116 }
117
118 /** @return immutable set of URIishFields that may be filled in. */
119 public Set<URIishField> getOptionalFields() {
120 return Collections.emptySet();
121 }
122
123 /** @return if a port is supported, the default port, else -1. */
124 public int getDefaultPort() {
125 return -1;
126 }
127
128 /**
129 * Determine if this protocol can handle a particular URI.
130 * <p>
131 * Implementations should try to avoid looking at the local filesystem, but
132 * may look at implementation specific configuration options in the remote
133 * block of {@code local.getConfig()} using {@code remoteName} if the name
134 * is non-null.
135 * <p>
136 * The default implementation of this method matches the scheme against
137 * {@link #getSchemes()}, required fields against
138 * {@link #getRequiredFields()}, and optional fields against
139 * {@link #getOptionalFields()}, returning true only if all of the fields
140 * match the specification.
141 *
142 * @param uri
143 * address of the Git repository; never null.
144 * @return true if this protocol can handle this URI; false otherwise.
145 */
146 public boolean canHandle(URIish uri) {
147 return canHandle(uri, null, null);
148 }
149
150 /**
151 * Determine if this protocol can handle a particular URI.
152 * <p>
153 * Implementations should try to avoid looking at the local filesystem, but
154 * may look at implementation specific configuration options in the remote
155 * block of {@code local.getConfig()} using {@code remoteName} if the name
156 * is non-null.
157 * <p>
158 * The default implementation of this method matches the scheme against
159 * {@link #getSchemes()}, required fields against
160 * {@link #getRequiredFields()}, and optional fields against
161 * {@link #getOptionalFields()}, returning true only if all of the fields
162 * match the specification.
163 *
164 * @param uri
165 * address of the Git repository; never null.
166 * @param local
167 * the local repository that will communicate with the other Git
168 * repository. May be null if the caller is only asking about a
169 * specific URI and does not have a local Repository.
170 * @param remoteName
171 * name of the remote, if the remote as configured in
172 * {@code local}; otherwise null.
173 * @return true if this protocol can handle this URI; false otherwise.
174 */
175 public boolean canHandle(URIish uri, Repository local, String remoteName) {
176 if (!getSchemes().isEmpty() && !getSchemes().contains(uri.getScheme()))
177 return false;
178
179 for (URIishField field : getRequiredFields()) {
180 switch (field) {
181 case USER:
182 if (uri.getUser() == null || uri.getUser().length() == 0)
183 return false;
184 break;
185
186 case PASS:
187 if (uri.getPass() == null || uri.getPass().length() == 0)
188 return false;
189 break;
190
191 case HOST:
192 if (uri.getHost() == null || uri.getHost().length() == 0)
193 return false;
194 break;
195
196 case PORT:
197 if (uri.getPort() <= 0)
198 return false;
199 break;
200
201 case PATH:
202 if (uri.getPath() == null || uri.getPath().length() == 0)
203 return false;
204 break;
205
206 default:
207 return false;
208 }
209 }
210
211 Set<URIishField> canHave = EnumSet.copyOf(getRequiredFields());
212 canHave.addAll(getOptionalFields());
213
214 if (uri.getUser() != null && !canHave.contains(URIishField.USER))
215 return false;
216 if (uri.getPass() != null && !canHave.contains(URIishField.PASS))
217 return false;
218 if (uri.getHost() != null && !canHave.contains(URIishField.HOST))
219 return false;
220 if (uri.getPort() > 0 && !canHave.contains(URIishField.PORT))
221 return false;
222 if (uri.getPath() != null && !canHave.contains(URIishField.PATH))
223 return false;
224
225 return true;
226 }
227
228 /**
229 * Open a Transport instance to the other repository.
230 * <p>
231 * Implementations should avoid making remote connections until an operation
232 * on the returned Transport is invoked, however they may fail fast here if
233 * they know a connection is impossible, such as when using the local
234 * filesystem and the target path does not exist.
235 * <p>
236 * Implementations may access implementation-specific configuration options
237 * within {@code local.getConfig()} using the remote block named by the
238 * {@code remoteName}, if the name is non-null.
239 *
240 * @param uri
241 * address of the Git repository.
242 * @param local
243 * the local repository that will communicate with the other Git
244 * repository.
245 * @param remoteName
246 * name of the remote, if the remote as configured in
247 * {@code local}; otherwise null.
248 * @return the transport.
249 * @throws NotSupportedException
250 * this protocol does not support the URI.
251 * @throws TransportException
252 * the transport cannot open this URI.
253 */
254 public abstract Transport open(URIish uri, Repository local,
255 String remoteName)
256 throws NotSupportedException, TransportException;
257
258 /**
259 * Open a new transport instance to the remote repository. Use default
260 * configuration instead of reading from configuration files.
261 *
262 * @param uri
263 * @return new Transport
264 * @throws NotSupportedException
265 * @throws TransportException
266 */
267 public Transport open(URIish uri)
268 throws NotSupportedException, TransportException {
269 throw new NotSupportedException(JGitText
270 .get().transportNeedsRepository);
271 }
272 }