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 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 }