Coverage Report - org.apache.maven.report.projectinfo.ProjectInfoReportUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ProjectInfoReportUtils
49%
23/47
33%
10/30
4,5
ProjectInfoReportUtils$1
0%
0/2
N/A
4,5
 
 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 679614 2008-07-24 23:43:50Z vsiveton $
 48  
  * @since 2.1
 49  
  */
 50  0
 public class ProjectInfoReportUtils
 51  
 {
 52  1
     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  1
         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  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  
                             /** {@inheritDoc} */
 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  
      * @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  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  
      * @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  4
         if ( StringUtils.isEmpty( link ) )
 196  
         {
 197  2
             return artifactId;
 198  
         }
 199  
 
 200  2
         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  3
         if ( StringUtils.isEmpty( url ) )
 210  
         {
 211  0
             return false;
 212  
         }
 213  
 
 214  3
         return URL_VALIDATOR.isValid( url );
 215  
     }
 216  
 }
 217