Class RepositoryUrlFragment

java.lang.Object
com.atlassian.bitbucket.scm.http.RepositoryUrlFragment

public class RepositoryUrlFragment extends Object
Utility class for parsing project namespaces, project keys and repository slugs from scm urls - HTTP and SSH.

Namespaces no longer serve any practical purpose in Bitbucket, but this class is still able to parse URLs with a namespace component.

Namespaces were required in the past to enable Bitbucket Mirrors to support mirroring multiple upstreams. To uniquify project/repository coordinates a namespace component was included in the Git URLs. Bitbucket provided clone URLs that included the namespace component for all users who used mirrors, regardless as to if multiple upstreams were used. In fact it was a rarely used feature.

Support for multiple upstreams was removed from Bitbucket, but so as not to break clients that used namespaced URLs the parser code continues to tolerate the namespace component, but always discards it as it no longer serves a purpose.

This class only works with paths that begin with:

  • <baseUrl>/PROJECT_KEY/REPOSITORY_SLUG/
  • <baseUrl>/PROJECT_NAMESPACE/PROJECT_KEY/REPOSITORY_SLUG/
  • <baseUrl>/PROJECT_KEY/REPOSITORY_SLUG.git/ (the .git suffix is not captured)
  • <baseUrl>/PROJECT_NAMESPACE/PROJECT_KEY/REPOSITORY_SLUG.git/ (the .git suffix is not captured)

This class captures any path suffix that did not describe the repository but still may be useful to identify.

  • Method Details

    • fromPathInfo

      @Nullable public static RepositoryUrlFragment fromPathInfo(@Nonnull String pathInfo)
      Extracts the repository URL fragment from the given path info.
      Parameters:
      pathInfo - the path info as provided by the HttpServletRequest.getPathInfo() method.
      Returns:
      a new repository URL fragment, or null if the path info does not contain a repository URL fragment
      Throws:
      NullPointerException - if the path info is null
    • fromNamespacedPathInfo

      @Deprecated @Nullable public static RepositoryUrlFragment fromNamespacedPathInfo(@Nonnull String pathInfo)
      Deprecated.
      in 7.18 use fromPathInfo(String) instead, unless when checking for namespaced URLs on mirrors
      Extracts the namespaced repository URL fragment from the given path info. On Mirrors, this method must be used if fromPathInfo(String) does not return a valid repository.
      Parameters:
      pathInfo - the path info as provided by the HttpServletRequest.getPathInfo() method.
      Returns:
      a new repository URL fragment, or null if the path info does not contain a namespaced repository URL fragment
      Throws:
      NullPointerException - if the path info is null
      Since:
      4.2
    • fromRepository

      @Nonnull public static RepositoryUrlFragment fromRepository(@Nonnull com.atlassian.bitbucket.repository.Repository repository)
      Parameters:
      repository - the repository to create a fragment for
      Returns:
      a URL fragment for the specified repository
    • fromKeyAndSlug

      @Nonnull public static RepositoryUrlFragment fromKeyAndSlug(@Nonnull String projectKey, @Nonnull String repositorySlug)
      Parameters:
      projectKey - the project key to use
      repositorySlug - the repository slug to use
      Returns:
      a URL fragment for the specified projectKey and repositorySlug
      Since:
      6.7
    • getPathSuffix

      @Nullable public String getPathSuffix()
      Returns:
      the suffix after the namespace / project key / repository part of the path or null if there was none
      Since:
      5.1
    • getProjectKey

      @Nonnull public String getProjectKey()
    • getRepositorySlug

      @Nonnull public String getRepositorySlug()
    • toPath

      @Nonnull public String toPath(@Nullable String basePath)
      Formats this fragment as a path suitable for use with a URI, such as with URI.resolve(String), optionally pre-pending a provided base path. The path suffix nor the .git extension if present in the original path are output here.
      Parameters:
      basePath - the base path to prepend, or null
      Returns:
      a URI-suitable path
    • toPath

      @Nonnull public String toPath(@Nullable String basePath, boolean includeSuffix)
      Formats this fragment as a path suitable for use with a URI, such as with URI.resolve(String), optionally pre-pending a provided base path. The path suffix is included if the includeSuffix parameter is true.
      Parameters:
      basePath - the base path to prepend, or null
      includeSuffix - whether to include the suffix
      Returns:
      a URI-suitable path
      Since:
      7.18
    • toString

      @Nonnull public String toString()
      Overrides:
      toString in class Object
      Returns:
      KEY/slug for URLs on primary instances