Coverage Report - org.apache.maven.index.updater.WagonHelper
 
Classes in this File Line Coverage Branch Coverage Complexity
WagonHelper
83 %
5/6
N/A
2,636
WagonHelper$WagonFetcher
35 %
23/65
33 %
4/12
2,636
WagonHelper$WagonFetcher$1
100 %
4/4
N/A
2,636
 
 1  
 package org.apache.maven.index.updater;
 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 org.apache.maven.wagon.ConnectionException;
 23  
 import org.apache.maven.wagon.ResourceDoesNotExistException;
 24  
 import org.apache.maven.wagon.Wagon;
 25  
 import org.apache.maven.wagon.WagonException;
 26  
 import org.apache.maven.wagon.authentication.AuthenticationException;
 27  
 import org.apache.maven.wagon.authentication.AuthenticationInfo;
 28  
 import org.apache.maven.wagon.authorization.AuthorizationException;
 29  
 import org.apache.maven.wagon.events.TransferListener;
 30  
 import org.apache.maven.wagon.proxy.ProxyInfo;
 31  
 import org.apache.maven.wagon.repository.Repository;
 32  
 import org.codehaus.plexus.PlexusContainer;
 33  
 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
 34  
 
 35  
 import java.io.File;
 36  
 import java.io.FileInputStream;
 37  
 import java.io.FileNotFoundException;
 38  
 import java.io.IOException;
 39  
 import java.io.InputStream;
 40  
 
 41  
 /**
 42  
  * This is a helper for obtaining Wagon based ResourceFetchers. Some Indexer integrations does have access to Wagon
 43  
  * already, so this is here just to help them. Since Wagon (et al) is just optional dependency, looking up this
 44  
  * component in integrations where Wagon is not present, should be avoided. This helper is rather limited, as it offers
 45  
  * only "HTTP" wagons! This is not made a Plexus component since SISU would crack in CLI, while trying to load up this
 46  
  * class, because of lacking Wagon classes from classpath!
 47  
  *
 48  
  * @author cstamas
 49  
  */
 50  
 public class WagonHelper
 51  
 {
 52  
     private final PlexusContainer plexusContainer;
 53  
 
 54  
     public WagonHelper( final PlexusContainer plexusContainer )
 55  3
     {
 56  3
         this.plexusContainer = plexusContainer;
 57  3
     }
 58  
 
 59  
     public WagonFetcher getWagonResourceFetcher( final TransferListener listener )
 60  
         throws ComponentLookupException
 61  
     {
 62  3
         return getWagonResourceFetcher( listener, null, null );
 63  
     }
 64  
 
 65  
     /**
 66  
      * @param listener
 67  
      * @param authenticationInfo
 68  
      * @param proxyInfo
 69  
      * @return
 70  
      * @throws ComponentLookupException
 71  
      * @deprecated use getWagonResourceFetcher with protocol argument
 72  
      */
 73  
     public WagonFetcher getWagonResourceFetcher( final TransferListener listener,
 74  
                                                  final AuthenticationInfo authenticationInfo,
 75  
                                                  final ProxyInfo proxyInfo )
 76  
         throws ComponentLookupException
 77  
     {
 78  
         // we limit ourselves to HTTP only
 79  3
         return new WagonFetcher( plexusContainer.lookup( Wagon.class, "http" ), listener, authenticationInfo,
 80  
                                  proxyInfo );
 81  
     }
 82  
 
 83  
     /**
 84  
      * @param listener
 85  
      * @param authenticationInfo
 86  
      * @param proxyInfo
 87  
      * @param protocol           protocol supported by wagon http/https
 88  
      * @return
 89  
      * @throws ComponentLookupException
 90  
      * @since 4.1.3
 91  
      */
 92  
     public WagonFetcher getWagonResourceFetcher( final TransferListener listener,
 93  
                                                  final AuthenticationInfo authenticationInfo, final ProxyInfo proxyInfo,
 94  
                                                  String protocol )
 95  
         throws ComponentLookupException
 96  
     {
 97  0
         return new WagonFetcher( plexusContainer.lookup( Wagon.class, protocol ), listener, authenticationInfo,
 98  
                                  proxyInfo );
 99  
     }
 100  
 
 101  
     public static class WagonFetcher
 102  
         implements ResourceFetcher
 103  
     {
 104  
         private final TransferListener listener;
 105  
 
 106  
         private final AuthenticationInfo authenticationInfo;
 107  
 
 108  
         private final ProxyInfo proxyInfo;
 109  
 
 110  
         private final Wagon wagon;
 111  
 
 112  
         public WagonFetcher( final Wagon wagon, final TransferListener listener,
 113  
                              final AuthenticationInfo authenticationInfo, final ProxyInfo proxyInfo )
 114  3
         {
 115  3
             this.wagon = wagon;
 116  3
             this.listener = listener;
 117  3
             this.authenticationInfo = authenticationInfo;
 118  3
             this.proxyInfo = proxyInfo;
 119  3
         }
 120  
 
 121  
         public void connect( final String id, final String url )
 122  
             throws IOException
 123  
         {
 124  3
             Repository repository = new Repository( id, url );
 125  
 
 126  
             try
 127  
             {
 128  
                 // wagon = wagonManager.getWagon( repository );
 129  
 
 130  3
                 if ( listener != null )
 131  
                 {
 132  0
                     wagon.addTransferListener( listener );
 133  
                 }
 134  
 
 135  
                 // when working in the context of Maven, the WagonManager is already
 136  
                 // populated with proxy information from the Maven environment
 137  
 
 138  3
                 if ( authenticationInfo != null )
 139  
                 {
 140  0
                     if ( proxyInfo != null )
 141  
                     {
 142  0
                         wagon.connect( repository, authenticationInfo, proxyInfo );
 143  
                     }
 144  
                     else
 145  
                     {
 146  0
                         wagon.connect( repository, authenticationInfo );
 147  
                     }
 148  
                 }
 149  
                 else
 150  
                 {
 151  3
                     if ( proxyInfo != null )
 152  
                     {
 153  0
                         wagon.connect( repository, proxyInfo );
 154  
                     }
 155  
                     else
 156  
                     {
 157  3
                         wagon.connect( repository );
 158  
                     }
 159  
                 }
 160  
             }
 161  0
             catch ( AuthenticationException ex )
 162  
             {
 163  0
                 String msg = "Authentication exception connecting to " + repository;
 164  0
                 logError( msg, ex );
 165  0
                 IOException ioException = new IOException( msg );
 166  0
                 ioException.initCause( ex );
 167  0
                 throw ioException;
 168  
             }
 169  0
             catch ( WagonException ex )
 170  
             {
 171  0
                 String msg = "Wagon exception connecting to " + repository;
 172  0
                 logError( msg, ex );
 173  0
                 IOException ioException = new IOException( msg );
 174  0
                 ioException.initCause( ex );
 175  0
                 throw ioException;
 176  3
             }
 177  3
         }
 178  
 
 179  
         public void disconnect()
 180  
             throws IOException
 181  
         {
 182  3
             if ( wagon != null )
 183  
             {
 184  
                 try
 185  
                 {
 186  3
                     wagon.disconnect();
 187  
                 }
 188  0
                 catch ( ConnectionException ex )
 189  
                 {
 190  0
                     IOException ioe = new IOException( ex.toString() );
 191  0
                     ioe.initCause( ex );
 192  0
                     throw ioe;
 193  3
                 }
 194  
             }
 195  3
         }
 196  
 
 197  
         public InputStream retrieve( String name )
 198  
             throws IOException, FileNotFoundException
 199  
         {
 200  6
             final File target = File.createTempFile( name, "" );
 201  6
             retrieve( name, target );
 202  6
             return new FileInputStream( target )
 203  6
             {
 204  
                 @Override
 205  
                 public void close()
 206  
                     throws IOException
 207  
                 {
 208  12
                     super.close();
 209  12
                     target.delete();
 210  12
                 }
 211  
             };
 212  
         }
 213  
 
 214  
         public void retrieve( final String name, final File targetFile )
 215  
             throws IOException, FileNotFoundException
 216  
         {
 217  
             try
 218  
             {
 219  6
                 wagon.get( name, targetFile );
 220  
             }
 221  0
             catch ( AuthorizationException e )
 222  
             {
 223  0
                 String msg = "Authorization exception retrieving " + name;
 224  0
                 logError( msg, e );
 225  0
                 IOException ioException = new IOException( msg );
 226  0
                 ioException.initCause( e );
 227  0
                 throw ioException;
 228  
             }
 229  0
             catch ( ResourceDoesNotExistException e )
 230  
             {
 231  0
                 String msg = "Resource " + name + " does not exist";
 232  0
                 logError( msg, e );
 233  0
                 FileNotFoundException fileNotFoundException = new FileNotFoundException( msg );
 234  0
                 fileNotFoundException.initCause( e );
 235  0
                 throw fileNotFoundException;
 236  
             }
 237  0
             catch ( WagonException e )
 238  
             {
 239  0
                 String msg = "Transfer for " + name + " failed";
 240  0
                 logError( msg, e );
 241  0
                 IOException ioException = new IOException( msg + "; " + e.getMessage() );
 242  0
                 ioException.initCause( e );
 243  0
                 throw ioException;
 244  6
             }
 245  6
         }
 246  
 
 247  
         private void logError( final String msg, final Exception ex )
 248  
         {
 249  0
             if ( listener != null )
 250  
             {
 251  0
                 listener.debug( msg + "; " + ex.getMessage() );
 252  
             }
 253  0
         }
 254  
     }
 255  
 }