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