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.rest.api.services.ManagedRepositoriesService;
24 import org.apache.archiva.rest.api.services.ProxyConnectorService;
25 import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
26 import org.apache.archiva.rest.api.services.RepositoriesService;
27 import org.apache.archiva.rest.api.services.RepositoryGroupService;
28 import org.apache.archiva.rest.api.services.SearchService;
29 import org.apache.archiva.webdav.RepositoryServlet;
30 import org.apache.catalina.Context;
31 import org.apache.catalina.deploy.ApplicationListener;
32 import org.apache.catalina.deploy.ApplicationParameter;
33 import org.apache.catalina.startup.Tomcat;
34 import org.apache.commons.lang.StringUtils;
35 import org.apache.cxf.common.util.Base64Utility;
36 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
37 import org.apache.cxf.jaxrs.client.WebClient;
38 import org.apache.cxf.transport.servlet.CXFServlet;
39 import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants;
40 import org.apache.archiva.redback.rest.api.model.User;
41 import org.apache.archiva.redback.rest.api.services.RoleManagementService;
42 import org.apache.archiva.redback.rest.api.services.UserService;
43 import org.apache.archiva.redback.rest.services.FakeCreateAdminService;
44 import org.eclipse.jetty.server.Connector;
45 import org.eclipse.jetty.server.Server;
46 import org.eclipse.jetty.server.session.SessionHandler;
47 import org.eclipse.jetty.servlet.ServletContextHandler;
48 import org.eclipse.jetty.servlet.ServletHolder;
49 import org.junit.After;
50 import org.junit.Before;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53 import org.springframework.web.context.ContextLoaderListener;
55 import java.util.ArrayList;
56 import java.util.Collections;
57 import java.util.Enumeration;
58 import java.util.List;
59 import java.util.zip.ZipEntry;
60 import java.util.zip.ZipFile;
62 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
63 import org.junit.runner.RunWith;
66 * @author Olivier Lamy
68 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
69 public abstract class AbstractDownloadTest
73 protected Logger log = LoggerFactory.getLogger( getClass() );
75 protected static String previousAppServerBase;
77 public String authorizationHeader = getAdminAuthzHeader();
79 public Server server = null;
85 public static String encode( String uid, String password )
87 return "Basic " + Base64Utility.encode( ( uid + ":" + password ).getBytes() );
90 public static String getAdminAuthzHeader()
92 String adminPwdSysProps = System.getProperty( "rest.admin.pwd" );
93 if ( StringUtils.isBlank( adminPwdSysProps ) )
95 return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, FakeCreateAdminService.ADMIN_TEST_PWD );
97 return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, adminPwdSysProps );
101 protected abstract String getSpringConfigLocation();
104 protected String getRestServicesPath()
106 return "restServices";
111 public void startServer()
115 System.setProperty( "redback.admin.creation.file", "target/auto-admin-creation.properties" );
117 tomcat = new Tomcat();
118 tomcat.setBaseDir( System.getProperty( "java.io.tmpdir" ) );
121 Context context = tomcat.addContext( "", System.getProperty( "java.io.tmpdir" ) );
123 ApplicationParameter applicationParameter = new ApplicationParameter();
124 applicationParameter.setName( "contextConfigLocation" );
125 applicationParameter.setValue( getSpringConfigLocation() );
126 context.addApplicationParameter( applicationParameter );
128 context.addApplicationListener( new ApplicationListener( ContextLoaderListener.class.getName(), false ) );
130 tomcat.addServlet( context, "cxf", new CXFServlet() );
131 context.addServletMapping( "/" + getRestServicesPath() + "/*" , "cxf" );
133 tomcat.addServlet( context, "archivarepo", new RepositoryServlet() );
134 context.addServletMapping( "/repository/*" , "archivarepo" );
138 port = tomcat.getConnector().getLocalPort();
141 log.info( "start server on port {}", this.port );
143 User user = new User();
144 user.setEmail( "toto@toto.fr" );
145 user.setFullName( "the root user" );
146 user.setUsername( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME );
147 user.setPassword( FakeCreateAdminService.ADMIN_TEST_PWD );
149 getUserService( null ).createAdminUser( user );
157 public void tearDown()
160 System.clearProperty( "redback.admin.creation.file" );
162 if ( this.server != null )
166 if (this.tomcat != null)
173 protected ProxyConnectorService getProxyConnectorService()
175 ProxyConnectorService service =
176 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
177 ProxyConnectorService.class,
178 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
180 WebClient.client( service ).header( "Authorization", authorizationHeader );
181 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
185 protected RemoteRepositoriesService getRemoteRepositoriesService()
187 RemoteRepositoriesService service =
188 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
189 RemoteRepositoriesService.class,
190 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
192 WebClient.client( service ).header( "Authorization", authorizationHeader );
193 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
197 protected ManagedRepositoriesService getManagedRepositoriesService()
199 ManagedRepositoriesService service =
200 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
201 ManagedRepositoriesService.class,
202 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
204 WebClient.client( service ).header( "Authorization", authorizationHeader );
205 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
210 protected RepositoryGroupService getRepositoryGroupService()
212 RepositoryGroupService service =
213 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
214 RepositoryGroupService.class,
215 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
217 WebClient.client( service ).header( "Authorization", authorizationHeader );
218 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
222 protected RepositoriesService getRepositoriesService()
224 RepositoriesService service =
225 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
226 RepositoriesService.class,
227 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
229 WebClient.client( service ).header( "Authorization", authorizationHeader );
230 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
234 protected SearchService getSearchService()
236 SearchService service =
237 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
239 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
241 WebClient.client( service ).header( "Authorization", authorizationHeader );
242 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
246 protected String getBaseUrl()
248 String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
249 return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
253 protected RoleManagementService getRoleManagementService( String authzHeader )
255 RoleManagementService service =
256 JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
257 RoleManagementService.class,
258 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
260 // for debuging purpose
261 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
263 if ( authzHeader != null )
265 WebClient.client( service ).header( "Authorization", authzHeader );
270 protected UserService getUserService( String authzHeader )
272 UserService service =
273 JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
274 UserService.class, Collections.singletonList( new JacksonJaxbJsonProvider() ) );
276 // for debuging purpose
277 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
279 if ( authzHeader != null )
281 WebClient.client( service ).header( "Authorization", authzHeader );
286 protected FakeCreateAdminService getFakeCreateAdminService()
288 return JAXRSClientFactory.create(
289 "http://localhost:" + port + "/" + getRestServicesPath() + "/fakeCreateAdminService/",
290 FakeCreateAdminService.class );
294 protected List<String> getZipEntriesNames( ZipFile zipFile )
298 List<String> entriesNames = new ArrayList<>();
299 Enumeration<? extends ZipEntry> entries = zipFile.entries();
300 while ( entries.hasMoreElements() )
302 entriesNames.add( entries.nextElement().getName() );
306 catch ( Throwable e )
308 log.info( "fail to get zipEntries {}", e.getMessage(), e );
310 return Collections.emptyList();