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