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 com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
22 import junit.framework.TestCase;
23 import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants;
24 import org.apache.archiva.redback.rest.api.model.User;
25 import org.apache.archiva.redback.rest.api.services.RoleManagementService;
26 import org.apache.archiva.redback.rest.api.services.UserService;
27 import org.apache.archiva.redback.rest.services.BaseSetup;
28 import org.apache.archiva.redback.rest.services.FakeCreateAdminService;
29 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
30 import org.apache.archiva.rest.api.services.ProxyConnectorService;
31 import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
32 import org.apache.archiva.rest.api.services.RepositoriesService;
33 import org.apache.archiva.rest.api.services.RepositoryGroupService;
34 import org.apache.archiva.rest.api.services.SearchService;
35 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
36 import org.apache.archiva.webdav.RepositoryServlet;
37 import org.apache.commons.io.FileUtils;
38 import org.apache.commons.lang3.StringUtils;
39 import org.apache.commons.lang3.SystemUtils;
40 import org.apache.cxf.common.util.Base64Utility;
41 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
42 import org.apache.cxf.jaxrs.client.WebClient;
43 import org.apache.cxf.transport.servlet.CXFServlet;
44 import org.eclipse.jetty.server.HttpConnectionFactory;
45 import org.eclipse.jetty.server.Server;
46 import org.eclipse.jetty.server.ServerConnector;
47 import org.eclipse.jetty.server.session.SessionHandler;
48 import org.eclipse.jetty.servlet.ServletContextHandler;
49 import org.eclipse.jetty.servlet.ServletHolder;
50 import org.junit.After;
51 import org.junit.Before;
52 import org.junit.runner.RunWith;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.springframework.web.context.ContextLoaderListener;
57 import java.nio.file.Files;
58 import java.nio.file.Path;
59 import java.nio.file.Paths;
60 import java.util.ArrayList;
61 import java.util.Collections;
62 import java.util.Enumeration;
63 import java.util.List;
64 import java.util.concurrent.atomic.AtomicReference;
65 import java.util.zip.ZipEntry;
66 import java.util.zip.ZipFile;
69 * @author Olivier Lamy
71 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
72 public abstract class AbstractDownloadTest
76 AtomicReference<Path> projectDir = new AtomicReference<>( );
77 AtomicReference<Path> basePath = new AtomicReference<>( );
79 protected List<Path> createdPaths = new ArrayList<>( );
81 protected final Logger log = LoggerFactory.getLogger( getClass() );
83 protected static String previousAppServerBase;
85 public String authorizationHeader = getAdminAuthzHeader();
87 public Server server = null;
89 ServerConnector serverConnector;
93 protected Path getProjectDirectory() {
94 if ( projectDir.get()==null) {
95 String propVal = System.getProperty("mvn.project.base.dir");
97 if (StringUtils.isEmpty(propVal)) {
98 newVal = Paths.get("").toAbsolutePath();
100 newVal = Paths.get(propVal).toAbsolutePath();
102 projectDir.compareAndSet(null, newVal);
104 return projectDir.get();
107 public Path getBasedir()
109 if (basePath.get()==null) {
110 String baseDir = System.getProperty( "basedir" );
111 final Path baseDirPath;
112 if (StringUtils.isNotEmpty( baseDir )) {
113 baseDirPath = Paths.get( baseDir );
115 baseDirPath = getProjectDirectory( );
117 basePath.compareAndSet( null, baseDirPath );
119 return basePath.get( );
124 public static String encode( String uid, String password )
126 return "Basic " + Base64Utility.encode( ( uid + ":" + password ).getBytes() );
129 public static String getAdminAuthzHeader()
131 String adminPwdSysProps = System.getProperty( "rest.admin.pwd" );
132 if ( StringUtils.isBlank( adminPwdSysProps ) )
134 return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, BaseSetup.getAdminPwd() );
136 return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, adminPwdSysProps );
140 protected abstract String getSpringConfigLocation();
143 protected String getRestServicesPath()
145 return "restServices";
150 public void startServer()
154 System.setProperty( "redback.admin.creation.file", "target/auto-admin-creation.properties" );
156 server = new Server();
157 serverConnector = new ServerConnector( server, new HttpConnectionFactory() );
158 server.addConnector( serverConnector );
160 ServletHolder servletHolder = new ServletHolder( new CXFServlet() );
161 ServletContextHandler context = new ServletContextHandler( ServletContextHandler.SESSIONS );
162 context.setResourceBase( SystemUtils.JAVA_IO_TMPDIR );
163 context.setSessionHandler( new SessionHandler() );
164 context.addServlet( servletHolder, "/" + getRestServicesPath() + "/*" );
165 context.setInitParameter( "contextConfigLocation", getSpringConfigLocation() );
166 context.addEventListener( new ContextLoaderListener() );
168 ServletHolder servletHolderRepo = new ServletHolder( new RepositoryServlet() );
169 context.addServlet( servletHolderRepo, "/repository/*" );
171 server.setHandler( context );
173 port = serverConnector.getLocalPort();
174 log.info( "start server on port {}", this.port );
176 User user = new User();
177 user.setEmail( "toto@toto.fr" );
178 user.setFullName( "the root user" );
179 user.setUsername( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME );
180 user.setPassword( BaseSetup.getAdminPwd() );
182 getUserService( null ).createAdminUser( user );
190 public void tearDown()
194 for(Path dir : createdPaths) {
195 if ( Files.exists( dir)) {
196 FileUtils.deleteQuietly( dir.toFile( ) );
199 createdPaths.clear();
201 System.clearProperty( "redback.admin.creation.file" );
203 if ( this.server != null )
210 protected ProxyConnectorService getProxyConnectorService()
212 ProxyConnectorService service =
213 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
214 ProxyConnectorService.class,
215 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
217 WebClient.client( service ).header( "Authorization", authorizationHeader );
218 WebClient.client( service ).header( "Referer", "http://localhost:" + port );
220 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
224 protected RemoteRepositoriesService getRemoteRepositoriesService()
226 RemoteRepositoriesService service =
227 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
228 RemoteRepositoriesService.class,
229 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
231 WebClient.client( service ).header( "Authorization", authorizationHeader );
232 WebClient.client( service ).header( "Referer", "http://localhost:" + port );
234 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
238 protected ManagedRepositoriesService getManagedRepositoriesService()
240 ManagedRepositoriesService service =
241 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
242 ManagedRepositoriesService.class,
243 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
245 WebClient.client( service ).header( "Authorization", authorizationHeader );
246 WebClient.client( service ).header( "Referer", "http://localhost:" + port );
248 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
253 protected RepositoryGroupService getRepositoryGroupService()
255 RepositoryGroupService service =
256 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
257 RepositoryGroupService.class,
258 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
260 WebClient.client( service ).header( "Authorization", authorizationHeader );
261 WebClient.client( service ).header( "Referer", "http://localhost:" + port );
263 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
267 protected RepositoriesService getRepositoriesService()
269 RepositoriesService service =
270 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
271 RepositoriesService.class,
272 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
274 WebClient.client( service ).header( "Authorization", authorizationHeader );
275 WebClient.client( service ).header( "Referer", "http://localhost:" + port );
277 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
281 protected SearchService getSearchService()
283 SearchService service =
284 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
286 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
288 WebClient.client( service ).header( "Authorization", authorizationHeader );
289 WebClient.client( service ).header( "Referer", "http://localhost:" + port );
291 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
295 protected String getBaseUrl()
297 String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
298 return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
302 protected RoleManagementService getRoleManagementService( String authzHeader )
304 RoleManagementService service =
305 JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
306 RoleManagementService.class,
307 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
309 WebClient.client( service ).header( "Referer", "http://localhost:" + port );
311 // for debuging purpose
312 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
314 if ( authzHeader != null )
316 WebClient.client( service ).header( "Authorization", authzHeader );
321 protected UserService getUserService( String authzHeader )
323 UserService service =
324 JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
325 UserService.class, Collections.singletonList( new JacksonJaxbJsonProvider() ) );
327 WebClient.client( service ).header( "Referer", "http://localhost:" + port );
329 // for debuging purpose
330 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
332 if ( authzHeader != null )
334 WebClient.client( service ).header( "Authorization", authzHeader );
339 protected FakeCreateAdminService getFakeCreateAdminService()
341 return JAXRSClientFactory.create(
342 "http://localhost:" + port + "/" + getRestServicesPath() + "/fakeCreateAdminService/",
343 FakeCreateAdminService.class );
347 protected List<String> getZipEntriesNames( ZipFile zipFile )
351 List<String> entriesNames = new ArrayList<>();
352 Enumeration<? extends ZipEntry> entries = zipFile.entries();
353 while ( entries.hasMoreElements() )
355 entriesNames.add( entries.nextElement().getName() );
359 catch ( Throwable e )
361 log.info( "fail to get zipEntries {}", e.getMessage(), e );
363 return Collections.emptyList();