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