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.

DownloadArtifactsTest.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package org.apache.archiva.web.remotedownload;
  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. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import org.apache.archiva.admin.model.beans.RemoteRepository;
  20. import org.apache.archiva.redback.rest.api.services.RoleManagementService;
  21. import org.apache.archiva.security.common.ArchivaRoleConstants;
  22. import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
  23. import org.apache.commons.io.FileUtils;
  24. import org.apache.commons.lang3.StringUtils;
  25. import org.apache.maven.wagon.providers.http.HttpWagon;
  26. import org.apache.maven.wagon.repository.Repository;
  27. import org.eclipse.jetty.server.HttpConnectionFactory;
  28. import org.eclipse.jetty.server.Server;
  29. import org.eclipse.jetty.server.ServerConnector;
  30. import org.eclipse.jetty.servlet.ServletContextHandler;
  31. import org.eclipse.jetty.servlet.ServletHolder;
  32. import org.junit.After;
  33. import org.junit.AfterClass;
  34. import org.junit.Before;
  35. import org.junit.BeforeClass;
  36. import org.junit.Test;
  37. import org.junit.runner.RunWith;
  38. import org.slf4j.Logger;
  39. import org.slf4j.LoggerFactory;
  40. import javax.servlet.ServletException;
  41. import javax.servlet.http.HttpServlet;
  42. import javax.servlet.http.HttpServletRequest;
  43. import javax.servlet.http.HttpServletResponse;
  44. import java.io.IOException;
  45. import java.nio.charset.Charset;
  46. import java.nio.file.Files;
  47. import java.nio.file.Path;
  48. import java.nio.file.Paths;
  49. import java.util.List;
  50. import java.util.concurrent.atomic.AtomicReference;
  51. import java.util.zip.ZipEntry;
  52. import java.util.zip.ZipFile;
  53. /**
  54. * @author Olivier Lamy
  55. */
  56. @RunWith( ArchivaBlockJUnit4ClassRunner.class )
  57. public class DownloadArtifactsTest
  58. extends AbstractDownloadTest
  59. {
  60. protected Logger log = LoggerFactory.getLogger( DownloadArtifactsTest.class );
  61. public Server redirectServer = null;
  62. public int redirectPort;
  63. public Server repoServer = null;
  64. public int repoServerPort;
  65. private static Path appServerBase;
  66. @BeforeClass
  67. public static void setAppServerBase()
  68. throws IOException
  69. {
  70. System.out.println( "Setting appserver base" );
  71. previousAppServerBase = System.getProperty( "appserver.base" );
  72. appServerBase = Files.createTempDirectory( "archiva-common-web_appsrv2_" ).toAbsolutePath( );
  73. System.setProperty( "appserver.base", appServerBase.toString( ) );
  74. }
  75. @AfterClass
  76. public static void resetAppServerBase()
  77. {
  78. if (Files.exists(appServerBase)) {
  79. FileUtils.deleteQuietly( appServerBase.toFile() );
  80. }
  81. System.setProperty( "appserver.base", previousAppServerBase );
  82. }
  83. @Override
  84. protected String getSpringConfigLocation()
  85. {
  86. System.out.println( "AppserverBase: " + System.getProperty( "appserver.base" ) );
  87. return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-artifacts-download.xml";
  88. }
  89. @Override
  90. @Before
  91. public void startServer()
  92. throws Exception
  93. {
  94. super.startServer();
  95. // repo handler
  96. this.repoServer = new Server( );
  97. ServerConnector repoServerConnector = new ServerConnector( this.repoServer, new HttpConnectionFactory());
  98. this.repoServer.addConnector( repoServerConnector );
  99. ServletHolder shRepo = new ServletHolder( RepoServlet.class );
  100. ServletContextHandler contextRepo = new ServletContextHandler();
  101. contextRepo.setContextPath( "/" );
  102. contextRepo.addServlet( shRepo, "/*" );
  103. repoServer.setHandler( contextRepo );
  104. repoServer.start();
  105. this.repoServerPort = repoServerConnector.getLocalPort();
  106. //redirect handler
  107. this.redirectServer = new Server( );
  108. ServerConnector redirectServerConnector = new ServerConnector( this.redirectServer, new HttpConnectionFactory());
  109. this.redirectServer.addConnector( redirectServerConnector );
  110. ServletHolder shRedirect = new ServletHolder( RedirectServlet.class );
  111. ServletContextHandler contextRedirect = new ServletContextHandler();
  112. contextRedirect.setAttribute( "redirectToPort", Integer.toString( this.repoServerPort ) );
  113. contextRedirect.setContextPath( "/" );
  114. contextRedirect.addServlet( shRedirect, "/*" );
  115. redirectServer.setHandler( contextRedirect );
  116. redirectServer.start();
  117. this.redirectPort = redirectServerConnector.getLocalPort();
  118. log.info( "redirect server port {}", redirectPort );
  119. }
  120. @After
  121. @Override
  122. public void tearDown()
  123. throws Exception
  124. {
  125. super.tearDown();
  126. if ( this.redirectServer != null )
  127. {
  128. this.redirectServer.stop();
  129. }
  130. }
  131. @Test
  132. public void downloadWithRemoteRedirect()
  133. throws Exception
  134. {
  135. RemoteRepository remoteRepository = getRemoteRepositoriesService().getRemoteRepository( "central" );
  136. remoteRepository.setUrl( "http://localhost:" + redirectPort );
  137. getRemoteRepositoriesService().updateRemoteRepository( remoteRepository );
  138. RoleManagementService roleManagementService = getRoleManagementService( authorizationHeader );
  139. if ( !roleManagementService.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER,
  140. "internal" ) )
  141. {
  142. roleManagementService.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, "internal" );
  143. }
  144. getUserService( authorizationHeader ).createGuestUser();
  145. roleManagementService.assignRole( ArchivaRoleConstants.TEMPLATE_GUEST, "guest" );
  146. roleManagementService.assignTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, "internal",
  147. "guest" );
  148. getUserService( authorizationHeader ).removeFromCache( "guest" );
  149. Path file = Paths.get( "target/junit-4.9.jar" );
  150. Files.deleteIfExists( file );
  151. HttpWagon httpWagon = new HttpWagon();
  152. httpWagon.connect( new Repository( "foo", "http://localhost:" + port ) );
  153. httpWagon.get( "repository/internal/junit/junit/4.9/junit-4.9.jar", file.toFile() );
  154. ZipFile zipFile = new ZipFile( file.toFile() );
  155. List<String> entries = getZipEntriesNames( zipFile );
  156. ZipEntry zipEntry = zipFile.getEntry( "org/junit/runners/JUnit4.class" );
  157. assertNotNull( "cannot find zipEntry org/junit/runners/JUnit4.class, entries: " + entries + ", content is: "
  158. + FileUtils.readFileToString( file.toFile(), Charset.forName( "UTF-8") ), zipEntry );
  159. zipFile.close();
  160. file.toFile().deleteOnExit();
  161. }
  162. public static class RedirectServlet
  163. extends HttpServlet
  164. {
  165. @Override
  166. protected void doGet( HttpServletRequest req, HttpServletResponse resp )
  167. throws ServletException, IOException
  168. {
  169. LoggerFactory.getLogger( getClass() ).info( "redirect servlet receive: {}", req.getRequestURI() );
  170. resp.setStatus( 302 );
  171. resp.getWriter().write( "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" + "<html><head>\n"
  172. + "<title>302 Found</title>\n" + "</head><body>\n" + "<h1>Found</h1>\n"
  173. + "<p>The document has moved <a href=\"https://repo.maven.apache.org/maven2/junit/junit/4.9/junit-4.9.jar\">here</a>.</p>\n"
  174. + "</body></html>\n" + "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
  175. + "<html><head>\n" );
  176. resp.sendRedirect( "http://localhost:" + getServletContext().getAttribute( "redirectToPort" ) + "/maven2/"
  177. + req.getRequestURI() );
  178. }
  179. }
  180. public static class RepoServlet
  181. extends HttpServlet
  182. {
  183. private AtomicReference<Path> projectDir = new AtomicReference<>( );
  184. protected Path getProjectDirectory() {
  185. if ( projectDir.get()==null) {
  186. String propVal = System.getProperty("mvn.project.base.dir");
  187. Path newVal;
  188. if ( StringUtils.isEmpty(propVal)) {
  189. newVal = Paths.get("").toAbsolutePath();
  190. } else {
  191. newVal = Paths.get(propVal).toAbsolutePath();
  192. }
  193. projectDir.compareAndSet(null, newVal);
  194. }
  195. return projectDir.get();
  196. }
  197. @Override
  198. protected void doGet( HttpServletRequest req, HttpServletResponse resp )
  199. throws ServletException, IOException
  200. {
  201. Path jar = getProjectDirectory().resolve( "src/test/junit-4.9.jar" );
  202. Files.copy( jar, resp.getOutputStream() );
  203. }
  204. }
  205. }