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