1 package org.apache.maven.continuum.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
36
37
38
39
40
41 @Service( "continuumUrlValidator#continuumUrl" )
42 public class ContinuumUrlValidator
43 implements Configurable
44 {
45
46
47
48 private Set<String> allowedSchemes = new HashSet<String>();
49
50
51
52
53 protected String[] defaultSchemes = {"http", "https", "ftp"};
54
55
56
57
58 public ContinuumUrlValidator()
59 {
60 this( null );
61 }
62
63
64
65
66
67
68
69
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
83
84
85
86
87
88 public boolean validate( String value )
89 {
90 return isValid( value );
91 }
92
93
94
95
96
97
98
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
120
121
122
123 public URLUserInfo extractURLUserInfo( String url )
124 throws URISyntaxException
125 {
126 URI uri = new URI( url );
127
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 }