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