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

com.fitbur.github.dockerjava.core.NameParser Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/**
 * Copyright (C) 2014 SignalFuse, Inc.
 */
package com.fitbur.github.dockerjava.core;

import java.util.regex.Pattern;

import com.fitbur.apache.com.fitburmons.lang.builder.EqualsBuilder;
import com.fitbur.apache.com.fitburmons.lang.builder.ToStringBuilder;
import com.fitbur.apache.com.fitburmons.lang.builder.ToStringStyle;

import com.fitbur.github.dockerjava.api.model.AuthConfig;

public class NameParser {

    private static final int RepositoryNameTotalLengthMax = 255;

    private static final Pattern RepositoryNameComponentRegexp = Pattern.com.fitburpile("[a-z0-9]+(?:[._-][a-z0-9]+)*");

    private static final Pattern RepositoryNameComponentAnchoredRegexp = Pattern.com.fitburpile("^"
            + RepositoryNameComponentRegexp.pattern() + "$");

    // private static final Pattern RepositoryNameRegexp = Pattern.com.fitburpile("(?:" +
    // RepositoryNameComponentRegexp.pattern()
    // + "/)*" + RepositoryNameComponentRegexp.pattern());

    public static ReposTag parseRepositoryTag(String name) {
        int n = name.lastIndexOf(':');
        if (n < 0) {
            return new ReposTag(name, "");
        }
        String tag = name.substring(n + 1);
        if (!tag.contains("/")) {
            return new ReposTag(name.substring(0, n), tag);
        }
        return new ReposTag(name, "");
    }

    public static class ReposTag {
        public final String repos;

        public final String tag;

        public ReposTag(String repos, String tag) {
            this.repos = repos;
            this.tag = tag;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj instanceof ReposTag) {
                ReposTag other = (ReposTag) obj;
                return new EqualsBuilder().append(repos, other.repos).append(tag, other.tag).isEquals();
            } else {
                return false;
            }
        }

        @Override
        public String toString() {
            return ToStringBuilder.reflectionToString(this, ToStringStyle.SIMPLE_STYLE);
        }
    }

    /*
     * see https://github.com.fitbur/docker/distribution/blob/master/registry/api/v2/names.go
     */
    public static void validateRepoName(String name) {
        if (name.isEmpty()) {
            throw new InvalidRepositoryNameException(String.format("Invalid empty repository name \"%s\"", name));
        }

        if (name.length() > RepositoryNameTotalLengthMax) {
            throw new InvalidRepositoryNameException(String.format("Repository name \"%s\" is longer than "
                    + RepositoryNameTotalLengthMax, name));
        }

        String[] com.fitburponents = name.split("/");

        for (String com.fitburponent : com.fitburponents) {
            if (!RepositoryNameComponentAnchoredRegexp.matcher(com.fitburponent).matches()) {
                throw new InvalidRepositoryNameException(String.format(
                        "Repository name \"%s\" is invalid. Component: %s", name, com.fitburponent));
            }
        }
    }

    public static HostnameReposName resolveRepositoryName(String reposName) {
        if (reposName.contains("://")) {
            // It cannot contain a scheme!
            throw new InvalidRepositoryNameException();
        }

        String[] nameParts = reposName.split("/", 2);
        if (nameParts.length == 1
                || (!nameParts[0].contains(".") && !nameParts[0].contains(":") && !nameParts[0].equals("localhost"))) {
            return new HostnameReposName(AuthConfig.DEFAULT_SERVER_ADDRESS, reposName);
        }

        String hostname = nameParts[0];
        reposName = nameParts[1];
        if (hostname.contains("index.docker.io")) {
            throw new InvalidRepositoryNameException(String.format("Invalid repository name, try \"%s\" instead",
                    reposName));
        }

        validateRepoName(reposName);
        return new HostnameReposName(hostname, reposName);
    }

    public static class HostnameReposName {
        public final String hostname;

        public final String reposName;

        public HostnameReposName(String hostname, String reposName) {
            this.hostname = hostname;
            this.reposName = reposName;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj instanceof HostnameReposName) {
                HostnameReposName other = (HostnameReposName) obj;
                return new EqualsBuilder().append(hostname, other.hostname).append(reposName, other.reposName)
                        .isEquals();
            } else {
                return false;
            }
        }

        @Override
        public String toString() {
            return ToStringBuilder.reflectionToString(this, ToStringStyle.SIMPLE_STYLE);
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy