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