1 | |
package org.apache.maven.wagon; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.io.InputStream; |
24 | |
import java.io.OutputStream; |
25 | |
|
26 | |
import org.apache.maven.wagon.authorization.AuthorizationException; |
27 | |
import org.apache.maven.wagon.events.TransferEvent; |
28 | |
import org.apache.maven.wagon.resource.Resource; |
29 | |
import org.codehaus.plexus.util.IOUtil; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 41 | public abstract class StreamWagon |
38 | |
extends AbstractWagon |
39 | |
implements StreamingWagon |
40 | |
{ |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public abstract void fillInputData( InputData inputData ) |
46 | |
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException; |
47 | |
|
48 | |
public abstract void fillOutputData( OutputData outputData ) |
49 | |
throws TransferFailedException; |
50 | |
|
51 | |
public abstract void closeConnection() |
52 | |
throws ConnectionException; |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public void get( String resourceName, File destination ) |
59 | |
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
60 | |
{ |
61 | 5 | getIfNewer( resourceName, destination, 0 ); |
62 | 2 | } |
63 | |
|
64 | |
protected void checkInputStream( InputStream is, Resource resource ) |
65 | |
throws TransferFailedException |
66 | |
{ |
67 | 9 | if ( is == null ) |
68 | |
{ |
69 | 1 | TransferFailedException e = |
70 | |
new TransferFailedException( getRepository().getUrl() |
71 | |
+ " - Could not open input stream for resource: '" + resource + "'" ); |
72 | 1 | fireTransferError( resource, e, TransferEvent.REQUEST_GET ); |
73 | 1 | throw e; |
74 | |
} |
75 | 8 | } |
76 | |
|
77 | |
public boolean getIfNewer( String resourceName, File destination, long timestamp ) |
78 | |
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
79 | |
{ |
80 | 8 | boolean retValue = false; |
81 | |
|
82 | 8 | Resource resource = new Resource( resourceName ); |
83 | |
|
84 | 8 | fireGetInitiated( resource, destination ); |
85 | |
|
86 | 8 | resource.setLastModified( timestamp ); |
87 | |
|
88 | 8 | InputStream is = getInputStream( resource ); |
89 | |
|
90 | |
|
91 | 8 | if ( timestamp == 0 || timestamp < resource.getLastModified() ) |
92 | |
{ |
93 | 6 | retValue = true; |
94 | |
|
95 | 6 | checkInputStream( is, resource ); |
96 | |
|
97 | 6 | getTransfer( resource, destination, is ); |
98 | |
} |
99 | |
else |
100 | |
{ |
101 | 2 | IOUtil.close( is ); |
102 | |
} |
103 | |
|
104 | 5 | return retValue; |
105 | |
} |
106 | |
|
107 | |
protected InputStream getInputStream( Resource resource ) |
108 | |
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
109 | |
{ |
110 | 16 | InputData inputData = new InputData(); |
111 | |
|
112 | 16 | inputData.setResource( resource ); |
113 | |
|
114 | |
try |
115 | |
{ |
116 | 16 | fillInputData( inputData ); |
117 | |
} |
118 | 1 | catch ( TransferFailedException e ) |
119 | |
{ |
120 | 1 | fireTransferError( resource, e, TransferEvent.REQUEST_GET ); |
121 | 1 | cleanupGetTransfer( resource ); |
122 | 1 | throw e; |
123 | |
} |
124 | 1 | catch ( ResourceDoesNotExistException e ) |
125 | |
{ |
126 | 1 | fireTransferError( resource, e, TransferEvent.REQUEST_GET ); |
127 | 1 | cleanupGetTransfer( resource ); |
128 | 1 | throw e; |
129 | |
} |
130 | 1 | catch ( AuthorizationException e ) |
131 | |
{ |
132 | 1 | fireTransferError( resource, e, TransferEvent.REQUEST_GET ); |
133 | 1 | cleanupGetTransfer( resource ); |
134 | 1 | throw e; |
135 | |
} |
136 | |
finally |
137 | |
{ |
138 | 16 | if ( inputData.getInputStream() == null ) |
139 | |
{ |
140 | 4 | cleanupGetTransfer( resource ); |
141 | |
} |
142 | |
} |
143 | |
|
144 | 13 | return inputData.getInputStream(); |
145 | |
} |
146 | |
|
147 | |
|
148 | |
public void put( File source, String resourceName ) |
149 | |
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
150 | |
{ |
151 | 3 | Resource resource = new Resource( resourceName ); |
152 | |
|
153 | 3 | firePutInitiated( resource, source ); |
154 | |
|
155 | 3 | resource.setContentLength( source.length() ); |
156 | |
|
157 | 3 | resource.setLastModified( source.lastModified() ); |
158 | |
|
159 | 3 | OutputStream os = getOutputStream( resource ); |
160 | |
|
161 | 3 | checkOutputStream( resource, os ); |
162 | |
|
163 | 2 | putTransfer( resource, source, os, true ); |
164 | 2 | } |
165 | |
|
166 | |
protected void checkOutputStream( Resource resource, OutputStream os ) |
167 | |
throws TransferFailedException |
168 | |
{ |
169 | 6 | if ( os == null ) |
170 | |
{ |
171 | 2 | TransferFailedException e = |
172 | |
new TransferFailedException( getRepository().getUrl() |
173 | |
+ " - Could not open output stream for resource: '" + resource + "'" ); |
174 | 2 | fireTransferError( resource, e, TransferEvent.REQUEST_PUT ); |
175 | 2 | throw e; |
176 | |
} |
177 | 4 | } |
178 | |
|
179 | |
protected OutputStream getOutputStream( Resource resource ) |
180 | |
throws TransferFailedException |
181 | |
{ |
182 | 7 | OutputData outputData = new OutputData(); |
183 | |
|
184 | 7 | outputData.setResource( resource ); |
185 | |
|
186 | |
try |
187 | |
{ |
188 | 7 | fillOutputData( outputData ); |
189 | |
} |
190 | 1 | catch ( TransferFailedException e ) |
191 | |
{ |
192 | 1 | fireTransferError( resource, e, TransferEvent.REQUEST_PUT ); |
193 | |
|
194 | 1 | throw e; |
195 | |
} |
196 | |
finally |
197 | |
{ |
198 | 7 | if ( outputData.getOutputStream() == null ) |
199 | |
{ |
200 | 3 | cleanupPutTransfer( resource ); |
201 | |
} |
202 | |
} |
203 | |
|
204 | 6 | return outputData.getOutputStream(); |
205 | |
} |
206 | |
|
207 | |
public boolean getIfNewerToStream( String resourceName, OutputStream stream, long timestamp ) |
208 | |
throws ResourceDoesNotExistException, TransferFailedException, AuthorizationException |
209 | |
{ |
210 | 8 | boolean retValue = false; |
211 | |
|
212 | 8 | Resource resource = new Resource( resourceName ); |
213 | |
|
214 | 8 | fireGetInitiated( resource, null ); |
215 | |
|
216 | 8 | InputStream is = getInputStream( resource ); |
217 | |
|
218 | |
|
219 | 5 | if ( timestamp == 0 || timestamp < resource.getLastModified() ) |
220 | |
{ |
221 | 3 | retValue = true; |
222 | |
|
223 | 3 | checkInputStream( is, resource ); |
224 | |
|
225 | 2 | fireGetStarted( resource, null ); |
226 | |
|
227 | 2 | getTransfer( resource, stream, is, true, Integer.MAX_VALUE ); |
228 | |
|
229 | 2 | fireGetCompleted( resource, null ); |
230 | |
} |
231 | |
else |
232 | |
{ |
233 | 2 | IOUtil.close( is ); |
234 | |
} |
235 | |
|
236 | 4 | return retValue; |
237 | |
} |
238 | |
|
239 | |
public void getToStream( String resourceName, OutputStream stream ) |
240 | |
throws ResourceDoesNotExistException, TransferFailedException, AuthorizationException |
241 | |
{ |
242 | 5 | getIfNewerToStream( resourceName, stream, 0 ); |
243 | 1 | } |
244 | |
|
245 | |
public void putFromStream( InputStream stream, String destination ) |
246 | |
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
247 | |
{ |
248 | 3 | Resource resource = new Resource( destination ); |
249 | |
|
250 | 3 | firePutInitiated( resource, null ); |
251 | |
|
252 | 3 | putFromStream( stream, resource ); |
253 | 1 | } |
254 | |
|
255 | |
public void putFromStream( InputStream stream, String destination, long contentLength, long lastModified ) |
256 | |
throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException |
257 | |
{ |
258 | 1 | Resource resource = new Resource( destination ); |
259 | |
|
260 | 1 | firePutInitiated( resource, null ); |
261 | |
|
262 | 1 | resource.setContentLength( contentLength ); |
263 | |
|
264 | 1 | resource.setLastModified( lastModified ); |
265 | |
|
266 | 1 | putFromStream( stream, resource ); |
267 | 1 | } |
268 | |
|
269 | |
private void putFromStream( InputStream stream, Resource resource ) |
270 | |
throws TransferFailedException, AuthorizationException, ResourceDoesNotExistException |
271 | |
{ |
272 | 4 | OutputStream os = getOutputStream( resource ); |
273 | |
|
274 | 3 | checkOutputStream( resource, os ); |
275 | |
|
276 | 2 | firePutStarted( resource, null ); |
277 | |
|
278 | 2 | putTransfer( resource, stream, os, true ); |
279 | |
|
280 | 2 | firePutCompleted( resource, null ); |
281 | 2 | } |
282 | |
} |