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.commons.lang.StringUtils;
31 import org.apache.cxf.common.util.Base64Utility;
32 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
33 import org.apache.cxf.jaxrs.client.WebClient;
34 import org.apache.cxf.transport.servlet.CXFServlet;
35 import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants;
36 import org.apache.archiva.redback.rest.api.model.User;
37 import org.apache.archiva.redback.rest.api.services.RoleManagementService;
38 import org.apache.archiva.redback.rest.api.services.UserService;
39 import org.apache.archiva.redback.rest.services.FakeCreateAdminService;
40 import org.eclipse.jetty.server.Connector;
41 import org.eclipse.jetty.server.Server;
42 import org.eclipse.jetty.server.session.SessionHandler;
43 import org.eclipse.jetty.servlet.ServletContextHandler;
44 import org.eclipse.jetty.servlet.ServletHolder;
45 import org.junit.After;
46 import org.junit.Before;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.springframework.web.context.ContextLoaderListener;
51 import java.util.Collections;
52 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
53 import org.junit.runner.RunWith;
56 * @author Olivier Lamy
58 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
59 public abstract class AbstractDownloadTest
63 protected Logger log = LoggerFactory.getLogger( getClass() );
65 static String previousAppServerBase;
67 public String authorizationHeader = getAdminAuthzHeader();
69 public Server server = null;
73 public static String encode( String uid, String password )
75 return "Basic " + Base64Utility.encode( ( uid + ":" + password ).getBytes() );
78 public static String getAdminAuthzHeader()
80 String adminPwdSysProps = System.getProperty( "rest.admin.pwd" );
81 if ( StringUtils.isBlank( adminPwdSysProps ) )
83 return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, FakeCreateAdminService.ADMIN_TEST_PWD );
85 return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, adminPwdSysProps );
89 protected abstract String getSpringConfigLocation();
92 protected String getRestServicesPath()
94 return "restServices";
99 public void startServer()
103 System.setProperty( "redback.admin.creation.file", "target/auto-admin-creation.properties" );
104 this.server = new Server( 0 );
106 ServletContextHandler context = new ServletContextHandler();
108 context.setContextPath( "/" );
110 context.setInitParameter( "contextConfigLocation", getSpringConfigLocation() );
112 ContextLoaderListener contextLoaderListener = new ContextLoaderListener();
114 context.addEventListener( contextLoaderListener );
116 ServletHolder sh = new ServletHolder( CXFServlet.class );
118 SessionHandler sessionHandler = new SessionHandler();
120 context.setSessionHandler( sessionHandler );
122 context.addServlet( sh, "/" + getRestServicesPath() + "/*" );
124 ServletHolder repoSh = new ServletHolder( RepositoryServlet.class );
125 context.addServlet( repoSh, "/repository/*" );
127 server.setHandler( context );
129 Connector connector = this.server.getConnectors()[0];
130 this.port = connector.getLocalPort();
131 log.info( "start server on port {}", this.port );
133 User user = new User();
134 user.setEmail( "toto@toto.fr" );
135 user.setFullName( "the root user" );
136 user.setUsername( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME );
137 user.setPassword( FakeCreateAdminService.ADMIN_TEST_PWD );
139 getUserService( null ).createAdminUser( user );
146 public void tearDown()
149 System.clearProperty( "redback.admin.creation.file" );
151 if ( this.server != null )
158 protected ProxyConnectorService getProxyConnectorService()
160 ProxyConnectorService service =
161 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
162 ProxyConnectorService.class,
163 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
165 WebClient.client( service ).header( "Authorization", authorizationHeader );
166 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
170 protected RemoteRepositoriesService getRemoteRepositoriesService()
172 RemoteRepositoriesService service =
173 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
174 RemoteRepositoriesService.class,
175 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
177 WebClient.client( service ).header( "Authorization", authorizationHeader );
178 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
182 protected ManagedRepositoriesService getManagedRepositoriesService()
184 ManagedRepositoriesService service =
185 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
186 ManagedRepositoriesService.class,
187 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
189 WebClient.client( service ).header( "Authorization", authorizationHeader );
190 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
195 protected RepositoryGroupService getRepositoryGroupService()
197 RepositoryGroupService service =
198 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
199 RepositoryGroupService.class,
200 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
202 WebClient.client( service ).header( "Authorization", authorizationHeader );
203 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
207 protected RepositoriesService getRepositoriesService()
209 RepositoriesService service =
210 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
211 RepositoriesService.class,
212 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
214 WebClient.client( service ).header( "Authorization", authorizationHeader );
215 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
219 protected SearchService getSearchService()
221 SearchService service =
222 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
224 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
226 WebClient.client( service ).header( "Authorization", authorizationHeader );
227 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
231 protected String getBaseUrl()
233 String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
234 return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
238 protected RoleManagementService getRoleManagementService( String authzHeader )
240 RoleManagementService service =
241 JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
242 RoleManagementService.class,
243 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
245 // for debuging purpose
246 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
248 if ( authzHeader != null )
250 WebClient.client( service ).header( "Authorization", authzHeader );
255 protected UserService getUserService( String authzHeader )
257 UserService service =
258 JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
259 UserService.class, Collections.singletonList( new JacksonJaxbJsonProvider() ) );
261 // for debuging purpose
262 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
264 if ( authzHeader != null )
266 WebClient.client( service ).header( "Authorization", authzHeader );
271 protected FakeCreateAdminService getFakeCreateAdminService()
273 return JAXRSClientFactory.create(
274 "http://localhost:" + port + "/" + getRestServicesPath() + "/fakeCreateAdminService/",
275 FakeCreateAdminService.class );