View Javadoc
1   package org.apache.maven.reporting;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.commons.validator.routines.RegexValidator;
23  import org.apache.commons.validator.routines.UrlValidator;
24  
25  /**
26   * Static utility class intended to help {@link AbstractMavenReportRenderer} in validating URLs. Validation uses two
27   * UrlValidator instances. The first validates public URLs, the second validates local URLs. At least one validator has
28   * to accept the given URL. A URL is called local if it uses an unqualified hostname (such as "localhost" or
29   * "workstation-12") or qualified domain names within the special use top level domain ".local".
30   *
31   * @author <a href="mailto:jan.schultze@gmail.com">Jan Schultze</a>
32   */
33  final class UrlValidationUtil
34  {
35  
36      private static final String LETTERS_DIGITS = "[a-zA-Z0-9]";
37  
38      private static final String LETTERS_DIGITS_HYPHEN = "[a-zA-Z0-9\\-]";
39  
40      private static final String LABEL = LETTERS_DIGITS + "(" + LETTERS_DIGITS_HYPHEN + "{0,61}" + LETTERS_DIGITS + ")?";
41  
42      private static final String OPTIONAL_PORT = "(:(([1-5]\\d{1,4})|([1-9]\\d{1,3})))?";
43  
44      private static final String AUTHORITY_REGEX = LABEL + "(\\." + LABEL + ")*\\.local\\.?" + OPTIONAL_PORT;
45  
46      private static final String[] SCHEMES = { "http", "https" };
47  
48      private UrlValidationUtil()
49      {
50          throw new RuntimeException( "Instantiation of " + UrlValidationUtil.class.getName() + " is not allowed." );
51      }
52  
53      static boolean isValidUrl( final String url )
54      {
55          return isValidPublicUrl( url ) || isValidLocalUrl( url );
56      }
57  
58      private static boolean isValidPublicUrl( final String url )
59      {
60          UrlValidator validator = configurePublicUrlValidator();
61          return validator.isValid( url );
62      }
63  
64      private static UrlValidator configurePublicUrlValidator()
65      {
66          return new UrlValidator( SCHEMES );
67      }
68  
69      private static boolean isValidLocalUrl( final String url )
70      {
71          UrlValidator validator = configureLocalUrlValidator();
72          return validator.isValid( url );
73      }
74  
75      private static UrlValidator configureLocalUrlValidator()
76      {
77          RegexValidator authorityValidator = configureLocalAuthorityValidator();
78          return new UrlValidator( SCHEMES, authorityValidator, UrlValidator.ALLOW_LOCAL_URLS );
79      }
80  
81      /* package-private for testing purposes */
82      static RegexValidator configureLocalAuthorityValidator()
83      {
84          return new RegexValidator( AUTHORITY_REGEX, false );
85      }
86  
87  }