View Javadoc

1   package org.apache.maven.report.projectinfo;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Utilities methods.
46   *
47   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
48   * @version $Id: ProjectInfoReportUtils.java 817018 2009-09-20 12:30:57Z olamy $
49   * @since 2.1
50   */
51  public class ProjectInfoReportUtils
52  {
53      private static final UrlValidator URL_VALIDATOR = new UrlValidator( new String[] { "http", "https" } );
54  
55      /**
56       * Get the input stream using ISO-8859-1 as charset from an URL.
57       *
58       * @param url not null
59       * @param settings not null to handle proxy settings
60       * @return the ISO-8859-1 inputstream found.
61       * @throws IOException if any
62       * @see #getInputStream(URL, Settings, String)
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       * Get the input stream from an URL.
72       *
73       * @param url not null
74       * @param settings not null to handle proxy settings
75       * @param encoding the wanted encoding for the inputstream. If null, encoding will be "ISO-8859-1".
76       * @return the inputstream found depending the wanted encoding.
77       * @throws IOException if any
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                             /** {@inheritDoc} */
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      * @param factory not null
149      * @param artifact not null
150      * @param mavenProjectBuilder not null
151      * @param remoteRepositories not null
152      * @param localRepository not null
153      * @return the artifact url or null if an error occurred.
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      * @param artifactId not null
192      * @param link could be null
193      * @return the artifactId cell with or without a link pattern
194      * @see AbstractMavenReportRenderer#linkPatternedText(String)
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      * @param url not null
208      * @return <code>true</code> if the url is valid, <code>false</code> otherwise.
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