001/*-----------------------------------------------------------------------+
002 | com.teamscale.index
003 |                                                                       |
004   $Id: codetemplates.xml 18709 2009-03-06 13:31:16Z hummelb $            
005 |                                                                       |
006 | Copyright (c)  2009-2016 CQSE GmbH                                 |
007 +-----------------------------------------------------------------------*/
008package org.conqat.engine.index.shared;
009
010import java.net.URI;
011import java.net.URISyntaxException;
012import java.util.Optional;
013
014import org.conqat.lib.commons.enums.EnumUtils;
015import org.conqat.lib.commons.net.UrlUtils;
016
017/**
018 * Enumeration of supported git transport protocols.
019 */
020public enum EGitProtocol {
021
022        /** File protocol. */
023        FILE("file://"),
024
025        /** HTTP protocol. */
026        HTTP("http://"),
027
028        /** HTTPS protocol. */
029        HTTPS("https://"),
030
031        /** SSH protocol */
032        SSH("ssh://git@"),
033
034        /** GIT protocol */
035        GIT("git://");
036
037        /** The usual prefix of corresponding URLs. This is more than just the scheme in some cases (e.g. SSH). Used for protocol rewriting. */
038        private final String prefix;
039
040        private EGitProtocol(String prefix) {
041                this.prefix = prefix;
042        }
043        
044        /**
045         * Returns the protocol from the given url or empty if the url is invalid or the
046         * protocol is unknown.
047         */
048        public static Optional<EGitProtocol> fromUrl(String url) {
049                try {
050                        URI location = UrlUtils.convertUriFromUrl(url);
051                        return Optional.ofNullable(EnumUtils.valueOfIgnoreCase(EGitProtocol.class, location.getScheme()));
052                } catch (URISyntaxException e) {
053                        return Optional.empty();
054                }
055        }
056
057        /**
058         * Returns the URL prefix corresponding to this protocol, e.g. file:// or
059         * http://.
060         */
061        public String getUrlPrefix() {
062                return prefix;
063        }
064
065}