]> source.dussan.org Git - archiva.git/blob
bd0e48a469b040146dea765ab8c7e5a14893bfc7
[archiva.git] /
1 package org.apache.archiva.remotedownload;
2 /*
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
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
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
18  * under the License.
19  */
20
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;
54
55 import java.util.Collections;
56 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
57 import org.junit.runner.RunWith;
58
59 /**
60  * @author Olivier Lamy
61  */
62 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
63 public abstract class AbstractDownloadTest
64     extends TestCase
65 {
66
67     protected Logger log = LoggerFactory.getLogger( getClass() );
68
69     static String previousAppServerBase;
70
71     public String authorizationHeader = getAdminAuthzHeader();
72
73     public Server server = null;
74
75     public Tomcat tomcat;
76
77     public int port;
78
79     public static String encode( String uid, String password )
80     {
81         return "Basic " + Base64Utility.encode( ( uid + ":" + password ).getBytes() );
82     }
83
84     public static String getAdminAuthzHeader()
85     {
86         String adminPwdSysProps = System.getProperty( "rest.admin.pwd" );
87         if ( StringUtils.isBlank( adminPwdSysProps ) )
88         {
89             return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, FakeCreateAdminService.ADMIN_TEST_PWD );
90         }
91         return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, adminPwdSysProps );
92     }
93
94
95     protected abstract String getSpringConfigLocation();
96
97
98     protected String getRestServicesPath()
99     {
100         return "restServices";
101     }
102
103
104     @Before
105     public void startServer()
106         throws Exception
107     {
108
109         System.setProperty( "redback.admin.creation.file", "target/auto-admin-creation.properties" );
110
111         /*
112         this.server = new Server( 0 );
113
114         ServletContextHandler context = new ServletContextHandler();
115
116         context.setContextPath( "/" );
117
118         context.setInitParameter( "contextConfigLocation", getSpringConfigLocation() );
119
120         ContextLoaderListener contextLoaderListener = new ContextLoaderListener();
121
122         context.addEventListener( contextLoaderListener );
123
124         ServletHolder sh = new ServletHolder( CXFServlet.class );
125
126         SessionHandler sessionHandler = new SessionHandler();
127
128         context.setSessionHandler( sessionHandler );
129
130         context.addServlet( sh, "/" + getRestServicesPath() + "/*" );
131
132         ServletHolder repoSh = new ServletHolder( RepositoryServlet.class );
133         context.addServlet( repoSh, "/repository/*" );
134
135         server.setHandler( context );
136         this.server.start();
137         Connector connector = this.server.getConnectors()[0];
138         this.port = connector.getLocalPort();
139         */
140         tomcat = new Tomcat();
141         tomcat.setBaseDir( System.getProperty( "java.io.tmpdir" ) );
142         tomcat.setPort( 0 );
143
144         Context context = tomcat.addContext( "", System.getProperty( "java.io.tmpdir" ) );
145
146         ApplicationParameter applicationParameter = new ApplicationParameter();
147         applicationParameter.setName( "contextConfigLocation" );
148         applicationParameter.setValue( getSpringConfigLocation() );
149         context.addApplicationParameter( applicationParameter );
150
151         context.addApplicationListener( new ApplicationListener( ContextLoaderListener.class.getName(), false ) );
152
153         tomcat.addServlet( context, "cxf", new CXFServlet() );
154         context.addServletMapping( "/" + getRestServicesPath() + "/*" , "cxf" );
155
156         tomcat.addServlet( context, "archivarepo", new RepositoryServlet() );
157         context.addServletMapping( "/repository/*" , "archivarepo" );
158
159         tomcat.start();
160
161         port = tomcat.getConnector().getLocalPort();
162
163
164         log.info( "start server on port {}", this.port );
165
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 );
171
172         getUserService( null ).createAdminUser( user );
173
174
175     }
176
177
178     @After
179     public void tearDown()
180         throws Exception
181     {
182         System.clearProperty( "redback.admin.creation.file" );
183         super.tearDown();
184         if ( this.server != null )
185         {
186             this.server.stop();
187         }
188         if (this.tomcat != null)
189         {
190             this.tomcat.stop();
191         }
192     }
193
194
195     protected ProxyConnectorService getProxyConnectorService()
196     {
197         ProxyConnectorService service =
198             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
199                                        ProxyConnectorService.class,
200                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
201
202         WebClient.client( service ).header( "Authorization", authorizationHeader );
203         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
204         return service;
205     }
206
207     protected RemoteRepositoriesService getRemoteRepositoriesService()
208     {
209         RemoteRepositoriesService service =
210             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
211                                        RemoteRepositoriesService.class,
212                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
213
214         WebClient.client( service ).header( "Authorization", authorizationHeader );
215         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
216         return service;
217     }
218
219     protected ManagedRepositoriesService getManagedRepositoriesService()
220     {
221         ManagedRepositoriesService service =
222             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
223                                        ManagedRepositoriesService.class,
224                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
225
226         WebClient.client( service ).header( "Authorization", authorizationHeader );
227         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
228         return service;
229     }
230
231
232     protected RepositoryGroupService getRepositoryGroupService()
233     {
234         RepositoryGroupService service =
235             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
236                                        RepositoryGroupService.class,
237                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
238
239         WebClient.client( service ).header( "Authorization", authorizationHeader );
240         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
241         return service;
242     }
243
244     protected RepositoriesService getRepositoriesService()
245     {
246         RepositoriesService service =
247             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
248                                        RepositoriesService.class,
249                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
250
251         WebClient.client( service ).header( "Authorization", authorizationHeader );
252         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
253         return service;
254     }
255
256     protected SearchService getSearchService()
257     {
258         SearchService service =
259             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
260                                        SearchService.class,
261                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
262
263         WebClient.client( service ).header( "Authorization", authorizationHeader );
264         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
265         return service;
266     }
267
268     protected String getBaseUrl()
269     {
270         String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
271         return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
272     }
273
274
275     protected RoleManagementService getRoleManagementService( String authzHeader )
276     {
277         RoleManagementService service =
278             JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
279                                        RoleManagementService.class,
280                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
281
282         // for debuging purpose
283         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
284
285         if ( authzHeader != null )
286         {
287             WebClient.client( service ).header( "Authorization", authzHeader );
288         }
289         return service;
290     }
291
292     protected UserService getUserService( String authzHeader )
293     {
294         UserService service =
295             JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
296                                        UserService.class, Collections.singletonList( new JacksonJaxbJsonProvider() ) );
297
298         // for debuging purpose
299         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
300
301         if ( authzHeader != null )
302         {
303             WebClient.client( service ).header( "Authorization", authzHeader );
304         }
305         return service;
306     }
307
308     protected FakeCreateAdminService getFakeCreateAdminService()
309     {
310         return JAXRSClientFactory.create(
311             "http://localhost:" + port + "/" + getRestServicesPath() + "/fakeCreateAdminService/",
312             FakeCreateAdminService.class );
313     }
314
315 }