1 package org.apache.archiva;
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 junit.framework.TestCase;
22 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
23 import org.apache.archiva.rest.api.services.ProxyConnectorService;
24 import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
25 import org.apache.archiva.rest.api.services.RepositoriesService;
26 import org.apache.archiva.rest.api.services.RepositoryGroupService;
27 import org.apache.archiva.rest.api.services.SearchService;
28 import org.apache.archiva.webdav.RepositoryServlet;
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.cxf.common.util.Base64Utility;
31 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
32 import org.apache.cxf.jaxrs.client.WebClient;
33 import org.apache.cxf.transport.servlet.CXFServlet;
34 import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider;
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;
54 * @author Olivier Lamy
56 public abstract class AbstractDownloadTest
60 protected Logger log = LoggerFactory.getLogger( getClass() );
62 static String previousAppServerBase;
64 public String authorizationHeader = getAdminAuthzHeader();
66 public Server server = null;
70 public static String encode( String uid, String password )
72 return "Basic " + Base64Utility.encode( ( uid + ":" + password ).getBytes() );
75 public static String getAdminAuthzHeader()
77 String adminPwdSysProps = System.getProperty( "rest.admin.pwd" );
78 if ( StringUtils.isBlank( adminPwdSysProps ) )
80 return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, FakeCreateAdminService.ADMIN_TEST_PWD );
82 return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, adminPwdSysProps );
86 protected abstract String getSpringConfigLocation();
89 protected String getRestServicesPath()
91 return "restServices";
96 public void startServer()
100 System.setProperty( "redback.admin.creation.file", "target/auto-admin-creation.properties" );
101 this.server = new Server( 0 );
103 ServletContextHandler context = new ServletContextHandler();
105 context.setContextPath( "/" );
107 context.setInitParameter( "contextConfigLocation", getSpringConfigLocation() );
109 ContextLoaderListener contextLoaderListener = new ContextLoaderListener();
111 context.addEventListener( contextLoaderListener );
113 ServletHolder sh = new ServletHolder( CXFServlet.class );
115 SessionHandler sessionHandler = new SessionHandler();
117 context.setSessionHandler( sessionHandler );
119 context.addServlet( sh, "/" + getRestServicesPath() + "/*" );
121 ServletHolder repoSh = new ServletHolder( RepositoryServlet.class );
122 context.addServlet( repoSh, "/repository/*" );
124 server.setHandler( context );
126 Connector connector = this.server.getConnectors()[0];
127 this.port = connector.getLocalPort();
128 log.info( "start server on port " + this.port );
130 User user = new User();
131 user.setEmail( "toto@toto.fr" );
132 user.setFullName( "the root user" );
133 user.setUsername( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME );
134 user.setPassword( FakeCreateAdminService.ADMIN_TEST_PWD );
136 getUserService( null ).createAdminUser( user );
143 public void tearDown()
146 System.clearProperty( "redback.admin.creation.file" );
148 if ( this.server != null )
155 protected ProxyConnectorService getProxyConnectorService()
157 ProxyConnectorService service =
158 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
159 ProxyConnectorService.class,
160 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
162 WebClient.client( service ).header( "Authorization", authorizationHeader );
163 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
167 protected RemoteRepositoriesService getRemoteRepositoriesService()
169 RemoteRepositoriesService service =
170 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
171 RemoteRepositoriesService.class,
172 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
174 WebClient.client( service ).header( "Authorization", authorizationHeader );
175 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
179 protected ManagedRepositoriesService getManagedRepositoriesService()
181 ManagedRepositoriesService service =
182 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
183 ManagedRepositoriesService.class,
184 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
186 WebClient.client( service ).header( "Authorization", authorizationHeader );
187 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
192 protected RepositoryGroupService getRepositoryGroupService()
194 RepositoryGroupService service =
195 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
196 RepositoryGroupService.class,
197 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
199 WebClient.client( service ).header( "Authorization", authorizationHeader );
200 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
204 protected RepositoriesService getRepositoriesService()
206 RepositoriesService service =
207 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
208 RepositoriesService.class,
209 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
211 WebClient.client( service ).header( "Authorization", authorizationHeader );
212 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
216 protected SearchService getSearchService()
218 SearchService service =
219 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
221 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
223 WebClient.client( service ).header( "Authorization", authorizationHeader );
224 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
228 protected String getBaseUrl()
230 String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
231 return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
235 protected RoleManagementService getRoleManagementService( String authzHeader )
237 RoleManagementService service =
238 JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
239 RoleManagementService.class,
240 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
242 // for debuging purpose
243 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
245 if ( authzHeader != null )
247 WebClient.client( service ).header( "Authorization", authzHeader );
252 protected UserService getUserService( String authzHeader )
254 UserService service =
255 JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
256 UserService.class, Collections.singletonList( new JacksonJaxbJsonProvider() ) );
258 // for debuging purpose
259 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
261 if ( authzHeader != null )
263 WebClient.client( service ).header( "Authorization", authzHeader );
268 protected FakeCreateAdminService getFakeCreateAdminService()
270 return JAXRSClientFactory.create(
271 "http://localhost:" + port + "/" + getRestServicesPath() + "/fakeCreateAdminService/",
272 FakeCreateAdminService.class );