All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.conqat.engine.index.shared.EGitProtocol Maven / Gradle / Ivy

There is a newer version: 2025.1.0-rc2
Show newest version
/*-----------------------------------------------------------------------+
 | com.teamscale.index
 |                                                                       |
   $Id: codetemplates.xml 18709 2009-03-06 13:31:16Z hummelb $            
 |                                                                       |
 | Copyright (c)  2009-2016 CQSE GmbH                                 |
 +-----------------------------------------------------------------------*/
package org.conqat.engine.index.shared;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Optional;

import org.conqat.lib.commons.enums.EnumUtils;
import org.conqat.lib.commons.net.UrlUtils;
import org.conqat.lib.commons.string.StringUtils;

/**
 * Enumeration of supported git transport protocols.
 */
public enum EGitProtocol {

	/** File protocol. */
	FILE("file://"),

	/** HTTP protocol. */
	HTTP("http://"),

	/** HTTPS protocol. */
	HTTPS("https://"),

	/** SSH protocol */
	SSH("ssh://git@"),

	/** GIT protocol */
	GIT("git://");

	/**
	 * The usual prefix of corresponding URLs. This is more than just the scheme in
	 * some cases (e.g. SSH). Used for protocol rewriting.
	 */
	private final String prefix;

	EGitProtocol(String prefix) {
		this.prefix = prefix;
	}

	/**
	 * Returns the protocol from the given url or empty if the url is invalid or the
	 * protocol is unknown.
	 */
	public static Optional fromUrl(String url) {
		try {
			URI location = UrlUtils.convertUriFromUrl(url);
			return Optional.ofNullable(EnumUtils.valueOfIgnoreCase(EGitProtocol.class, location.getScheme()));
		} catch (URISyntaxException e) {
			return Optional.empty();
		}
	}

	/**
	 * Returns the URL prefix corresponding to this protocol, e.g. file:// or
	 * http://.
	 */
	public String getUrlPrefix() {
		return prefix;
	}

	/** Strips the prefix of this protocol from URL. */
	public String stripUrlPrefix(String url) {
		return StringUtils.stripPrefix(url, getUrlPrefix());
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy