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.Collections;
56 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
57 import org.junit.runner.RunWith;
60 * @author Olivier Lamy
62 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
63 public abstract class AbstractDownloadTest
67 protected Logger log = LoggerFactory.getLogger( getClass() );
69 static String previousAppServerBase;
71 public String authorizationHeader = getAdminAuthzHeader();
73 public Server server = null;
79 public static String encode( String uid, String password )
81 return "Basic " + Base64Utility.encode( ( uid + ":" + password ).getBytes() );
84 public static String getAdminAuthzHeader()
86 String adminPwdSysProps = System.getProperty( "rest.admin.pwd" );
87 if ( StringUtils.isBlank( adminPwdSysProps ) )
89 return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, FakeCreateAdminService.ADMIN_TEST_PWD );
91 return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, adminPwdSysProps );
95 protected abstract String getSpringConfigLocation();
98 protected String getRestServicesPath()
100 return "restServices";
105 public void startServer()
109 System.setProperty( "redback.admin.creation.file", "target/auto-admin-creation.properties" );
112 this.server = new Server( 0 );
114 ServletContextHandler context = new ServletContextHandler();
116 context.setContextPath( "/" );
118 context.setInitParameter( "contextConfigLocation", getSpringConfigLocation() );
120 ContextLoaderListener contextLoaderListener = new ContextLoaderListener();
122 context.addEventListener( contextLoaderListener );
124 ServletHolder sh = new ServletHolder( CXFServlet.class );
126 SessionHandler sessionHandler = new SessionHandler();
128 context.setSessionHandler( sessionHandler );
130 context.addServlet( sh, "/" + getRestServicesPath() + "/*" );
132 ServletHolder repoSh = new ServletHolder( RepositoryServlet.class );
133 context.addServlet( repoSh, "/repository/*" );
135 server.setHandler( context );
137 Connector connector = this.server.getConnectors()[0];
138 this.port = connector.getLocalPort();
140 tomcat = new Tomcat();
141 tomcat.setBaseDir( System.getProperty( "java.io.tmpdir" ) );
144 Context context = tomcat.addContext( "", System.getProperty( "java.io.tmpdir" ) );
146 ApplicationParameter applicationParameter = new ApplicationParameter();
147 applicationParameter.setName( "contextConfigLocation" );
148 applicationParameter.setValue( getSpringConfigLocation() );
149 context.addApplicationParameter( applicationParameter );
151 context.addApplicationListener( new ApplicationListener( ContextLoaderListener.class.getName(), false ) );
153 tomcat.addServlet( context, "cxf", new CXFServlet() );
154 context.addServletMapping( "/" + getRestServicesPath() + "/*" , "cxf" );
156 tomcat.addServlet( context, "archivarepo", new RepositoryServlet() );
157 context.addServletMapping( "/repository/*" , "archivarepo" );
161 port = tomcat.getConnector().getLocalPort();
164 log.info( "start server on port {}", this.port );
166 User user = new User();
167 user.setEmail( "toto@toto.fr" );
168 user.setFullName( "the root user" );
169 user.setUsername( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME );
170 user.setPassword( FakeCreateAdminService.ADMIN_TEST_PWD );
172 getUserService( null ).createAdminUser( user );
179 public void tearDown()
182 System.clearProperty( "redback.admin.creation.file" );
184 if ( this.server != null )
188 if (this.tomcat != null)
195 protected ProxyConnectorService getProxyConnectorService()
197 ProxyConnectorService service =
198 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
199 ProxyConnectorService.class,
200 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
202 WebClient.client( service ).header( "Authorization", authorizationHeader );
203 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
207 protected RemoteRepositoriesService getRemoteRepositoriesService()
209 RemoteRepositoriesService service =
210 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
211 RemoteRepositoriesService.class,
212 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
214 WebClient.client( service ).header( "Authorization", authorizationHeader );
215 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
219 protected ManagedRepositoriesService getManagedRepositoriesService()
221 ManagedRepositoriesService service =
222 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
223 ManagedRepositoriesService.class,
224 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
226 WebClient.client( service ).header( "Authorization", authorizationHeader );
227 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
232 protected RepositoryGroupService getRepositoryGroupService()
234 RepositoryGroupService service =
235 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
236 RepositoryGroupService.class,
237 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
239 WebClient.client( service ).header( "Authorization", authorizationHeader );
240 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
244 protected RepositoriesService getRepositoriesService()
246 RepositoriesService service =
247 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
248 RepositoriesService.class,
249 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
251 WebClient.client( service ).header( "Authorization", authorizationHeader );
252 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
256 protected SearchService getSearchService()
258 SearchService service =
259 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
261 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
263 WebClient.client( service ).header( "Authorization", authorizationHeader );
264 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
268 protected String getBaseUrl()
270 String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
271 return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
275 protected RoleManagementService getRoleManagementService( String authzHeader )
277 RoleManagementService service =
278 JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
279 RoleManagementService.class,
280 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
282 // for debuging purpose
283 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
285 if ( authzHeader != null )
287 WebClient.client( service ).header( "Authorization", authzHeader );
292 protected UserService getUserService( String authzHeader )
294 UserService service =
295 JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
296 UserService.class, Collections.singletonList( new JacksonJaxbJsonProvider() ) );
298 // for debuging purpose
299 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
301 if ( authzHeader != null )
303 WebClient.client( service ).header( "Authorization", authzHeader );
308 protected FakeCreateAdminService getFakeCreateAdminService()
310 return JAXRSClientFactory.create(
311 "http://localhost:" + port + "/" + getRestServicesPath() + "/fakeCreateAdminService/",
312 FakeCreateAdminService.class );