View Javadoc

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