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 9.9KB

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