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.

AbstractRepositoryServletProxiedTestCase.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package org.apache.archiva.webdav;
  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 com.gargoylesoftware.htmlunit.WebClient;
  21. import org.apache.archiva.common.utils.FileUtils;
  22. import org.apache.archiva.configuration.ProxyConnectorConfiguration;
  23. import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
  24. import org.apache.archiva.policies.CachedFailuresPolicy;
  25. import org.apache.archiva.policies.ChecksumPolicy;
  26. import org.apache.archiva.policies.ReleasesPolicy;
  27. import org.apache.archiva.policies.SnapshotsPolicy;
  28. import org.apache.commons.lang.StringUtils;
  29. import org.eclipse.jetty.server.HttpConnectionFactory;
  30. import org.eclipse.jetty.server.Server;
  31. import org.eclipse.jetty.server.ServerConnector;
  32. import org.eclipse.jetty.server.handler.ContextHandlerCollection;
  33. import org.eclipse.jetty.servlet.DefaultServlet;
  34. import org.eclipse.jetty.servlet.ServletContextHandler;
  35. import org.eclipse.jetty.servlet.ServletHolder;
  36. import org.junit.After;
  37. import org.junit.Before;
  38. import org.junit.Rule;
  39. import javax.servlet.http.HttpServletResponse;
  40. import java.nio.charset.Charset;
  41. import java.nio.file.Files;
  42. import java.nio.file.Path;
  43. import java.nio.file.Paths;
  44. import static org.assertj.core.api.Assertions.assertThat;
  45. /**
  46. * AbstractRepositoryServletProxiedTestCase
  47. */
  48. public abstract class AbstractRepositoryServletProxiedTestCase
  49. extends AbstractRepositoryServletTestCase
  50. {
  51. class RemoteRepoInfo
  52. {
  53. public String id;
  54. public String url;
  55. public String context;
  56. public Server server;
  57. public Path root;
  58. public RemoteRepositoryConfiguration config;
  59. }
  60. protected static final long ONE_SECOND = ( 1000 /* milliseconds */ );
  61. protected static final long ONE_MINUTE = ( ONE_SECOND * 60 );
  62. protected static final long ONE_HOUR = ( ONE_MINUTE * 60 );
  63. protected static final long ONE_DAY = ( ONE_HOUR * 24 );
  64. protected static final long OVER_ONE_HOUR = ( ONE_HOUR + ONE_MINUTE );
  65. protected static final long OVER_ONE_DAY = ( ONE_DAY + ONE_HOUR );
  66. protected static final long OLDER = ( -1 );
  67. protected static final long NEWER = 0;
  68. protected static final int EXPECT_MANAGED_CONTENTS = 1;
  69. protected static final int EXPECT_REMOTE_CONTENTS = 2;
  70. protected static final int EXPECT_NOT_FOUND = 3;
  71. protected static final boolean HAS_MANAGED_COPY = true;
  72. protected static final boolean NO_MANAGED_COPY = false;
  73. protected RemoteRepoInfo remoteCentral;
  74. protected RemoteRepoInfo remoteSnapshots;
  75. @Rule
  76. public ArchivaTemporaryFolderRule repoRootInternali = new ArchivaTemporaryFolderRule();
  77. @Before
  78. @Override
  79. public void setUp()
  80. throws Exception
  81. {
  82. super.setUp();
  83. startRepository();
  84. }
  85. @Override
  86. @After
  87. public void tearDown()
  88. throws Exception
  89. {
  90. shutdownServer( remoteCentral );
  91. shutdownServer( remoteSnapshots );
  92. super.tearDown();
  93. String baseDirProp = System.getProperty( "appserver.base" );
  94. if ( StringUtils.isNotEmpty( baseDirProp )) {
  95. Path baseDir = Paths.get(baseDirProp);
  96. log.info("Deleting appserver base {}", baseDir);
  97. FileUtils.deleteDirectory( baseDir );
  98. log.info("exist {}", Files.exists(baseDir));
  99. }
  100. }
  101. protected RemoteRepoInfo createServer( String id )
  102. throws Exception
  103. {
  104. RemoteRepoInfo repo = new RemoteRepoInfo();
  105. repo.id = id;
  106. repo.context = "/" + id;
  107. repo.root = repoRootInternali.getRoot();/*Files.createTempDirectory(
  108. "temp" ).toFile();*/// new File( System.getProperty( "basedir" ) + "target/remote-repos/" + id + "/" );
  109. // Remove exising root contents.
  110. if ( Files.exists(repo.root) )
  111. {
  112. FileUtils.deleteDirectory( repo.root );
  113. }
  114. // Establish root directory.
  115. if ( !Files.exists(repo.root) )
  116. {
  117. Files.createDirectories( repo.root );
  118. }
  119. repo.server = new Server( );
  120. ServerConnector serverConnector = new ServerConnector( repo.server, new HttpConnectionFactory());
  121. repo.server.addConnector( serverConnector );
  122. ContextHandlerCollection contexts = new ContextHandlerCollection();
  123. repo.server.setHandler( contexts );
  124. ServletContextHandler context = new ServletContextHandler();
  125. context.setContextPath( repo.context );
  126. context.setResourceBase( repo.root.toAbsolutePath().toString() );
  127. context.setAttribute( "dirAllowed", true );
  128. context.setAttribute( "maxCacheSize", 0 );
  129. ServletHolder sh = new ServletHolder( DefaultServlet.class );
  130. context.addServlet( sh, "/" );
  131. contexts.addHandler( context );
  132. repo.server.start();
  133. int port = serverConnector.getLocalPort();
  134. repo.url = "http://localhost:" + port + repo.context;
  135. log.info( "Remote HTTP Server started on {}", repo.url );
  136. repo.config = createRemoteRepository( repo.id, "Testable [" + repo.id + "] Remote Repo", repo.url );
  137. return repo;
  138. }
  139. protected void assertServerSetupCorrectly( RemoteRepoInfo remoteRepo )
  140. throws Exception
  141. {
  142. WebClient client = newClient();
  143. int status = client.getPage( remoteRepo.url ).getWebResponse().getStatusCode();
  144. assertThat( status ).isEqualTo( HttpServletResponse.SC_OK );
  145. }
  146. private void setupConnector( String repoId, RemoteRepoInfo remoteRepo, String releasesPolicy,
  147. String snapshotsPolicy )
  148. {
  149. ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
  150. connector.setSourceRepoId( repoId );
  151. connector.setTargetRepoId( remoteRepo.id );
  152. connector.addPolicy( ProxyConnectorConfiguration.POLICY_RELEASES, releasesPolicy );
  153. connector.addPolicy( ProxyConnectorConfiguration.POLICY_SNAPSHOTS, snapshotsPolicy );
  154. connector.addPolicy( ProxyConnectorConfiguration.POLICY_CHECKSUM, ChecksumPolicy.IGNORE );
  155. connector.addPolicy( ProxyConnectorConfiguration.POLICY_CACHE_FAILURES, CachedFailuresPolicy.NO );
  156. archivaConfiguration.getConfiguration().addProxyConnector( connector );
  157. }
  158. protected void shutdownServer( RemoteRepoInfo remoteRepo )
  159. {
  160. if ( remoteRepo != null )
  161. {
  162. if ( remoteRepo.server != null )
  163. {
  164. if ( remoteRepo.server.isRunning() )
  165. {
  166. try
  167. {
  168. remoteRepo.server.stop();
  169. // int graceful = remoteRepo.server.getGracefulShutdown();
  170. // System.out.println( "server set to graceful shutdown: " + graceful );
  171. // remoteRepo = null;
  172. }
  173. catch ( Exception e )
  174. {
  175. e.printStackTrace( System.err );
  176. }
  177. }
  178. }
  179. }
  180. }
  181. protected Path populateRepo( RemoteRepoInfo remoteRepo, String path, String contents )
  182. throws Exception
  183. {
  184. Path destFile = remoteRepo.root.resolve( path );
  185. if ( Files.exists(destFile) )
  186. {
  187. Files.delete(destFile);
  188. }
  189. Files.createDirectories( destFile.getParent());
  190. org.apache.archiva.common.utils.FileUtils.writeStringToFile( destFile, Charset.defaultCharset(), contents);
  191. return destFile;
  192. }
  193. protected void setupCentralRemoteRepo()
  194. throws Exception
  195. {
  196. remoteCentral = createServer( "central" );
  197. assertServerSetupCorrectly( remoteCentral );
  198. RemoteRepositoryConfiguration remoteRepositoryConfiguration =
  199. archivaConfiguration.getConfiguration().getRemoteRepositoriesAsMap().get( remoteCentral.id );
  200. if ( remoteRepositoryConfiguration != null )
  201. {
  202. archivaConfiguration.getConfiguration().removeRemoteRepository( remoteRepositoryConfiguration );
  203. }
  204. archivaConfiguration.getConfiguration().addRemoteRepository( remoteCentral.config );
  205. setupCleanRepo( remoteCentral.root );
  206. }
  207. protected void setupConnector( String repoId, RemoteRepoInfo remoteRepo )
  208. {
  209. setupConnector( repoId, remoteRepo, ReleasesPolicy.ALWAYS, SnapshotsPolicy.ALWAYS );
  210. }
  211. protected void setupReleaseConnector( String managedRepoId, RemoteRepoInfo remoteRepo, String releasePolicy )
  212. {
  213. setupConnector( managedRepoId, remoteRepo, releasePolicy, SnapshotsPolicy.ALWAYS );
  214. }
  215. protected void setupSnapshotConnector( String managedRepoId, RemoteRepoInfo remoteRepo, String snapshotsPolicy )
  216. {
  217. setupConnector( managedRepoId, remoteRepo, ReleasesPolicy.ALWAYS, snapshotsPolicy );
  218. }
  219. protected void setupSnapshotsRemoteRepo()
  220. throws Exception
  221. {
  222. remoteSnapshots = createServer( "snapshots" );
  223. assertServerSetupCorrectly( remoteSnapshots );
  224. RemoteRepositoryConfiguration remoteRepositoryConfiguration =
  225. archivaConfiguration.getConfiguration().getRemoteRepositoriesAsMap().get( remoteSnapshots.id );
  226. if ( remoteRepositoryConfiguration != null )
  227. {
  228. archivaConfiguration.getConfiguration().removeRemoteRepository( remoteRepositoryConfiguration );
  229. }
  230. archivaConfiguration.getConfiguration().addRemoteRepository( remoteSnapshots.config );
  231. setupCleanRepo( remoteSnapshots.root );
  232. }
  233. }