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.commons.compress.utils.IOUtils;
25 import org.apache.commons.io.FileUtils;
26 import org.apache.maven.wagon.providers.http.HttpWagon;
27 import org.apache.maven.wagon.repository.Repository;
28 import org.eclipse.jetty.server.Server;
29 import org.eclipse.jetty.servlet.ServletContextHandler;
30 import org.eclipse.jetty.servlet.ServletHolder;
31 import org.junit.After;
32 import org.junit.AfterClass;
33 import org.junit.Before;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.slf4j.Logger;
38 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;
45 import java.io.FileInputStream;
46 import java.io.IOException;
47 import java.util.List;
48 import java.util.zip.ZipEntry;
49 import java.util.zip.ZipFile;
50 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
53 * @author Olivier Lamy
55 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
56 public class DownloadArtifactsTest
57 extends AbstractDownloadTest
60 protected Logger log = LoggerFactory.getLogger( DownloadArtifactsTest.class );
62 public Server redirectServer = null;
64 public int redirectPort;
66 public Server repoServer = null;
68 public int repoServerPort;
71 public static void setAppServerBase()
73 previousAppServerBase = System.getProperty( "appserver.base" );
74 System.setProperty( "appserver.base", "target/" + DownloadArtifactsTest.class.getName() );
79 public static void resetAppServerBase()
81 System.setProperty( "appserver.base", previousAppServerBase );
85 protected String getSpringConfigLocation()
87 return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-artifacts-download.xml";
92 public void startServer()
99 this.repoServer = new Server( 0 );
101 ServletHolder shRepo = new ServletHolder( RepoServlet.class );
102 ServletContextHandler contextRepo = new ServletContextHandler();
104 contextRepo.setContextPath( "/" );
105 contextRepo.addServlet( shRepo, "/*" );
107 repoServer.setHandler( contextRepo );
109 this.repoServerPort = repoServer.getConnectors()[0].getLocalPort();
113 this.redirectServer = new Server( 0 );
114 ServletHolder shRedirect = new ServletHolder( RedirectServlet.class );
115 ServletContextHandler contextRedirect = new ServletContextHandler();
116 contextRedirect.setAttribute( "redirectToPort", Integer.toString( this.repoServerPort ) );
118 contextRedirect.setContextPath( "/" );
119 contextRedirect.addServlet( shRedirect, "/*" );
121 redirectServer.setHandler( contextRedirect );
122 redirectServer.start();
123 this.redirectPort = redirectServer.getConnectors()[0].getLocalPort();
124 log.info( "redirect server port {}", redirectPort );
130 public void tearDown()
134 if ( this.redirectServer != null )
136 this.redirectServer.stop();
141 public void downloadWithRemoteRedirect()
144 RemoteRepository remoteRepository = getRemoteRepositoriesService().getRemoteRepository( "central" );
145 remoteRepository.setUrl( "http://localhost:" + redirectPort );
146 getRemoteRepositoriesService().updateRemoteRepository( remoteRepository );
148 RoleManagementService roleManagementService = getRoleManagementService( authorizationHeader );
150 if ( !roleManagementService.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER,
153 roleManagementService.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, "internal" );
156 getUserService( authorizationHeader ).createGuestUser();
157 roleManagementService.assignRole( ArchivaRoleConstants.TEMPLATE_GUEST, "guest" );
159 roleManagementService.assignTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, "internal",
162 getUserService( authorizationHeader ).removeFromCache( "guest" );
164 File file = new File( "target/junit-4.9.jar" );
170 HttpWagon httpWagon = new HttpWagon();
171 httpWagon.connect( new Repository( "foo", "http://localhost:" + port ) );
173 httpWagon.get( "/repository/internal/junit/junit/4.9/junit-4.9.jar", file );
175 ZipFile zipFile = new ZipFile( file );
176 List<String> entries = getZipEntriesNames( zipFile );
177 ZipEntry zipEntry = zipFile.getEntry( "org/junit/runners/JUnit4.class" );
178 assertNotNull( "cannot find zipEntry org/junit/runners/JUnit4.class, entries: " + entries + ", content is: "
179 + FileUtils.readFileToString( file ), zipEntry );
187 public static class RedirectServlet
191 protected void doGet( HttpServletRequest req, HttpServletResponse resp )
192 throws ServletException, IOException
195 LoggerFactory.getLogger( getClass() ).info( "redirect servlet receive: {}", req.getRequestURI() );
196 resp.setStatus( 302 );
197 resp.getWriter().write( "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" + "<html><head>\n"
198 + "<title>302 Found</title>\n" + "</head><body>\n" + "<h1>Found</h1>\n"
199 + "<p>The document has moved <a href=\"http://repo.maven.apache.org/maven2/junit/junit/4.9/junit-4.9.jar\">here</a>.</p>\n"
200 + "</body></html>\n" + "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
201 + "<html><head>\n" );
202 resp.sendRedirect( "http://localhost:" + getServletContext().getAttribute( "redirectToPort" ) + "/maven2/"
203 + req.getRequestURI() );
207 public static class RepoServlet
211 protected void doGet( HttpServletRequest req, HttpServletResponse resp )
212 throws ServletException, IOException
214 File jar = new File( System.getProperty( "basedir" ), "src/test/junit-4.9.jar" );
215 IOUtils.copy( new FileInputStream( jar ), resp.getOutputStream() );