1 package org.apache.archiva.remotedownload;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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
21 import org.apache.archiva.admin.model.beans.RemoteRepository;
22 import org.apache.archiva.redback.rest.api.services.RoleManagementService;
23 import org.apache.archiva.security.common.ArchivaRoleConstants;
24 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
25 import org.apache.commons.io.FileUtils;
26 import org.apache.commons.lang3.StringUtils;
27 import org.apache.maven.wagon.providers.http.HttpWagon;
28 import org.apache.maven.wagon.repository.Repository;
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.servlet.ServletContextHandler;
33 import org.eclipse.jetty.servlet.ServletHolder;
34 import org.junit.After;
35 import org.junit.AfterClass;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import javax.servlet.ServletException;
44 import javax.servlet.http.HttpServlet;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
47 import java.io.IOException;
48 import java.nio.charset.Charset;
49 import java.nio.file.Files;
50 import java.nio.file.Path;
51 import java.nio.file.Paths;
52 import java.util.List;
53 import java.util.concurrent.atomic.AtomicReference;
54 import java.util.zip.ZipEntry;
55 import java.util.zip.ZipFile;
58 * @author Olivier Lamy
60 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
61 public class DownloadArtifactsTest
62 extends AbstractDownloadTest
65 protected Logger log = LoggerFactory.getLogger( DownloadArtifactsTest.class );
67 public Server redirectServer = null;
69 public int redirectPort;
71 public Server repoServer = null;
73 public int repoServerPort;
75 private static Path appServerBase;
78 public static void setAppServerBase()
81 System.out.println( "Setting appserver base" );
82 previousAppServerBase = System.getProperty( "appserver.base" );
83 appServerBase = Files.createTempDirectory( "archiva-common-web_appsrv2_" ).toAbsolutePath( );
84 System.setProperty( "appserver.base", appServerBase.toString( ) );
88 public static void resetAppServerBase()
90 if (Files.exists(appServerBase)) {
91 FileUtils.deleteQuietly( appServerBase.toFile() );
93 System.setProperty( "appserver.base", previousAppServerBase );
97 protected String getSpringConfigLocation()
99 System.out.println( "AppserverBase: " + System.getProperty( "appserver.base" ) );
100 return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-artifacts-download.xml";
106 public void startServer()
113 this.repoServer = new Server( );
114 ServerConnector repoServerConnector = new ServerConnector( this.repoServer, new HttpConnectionFactory());
115 this.repoServer.addConnector( repoServerConnector );
117 ServletHolder shRepo = new ServletHolder( RepoServlet.class );
118 ServletContextHandler contextRepo = new ServletContextHandler();
120 contextRepo.setContextPath( "/" );
121 contextRepo.addServlet( shRepo, "/*" );
123 repoServer.setHandler( contextRepo );
126 this.repoServerPort = repoServerConnector.getLocalPort();
130 this.redirectServer = new Server( );
131 ServerConnector redirectServerConnector = new ServerConnector( this.redirectServer, new HttpConnectionFactory());
132 this.redirectServer.addConnector( redirectServerConnector );
134 ServletHolder shRedirect = new ServletHolder( RedirectServlet.class );
135 ServletContextHandler contextRedirect = new ServletContextHandler();
136 contextRedirect.setAttribute( "redirectToPort", Integer.toString( this.repoServerPort ) );
138 contextRedirect.setContextPath( "/" );
139 contextRedirect.addServlet( shRedirect, "/*" );
141 redirectServer.setHandler( contextRedirect );
142 redirectServer.start();
143 this.redirectPort = redirectServerConnector.getLocalPort();
144 log.info( "redirect server port {}", redirectPort );
150 public void tearDown()
154 if ( this.redirectServer != null )
156 this.redirectServer.stop();
161 public void downloadWithRemoteRedirect()
164 RemoteRepository remoteRepository = getRemoteRepositoriesService().getRemoteRepository( "central" );
165 remoteRepository.setUrl( "http://localhost:" + redirectPort );
166 getRemoteRepositoriesService().updateRemoteRepository( remoteRepository );
168 RoleManagementService roleManagementService = getRoleManagementService( authorizationHeader );
170 if ( !roleManagementService.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER,
173 roleManagementService.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, "internal" );
176 getUserService( authorizationHeader ).createGuestUser();
177 roleManagementService.assignRole( ArchivaRoleConstants.TEMPLATE_GUEST, "guest" );
179 roleManagementService.assignTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, "internal",
182 getUserService( authorizationHeader ).removeFromCache( "guest" );
184 Path file = Paths.get( "target/junit-4.9.jar" );
185 Files.deleteIfExists( file );
187 HttpWagon httpWagon = new HttpWagon();
188 httpWagon.connect( new Repository( "foo", "http://localhost:" + port ) );
190 httpWagon.get( "repository/internal/junit/junit/4.9/junit-4.9.jar", file.toFile() );
192 ZipFile zipFile = new ZipFile( file.toFile() );
193 List<String> entries = getZipEntriesNames( zipFile );
194 ZipEntry zipEntry = zipFile.getEntry( "org/junit/runners/JUnit4.class" );
195 assertNotNull( "cannot find zipEntry org/junit/runners/JUnit4.class, entries: " + entries + ", content is: "
196 + FileUtils.readFileToString( file.toFile(), Charset.forName( "UTF-8") ), zipEntry );
198 file.toFile().deleteOnExit();
202 public static class RedirectServlet
206 protected void doGet( HttpServletRequest req, HttpServletResponse resp )
207 throws ServletException, IOException
210 LoggerFactory.getLogger( getClass() ).info( "redirect servlet receive: {}", req.getRequestURI() );
211 resp.setStatus( 302 );
212 resp.getWriter().write( "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" + "<html><head>\n"
213 + "<title>302 Found</title>\n" + "</head><body>\n" + "<h1>Found</h1>\n"
214 + "<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"
215 + "</body></html>\n" + "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
216 + "<html><head>\n" );
217 resp.sendRedirect( "http://localhost:" + getServletContext().getAttribute( "redirectToPort" ) + "/maven2/"
218 + req.getRequestURI() );
222 public static class RepoServlet
226 private AtomicReference<Path> projectDir = new AtomicReference<>( );
228 protected Path getProjectDirectory() {
229 if ( projectDir.get()==null) {
230 String propVal = System.getProperty("mvn.project.base.dir");
232 if ( StringUtils.isEmpty(propVal)) {
233 newVal = Paths.get("").toAbsolutePath();
235 newVal = Paths.get(propVal).toAbsolutePath();
237 projectDir.compareAndSet(null, newVal);
239 return projectDir.get();
243 protected void doGet( HttpServletRequest req, HttpServletResponse resp )
244 throws ServletException, IOException
246 Path jar = getProjectDirectory().resolve( "src/test/junit-4.9.jar" );
247 Files.copy( jar, resp.getOutputStream() );