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.util.Collections;
28 import java.util.List;
29 import java.util.Properties;
30
31 import org.apache.commons.validator.UrlValidator;
32 import org.apache.maven.artifact.Artifact;
33 import org.apache.maven.artifact.ArtifactUtils;
34 import org.apache.maven.artifact.factory.ArtifactFactory;
35 import org.apache.maven.artifact.repository.ArtifactRepository;
36 import org.apache.maven.project.MavenProject;
37 import org.apache.maven.project.MavenProjectBuilder;
38 import org.apache.maven.project.ProjectBuildingException;
39 import org.apache.maven.settings.Proxy;
40 import org.apache.maven.settings.Settings;
41 import org.codehaus.plexus.util.IOUtil;
42 import org.codehaus.plexus.util.StringUtils;
43
44
45
46
47
48
49
50
51 public class ProjectInfoReportUtils
52 {
53 private static final UrlValidator URL_VALIDATOR = new UrlValidator( new String[] { "http", "https" } );
54
55
56
57
58
59
60
61
62
63
64 public static String getInputStream( URL url, Settings settings )
65 throws IOException
66 {
67 return getInputStream( url, settings, "ISO-8859-1" );
68 }
69
70
71
72
73
74
75
76
77
78
79 public static String getInputStream( URL url, Settings settings, String encoding )
80 throws IOException
81 {
82 String scheme = url.getProtocol();
83 if ( !"file".equals( scheme ) )
84 {
85 Proxy proxy = settings.getActiveProxy();
86 if ( proxy != null )
87 {
88 if ( "http".equals( scheme ) || "https".equals( scheme ) )
89 {
90 scheme = "http.";
91 }
92 else if ( "ftp".equals( scheme ) )
93 {
94 scheme = "ftp.";
95 }
96 else
97 {
98 scheme = "";
99 }
100
101 String host = proxy.getHost();
102 if ( !StringUtils.isEmpty( host ) )
103 {
104 Properties p = System.getProperties();
105 p.setProperty( scheme + "proxySet", "true" );
106 p.setProperty( scheme + "proxyHost", host );
107 p.setProperty( scheme + "proxyPort", String.valueOf( proxy.getPort() ) );
108 if ( !StringUtils.isEmpty( proxy.getNonProxyHosts() ) )
109 {
110 p.setProperty( scheme + "nonProxyHosts", proxy.getNonProxyHosts() );
111 }
112
113 final String userName = proxy.getUsername();
114 if ( !StringUtils.isEmpty( userName ) )
115 {
116 final String pwd = StringUtils.isEmpty( proxy.getPassword() ) ? "" : proxy.getPassword();
117 Authenticator.setDefault( new Authenticator()
118 {
119
120 protected PasswordAuthentication getPasswordAuthentication()
121 {
122 return new PasswordAuthentication( userName, pwd.toCharArray() );
123 }
124 } );
125 }
126 }
127 }
128 }
129
130 InputStream in = null;
131 try
132 {
133 in = url.openStream();
134
135 if ( encoding == null )
136 {
137 return IOUtil.toString( in, "ISO-8859-1" );
138 }
139 return IOUtil.toString( in, encoding );
140 }
141 finally
142 {
143 IOUtil.close( in );
144 }
145 }
146
147
148
149
150
151
152
153
154
155 public static String getArtifactUrl( ArtifactFactory factory, Artifact artifact,
156 MavenProjectBuilder mavenProjectBuilder, List remoteRepositories,
157 ArtifactRepository localRepository )
158 {
159 if ( Artifact.SCOPE_SYSTEM.equals( artifact.getScope() ) )
160 {
161 return null;
162 }
163
164 Artifact copyArtifact = ArtifactUtils.copyArtifact( artifact );
165 if ( !"pom".equals( copyArtifact.getType() ) )
166 {
167 copyArtifact = factory.createProjectArtifact( copyArtifact.getGroupId(), copyArtifact.getArtifactId(),
168 copyArtifact.getVersion(), copyArtifact.getScope() );
169 }
170 try
171 {
172 MavenProject pluginProject =
173 mavenProjectBuilder.buildFromRepository( copyArtifact,
174 remoteRepositories == null ? Collections.EMPTY_LIST
175 : remoteRepositories, localRepository );
176
177 if ( isArtifactUrlValid( pluginProject.getUrl() ) )
178 {
179 return pluginProject.getUrl();
180 }
181
182 return null;
183 }
184 catch ( ProjectBuildingException e )
185 {
186 return null;
187 }
188 }
189
190
191
192
193
194
195
196 public static String getArtifactIdCell( String artifactId, String link )
197 {
198 if ( StringUtils.isEmpty( link ) )
199 {
200 return artifactId;
201 }
202
203 return "{" + artifactId + "," + link + "}";
204 }
205
206
207
208
209
210 public static boolean isArtifactUrlValid( String url )
211 {
212 if ( StringUtils.isEmpty( url ) )
213 {
214 return false;
215 }
216
217 return URL_VALIDATOR.isValid( url );
218 }
219 }
220