View Javadoc
1   package org.apache.maven.wagon;
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 java.io.File;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.OutputStream;
26  
27  import org.apache.maven.wagon.authorization.AuthorizationException;
28  import org.apache.maven.wagon.events.TransferEvent;
29  import org.apache.maven.wagon.resource.Resource;
30  
31  /**
32   * Base class for wagon which provide stream based API.
33   *
34   * @author <a href="mailto:michal@codehaus.org">Michal Maczka</a>
35   *
36   */
37  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          getIfNewer( resourceName, destination, 0 );
62      }
63  
64      protected void checkInputStream( InputStream is, Resource resource )
65          throws TransferFailedException
66      {
67          if ( is == null )
68          {
69              TransferFailedException e =
70                  new TransferFailedException( getRepository().getUrl()
71                      + " - Could not open input stream for resource: '" + resource + "'" );
72              fireTransferError( resource, e, TransferEvent.REQUEST_GET );
73              throw e;
74          }
75      }
76  
77      public boolean getIfNewer( String resourceName, File destination, long timestamp )
78          throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
79      {
80          boolean retValue = false;
81  
82          Resource resource = new Resource( resourceName );
83  
84          fireGetInitiated( resource, destination );
85  
86          resource.setLastModified( timestamp );
87          
88          InputStream is = getInputStream( resource );
89  
90          // always get if timestamp is 0 (ie, target doesn't exist), otherwise only if older than the remote file
91          if ( timestamp == 0 || timestamp < resource.getLastModified() )
92          {
93              retValue = true;
94  
95              checkInputStream( is, resource );
96  
97              getTransfer( resource, destination, is );
98          }
99          else
100         {
101             try
102             {
103                 if ( is != null )
104                 {
105                     is.close();
106                 }
107             }
108             catch ( final IOException e )
109             {
110                 throw new TransferFailedException( "Failure transferring " + resourceName, e );
111             }
112         }
113 
114         return retValue;
115     }
116 
117     protected InputStream getInputStream( Resource resource )
118         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
119     {
120         InputData inputData = new InputData();
121 
122         inputData.setResource( resource );
123 
124         try
125         {
126             fillInputData( inputData );
127         }
128         catch ( TransferFailedException e )
129         {
130             fireTransferError( resource, e, TransferEvent.REQUEST_GET );
131             cleanupGetTransfer( resource );
132             throw e;
133         }
134         catch ( ResourceDoesNotExistException e )
135         {
136             fireTransferError( resource, e, TransferEvent.REQUEST_GET );
137             cleanupGetTransfer( resource );
138             throw e;
139         }
140         catch ( AuthorizationException e )
141         {
142             fireTransferError( resource, e, TransferEvent.REQUEST_GET );
143             cleanupGetTransfer( resource );
144             throw e;
145         }
146         finally
147         {
148             if ( inputData.getInputStream() == null )
149             {
150                 cleanupGetTransfer( resource );
151             }
152         }
153 
154         return inputData.getInputStream();
155     }
156 
157     // source doesn't exist exception
158     public void put( File source, String resourceName )
159         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
160     {
161         Resource resource = new Resource( resourceName );
162 
163         firePutInitiated( resource, source );
164 
165         resource.setContentLength( source.length() );
166 
167         resource.setLastModified( source.lastModified() );
168 
169         OutputStream os = getOutputStream( resource );
170 
171         checkOutputStream( resource, os );
172 
173         putTransfer( resource, source, os, true );
174     }
175 
176     protected void checkOutputStream( Resource resource, OutputStream os )
177         throws TransferFailedException
178     {
179         if ( os == null )
180         {
181             TransferFailedException e =
182                 new TransferFailedException( getRepository().getUrl()
183                     + " - Could not open output stream for resource: '" + resource + "'" );
184             fireTransferError( resource, e, TransferEvent.REQUEST_PUT );
185             throw e;
186         }
187     }
188 
189     protected OutputStream getOutputStream( Resource resource )
190         throws TransferFailedException
191     {
192         OutputData outputData = new OutputData();
193 
194         outputData.setResource( resource );
195 
196         try
197         {
198             fillOutputData( outputData );
199         }
200         catch ( TransferFailedException e )
201         {
202             fireTransferError( resource, e, TransferEvent.REQUEST_PUT );
203 
204             throw e;
205         }
206         finally
207         {
208             if ( outputData.getOutputStream() == null )
209             {
210                 cleanupPutTransfer( resource );
211             }
212         }
213 
214         return outputData.getOutputStream();
215     }
216 
217     public boolean getIfNewerToStream( String resourceName, OutputStream stream, long timestamp )
218         throws ResourceDoesNotExistException, TransferFailedException, AuthorizationException
219     {
220         boolean retValue = false;
221 
222         Resource resource = new Resource( resourceName );
223 
224         fireGetInitiated( resource, null );
225 
226         InputStream is = getInputStream( resource );
227 
228         // always get if timestamp is 0 (ie, target doesn't exist), otherwise only if older than the remote file
229         if ( timestamp == 0 || timestamp < resource.getLastModified() )
230         {
231             retValue = true;
232 
233             checkInputStream( is, resource );
234 
235             fireGetStarted( resource, null );
236 
237             getTransfer( resource, stream, is, true, Integer.MAX_VALUE );
238 
239             fireGetCompleted( resource, null );
240         }
241         else
242         {
243             try
244             {
245                 if ( is != null )
246                 {
247                     is.close();
248                 }
249             }
250             catch ( final IOException e )
251             {
252                 throw new TransferFailedException( "Failure transferring " + resourceName, e );
253             }
254         }
255         
256         return retValue;
257     }
258 
259     public void getToStream( String resourceName, OutputStream stream )
260         throws ResourceDoesNotExistException, TransferFailedException, AuthorizationException
261     {
262         getIfNewerToStream( resourceName, stream, 0 );
263     }
264 
265     public void putFromStream( InputStream stream, String destination )
266         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
267     {
268         Resource resource = new Resource( destination );
269 
270         firePutInitiated( resource, null );
271 
272         putFromStream( stream, resource );
273     }
274 
275     public void putFromStream( InputStream stream, String destination, long contentLength, long lastModified )
276         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
277     {
278         Resource resource = new Resource( destination );
279 
280         firePutInitiated( resource, null );
281 
282         resource.setContentLength( contentLength );
283 
284         resource.setLastModified( lastModified );
285 
286         putFromStream( stream, resource );
287     }
288 
289     protected void putFromStream( InputStream stream, Resource resource )
290         throws TransferFailedException, AuthorizationException, ResourceDoesNotExistException
291     {
292         OutputStream os = getOutputStream( resource );
293 
294         checkOutputStream( resource, os );
295 
296         firePutStarted( resource, null );
297 
298         putTransfer( resource, stream, os, true );
299 
300         firePutCompleted( resource, null );
301     }
302 }