View Javadoc
1   package org.apache.archiva.proxy;
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 org.apache.commons.io.FileUtils;
23  import org.apache.maven.wagon.ConnectionException;
24  import org.apache.maven.wagon.ResourceDoesNotExistException;
25  import org.apache.maven.wagon.TransferFailedException;
26  import org.apache.maven.wagon.Wagon;
27  import org.apache.maven.wagon.authentication.AuthenticationException;
28  import org.apache.maven.wagon.authentication.AuthenticationInfo;
29  import org.apache.maven.wagon.authorization.AuthorizationException;
30  import org.apache.maven.wagon.events.SessionListener;
31  import org.apache.maven.wagon.events.TransferListener;
32  import org.apache.maven.wagon.proxy.ProxyInfo;
33  import org.apache.maven.wagon.proxy.ProxyInfoProvider;
34  import org.apache.maven.wagon.repository.Repository;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  import org.springframework.stereotype.Service;
38  
39  import java.io.File;
40  import java.io.IOException;
41  import java.nio.charset.Charset;
42  import java.util.List;
43  
44  /**
45   * A dummy wagon implementation
46   */
47  @Service ("wagon#test")
48  public class WagonDelegate
49      implements Wagon
50  {
51      private Logger log = LoggerFactory.getLogger( WagonDelegate.class );
52  
53      private Wagon delegate;
54  
55      private String contentToGet;
56  
57      @Override
58      public void get( String resourceName, File destination )
59          throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
60      {
61          log.debug( ".get({}, {})", resourceName, destination );
62          delegate.get( resourceName, destination );
63          create( destination );
64      }
65  
66      @Override
67      public boolean getIfNewer( String resourceName, File destination, long timestamp )
68          throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
69      {
70          log.info( ".getIfNewer({}, {}, {})", resourceName, destination, timestamp );
71  
72          boolean result = delegate.getIfNewer( resourceName, destination, timestamp );
73          createIfMissing( destination );
74          return result;
75      }
76  
77      @Override
78      public void put( File source, String destination )
79          throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
80      {
81          delegate.put( source, destination );
82      }
83  
84      @Override
85      public void putDirectory( File sourceDirectory, String destinationDirectory )
86          throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
87      {
88          delegate.putDirectory( sourceDirectory, destinationDirectory );
89      }
90  
91      @Override
92      public boolean resourceExists( String resourceName )
93          throws TransferFailedException, AuthorizationException
94      {
95          return delegate.resourceExists( resourceName );
96      }
97  
98      @SuppressWarnings ("unchecked")
99      @Override
100     public List<String> getFileList( String destinationDirectory )
101         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
102     {
103         return delegate.getFileList( destinationDirectory );
104     }
105 
106     @Override
107     public boolean supportsDirectoryCopy()
108     {
109         return delegate.supportsDirectoryCopy();
110     }
111 
112     @Override
113     public void setTimeout( int val )
114     {
115         // ignore
116     }
117 
118     @Override
119     public int getTimeout()
120     {
121         return 0;
122     }
123 
124     @Override
125     public void setReadTimeout( int timeoutValue )
126     {
127         // ignore
128     }
129 
130     @Override
131     public int getReadTimeout()
132     {
133         return 0;
134     }
135 
136     @Override
137     public Repository getRepository()
138     {
139         return delegate.getRepository();
140     }
141 
142     @Override
143     public void connect( Repository source )
144         throws ConnectionException, AuthenticationException
145     {
146         delegate.connect( source );
147     }
148 
149     @Override
150     public void connect( Repository source, ProxyInfo proxyInfo )
151         throws ConnectionException, AuthenticationException
152     {
153         delegate.connect( source, proxyInfo );
154     }
155 
156     @Override
157     public void connect( Repository source, ProxyInfoProvider proxyInfoProvider )
158         throws ConnectionException, AuthenticationException
159     {
160         delegate.connect( source, proxyInfoProvider );
161     }
162 
163     @Override
164     public void connect( Repository source, AuthenticationInfo authenticationInfo )
165         throws ConnectionException, AuthenticationException
166     {
167         delegate.connect( source, authenticationInfo );
168     }
169 
170     @Override
171     public void connect( Repository source, AuthenticationInfo authenticationInfo, ProxyInfo proxyInfo )
172         throws ConnectionException, AuthenticationException
173     {
174         delegate.connect( source, authenticationInfo, proxyInfo );
175     }
176 
177     @Override
178     public void connect( Repository source, AuthenticationInfo authenticationInfo, ProxyInfoProvider proxyInfoProvider )
179         throws ConnectionException, AuthenticationException
180     {
181         delegate.connect( source, authenticationInfo, proxyInfoProvider );
182     }
183 
184     @SuppressWarnings ("deprecation")
185     @Override
186     public void openConnection()
187         throws ConnectionException, AuthenticationException
188     {
189         delegate.openConnection();
190     }
191 
192     @Override
193     public void disconnect()
194         throws ConnectionException
195     {
196         delegate.disconnect();
197     }
198 
199     @Override
200     public void addSessionListener( SessionListener listener )
201     {
202         delegate.addSessionListener( listener );
203     }
204 
205     @Override
206     public void removeSessionListener( SessionListener listener )
207     {
208         delegate.removeSessionListener( listener );
209     }
210 
211     @Override
212     public boolean hasSessionListener( SessionListener listener )
213     {
214         return delegate.hasSessionListener( listener );
215     }
216 
217     @Override
218     public void addTransferListener( TransferListener listener )
219     {
220         delegate.addTransferListener( listener );
221     }
222 
223     @Override
224     public void removeTransferListener( TransferListener listener )
225     {
226         delegate.removeTransferListener( listener );
227     }
228 
229     @Override
230     public boolean hasTransferListener( TransferListener listener )
231     {
232         return delegate.hasTransferListener( listener );
233     }
234 
235     @Override
236     public boolean isInteractive()
237     {
238         return delegate.isInteractive();
239     }
240 
241     @Override
242     public void setInteractive( boolean interactive )
243     {
244         delegate.setInteractive( interactive );
245     }
246 
247     public void setDelegate( Wagon delegate )
248     {
249         this.delegate = delegate;
250     }
251 
252     void setContentToGet( String content )
253     {
254         contentToGet = content;
255     }
256 
257     private void createIfMissing( File destination )
258     {
259         // since the mock won't actually copy a file, create an empty one to simulate file existence
260         if ( !destination.exists() )
261         {
262             create( destination );
263         }
264     }
265 
266     private void create( File destination )
267     {
268         try
269         {
270             destination.getParentFile().mkdirs();
271             if ( contentToGet == null )
272             {
273                 destination.createNewFile();
274             }
275             else
276             {
277                 FileUtils.writeStringToFile( new File( destination.getAbsolutePath() ), contentToGet,
278                                              Charset.defaultCharset() );
279             }
280         }
281         catch ( IOException e )
282         {
283             throw new RuntimeException( e.getMessage(), e );
284         }
285     }
286 }