1 package org.apache.maven.index.updater;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
43
44
45
46
47
48
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
67
68
69
70
71
72
73 public WagonFetcher getWagonResourceFetcher( final TransferListener listener,
74 final AuthenticationInfo authenticationInfo,
75 final ProxyInfo proxyInfo )
76 throws ComponentLookupException
77 {
78
79 return new WagonFetcher( plexusContainer.lookup( Wagon.class, "http" ), listener, authenticationInfo,
80 proxyInfo );
81 }
82
83
84
85
86
87
88
89
90
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
129
130 if ( listener != null )
131 {
132 wagon.addTransferListener( listener );
133 }
134
135
136
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 target.deleteOnExit();
202 retrieve( name, target );
203 return new FileInputStream( target )
204 {
205 @Override
206 public void close()
207 throws IOException
208 {
209 super.close();
210 target.delete();
211 }
212 };
213 }
214
215 public void retrieve( final String name, final File targetFile )
216 throws IOException, FileNotFoundException
217 {
218 try
219 {
220 wagon.get( name, targetFile );
221 }
222 catch ( AuthorizationException e )
223 {
224 targetFile.delete();
225 String msg = "Authorization exception retrieving " + name;
226 logError( msg, e );
227 IOException ioException = new IOException( msg );
228 ioException.initCause( e );
229 throw ioException;
230 }
231 catch ( ResourceDoesNotExistException e )
232 {
233 targetFile.delete();
234 String msg = "Resource " + name + " does not exist";
235 logError( msg, e );
236 FileNotFoundException fileNotFoundException = new FileNotFoundException( msg );
237 fileNotFoundException.initCause( e );
238 throw fileNotFoundException;
239 }
240 catch ( WagonException e )
241 {
242 targetFile.delete();
243 String msg = "Transfer for " + name + " failed";
244 logError( msg, e );
245 IOException ioException = new IOException( msg + "; " + e.getMessage() );
246 ioException.initCause( e );
247 throw ioException;
248 }
249 }
250
251 private void logError( final String msg, final Exception ex )
252 {
253 if ( listener != null )
254 {
255 listener.debug( msg + "; " + ex.getMessage() );
256 }
257 }
258 }
259 }