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.FakeCreateAdminService;
28 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
29 import org.apache.archiva.rest.api.services.ProxyConnectorService;
30 import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
31 import org.apache.archiva.rest.api.services.RepositoriesService;
32 import org.apache.archiva.rest.api.services.RepositoryGroupService;
33 import org.apache.archiva.rest.api.services.SearchService;
34 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
35 import org.apache.archiva.webdav.RepositoryServlet;
36 import org.apache.catalina.Context;
37 import org.apache.catalina.deploy.ApplicationListener;
38 import org.apache.catalina.deploy.ApplicationParameter;
39 import org.apache.catalina.startup.Tomcat;
40 import org.apache.commons.lang.StringUtils;
41 import org.apache.cxf.common.util.Base64Utility;
42 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
43 import org.apache.cxf.jaxrs.client.WebClient;
44 import org.apache.cxf.transport.servlet.CXFServlet;
45 import org.eclipse.jetty.server.Server;
46 import org.junit.After;
47 import org.junit.Before;
48 import org.junit.runner.RunWith;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import org.springframework.web.context.ContextLoaderListener;
53 import java.util.ArrayList;
54 import java.util.Collections;
55 import java.util.Enumeration;
56 import java.util.List;
57 import java.util.zip.ZipEntry;
58 import java.util.zip.ZipFile;
61 * @author Olivier Lamy
63 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
64 public abstract class AbstractDownloadTest
68 protected final Logger log = LoggerFactory.getLogger( getClass() );
70 protected static String previousAppServerBase;
72 public String authorizationHeader = getAdminAuthzHeader();
74 public Server server = null;
80 public static String encode( String uid, String password )
82 return "Basic " + Base64Utility.encode( ( uid + ":" + password ).getBytes() );
85 public static String getAdminAuthzHeader()
87 String adminPwdSysProps = System.getProperty( "rest.admin.pwd" );
88 if ( StringUtils.isBlank( adminPwdSysProps ) )
90 return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, FakeCreateAdminService.ADMIN_TEST_PWD );
92 return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, adminPwdSysProps );
96 protected abstract String getSpringConfigLocation();
99 protected String getRestServicesPath()
101 return "restServices";
106 public void startServer()
110 System.setProperty( "redback.admin.creation.file", "target/auto-admin-creation.properties" );
112 tomcat = new Tomcat();
113 tomcat.setBaseDir( System.getProperty( "java.io.tmpdir" ) );
116 Context context = tomcat.addContext( "", System.getProperty( "java.io.tmpdir" ) );
118 ApplicationParameter applicationParameter = new ApplicationParameter();
119 applicationParameter.setName( "contextConfigLocation" );
120 applicationParameter.setValue( getSpringConfigLocation() );
121 context.addApplicationParameter( applicationParameter );
123 context.addApplicationListener( new ApplicationListener( ContextLoaderListener.class.getName(), false ) );
125 tomcat.addServlet( context, "cxf", new CXFServlet() );
126 context.addServletMapping( "/" + getRestServicesPath() + "/*" , "cxf" );
128 tomcat.addServlet( context, "archivarepo", new RepositoryServlet() );
129 context.addServletMapping( "/repository/*" , "archivarepo" );
133 port = tomcat.getConnector().getLocalPort();
136 log.info( "start server on port {}", this.port );
138 User user = new User();
139 user.setEmail( "toto@toto.fr" );
140 user.setFullName( "the root user" );
141 user.setUsername( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME );
142 user.setPassword( FakeCreateAdminService.ADMIN_TEST_PWD );
144 getUserService( null ).createAdminUser( user );
152 public void tearDown()
155 System.clearProperty( "redback.admin.creation.file" );
157 if ( this.server != null )
161 if (this.tomcat != null)
168 protected ProxyConnectorService getProxyConnectorService()
170 ProxyConnectorService service =
171 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
172 ProxyConnectorService.class,
173 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
175 WebClient.client( service ).header( "Authorization", authorizationHeader );
176 WebClient.client(service).header("Referer","http://localhost:"+port);
178 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
182 protected RemoteRepositoriesService getRemoteRepositoriesService()
184 RemoteRepositoriesService service =
185 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
186 RemoteRepositoriesService.class,
187 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
189 WebClient.client( service ).header( "Authorization", authorizationHeader );
190 WebClient.client(service).header("Referer","http://localhost:"+port);
192 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
196 protected ManagedRepositoriesService getManagedRepositoriesService()
198 ManagedRepositoriesService service =
199 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
200 ManagedRepositoriesService.class,
201 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
203 WebClient.client( service ).header( "Authorization", authorizationHeader );
204 WebClient.client(service).header("Referer","http://localhost:"+port);
206 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
211 protected RepositoryGroupService getRepositoryGroupService()
213 RepositoryGroupService service =
214 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
215 RepositoryGroupService.class,
216 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
218 WebClient.client( service ).header( "Authorization", authorizationHeader );
219 WebClient.client(service).header("Referer","http://localhost:"+port);
221 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
225 protected RepositoriesService getRepositoriesService()
227 RepositoriesService service =
228 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
229 RepositoriesService.class,
230 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
232 WebClient.client( service ).header( "Authorization", authorizationHeader );
233 WebClient.client(service).header("Referer","http://localhost:"+port);
235 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
239 protected SearchService getSearchService()
241 SearchService service =
242 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
244 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
246 WebClient.client( service ).header( "Authorization", authorizationHeader );
247 WebClient.client(service).header("Referer","http://localhost:"+port);
249 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
253 protected String getBaseUrl()
255 String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
256 return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
260 protected RoleManagementService getRoleManagementService( String authzHeader )
262 RoleManagementService service =
263 JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
264 RoleManagementService.class,
265 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
267 WebClient.client(service).header("Referer","http://localhost:"+port);
269 // for debuging purpose
270 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
272 if ( authzHeader != null )
274 WebClient.client( service ).header( "Authorization", authzHeader );
279 protected UserService getUserService( String authzHeader )
281 UserService service =
282 JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
283 UserService.class, Collections.singletonList( new JacksonJaxbJsonProvider() ) );
285 WebClient.client(service).header("Referer","http://localhost:"+port);
287 // for debuging purpose
288 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
290 if ( authzHeader != null )
292 WebClient.client( service ).header( "Authorization", authzHeader );
297 protected FakeCreateAdminService getFakeCreateAdminService()
299 return JAXRSClientFactory.create(
300 "http://localhost:" + port + "/" + getRestServicesPath() + "/fakeCreateAdminService/",
301 FakeCreateAdminService.class );
305 protected List<String> getZipEntriesNames( ZipFile zipFile )
309 List<String> entriesNames = new ArrayList<>();
310 Enumeration<? extends ZipEntry> entries = zipFile.entries();
311 while ( entries.hasMoreElements() )
313 entriesNames.add( entries.nextElement().getName() );
317 catch ( Throwable e )
319 log.info( "fail to get zipEntries {}", e.getMessage(), e );
321 return Collections.emptyList();