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