1 package org.apache.maven.report.projectinfo;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.net.Authenticator;
25 import java.net.PasswordAuthentication;
26 import java.net.URL;
27 import java.net.URLConnection;
28 import java.security.KeyManagementException;
29 import java.security.NoSuchAlgorithmException;
30 import java.security.SecureRandom;
31 import java.security.cert.X509Certificate;
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.Properties;
35
36 import javax.net.ssl.HostnameVerifier;
37 import javax.net.ssl.HttpsURLConnection;
38 import javax.net.ssl.SSLContext;
39 import javax.net.ssl.SSLSession;
40 import javax.net.ssl.SSLSocketFactory;
41 import javax.net.ssl.TrustManager;
42 import javax.net.ssl.X509TrustManager;
43
44 import org.apache.commons.lang.math.NumberUtils;
45 import org.apache.commons.validator.routines.RegexValidator;
46 import org.apache.commons.validator.routines.UrlValidator;
47 import org.apache.maven.artifact.Artifact;
48 import org.apache.maven.artifact.ArtifactUtils;
49 import org.apache.maven.artifact.factory.ArtifactFactory;
50 import org.apache.maven.artifact.repository.ArtifactRepository;
51 import org.apache.maven.project.MavenProject;
52 import org.apache.maven.project.MavenProjectBuilder;
53 import org.apache.maven.project.ProjectBuildingException;
54
55 import org.apache.maven.reporting.AbstractMavenReportRenderer;
56
57 import org.apache.maven.settings.Proxy;
58 import org.apache.maven.settings.Server;
59 import org.apache.maven.settings.Settings;
60 import org.codehaus.plexus.util.Base64;
61 import org.codehaus.plexus.util.IOUtil;
62 import org.codehaus.plexus.util.StringUtils;
63
64
65
66
67
68
69
70
71 public class ProjectInfoReportUtils
72 {
73 private static final UrlValidator URL_VALIDATOR = new UrlValidator( new String[] { "http", "https" },
74 new RegexValidator( "^([" + "\\p{Alnum}\\-\\."
75 + "]*)(:\\d*)?(.*)?" ), 0 );
76
77
78 private static final int TIMEOUT = 1000 * 5;
79
80
81 private static final String DEFAULT_ENCODING = "UTF-8";
82
83
84
85
86
87
88
89
90
91
92 public static String getContent( URL url, Settings settings )
93 throws IOException
94 {
95 return getContent( url, settings, DEFAULT_ENCODING );
96 }
97
98
99
100
101
102
103
104
105
106
107 public static String getContent( URL url, Settings settings, String encoding )
108 throws IOException
109 {
110 return getContent( url, null, settings, encoding );
111 }
112
113
114
115
116
117
118
119
120
121
122
123
124 public static String getContent( URL url, MavenProject project, Settings settings, String encoding )
125 throws IOException
126 {
127 String scheme = url.getProtocol();
128
129 if ( StringUtils.isEmpty( encoding ) )
130 {
131 encoding = DEFAULT_ENCODING;
132 }
133
134 if ( "file".equals( scheme ) )
135 {
136 InputStream in = null;
137 try
138 {
139 URLConnection conn = url.openConnection();
140 in = conn.getInputStream();
141
142 return IOUtil.toString( in, encoding );
143 }
144 finally
145 {
146 IOUtil.close( in );
147 }
148 }
149
150 Proxy proxy = settings.getActiveProxy();
151 if ( proxy != null )
152 {
153 if ( "http".equals( scheme ) || "https".equals( scheme ) || "ftp".equals( scheme ) )
154 {
155 scheme += ".";
156 }
157 else
158 {
159 scheme = "";
160 }
161
162 String host = proxy.getHost();
163 if ( !StringUtils.isEmpty( host ) )
164 {
165 Properties p = System.getProperties();
166 p.setProperty( scheme + "proxySet", "true" );
167 p.setProperty( scheme + "proxyHost", host );
168 p.setProperty( scheme + "proxyPort", String.valueOf( proxy.getPort() ) );
169 if ( !StringUtils.isEmpty( proxy.getNonProxyHosts() ) )
170 {
171 p.setProperty( scheme + "nonProxyHosts", proxy.getNonProxyHosts() );
172 }
173
174 final String userName = proxy.getUsername();
175 if ( !StringUtils.isEmpty( userName ) )
176 {
177 final String pwd = StringUtils.isEmpty( proxy.getPassword() ) ? "" : proxy.getPassword();
178 Authenticator.setDefault( new Authenticator()
179 {
180
181 protected PasswordAuthentication getPasswordAuthentication()
182 {
183 return new PasswordAuthentication( userName, pwd.toCharArray() );
184 }
185 } );
186 }
187 }
188 }
189
190 InputStream in = null;
191 try
192 {
193 URLConnection conn = getURLConnection( url, project, settings );
194 in = conn.getInputStream();
195
196 return IOUtil.toString( in, encoding );
197 }
198 finally
199 {
200 IOUtil.close( in );
201 }
202 }
203
204
205
206
207
208
209
210
211
212
213 public static String getArtifactUrl( ArtifactFactory factory, Artifact artifact,
214 MavenProjectBuilder mavenProjectBuilder,
215 List<ArtifactRepository> remoteRepositories, ArtifactRepository localRepository )
216
217 {
218 if ( Artifact.SCOPE_SYSTEM.equals( artifact.getScope() ) )
219 {
220 return null;
221 }
222
223 Artifact copyArtifact = ArtifactUtils.copyArtifact( artifact );
224 if ( !"pom".equals( copyArtifact.getType() ) )
225 {
226 copyArtifact =
227 factory.createProjectArtifact( copyArtifact.getGroupId(), copyArtifact.getArtifactId(),
228 copyArtifact.getVersion(), copyArtifact.getScope() );
229 }
230 try
231 {
232 MavenProject pluginProject =
233 mavenProjectBuilder.buildFromRepository( copyArtifact,
234 remoteRepositories == null ? Collections.EMPTY_LIST
235 : remoteRepositories, localRepository );
236
237 if ( isArtifactUrlValid( pluginProject.getUrl() ) )
238 {
239 return pluginProject.getUrl();
240 }
241
242 return null;
243 }
244 catch ( ProjectBuildingException e )
245 {
246 return null;
247 }
248 }
249
250
251
252
253
254
255
256 public static String getArtifactIdCell( String artifactId, String link )
257 {
258 if ( StringUtils.isEmpty( link ) )
259 {
260 return artifactId;
261 }
262
263 return "{" + artifactId + "," + link + "}";
264 }
265
266
267
268
269
270 public static boolean isArtifactUrlValid( String url )
271 {
272 if ( StringUtils.isEmpty( url ) )
273 {
274 return false;
275 }
276
277 return URL_VALIDATOR.isValid( url );
278 }
279
280
281
282
283
284
285
286
287 private static URLConnection getURLConnection( URL url, MavenProject project, Settings settings )
288 throws IOException
289 {
290 URLConnection conn = url.openConnection();
291 conn.setConnectTimeout( TIMEOUT );
292 conn.setReadTimeout( TIMEOUT );
293
294
295
296 if ( settings.getServers() != null
297 && !settings.getServers().isEmpty()
298 && project != null
299 && project.getDistributionManagement() != null
300 && (
301 project.getDistributionManagement().getRepository() != null
302 || project.getDistributionManagement().getSnapshotRepository() != null
303 )
304 && ( StringUtils.isNotEmpty( project.getDistributionManagement().getRepository().getUrl() )
305 || StringUtils.isNotEmpty( project.getDistributionManagement().getSnapshotRepository().getUrl() ) )
306 )
307
308 {
309 Server server = null;
310 if ( url.toString().contains( project.getDistributionManagement().getRepository().getUrl() ) )
311 {
312 server = settings.getServer( project.getDistributionManagement().getRepository().getId() );
313 }
314 if ( server == null
315 && url.toString().contains( project.getDistributionManagement().getSnapshotRepository().getUrl() ) )
316 {
317 server = settings.getServer( project.getDistributionManagement().getSnapshotRepository().getId() );
318 }
319
320 if ( server != null && StringUtils.isNotEmpty( server.getUsername() )
321 && StringUtils.isNotEmpty( server.getPassword() ) )
322 {
323 String up = server.getUsername().trim() + ":" + server.getPassword().trim();
324 String upEncoded = new String( Base64.encodeBase64Chunked( up.getBytes() ) ).trim();
325
326 conn.setRequestProperty( "Authorization", "Basic " + upEncoded );
327 }
328 }
329
330 if ( conn instanceof HttpsURLConnection )
331 {
332 HostnameVerifier hostnameverifier = new HostnameVerifier()
333 {
334
335 public boolean verify( String urlHostName, SSLSession session )
336 {
337 return true;
338 }
339 };
340 ( (HttpsURLConnection) conn ).setHostnameVerifier( hostnameverifier );
341
342 TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager()
343 {
344
345 public void checkClientTrusted( final X509Certificate[] chain, final String authType )
346 {
347 }
348
349
350 public void checkServerTrusted( final X509Certificate[] chain, final String authType )
351 {
352 }
353
354
355 public X509Certificate[] getAcceptedIssuers()
356 {
357 return null;
358 }
359 } };
360
361 try
362 {
363 SSLContext sslContext = SSLContext.getInstance( "SSL" );
364 sslContext.init( null, trustAllCerts, new SecureRandom() );
365
366 SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
367
368 ( (HttpsURLConnection) conn ).setSSLSocketFactory( sslSocketFactory );
369 }
370 catch ( NoSuchAlgorithmException e1 )
371 {
372
373 }
374 catch ( KeyManagementException e )
375 {
376
377 }
378 }
379
380 return conn;
381 }
382
383
384
385
386
387 public static boolean isNumber( String str )
388 {
389 if ( str.startsWith( "+" ) )
390 {
391 str = str.substring( 1 );
392 }
393 return NumberUtils.isNumber( str );
394 }
395
396
397
398
399
400
401 public static float toFloat( String str, float defaultValue )
402 {
403 if ( str.startsWith( "+" ) )
404 {
405 str = str.substring( 1 );
406 }
407 return NumberUtils.toFloat( str, defaultValue );
408 }
409 }