View Javadoc

1   package org.apache.maven.continuum.utils;
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.lang.StringUtils;
23  import org.codehaus.plexus.configuration.PlexusConfiguration;
24  import org.codehaus.plexus.configuration.PlexusConfigurationException;
25  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Configurable;
26  import org.springframework.stereotype.Service;
27  
28  import java.net.URI;
29  import java.net.URISyntaxException;
30  import java.util.Arrays;
31  import java.util.HashSet;
32  import java.util.Set;
33  
34  /**
35   * @author <a href="mailto:olamy@apache.org">olamy</a>
36   * @version $Id: ContinuumUrlValidator.java 1372260 2012-08-13 04:29:09Z brett $
37   * @plexus.component role="org.apache.maven.continuum.utils.ContinuumUrlValidator"
38   * role-hint="continuumUrl"
39   * @since 27 mars 2008
40   */
41  @Service( "continuumUrlValidator#continuumUrl" )
42  public class ContinuumUrlValidator
43      implements Configurable
44  {
45      /**
46       * The set of schemes that are allowed to be in a URL.
47       */
48      private Set<String> allowedSchemes = new HashSet<String>();
49  
50      /**
51       * If no schemes are provided, default to this set.
52       */
53      protected String[] defaultSchemes = {"http", "https", "ftp"};
54  
55      /**
56       * Create a UrlValidator with default properties.
57       */
58      public ContinuumUrlValidator()
59      {
60          this( null );
61      }
62  
63      /**
64       * Behavior of validation is modified by passing in several strings options:
65       *
66       * @param schemes Pass in one or more url schemes to consider valid, passing in
67       *                a null will default to "http,https,ftp" being valid.
68       *                If a non-null schemes is specified then all valid schemes must
69       *                be specified.
70       */
71      public ContinuumUrlValidator( String[] schemes )
72      {
73          if ( schemes == null && this.allowedSchemes.isEmpty() )
74          {
75              schemes = this.defaultSchemes;
76          }
77  
78          this.allowedSchemes.addAll( Arrays.asList( schemes ) );
79      }
80  
81      /**
82       * <p>Checks if a field has a valid url address.</p>
83       *
84       * @param value The value validation is being performed on.  A <code>null</code>
85       *              value is considered invalid.
86       * @return true if the url is valid.
87       */
88      public boolean validate( String value )
89      {
90          return isValid( value );
91      }
92  
93      /**
94       * <p>Checks if a field has a valid url address.</p>
95       *
96       * @param value The value validation is being performed on.  A <code>null</code>
97       *              value is considered valid.
98       * @return true if the url is valid.
99       */
100     public boolean isValid( String value )
101     {
102         if ( StringUtils.isEmpty( value ) )
103         {
104             return true;
105         }
106 
107         try
108         {
109             URI uri = new URI( value );
110             return this.allowedSchemes.contains( uri.getScheme() );
111         }
112         catch ( URISyntaxException e )
113         {
114             return false;
115         }
116     }
117 
118     /**
119      * @param url
120      * @return URLUserInfo cannot be null
121      * @throws URISyntaxException
122      */
123     public URLUserInfo extractURLUserInfo( String url )
124         throws URISyntaxException
125     {
126         URI uri = new URI( url );
127         // can contains user:password
128         String userInfoRaw = uri.getUserInfo();
129         URLUserInfo urlUserInfo = new URLUserInfo();
130 
131         if ( !StringUtils.isEmpty( userInfoRaw ) )
132         {
133             int index = userInfoRaw.indexOf( ':' );
134             if ( index >= 0 )
135             {
136                 urlUserInfo.setUsername( userInfoRaw.substring( 0, index ) );
137                 urlUserInfo.setPassword( userInfoRaw.substring( index + 1, userInfoRaw.length() ) );
138             }
139             else
140             {
141                 urlUserInfo.setUsername( userInfoRaw );
142             }
143         }
144         return urlUserInfo;
145     }
146 
147     public void configure( PlexusConfiguration plexusConfiguration )
148         throws PlexusConfigurationException
149     {
150         PlexusConfiguration allowedSchemesElement = plexusConfiguration.getChild( "allowedSchemes" );
151         if ( allowedSchemesElement != null )
152         {
153             PlexusConfiguration[] allowedSchemeElements = allowedSchemesElement.getChildren( "allowedScheme" );
154             for ( int i = 0, size = allowedSchemeElements.length; i < size; i++ )
155             {
156                 this.allowedSchemes.add( allowedSchemeElements[i].getValue() );
157             }
158         }
159     }
160 
161 }