1 | /** |
2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
3 | * contributor license agreements. See the NOTICE file |
4 | * distributed with this work for additional information |
5 | * regarding copyright ownership. jclouds licenses this file |
6 | * to you under the Apache License, Version 2.0 (the |
7 | * "License"); you may not use this file except in compliance |
8 | * with the License. You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, |
13 | * software distributed under the License is distributed on an |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | * KIND, either express or implied. See the License for the |
16 | * specific language governing permissions and limitations |
17 | * under the License. |
18 | */ |
19 | package org.jclouds.predicates.validators; |
20 | |
21 | import static com.google.common.base.CharMatcher.inRange; |
22 | import static com.google.common.base.CharMatcher.is; |
23 | |
24 | import javax.inject.Inject; |
25 | import javax.inject.Named; |
26 | |
27 | import org.jclouds.predicates.Validator; |
28 | |
29 | import com.google.common.base.CharMatcher; |
30 | import com.google.inject.Singleton; |
31 | |
32 | /** |
33 | * Validates name for dns-style names |
34 | * |
35 | * @see org.jclouds.rest.InputParamValidator |
36 | * @see org.jclouds.predicates.Validator |
37 | * |
38 | * @author Adrian Cole |
39 | */ |
40 | @Singleton |
41 | public class DnsNameValidator extends Validator<String> { |
42 | private int min; |
43 | private int max; |
44 | |
45 | @Inject |
46 | protected DnsNameValidator(@Named("jclouds.dns_name_length_min") int min, |
47 | @Named("jclouds.dns_name_length_max") int max) { |
48 | this.min = min; |
49 | this.max = max; |
50 | } |
51 | |
52 | public void validate(String name) { |
53 | |
54 | if (name == null || name.length() < min || name.length() > max) |
55 | throw exception(name, "Can't be null or empty. Length must be " + min + " to " + max |
56 | + " symbols."); |
57 | if (CharMatcher.JAVA_LETTER_OR_DIGIT.indexIn(name) != 0) |
58 | throw exception(name, "Should start with letter/number"); |
59 | if (!name.toLowerCase().equals(name)) |
60 | throw exception(name, "Should be only lowercase"); |
61 | |
62 | /* |
63 | * The name must be a valid DNS name. From wikipedia: "The characters allowed in a label are a |
64 | * subset of the ASCII character set, a and includes the characters a through z, A through Z, |
65 | * digits 0 through 9". From Azure: Every Dash (-) Must Be Immediately Preceded and Followed |
66 | * by a Letter or Number. |
67 | */ |
68 | CharMatcher range = getAcceptableRange(); |
69 | if (!range.matchesAllOf(name)) |
70 | throw exception(name, "Should have lowercase ASCII letters, " + "numbers, or dashes"); |
71 | } |
72 | |
73 | protected CharMatcher getAcceptableRange() { |
74 | return inRange('a', 'z').or(inRange('0', '9').or(is('-'))); |
75 | } |
76 | |
77 | protected IllegalArgumentException exception(String vAppName, String reason) { |
78 | return new IllegalArgumentException(String.format( |
79 | "Object '%s' doesn't match dns naming constraints. " + "Reason: %s.", vAppName, |
80 | reason)); |
81 | } |
82 | |
83 | } |