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 | 0 | public class ProjectInfoReportUtils |
51 | |
{ |
52 | 1 | 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 | 1 | 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 | 1 | String scheme = url.getProtocol(); |
82 | 1 | if ( !"file".equals( scheme ) ) |
83 | |
{ |
84 | 1 | Proxy proxy = settings.getActiveProxy(); |
85 | 1 | if ( proxy != null ) |
86 | |
{ |
87 | 0 | if ( "http".equals( scheme ) || "https".equals( scheme ) ) |
88 | |
{ |
89 | 0 | scheme = "http."; |
90 | |
} |
91 | 0 | else if ( "ftp".equals( scheme ) ) |
92 | |
{ |
93 | 0 | scheme = "ftp."; |
94 | |
} |
95 | |
else |
96 | |
{ |
97 | 0 | scheme = ""; |
98 | |
} |
99 | |
|
100 | 0 | String host = proxy.getHost(); |
101 | 0 | if ( !StringUtils.isEmpty( host ) ) |
102 | |
{ |
103 | 0 | Properties p = System.getProperties(); |
104 | 0 | p.setProperty( scheme + "proxySet", "true" ); |
105 | 0 | p.setProperty( scheme + "proxyHost", host ); |
106 | 0 | p.setProperty( scheme + "proxyPort", String.valueOf( proxy.getPort() ) ); |
107 | 0 | if ( !StringUtils.isEmpty( proxy.getNonProxyHosts() ) ) |
108 | |
{ |
109 | 0 | p.setProperty( scheme + "nonProxyHosts", proxy.getNonProxyHosts() ); |
110 | |
} |
111 | |
|
112 | 0 | final String userName = proxy.getUsername(); |
113 | 0 | if ( !StringUtils.isEmpty( userName ) ) |
114 | |
{ |
115 | 0 | final String pwd = StringUtils.isEmpty( proxy.getPassword() ) ? "" : proxy.getPassword(); |
116 | 0 | Authenticator.setDefault( new Authenticator() |
117 | |
{ |
118 | |
|
119 | 0 | protected PasswordAuthentication getPasswordAuthentication() |
120 | |
{ |
121 | 0 | return new PasswordAuthentication( userName, pwd.toCharArray() ); |
122 | |
} |
123 | |
} ); |
124 | |
} |
125 | |
} |
126 | |
} |
127 | |
} |
128 | |
|
129 | 1 | InputStream in = null; |
130 | |
try |
131 | |
{ |
132 | 1 | in = url.openStream(); |
133 | |
|
134 | 1 | if ( encoding == null ) |
135 | |
{ |
136 | 0 | return IOUtil.toString( in, "ISO-8859-1" ); |
137 | |
} |
138 | 1 | return IOUtil.toString( in, encoding ); |
139 | |
} |
140 | |
finally |
141 | |
{ |
142 | 1 | 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 | 2 | if ( Artifact.SCOPE_SYSTEM.equals( artifact.getScope() ) ) |
159 | |
{ |
160 | 0 | return null; |
161 | |
} |
162 | |
|
163 | 2 | Artifact copyArtifact = ArtifactUtils.copyArtifact( artifact ); |
164 | 2 | if ( !"pom".equals( copyArtifact.getType() ) ) |
165 | |
{ |
166 | 1 | copyArtifact = factory.createProjectArtifact( copyArtifact.getGroupId(), copyArtifact.getArtifactId(), |
167 | |
copyArtifact.getVersion(), copyArtifact.getScope() ); |
168 | |
} |
169 | |
try |
170 | |
{ |
171 | 2 | MavenProject pluginProject = mavenProjectBuilder.buildFromRepository( copyArtifact, remoteRepositories, |
172 | |
localRepository ); |
173 | |
|
174 | 2 | if ( isArtifactUrlValid( pluginProject.getUrl() ) ) |
175 | |
{ |
176 | 2 | return pluginProject.getUrl(); |
177 | |
} |
178 | |
|
179 | 0 | return null; |
180 | |
} |
181 | 0 | catch ( ProjectBuildingException e ) |
182 | |
{ |
183 | 0 | return null; |
184 | |
} |
185 | |
} |
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
public static String getArtifactIdCell( String artifactId, String link ) |
194 | |
{ |
195 | 4 | if ( StringUtils.isEmpty( link ) ) |
196 | |
{ |
197 | 2 | return artifactId; |
198 | |
} |
199 | |
|
200 | 2 | return "{" + artifactId + "," + link + "}"; |
201 | |
} |
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
public static boolean isArtifactUrlValid( String url ) |
208 | |
{ |
209 | 3 | if ( StringUtils.isEmpty( url ) ) |
210 | |
{ |
211 | 0 | return false; |
212 | |
} |
213 | |
|
214 | 3 | return URL_VALIDATOR.isValid( url ); |
215 | |
} |
216 | |
} |
217 | |
|