You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WagonDelegate.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. package org.apache.archiva.proxy;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.maven.wagon.ConnectionException;
  21. import org.apache.maven.wagon.ResourceDoesNotExistException;
  22. import org.apache.maven.wagon.TransferFailedException;
  23. import org.apache.maven.wagon.Wagon;
  24. import org.apache.maven.wagon.authentication.AuthenticationException;
  25. import org.apache.maven.wagon.authentication.AuthenticationInfo;
  26. import org.apache.maven.wagon.authorization.AuthorizationException;
  27. import org.apache.maven.wagon.events.SessionListener;
  28. import org.apache.maven.wagon.events.TransferListener;
  29. import org.apache.maven.wagon.proxy.ProxyInfo;
  30. import org.apache.maven.wagon.proxy.ProxyInfoProvider;
  31. import org.apache.maven.wagon.repository.Repository;
  32. import org.slf4j.Logger;
  33. import org.slf4j.LoggerFactory;
  34. import org.springframework.stereotype.Service;
  35. import java.io.File;
  36. import java.io.IOException;
  37. import java.nio.charset.Charset;
  38. import java.nio.file.Files;
  39. import java.nio.file.Path;
  40. import java.util.List;
  41. /**
  42. * A dummy wagon implementation
  43. */
  44. @Service ("wagon#test")
  45. public class WagonDelegate
  46. implements Wagon
  47. {
  48. private Logger log = LoggerFactory.getLogger( WagonDelegate.class );
  49. private Wagon delegate;
  50. private String contentToGet;
  51. @Override
  52. public void get( String resourceName, File destination )
  53. throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
  54. {
  55. log.debug( ".get({}, {})", resourceName, destination );
  56. delegate.get( resourceName, destination );
  57. create( destination.toPath() );
  58. }
  59. @Override
  60. public boolean getIfNewer( String resourceName, File destination, long timestamp )
  61. throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
  62. {
  63. log.info( ".getIfNewer({}, {}, {})", resourceName, destination, timestamp );
  64. boolean result = delegate.getIfNewer( resourceName, destination, timestamp );
  65. createIfMissing( destination.toPath() );
  66. return result;
  67. }
  68. @Override
  69. public void put( File source, String destination )
  70. throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
  71. {
  72. delegate.put( source, destination );
  73. }
  74. @Override
  75. public void putDirectory( File sourceDirectory, String destinationDirectory )
  76. throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
  77. {
  78. delegate.putDirectory( sourceDirectory, destinationDirectory );
  79. }
  80. @Override
  81. public boolean resourceExists( String resourceName )
  82. throws TransferFailedException, AuthorizationException
  83. {
  84. return delegate.resourceExists( resourceName );
  85. }
  86. @SuppressWarnings ("unchecked")
  87. @Override
  88. public List<String> getFileList( String destinationDirectory )
  89. throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
  90. {
  91. return delegate.getFileList( destinationDirectory );
  92. }
  93. @Override
  94. public boolean supportsDirectoryCopy()
  95. {
  96. return delegate.supportsDirectoryCopy();
  97. }
  98. @Override
  99. public void setTimeout( int val )
  100. {
  101. // ignore
  102. }
  103. @Override
  104. public int getTimeout()
  105. {
  106. return 0;
  107. }
  108. @Override
  109. public void setReadTimeout( int timeoutValue )
  110. {
  111. // ignore
  112. }
  113. @Override
  114. public int getReadTimeout()
  115. {
  116. return 0;
  117. }
  118. @Override
  119. public Repository getRepository()
  120. {
  121. return delegate.getRepository();
  122. }
  123. @Override
  124. public void connect( Repository source )
  125. throws ConnectionException, AuthenticationException
  126. {
  127. delegate.connect( source );
  128. }
  129. @Override
  130. public void connect( Repository source, ProxyInfo proxyInfo )
  131. throws ConnectionException, AuthenticationException
  132. {
  133. delegate.connect( source, proxyInfo );
  134. }
  135. @Override
  136. public void connect( Repository source, ProxyInfoProvider proxyInfoProvider )
  137. throws ConnectionException, AuthenticationException
  138. {
  139. delegate.connect( source, proxyInfoProvider );
  140. }
  141. @Override
  142. public void connect( Repository source, AuthenticationInfo authenticationInfo )
  143. throws ConnectionException, AuthenticationException
  144. {
  145. delegate.connect( source, authenticationInfo );
  146. }
  147. @Override
  148. public void connect( Repository source, AuthenticationInfo authenticationInfo, ProxyInfo proxyInfo )
  149. throws ConnectionException, AuthenticationException
  150. {
  151. delegate.connect( source, authenticationInfo, proxyInfo );
  152. }
  153. @Override
  154. public void connect( Repository source, AuthenticationInfo authenticationInfo, ProxyInfoProvider proxyInfoProvider )
  155. throws ConnectionException, AuthenticationException
  156. {
  157. delegate.connect( source, authenticationInfo, proxyInfoProvider );
  158. }
  159. @SuppressWarnings ("deprecation")
  160. @Override
  161. public void openConnection()
  162. throws ConnectionException, AuthenticationException
  163. {
  164. delegate.openConnection();
  165. }
  166. @Override
  167. public void disconnect()
  168. throws ConnectionException
  169. {
  170. delegate.disconnect();
  171. }
  172. @Override
  173. public void addSessionListener( SessionListener listener )
  174. {
  175. delegate.addSessionListener( listener );
  176. }
  177. @Override
  178. public void removeSessionListener( SessionListener listener )
  179. {
  180. delegate.removeSessionListener( listener );
  181. }
  182. @Override
  183. public boolean hasSessionListener( SessionListener listener )
  184. {
  185. return delegate.hasSessionListener( listener );
  186. }
  187. @Override
  188. public void addTransferListener( TransferListener listener )
  189. {
  190. delegate.addTransferListener( listener );
  191. }
  192. @Override
  193. public void removeTransferListener( TransferListener listener )
  194. {
  195. delegate.removeTransferListener( listener );
  196. }
  197. @Override
  198. public boolean hasTransferListener( TransferListener listener )
  199. {
  200. return delegate.hasTransferListener( listener );
  201. }
  202. @Override
  203. public boolean isInteractive()
  204. {
  205. return delegate.isInteractive();
  206. }
  207. @Override
  208. public void setInteractive( boolean interactive )
  209. {
  210. delegate.setInteractive( interactive );
  211. }
  212. public void setDelegate( Wagon delegate )
  213. {
  214. this.delegate = delegate;
  215. }
  216. void setContentToGet( String content )
  217. {
  218. contentToGet = content;
  219. }
  220. private void createIfMissing( Path destination )
  221. {
  222. // since the mock won't actually copy a file, create an empty one to simulate file existence
  223. if ( !Files.exists(destination) )
  224. {
  225. create( destination );
  226. }
  227. }
  228. private void create( Path destination )
  229. {
  230. try
  231. {
  232. Files.createDirectories(destination.getParent());
  233. if ( contentToGet == null )
  234. {
  235. Files.createFile(destination);
  236. }
  237. else
  238. {
  239. org.apache.archiva.common.utils.FileUtils.writeStringToFile(destination, Charset.defaultCharset(), contentToGet);
  240. }
  241. }
  242. catch ( IOException e )
  243. {
  244. throw new RuntimeException( e.getMessage(), e );
  245. }
  246. }
  247. }