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.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   * 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             IOUtil.close( is );
102         }
103 
104         return retValue;
105     }
106 
107     protected InputStream getInputStream( Resource resource )
108         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
109     {
110         InputData inputData = new InputData();
111 
112         inputData.setResource( resource );
113 
114         try
115         {
116             fillInputData( inputData );
117         }
118         catch ( TransferFailedException e )
119         {
120             fireTransferError( resource, e, TransferEvent.REQUEST_GET );
121             cleanupGetTransfer( resource );
122             throw e;
123         }
124         catch ( ResourceDoesNotExistException e )
125         {
126             fireTransferError( resource, e, TransferEvent.REQUEST_GET );
127             cleanupGetTransfer( resource );
128             throw e;
129         }
130         catch ( AuthorizationException e )
131         {
132             fireTransferError( resource, e, TransferEvent.REQUEST_GET );
133             cleanupGetTransfer( resource );
134             throw e;
135         }
136         finally
137         {
138             if ( inputData.getInputStream() == null )
139             {
140                 cleanupGetTransfer( resource );
141             }
142         }
143 
144         return inputData.getInputStream();
145     }
146 
147     // source doesn't exist exception
148     public void put( File source, String resourceName )
149         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
150     {
151         Resource resource = new Resource( resourceName );
152 
153         firePutInitiated( resource, source );
154 
155         resource.setContentLength( source.length() );
156 
157         resource.setLastModified( source.lastModified() );
158 
159         OutputStream os = getOutputStream( resource );
160 
161         checkOutputStream( resource, os );
162 
163         putTransfer( resource, source, os, true );
164     }
165 
166     protected void checkOutputStream( Resource resource, OutputStream os )
167         throws TransferFailedException
168     {
169         if ( os == null )
170         {
171             TransferFailedException e =
172                 new TransferFailedException( getRepository().getUrl()
173                     + " - Could not open output stream for resource: '" + resource + "'" );
174             fireTransferError( resource, e, TransferEvent.REQUEST_PUT );
175             throw e;
176         }
177     }
178 
179     protected OutputStream getOutputStream( Resource resource )
180         throws TransferFailedException
181     {
182         OutputData outputData = new OutputData();
183 
184         outputData.setResource( resource );
185 
186         try
187         {
188             fillOutputData( outputData );
189         }
190         catch ( TransferFailedException e )
191         {
192             fireTransferError( resource, e, TransferEvent.REQUEST_PUT );
193 
194             throw e;
195         }
196         finally
197         {
198             if ( outputData.getOutputStream() == null )
199             {
200                 cleanupPutTransfer( resource );
201             }
202         }
203 
204         return outputData.getOutputStream();
205     }
206 
207     public boolean getIfNewerToStream( String resourceName, OutputStream stream, long timestamp )
208         throws ResourceDoesNotExistException, TransferFailedException, AuthorizationException
209     {
210         boolean retValue = false;
211 
212         Resource resource = new Resource( resourceName );
213 
214         fireGetInitiated( resource, null );
215 
216         InputStream is = getInputStream( resource );
217 
218         // always get if timestamp is 0 (ie, target doesn't exist), otherwise only if older than the remote file
219         if ( timestamp == 0 || timestamp < resource.getLastModified() )
220         {
221             retValue = true;
222 
223             checkInputStream( is, resource );
224 
225             fireGetStarted( resource, null );
226 
227             getTransfer( resource, stream, is, true, Integer.MAX_VALUE );
228 
229             fireGetCompleted( resource, null );
230         }
231         else
232         {
233             IOUtil.close( is );
234         }
235         
236         return retValue;
237     }
238 
239     public void getToStream( String resourceName, OutputStream stream )
240         throws ResourceDoesNotExistException, TransferFailedException, AuthorizationException
241     {
242         getIfNewerToStream( resourceName, stream, 0 );
243     }
244 
245     public void putFromStream( InputStream stream, String destination )
246         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
247     {
248         Resource resource = new Resource( destination );
249 
250         firePutInitiated( resource, null );
251 
252         putFromStream( stream, resource );
253     }
254 
255     public void putFromStream( InputStream stream, String destination, long contentLength, long lastModified )
256         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
257     {
258         Resource resource = new Resource( destination );
259 
260         firePutInitiated( resource, null );
261 
262         resource.setContentLength( contentLength );
263 
264         resource.setLastModified( lastModified );
265 
266         putFromStream( stream, resource );
267     }
268 
269     protected void putFromStream( InputStream stream, Resource resource )
270         throws TransferFailedException, AuthorizationException, ResourceDoesNotExistException
271     {
272         OutputStream os = getOutputStream( resource );
273 
274         checkOutputStream( resource, os );
275 
276         firePutStarted( resource, null );
277 
278         putTransfer( resource, stream, os, true );
279 
280         firePutCompleted( resource, null );
281     }
282 }