]> source.dussan.org Git - archiva.git/blob
34a93d15e7f1066e548b657d11a5d9f25d48c7ee
[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.BaseSetup;
28 import org.apache.archiva.redback.rest.services.FakeCreateAdminService;
29 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
30 import org.apache.archiva.rest.api.services.ProxyConnectorService;
31 import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
32 import org.apache.archiva.rest.api.services.RepositoriesService;
33 import org.apache.archiva.rest.api.services.RepositoryGroupService;
34 import org.apache.archiva.rest.api.services.SearchService;
35 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
36 import org.apache.archiva.webdav.RepositoryServlet;
37 import org.apache.commons.io.FileUtils;
38 import org.apache.commons.lang3.StringUtils;
39 import org.apache.commons.lang3.SystemUtils;
40 import org.apache.cxf.common.util.Base64Utility;
41 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
42 import org.apache.cxf.jaxrs.client.WebClient;
43 import org.apache.cxf.transport.servlet.CXFServlet;
44 import org.eclipse.jetty.server.HttpConnectionFactory;
45 import org.eclipse.jetty.server.Server;
46 import org.eclipse.jetty.server.ServerConnector;
47 import org.eclipse.jetty.server.session.SessionHandler;
48 import org.eclipse.jetty.servlet.ServletContextHandler;
49 import org.eclipse.jetty.servlet.ServletHolder;
50 import org.junit.After;
51 import org.junit.Before;
52 import org.junit.runner.RunWith;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55 import org.springframework.web.context.ContextLoaderListener;
56
57 import java.nio.file.Files;
58 import java.nio.file.Path;
59 import java.nio.file.Paths;
60 import java.util.ArrayList;
61 import java.util.Collections;
62 import java.util.Enumeration;
63 import java.util.List;
64 import java.util.concurrent.atomic.AtomicReference;
65 import java.util.zip.ZipEntry;
66 import java.util.zip.ZipFile;
67
68 /**
69  * @author Olivier Lamy
70  */
71 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
72 public abstract class AbstractDownloadTest
73     extends TestCase
74 {
75
76     AtomicReference<Path> projectDir = new AtomicReference<>( );
77     AtomicReference<Path> basePath = new AtomicReference<>( );
78
79     protected List<Path> createdPaths = new ArrayList<>( );
80
81     protected final Logger log = LoggerFactory.getLogger( getClass() );
82
83     protected static String previousAppServerBase;
84
85     public String authorizationHeader = getAdminAuthzHeader();
86
87     public Server server = null;
88
89     ServerConnector serverConnector;
90
91     public int port;
92
93     protected Path getProjectDirectory() {
94         if ( projectDir.get()==null) {
95             String propVal = System.getProperty("mvn.project.base.dir");
96             Path newVal;
97             if (StringUtils.isEmpty(propVal)) {
98                 newVal = Paths.get("").toAbsolutePath();
99             } else {
100                 newVal = Paths.get(propVal).toAbsolutePath();
101             }
102             projectDir.compareAndSet(null, newVal);
103         }
104         return projectDir.get();
105     }
106
107     public Path getBasedir()
108     {
109         if (basePath.get()==null) {
110             String baseDir = System.getProperty( "basedir" );
111             final Path baseDirPath;
112             if (StringUtils.isNotEmpty( baseDir ))  {
113                 baseDirPath = Paths.get( baseDir );
114             } else {
115                 baseDirPath = getProjectDirectory( );
116             }
117             basePath.compareAndSet( null, baseDirPath );
118         }
119         return basePath.get( );
120     }
121
122
123
124     public static String encode( String uid, String password )
125     {
126         return "Basic " + Base64Utility.encode( ( uid + ":" + password ).getBytes() );
127     }
128
129     public static String getAdminAuthzHeader()
130     {
131         String adminPwdSysProps = System.getProperty( "rest.admin.pwd" );
132         if ( StringUtils.isBlank( adminPwdSysProps ) )
133         {
134             return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, BaseSetup.getAdminPwd() );
135         }
136         return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, adminPwdSysProps );
137     }
138
139
140     protected abstract String getSpringConfigLocation();
141
142
143     protected String getRestServicesPath()
144     {
145         return "restServices";
146     }
147
148
149     @Before
150     public void startServer()
151         throws Exception
152     {
153
154         System.setProperty( "redback.admin.creation.file", "target/auto-admin-creation.properties" );
155
156         server = new Server();
157         serverConnector = new ServerConnector( server, new HttpConnectionFactory() );
158         server.addConnector( serverConnector );
159
160         ServletHolder servletHolder = new ServletHolder( new CXFServlet() );
161         ServletContextHandler context = new ServletContextHandler( ServletContextHandler.SESSIONS );
162         context.setResourceBase( SystemUtils.JAVA_IO_TMPDIR );
163         context.setSessionHandler( new SessionHandler() );
164         context.addServlet( servletHolder, "/" + getRestServicesPath() + "/*" );
165         context.setInitParameter( "contextConfigLocation", getSpringConfigLocation() );
166         context.addEventListener( new ContextLoaderListener() );
167
168         ServletHolder servletHolderRepo = new ServletHolder( new RepositoryServlet() );
169         context.addServlet( servletHolderRepo, "/repository/*" );
170
171         server.setHandler( context );
172         server.start();
173         port = serverConnector.getLocalPort();
174         log.info( "start server on port {}", this.port );
175
176         User user = new User();
177         user.setEmail( "toto@toto.fr" );
178         user.setFullName( "the root user" );
179         user.setUsername( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME );
180         user.setPassword( BaseSetup.getAdminPwd() );
181
182         getUserService( null ).createAdminUser( user );
183
184
185     }
186
187
188     @After
189     @Override
190     public void tearDown()
191         throws Exception
192     {
193
194         for(Path dir : createdPaths) {
195             if ( Files.exists( dir)) {
196                 FileUtils.deleteQuietly( dir.toFile( ) );
197             }
198         }
199         createdPaths.clear();
200
201         System.clearProperty( "redback.admin.creation.file" );
202         super.tearDown();
203         if ( this.server != null )
204         {
205             this.server.stop();
206         }
207     }
208
209
210     protected ProxyConnectorService getProxyConnectorService()
211     {
212         ProxyConnectorService service =
213             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
214                                        ProxyConnectorService.class,
215                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
216
217         WebClient.client( service ).header( "Authorization", authorizationHeader );
218         WebClient.client( service ).header( "Referer", "http://localhost:" + port );
219
220         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
221         return service;
222     }
223
224     protected RemoteRepositoriesService getRemoteRepositoriesService()
225     {
226         RemoteRepositoriesService service =
227             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
228                                        RemoteRepositoriesService.class,
229                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
230
231         WebClient.client( service ).header( "Authorization", authorizationHeader );
232         WebClient.client( service ).header( "Referer", "http://localhost:" + port );
233
234         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
235         return service;
236     }
237
238     protected ManagedRepositoriesService getManagedRepositoriesService()
239     {
240         ManagedRepositoriesService service =
241             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
242                                        ManagedRepositoriesService.class,
243                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
244
245         WebClient.client( service ).header( "Authorization", authorizationHeader );
246         WebClient.client( service ).header( "Referer", "http://localhost:" + port );
247
248         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
249         return service;
250     }
251
252
253     protected RepositoryGroupService getRepositoryGroupService()
254     {
255         RepositoryGroupService service =
256             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
257                                        RepositoryGroupService.class,
258                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
259
260         WebClient.client( service ).header( "Authorization", authorizationHeader );
261         WebClient.client( service ).header( "Referer", "http://localhost:" + port );
262
263         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
264         return service;
265     }
266
267     protected RepositoriesService getRepositoriesService()
268     {
269         RepositoriesService service =
270             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
271                                        RepositoriesService.class,
272                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
273
274         WebClient.client( service ).header( "Authorization", authorizationHeader );
275         WebClient.client( service ).header( "Referer", "http://localhost:" + port );
276
277         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
278         return service;
279     }
280
281     protected SearchService getSearchService()
282     {
283         SearchService service =
284             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
285                                        SearchService.class,
286                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
287
288         WebClient.client( service ).header( "Authorization", authorizationHeader );
289         WebClient.client( service ).header( "Referer", "http://localhost:" + port );
290
291         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
292         return service;
293     }
294
295     protected String getBaseUrl()
296     {
297         String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
298         return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
299     }
300
301
302     protected RoleManagementService getRoleManagementService( String authzHeader )
303     {
304         RoleManagementService service =
305             JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
306                                        RoleManagementService.class,
307                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
308
309         WebClient.client( service ).header( "Referer", "http://localhost:" + port );
310
311         // for debuging purpose
312         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
313
314         if ( authzHeader != null )
315         {
316             WebClient.client( service ).header( "Authorization", authzHeader );
317         }
318         return service;
319     }
320
321     protected UserService getUserService( String authzHeader )
322     {
323         UserService service =
324             JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
325                                        UserService.class, Collections.singletonList( new JacksonJaxbJsonProvider() ) );
326
327         WebClient.client( service ).header( "Referer", "http://localhost:" + port );
328
329         // for debuging purpose
330         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
331
332         if ( authzHeader != null )
333         {
334             WebClient.client( service ).header( "Authorization", authzHeader );
335         }
336         return service;
337     }
338
339     protected FakeCreateAdminService getFakeCreateAdminService()
340     {
341         return JAXRSClientFactory.create(
342             "http://localhost:" + port + "/" + getRestServicesPath() + "/fakeCreateAdminService/",
343             FakeCreateAdminService.class );
344     }
345
346
347     protected List<String> getZipEntriesNames( ZipFile zipFile )
348     {
349         try
350         {
351             List<String> entriesNames = new ArrayList<>();
352             Enumeration<? extends ZipEntry> entries = zipFile.entries();
353             while ( entries.hasMoreElements() )
354             {
355                 entriesNames.add( entries.nextElement().getName() );
356             }
357             return entriesNames;
358         }
359         catch ( Throwable e )
360         {
361             log.info( "fail to get zipEntries {}", e.getMessage(), e );
362         }
363         return Collections.emptyList();
364     }
365 }