summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/net/DomainName.java
blob: 5ac13d0306cd9ad759b0c2c3b5490c6cbc781c2f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.net;

import ai.vespa.validation.StringWrapper;

import java.util.regex.Pattern;

import static ai.vespa.validation.Validation.requireInRange;
import static ai.vespa.validation.Validation.requireMatch;

/**
 * A valid domain name, which can be used in a {@link java.net.URI}.
 *
 * @author jonmv
 */
public class DomainName extends StringWrapper<DomainName> {

    public static final Pattern labelPattern = Pattern.compile("([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])");
    public static final Pattern domainNamePattern = Pattern.compile("(" + labelPattern + "\\.)*" + labelPattern);

    public static final DomainName localhost = DomainName.of("localhost");

    private DomainName(String value) {
        super(value);
    }

    public static DomainName of(String value) {
        requireInRange(value.length(), "domain name", 1, 255);
        return new DomainName(requireMatch(value, "domain name", domainNamePattern));
   }

    public static String requireLabel(String label) {
        return requireMatch(label, "domain name label", labelPattern);
    }

}