1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.report.projectinfo;
20
21 import javax.net.ssl.HostnameVerifier;
22 import javax.net.ssl.HttpsURLConnection;
23 import javax.net.ssl.SSLContext;
24 import javax.net.ssl.SSLSession;
25 import javax.net.ssl.SSLSocketFactory;
26 import javax.net.ssl.TrustManager;
27 import javax.net.ssl.X509TrustManager;
28
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.net.Authenticator;
32 import java.net.PasswordAuthentication;
33 import java.net.URI;
34 import java.net.URL;
35 import java.net.URLConnection;
36 import java.security.KeyManagementException;
37 import java.security.NoSuchAlgorithmException;
38 import java.security.SecureRandom;
39 import java.security.cert.X509Certificate;
40 import java.util.Properties;
41
42 import org.apache.commons.validator.routines.RegexValidator;
43 import org.apache.commons.validator.routines.UrlValidator;
44 import org.apache.maven.project.MavenProject;
45 import org.apache.maven.reporting.AbstractMavenReportRenderer;
46 import org.apache.maven.settings.Proxy;
47 import org.apache.maven.settings.Server;
48 import org.apache.maven.settings.Settings;
49 import org.codehaus.plexus.util.Base64;
50 import org.codehaus.plexus.util.IOUtil;
51 import org.codehaus.plexus.util.StringUtils;
52
53
54
55
56
57
58
59
60 public class ProjectInfoReportUtils {
61 private static final UrlValidator URL_VALIDATOR = new UrlValidator(
62 new String[] {"http", "https"}, new RegexValidator("^([" + "\\p{Alnum}\\-\\." + "]*)(:\\d*)?(.*)?"), 0);
63
64
65 private static final int TIMEOUT = 1000 * 5;
66
67
68
69
70
71
72
73
74
75
76 public static String getContent(URL url, Settings settings) throws IOException {
77 return getContent(url, settings, "UTF-8");
78 }
79
80
81
82
83
84
85
86
87
88
89 public static String getContent(URL url, Settings settings, String encoding) throws IOException {
90 return getContent(url, null, settings, encoding);
91 }
92
93
94
95
96
97
98
99
100
101
102
103
104 public static String getContent(URL url, MavenProject project, Settings settings, String encoding)
105 throws IOException {
106 String scheme = url.getProtocol();
107
108 if (encoding == null || encoding.isEmpty()) {
109 encoding = "UTF-8";
110 }
111
112 if ("file".equals(scheme)) {
113 try (InputStream in = url.openConnection().getInputStream()) {
114 final String content = IOUtil.toString(in, encoding);
115 return content;
116 }
117 }
118
119 Proxy proxy = settings.getActiveProxy();
120 if (proxy != null) {
121 if ("http".equals(scheme) || "https".equals(scheme) || "ftp".equals(scheme)) {
122 scheme += ".";
123 } else {
124 scheme = "";
125 }
126
127 String host = proxy.getHost();
128 if (!(host == null || host.isEmpty())) {
129 Properties p = System.getProperties();
130 p.setProperty(scheme + "proxySet", "true");
131 p.setProperty(scheme + "proxyHost", host);
132 p.setProperty(scheme + "proxyPort", String.valueOf(proxy.getPort()));
133 if (!StringUtils.isEmpty(proxy.getNonProxyHosts())) {
134 p.setProperty(scheme + "nonProxyHosts", proxy.getNonProxyHosts());
135 }
136
137 final String userName = proxy.getUsername();
138 if (!(userName == null || userName.isEmpty())) {
139 final String pwd = StringUtils.isEmpty(proxy.getPassword()) ? "" : proxy.getPassword();
140 Authenticator.setDefault(new Authenticator() {
141
142 @Override
143 protected PasswordAuthentication getPasswordAuthentication() {
144 return new PasswordAuthentication(userName, pwd.toCharArray());
145 }
146 });
147 }
148 }
149 }
150
151 try (InputStream in = getURLConnection(url, project, settings).getInputStream()) {
152 return IOUtil.toString(in, encoding);
153 }
154 }
155
156
157
158
159
160 public static String getProjectUrl(MavenProject project) {
161
162 if (project != null && isArtifactUrlValid(project.getUrl())) {
163 return project.getUrl();
164 }
165
166 return null;
167 }
168
169
170
171
172
173
174
175 public static String getArtifactIdCell(String artifactId, String link) {
176 if (link == null || link.isEmpty()) {
177 return artifactId;
178 }
179
180 return "{" + artifactId + "," + link + "}";
181 }
182
183
184
185
186
187 public static boolean isArtifactUrlValid(String url) {
188 if (url == null || url.isEmpty()) {
189 return false;
190 }
191
192 return URL_VALIDATOR.isValid(url);
193 }
194
195
196
197
198
199
200
201
202
203 public static String getArchiveServer(String uri) {
204 if (uri == null) {
205 return "???UNKNOWN???";
206 }
207 return URI.create(uri).getHost();
208 }
209
210
211
212
213
214
215
216
217 private static URLConnection getURLConnection(URL url, MavenProject project, Settings settings) throws IOException {
218 URLConnection conn = url.openConnection();
219 conn.setConnectTimeout(TIMEOUT);
220 conn.setReadTimeout(TIMEOUT);
221
222
223
224 if (settings.getServers() != null
225 && !settings.getServers().isEmpty()
226 && project != null
227 && project.getDistributionManagement() != null
228 && (project.getDistributionManagement().getRepository() != null
229 || project.getDistributionManagement().getSnapshotRepository() != null)
230 && (StringUtils.isNotEmpty(project.getDistributionManagement()
231 .getRepository()
232 .getUrl())
233 || StringUtils.isNotEmpty(project.getDistributionManagement()
234 .getSnapshotRepository()
235 .getUrl())))
236
237 {
238 Server server = null;
239 if (url.toString()
240 .contains(
241 project.getDistributionManagement().getRepository().getUrl())) {
242 server = settings.getServer(
243 project.getDistributionManagement().getRepository().getId());
244 }
245 if (server == null
246 && url.toString()
247 .contains(project.getDistributionManagement()
248 .getSnapshotRepository()
249 .getUrl())) {
250 server = settings.getServer(project.getDistributionManagement()
251 .getSnapshotRepository()
252 .getId());
253 }
254
255 if (server != null
256 && StringUtils.isNotEmpty(server.getUsername())
257 && StringUtils.isNotEmpty(server.getPassword())) {
258 String up =
259 server.getUsername().trim() + ":" + server.getPassword().trim();
260 String upEncoded = new String(Base64.encodeBase64Chunked(up.getBytes())).trim();
261
262 conn.setRequestProperty("Authorization", "Basic " + upEncoded);
263 }
264 }
265
266 if (conn instanceof HttpsURLConnection) {
267 HostnameVerifier hostnameverifier = new HostnameVerifier() {
268
269 public boolean verify(String urlHostName, SSLSession session) {
270 return true;
271 }
272 };
273 ((HttpsURLConnection) conn).setHostnameVerifier(hostnameverifier);
274
275 TrustManager[] trustAllCerts = new TrustManager[] {
276 new X509TrustManager() {
277
278 public void checkClientTrusted(final X509Certificate[] chain, final String authType) {}
279
280
281 public void checkServerTrusted(final X509Certificate[] chain, final String authType) {}
282
283
284 public X509Certificate[] getAcceptedIssuers() {
285 return null;
286 }
287 }
288 };
289
290 try {
291 SSLContext sslContext = SSLContext.getInstance("SSL");
292 sslContext.init(null, trustAllCerts, new SecureRandom());
293
294 SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
295
296 ((HttpsURLConnection) conn).setSSLSocketFactory(sslSocketFactory);
297 } catch (NoSuchAlgorithmException | KeyManagementException e1) {
298
299 }
300 }
301
302 return conn;
303 }
304 }