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 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
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 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
82
83
84
85
86
87 public boolean validate( String value )
88 {
89 return isValid( value );
90 }
91
92
93
94
95
96
97
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
119
120
121
122 public URLUserInfo extractURLUserInfo( String url )
123 throws URISyntaxException
124 {
125 URI uri = new URI( url );
126
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 }