1 package org.apache.archiva;
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 junit.framework.TestCase;
22 import org.apache.archiva.admin.model.beans.RemoteRepository;
23 import org.apache.archiva.rest.api.services.ProxyConnectorService;
24 import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
25 import org.apache.archiva.security.common.ArchivaRoleConstants;
26 import org.apache.archiva.webdav.RepositoryServlet;
27 import org.apache.commons.io.FileUtils;
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.cxf.common.util.Base64Utility;
30 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
31 import org.apache.cxf.jaxrs.client.WebClient;
32 import org.apache.cxf.transport.servlet.CXFServlet;
33 import org.codehaus.plexus.util.IOUtil;
34 import org.codehaus.redback.integration.security.role.RedbackRoleConstants;
35 import org.codehaus.redback.rest.api.services.RoleManagementService;
36 import org.codehaus.redback.rest.api.services.UserService;
37 import org.codehaus.redback.rest.services.FakeCreateAdminService;
38 import org.eclipse.jetty.server.Connector;
39 import org.eclipse.jetty.server.Server;
40 import org.eclipse.jetty.server.session.SessionHandler;
41 import org.eclipse.jetty.servlet.ServletContextHandler;
42 import org.eclipse.jetty.servlet.ServletHolder;
43 import org.junit.After;
44 import org.junit.AfterClass;
45 import org.junit.Before;
46 import org.junit.BeforeClass;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 import org.junit.runners.JUnit4;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52 import org.springframework.web.context.ContextLoaderListener;
54 import javax.servlet.ServletException;
55 import javax.servlet.http.HttpServlet;
56 import javax.servlet.http.HttpServletRequest;
57 import javax.servlet.http.HttpServletResponse;
59 import java.io.FileWriter;
60 import java.io.IOException;
61 import java.io.InputStream;
62 import java.net.HttpURLConnection;
64 import java.util.ArrayList;
65 import java.util.Enumeration;
66 import java.util.List;
67 import java.util.zip.ZipEntry;
68 import java.util.zip.ZipFile;
71 * @author Olivier Lamy
73 @RunWith( JUnit4.class )
74 public class DownloadArtifactsTest
78 protected static Logger log = LoggerFactory.getLogger( DownloadArtifactsTest.class );
80 public String authorizationHeader = getAdminAuthzHeader();
82 public Server server = null;
84 public Server redirectServer = null;
88 public int redirectPort;
90 public static String encode( String uid, String password )
92 return "Basic " + Base64Utility.encode( ( uid + ":" + password ).getBytes() );
95 public static String getAdminAuthzHeader()
97 String adminPwdSysProps = System.getProperty( "rest.admin.pwd" );
98 if ( StringUtils.isBlank( adminPwdSysProps ) )
100 return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, FakeCreateAdminService.ADMIN_TEST_PWD );
102 return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, adminPwdSysProps );
105 protected String getSpringConfigLocation()
107 return "classpath*:META-INF/spring-context.xml classpath*:spring-context-artifacts-download.xml";
111 protected String getRestServicesPath()
113 return "restServices";
116 static String previousAppServerBase;
119 public static void setAppServerBase()
122 previousAppServerBase = System.getProperty( "appserver.base" );
123 System.setProperty( "appserver.base", "target/" + DownloadArtifactsTest.class.getName() );
128 public static void resetAppServerBase()
131 System.setProperty( "appserver.base", previousAppServerBase );
135 public void startServer()
139 System.setProperty( "redback.admin.creation.file", "target/auto-admin-creation.properties" );
140 this.server = new Server( 0 );
142 ServletContextHandler context = new ServletContextHandler();
144 context.setContextPath( "/" );
146 context.setInitParameter( "contextConfigLocation", getSpringConfigLocation() );
148 ContextLoaderListener contextLoaderListener = new ContextLoaderListener();
150 context.addEventListener( contextLoaderListener );
152 ServletHolder sh = new ServletHolder( CXFServlet.class );
154 SessionHandler sessionHandler = new SessionHandler();
156 context.setSessionHandler( sessionHandler );
158 context.addServlet( sh, "/" + getRestServicesPath() + "/*" );
160 ServletHolder repoSh = new ServletHolder( RepositoryServlet.class );
161 context.addServlet( repoSh, "/repository/*" );
163 server.setHandler( context );
165 Connector connector = this.server.getConnectors()[0];
166 this.port = connector.getLocalPort();
167 log.info( "start server on port " + this.port );
171 this.redirectServer = new Server( 0 );
172 ServletHolder shRedirect = new ServletHolder( RedirectServlet.class );
173 ServletContextHandler contextRedirect = new ServletContextHandler();
175 contextRedirect.setContextPath( "/" );
176 contextRedirect.addServlet( shRedirect, "/*" );
178 redirectServer.setHandler( contextRedirect );
179 redirectServer.start();
180 this.redirectPort = redirectServer.getConnectors()[0].getLocalPort();
181 log.info( "redirect server port {}", redirectPort );
183 FakeCreateAdminService fakeCreateAdminService = getFakeCreateAdminService();
185 Boolean res = fakeCreateAdminService.createAdminIfNeeded();
186 assertTrue( res.booleanValue() );
192 public void tearDown()
195 System.clearProperty( "redback.admin.creation.file" );
200 public void downloadWithRemoteRedirect()
203 RemoteRepository remoteRepository = getRemoteRepositoriesService().getRemoteRepository( "central" );
204 remoteRepository.setUrl( "http://localhost:" + redirectPort );
205 getRemoteRepositoriesService().updateRemoteRepository( remoteRepository );
207 RoleManagementService roleManagementService = getRoleManagementService( authorizationHeader );
209 if ( !roleManagementService.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER,
212 roleManagementService.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, "internal" );
215 getUserService( authorizationHeader ).createGuestUser();
216 roleManagementService.assignRole( ArchivaRoleConstants.TEMPLATE_GUEST, "guest" );
218 roleManagementService.assignTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, "internal",
221 getUserService( authorizationHeader ).removeFromCache( "guest" );
223 URL url = new URL( "http://localhost:" + port + "/repository/internal/junit/junit/4.9/junit-4.9.jar" );
224 HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
225 //urlConnection.setRequestProperty( "Authorization", authorizationHeader );
226 InputStream is = urlConnection.getInputStream();
227 File file = new File( "target/junit-4.9.jar" );
233 FileWriter fw = new FileWriter( file );
234 IOUtil.copy( is, fw );
235 // assert jar contains org/junit/runners/JUnit4.class
236 ZipFile zipFile = new ZipFile( file );
237 List<String> entries = getZipEntriesNames( zipFile );
238 ZipEntry zipEntry = zipFile.getEntry( "org/junit/runners/JUnit4.class" );
239 assertNotNull( "cannot find zipEntry org/junit/runners/JUnit4.class, entries: " + entries + ", content is: "
240 + FileUtils.readFileToString( file ), zipEntry );
245 private List<String> getZipEntriesNames( ZipFile zipFile )
247 List<String> entriesNames = new ArrayList<String>();
248 Enumeration<? extends ZipEntry> entries = zipFile.entries();
249 while ( entries.hasMoreElements() )
251 entriesNames.add( entries.nextElement().getName() );
256 public static class RedirectServlet
260 protected void doGet( HttpServletRequest req, HttpServletResponse resp )
261 throws ServletException, IOException
264 log.info( "redirect servlet receive: {}", req.getRequestURI() );
265 resp.setStatus( 302 );
266 resp.getWriter().write( "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" + "<html><head>\n"
267 + "<title>302 Found</title>\n" + "</head><body>\n" + "<h1>Found</h1>\n"
268 + "<p>The document has moved <a href=\"http://repo1.maven.apache.org/maven2/junit/junit/4.9/junit-4.9.jar\">here</a>.</p>\n"
269 + "</body></html>\n" + "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
270 + "<html><head>\n" );
271 resp.sendRedirect( "http://repo1.maven.apache.org/maven2/" + req.getRequestURI() );
275 protected ProxyConnectorService getProxyConnectorService()
277 ProxyConnectorService service =
278 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
279 ProxyConnectorService.class );
281 WebClient.client( service ).header( "Authorization", authorizationHeader );
282 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
286 protected RemoteRepositoriesService getRemoteRepositoriesService()
288 RemoteRepositoriesService service =
289 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
290 RemoteRepositoriesService.class );
292 WebClient.client( service ).header( "Authorization", authorizationHeader );
293 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
297 protected String getBaseUrl()
299 String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
300 return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
304 protected RoleManagementService getRoleManagementService( String authzHeader )
306 RoleManagementService service =
307 JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
308 RoleManagementService.class );
310 // for debuging purpose
311 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
313 if ( authzHeader != null )
315 WebClient.client( service ).header( "Authorization", authzHeader );
320 protected UserService getUserService( String authzHeader )
322 UserService service =
323 JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
326 // for debuging purpose
327 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
329 if ( authzHeader != null )
331 WebClient.client( service ).header( "Authorization", authzHeader );
336 protected FakeCreateAdminService getFakeCreateAdminService()
338 return JAXRSClientFactory.create(
339 "http://localhost:" + port + "/" + getRestServicesPath() + "/fakeCreateAdminService/",
340 FakeCreateAdminService.class );