View Javadoc

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      {
56          this.plexusContainer = plexusContainer;
57      }
58  
59      public WagonFetcher getWagonResourceFetcher( final TransferListener listener )
60          throws ComponentLookupException
61      {
62          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          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          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         {
115             this.wagon = wagon;
116             this.listener = listener;
117             this.authenticationInfo = authenticationInfo;
118             this.proxyInfo = proxyInfo;
119         }
120 
121         public void connect( final String id, final String url )
122             throws IOException
123         {
124             Repository repository = new Repository( id, url );
125 
126             try
127             {
128                 // wagon = wagonManager.getWagon( repository );
129 
130                 if ( listener != null )
131                 {
132                     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                 if ( authenticationInfo != null )
139                 {
140                     if ( proxyInfo != null )
141                     {
142                         wagon.connect( repository, authenticationInfo, proxyInfo );
143                     }
144                     else
145                     {
146                         wagon.connect( repository, authenticationInfo );
147                     }
148                 }
149                 else
150                 {
151                     if ( proxyInfo != null )
152                     {
153                         wagon.connect( repository, proxyInfo );
154                     }
155                     else
156                     {
157                         wagon.connect( repository );
158                     }
159                 }
160             }
161             catch ( AuthenticationException ex )
162             {
163                 String msg = "Authentication exception connecting to " + repository;
164                 logError( msg, ex );
165                 IOException ioException = new IOException( msg );
166                 ioException.initCause( ex );
167                 throw ioException;
168             }
169             catch ( WagonException ex )
170             {
171                 String msg = "Wagon exception connecting to " + repository;
172                 logError( msg, ex );
173                 IOException ioException = new IOException( msg );
174                 ioException.initCause( ex );
175                 throw ioException;
176             }
177         }
178 
179         public void disconnect()
180             throws IOException
181         {
182             if ( wagon != null )
183             {
184                 try
185                 {
186                     wagon.disconnect();
187                 }
188                 catch ( ConnectionException ex )
189                 {
190                     IOException ioe = new IOException( ex.toString() );
191                     ioe.initCause( ex );
192                     throw ioe;
193                 }
194             }
195         }
196 
197         public InputStream retrieve( String name )
198             throws IOException, FileNotFoundException
199         {
200             final File target = File.createTempFile( name, "" );
201             retrieve( name, target );
202             return new FileInputStream( target )
203             {
204                 @Override
205                 public void close()
206                     throws IOException
207                 {
208                     super.close();
209                     target.delete();
210                 }
211             };
212         }
213 
214         public void retrieve( final String name, final File targetFile )
215             throws IOException, FileNotFoundException
216         {
217             try
218             {
219                 wagon.get( name, targetFile );
220             }
221             catch ( AuthorizationException e )
222             {
223                 String msg = "Authorization exception retrieving " + name;
224                 logError( msg, e );
225                 IOException ioException = new IOException( msg );
226                 ioException.initCause( e );
227                 throw ioException;
228             }
229             catch ( ResourceDoesNotExistException e )
230             {
231                 String msg = "Resource " + name + " does not exist";
232                 logError( msg, e );
233                 FileNotFoundException fileNotFoundException = new FileNotFoundException( msg );
234                 fileNotFoundException.initCause( e );
235                 throw fileNotFoundException;
236             }
237             catch ( WagonException e )
238             {
239                 String msg = "Transfer for " + name + " failed";
240                 logError( msg, e );
241                 IOException ioException = new IOException( msg + "; " + e.getMessage() );
242                 ioException.initCause( e );
243                 throw ioException;
244             }
245         }
246 
247         private void logError( final String msg, final Exception ex )
248         {
249             if ( listener != null )
250             {
251                 listener.debug( msg + "; " + ex.getMessage() );
252             }
253         }
254     }
255 }