TransportProtocol.java

  1. /*
  2.  * Copyright (C) 2011, 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. package org.eclipse.jgit.transport;

  11. import java.util.Collections;
  12. import java.util.EnumSet;
  13. import java.util.Set;

  14. import org.eclipse.jgit.errors.NotSupportedException;
  15. import org.eclipse.jgit.errors.TransportException;
  16. import org.eclipse.jgit.internal.JGitText;
  17. import org.eclipse.jgit.lib.Repository;

  18. /**
  19.  * Describes a way to connect to another Git repository.
  20.  * <p>
  21.  * Implementations of this class are typically immutable singletons held by
  22.  * static class members, for example:
  23.  *
  24.  * <pre>
  25.  * package com.example.my_transport;
  26.  *
  27.  * class MyTransport extends Transport {
  28.  *  public static final TransportProtocol PROTO = new TransportProtocol() {
  29.  *      public String getName() {
  30.  *          return &quot;My Protocol&quot;;
  31.  *      }
  32.  *  };
  33.  * }
  34.  * </pre>
  35.  *
  36.  * <p>
  37.  * Applications may register additional protocols for use by JGit by calling
  38.  * {@link org.eclipse.jgit.transport.Transport#register(TransportProtocol)}.
  39.  * Because that API holds onto the protocol object by a WeakReference,
  40.  * applications must ensure their own ClassLoader retains the TransportProtocol
  41.  * for the life of the application. Using a static singleton pattern as above
  42.  * will ensure the protocol is valid so long as the ClassLoader that defines it
  43.  * remains valid.
  44.  * <p>
  45.  * Applications may automatically register additional protocols by filling in
  46.  * the names of their TransportProtocol defining classes using the services file
  47.  * {@code META-INF/services/org.eclipse.jgit.transport.Transport}. For each
  48.  * class name listed in the services file, any static fields of type
  49.  * {@code TransportProtocol} will be automatically registered. For the above
  50.  * example the string {@code com.example.my_transport.MyTransport} should be
  51.  * listed in the file, as that is the name of the class that defines the static
  52.  * PROTO singleton.
  53.  */
  54. public abstract class TransportProtocol {
  55.     /** Fields within a {@link URIish} that a transport uses. */
  56.     public enum URIishField {
  57.         /** the user field */
  58.         USER,
  59.         /** the pass (aka password) field */
  60.         PASS,
  61.         /** the host field */
  62.         HOST,
  63.         /** the port field */
  64.         PORT,
  65.         /** the path field */
  66.         PATH,
  67.     }

  68.     /**
  69.      * Get text name of the protocol suitable for display to a user.
  70.      *
  71.      * @return text name of the protocol suitable for display to a user.
  72.      */
  73.     public abstract String getName();

  74.     /**
  75.      * Get immutable set of schemes supported by this protocol.
  76.      *
  77.      * @return immutable set of schemes supported by this protocol.
  78.      */
  79.     public Set<String> getSchemes() {
  80.         return Collections.emptySet();
  81.     }

  82.     /**
  83.      * Get immutable set of URIishFields that must be filled in.
  84.      *
  85.      * @return immutable set of URIishFields that must be filled in.
  86.      */
  87.     public Set<URIishField> getRequiredFields() {
  88.         return Collections.unmodifiableSet(EnumSet.of(URIishField.PATH));
  89.     }

  90.     /**
  91.      * Get immutable set of URIishFields that may be filled in.
  92.      *
  93.      * @return immutable set of URIishFields that may be filled in.
  94.      */
  95.     public Set<URIishField> getOptionalFields() {
  96.         return Collections.emptySet();
  97.     }

  98.     /**
  99.      * Get the default port if the protocol supports a port, else -1.
  100.      *
  101.      * @return the default port if the protocol supports a port, else -1.
  102.      */
  103.     public int getDefaultPort() {
  104.         return -1;
  105.     }

  106.     /**
  107.      * Determine if this protocol can handle a particular URI.
  108.      * <p>
  109.      * Implementations should try to avoid looking at the local filesystem, but
  110.      * may look at implementation specific configuration options in the remote
  111.      * block of {@code local.getConfig()} using {@code remoteName} if the name
  112.      * is non-null.
  113.      * <p>
  114.      * The default implementation of this method matches the scheme against
  115.      * {@link #getSchemes()}, required fields against
  116.      * {@link #getRequiredFields()}, and optional fields against
  117.      * {@link #getOptionalFields()}, returning true only if all of the fields
  118.      * match the specification.
  119.      *
  120.      * @param uri
  121.      *            address of the Git repository; never null.
  122.      * @return true if this protocol can handle this URI; false otherwise.
  123.      */
  124.     public boolean canHandle(URIish uri) {
  125.         return canHandle(uri, null, null);
  126.     }

  127.     /**
  128.      * Determine if this protocol can handle a particular URI.
  129.      * <p>
  130.      * Implementations should try to avoid looking at the local filesystem, but
  131.      * may look at implementation specific configuration options in the remote
  132.      * block of {@code local.getConfig()} using {@code remoteName} if the name
  133.      * is non-null.
  134.      * <p>
  135.      * The default implementation of this method matches the scheme against
  136.      * {@link #getSchemes()}, required fields against
  137.      * {@link #getRequiredFields()}, and optional fields against
  138.      * {@link #getOptionalFields()}, returning true only if all of the fields
  139.      * match the specification.
  140.      *
  141.      * @param uri
  142.      *            address of the Git repository; never null.
  143.      * @param local
  144.      *            the local repository that will communicate with the other Git
  145.      *            repository. May be null if the caller is only asking about a
  146.      *            specific URI and does not have a local Repository.
  147.      * @param remoteName
  148.      *            name of the remote, if the remote as configured in
  149.      *            {@code local}; otherwise null.
  150.      * @return true if this protocol can handle this URI; false otherwise.
  151.      */
  152.     public boolean canHandle(URIish uri, Repository local, String remoteName) {
  153.         if (!getSchemes().isEmpty() && !getSchemes().contains(uri.getScheme()))
  154.             return false;

  155.         for (URIishField field : getRequiredFields()) {
  156.             switch (field) {
  157.             case USER:
  158.                 if (uri.getUser() == null || uri.getUser().length() == 0)
  159.                     return false;
  160.                 break;

  161.             case PASS:
  162.                 if (uri.getPass() == null || uri.getPass().length() == 0)
  163.                     return false;
  164.                 break;

  165.             case HOST:
  166.                 if (uri.getHost() == null || uri.getHost().length() == 0)
  167.                     return false;
  168.                 break;

  169.             case PORT:
  170.                 if (uri.getPort() <= 0)
  171.                     return false;
  172.                 break;

  173.             case PATH:
  174.                 if (uri.getPath() == null || uri.getPath().length() == 0)
  175.                     return false;
  176.                 break;

  177.             default:
  178.                 return false;
  179.             }
  180.         }

  181.         Set<URIishField> canHave = EnumSet.copyOf(getRequiredFields());
  182.         canHave.addAll(getOptionalFields());

  183.         if (uri.getUser() != null && !canHave.contains(URIishField.USER))
  184.             return false;
  185.         if (uri.getPass() != null && !canHave.contains(URIishField.PASS))
  186.             return false;
  187.         if (uri.getHost() != null && !canHave.contains(URIishField.HOST))
  188.             return false;
  189.         if (uri.getPort() > 0 && !canHave.contains(URIishField.PORT))
  190.             return false;
  191.         if (uri.getPath() != null && !canHave.contains(URIishField.PATH))
  192.             return false;

  193.         return true;
  194.     }

  195.     /**
  196.      * Open a Transport instance to the other repository.
  197.      * <p>
  198.      * Implementations should avoid making remote connections until an operation
  199.      * on the returned Transport is invoked, however they may fail fast here if
  200.      * they know a connection is impossible, such as when using the local
  201.      * filesystem and the target path does not exist.
  202.      * <p>
  203.      * Implementations may access implementation-specific configuration options
  204.      * within {@code local.getConfig()} using the remote block named by the
  205.      * {@code remoteName}, if the name is non-null.
  206.      *
  207.      * @param uri
  208.      *            address of the Git repository.
  209.      * @param local
  210.      *            the local repository that will communicate with the other Git
  211.      *            repository.
  212.      * @param remoteName
  213.      *            name of the remote, if the remote as configured in
  214.      *            {@code local}; otherwise null.
  215.      * @return the transport.
  216.      * @throws org.eclipse.jgit.errors.NotSupportedException
  217.      *             this protocol does not support the URI.
  218.      * @throws org.eclipse.jgit.errors.TransportException
  219.      *             the transport cannot open this URI.
  220.      */
  221.     public abstract Transport open(URIish uri, Repository local,
  222.             String remoteName)
  223.             throws NotSupportedException, TransportException;

  224.     /**
  225.      * Open a new transport instance to the remote repository. Use default
  226.      * configuration instead of reading from configuration files.
  227.      *
  228.      * @param uri
  229.      *            a {@link org.eclipse.jgit.transport.URIish} object.
  230.      * @return new Transport
  231.      * @throws org.eclipse.jgit.errors.NotSupportedException
  232.      * @throws org.eclipse.jgit.errors.TransportException
  233.      */
  234.     public Transport open(URIish uri)
  235.             throws NotSupportedException, TransportException {
  236.         throw new NotSupportedException(JGitText
  237.                 .get().transportNeedsRepository);
  238.     }
  239. }