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.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
180 protected RemoteRepositoriesService getRemoteRepositoriesService()
182 RemoteRepositoriesService service =
183 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
184 RemoteRepositoriesService.class,
185 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
187 WebClient.client( service ).header( "Authorization", authorizationHeader );
188 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
192 protected ManagedRepositoriesService getManagedRepositoriesService()
194 ManagedRepositoriesService service =
195 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
196 ManagedRepositoriesService.class,
197 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
199 WebClient.client( service ).header( "Authorization", authorizationHeader );
200 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
205 protected RepositoryGroupService getRepositoryGroupService()
207 RepositoryGroupService service =
208 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
209 RepositoryGroupService.class,
210 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
212 WebClient.client( service ).header( "Authorization", authorizationHeader );
213 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
217 protected RepositoriesService getRepositoriesService()
219 RepositoriesService service =
220 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
221 RepositoriesService.class,
222 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
224 WebClient.client( service ).header( "Authorization", authorizationHeader );
225 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
229 protected SearchService getSearchService()
231 SearchService service =
232 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
234 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
236 WebClient.client( service ).header( "Authorization", authorizationHeader );
237 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
241 protected String getBaseUrl()
243 String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
244 return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
248 protected RoleManagementService getRoleManagementService( String authzHeader )
250 RoleManagementService service =
251 JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
252 RoleManagementService.class,
253 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
255 // for debuging purpose
256 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
258 if ( authzHeader != null )
260 WebClient.client( service ).header( "Authorization", authzHeader );
265 protected UserService getUserService( String authzHeader )
267 UserService service =
268 JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
269 UserService.class, Collections.singletonList( new JacksonJaxbJsonProvider() ) );
271 // for debuging purpose
272 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
274 if ( authzHeader != null )
276 WebClient.client( service ).header( "Authorization", authzHeader );
281 protected FakeCreateAdminService getFakeCreateAdminService()
283 return JAXRSClientFactory.create(
284 "http://localhost:" + port + "/" + getRestServicesPath() + "/fakeCreateAdminService/",
285 FakeCreateAdminService.class );
289 protected List<String> getZipEntriesNames( ZipFile zipFile )
293 List<String> entriesNames = new ArrayList<>();
294 Enumeration<? extends ZipEntry> entries = zipFile.entries();
295 while ( entries.hasMoreElements() )
297 entriesNames.add( entries.nextElement().getName() );
301 catch ( Throwable e )
303 log.info( "fail to get zipEntries {}", e.getMessage(), e );
305 return Collections.emptyList();