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.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   * Utilities methods.
45   *
46   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
47   * @version $Id: ProjectInfoReportUtils.java 679796 2008-07-25 13:14:34Z vsiveton $
48   * @since 2.1
49   */
50  public class ProjectInfoReportUtils
51  {
52      private static final UrlValidator URL_VALIDATOR = new UrlValidator( new String[] { "http", "https" } );
53  
54      /**
55       * Get the input stream using ISO-8859-1 as charset from an URL.
56       *
57       * @param url not null
58       * @param settings not null to handle proxy settings
59       * @return the ISO-8859-1 inputstream found.
60       * @throws IOException if any
61       * @see #getInputStream(URL, Settings, String)
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       * Get the input stream from an URL.
71       *
72       * @param url not null
73       * @param settings not null to handle proxy settings
74       * @param encoding the wanted encoding for the inputstream. If null, encoding will be "ISO-8859-1".
75       * @return the inputstream found depending the wanted encoding.
76       * @throws IOException if any
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                             /** {@inheritDoc} */
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      * @param factory not null
148      * @param artifact not null
149      * @param mavenProjectBuilder not null
150      * @param remoteRepositories not null
151      * @param localRepository not null
152      * @return the artifact url or null if an error occurred.
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      * @param artifactId not null
189      * @param link could be null
190      * @return the artifactId cell with or without a link pattern
191      * @see AbstractMavenReportRenderer#linkPatternedText(String)
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      * @param url not null
205      * @return <code>true</code> if the url is valid, <code>false</code> otherwise.
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