]> source.dussan.org Git - archiva.git/blob
b81f35771008c55fd8592b6119456b548dd06892
[archiva.git] /
1 package org.apache.archiva.rest.services;
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 org.apache.archiva.admin.model.RepositoryAdminException;
22 import org.apache.archiva.admin.model.beans.PropertyEntry;
23 import org.apache.archiva.admin.model.beans.RedbackRuntimeConfiguration;
24 import org.apache.archiva.admin.model.beans.LdapConfiguration;
25 import org.apache.archiva.admin.model.runtime.RedbackRuntimeConfigurationAdmin;
26 import org.apache.archiva.redback.authentication.AuthenticationException;
27 import org.apache.archiva.redback.authentication.Authenticator;
28 import org.apache.archiva.redback.common.ldap.user.LdapUserMapper;
29 import org.apache.archiva.redback.common.ldap.connection.LdapConnection;
30 import org.apache.archiva.redback.common.ldap.connection.LdapConnectionConfiguration;
31 import org.apache.archiva.redback.common.ldap.connection.LdapConnectionFactory;
32 import org.apache.archiva.redback.common.ldap.connection.LdapException;
33 import org.apache.archiva.redback.components.cache.Cache;
34 import org.apache.archiva.redback.policy.CookieSettings;
35 import org.apache.archiva.redback.policy.PasswordRule;
36 import org.apache.archiva.redback.rbac.RBACManager;
37 import org.apache.archiva.redback.users.UserManager;
38 import org.apache.archiva.rest.api.model.RBACManagerImplementationInformation;
39 import org.apache.archiva.rest.api.model.UserManagerImplementationInformation;
40 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
41 import org.apache.archiva.rest.api.services.RedbackRuntimeConfigurationService;
42
43 import org.apache.commons.lang.StringUtils;
44 import org.springframework.context.ApplicationContext;
45 import org.springframework.stereotype.Service;
46
47 import javax.inject.Inject;
48 import javax.inject.Named;
49 import javax.naming.InvalidNameException;
50 import java.util.ArrayList;
51 import java.util.Collection;
52 import java.util.Collections;
53 import java.util.Comparator;
54 import java.util.List;
55 import java.util.Map;
56 import java.util.Properties;
57
58 /**
59  * @author Olivier Lamy
60  * @since 1.4-M4
61  */
62 @Service("redbackRuntimeConfigurationService#rest")
63 public class DefaultRedbackRuntimeConfigurationService
64     extends AbstractRestService
65     implements RedbackRuntimeConfigurationService
66 {
67     @Inject
68     private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin;
69
70     @Inject
71     @Named(value = "userManager#configurable")
72     private UserManager userManager;
73
74     @Inject
75     private ApplicationContext applicationContext;
76
77     @Inject
78     @Named(value = "ldapConnectionFactory#configurable")
79     private LdapConnectionFactory ldapConnectionFactory;
80
81     @Inject
82     @Named(value = "cache#users")
83     private Cache usersCache;
84
85     @Inject
86     private LdapUserMapper ldapUserMapper;
87
88
89     public RedbackRuntimeConfiguration getRedbackRuntimeConfiguration()
90         throws ArchivaRestServiceException
91     {
92         try
93         {
94             return redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration();
95         }
96         catch ( RepositoryAdminException e )
97         {
98             throw new ArchivaRestServiceException( e.getMessage(), e );
99         }
100     }
101
102     public Boolean updateRedbackRuntimeConfiguration( RedbackRuntimeConfiguration redbackRuntimeConfiguration )
103         throws ArchivaRestServiceException
104     {
105         try
106         {
107             // has user manager impl changed ?
108             boolean userManagerChanged = redbackRuntimeConfiguration.getUserManagerImpls().size()
109                 != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls().size();
110
111             userManagerChanged =
112                 userManagerChanged || ( redbackRuntimeConfiguration.getUserManagerImpls().toString().hashCode()
113                     != redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls().toString().hashCode() );
114
115             redbackRuntimeConfigurationAdmin.updateRedbackRuntimeConfiguration( redbackRuntimeConfiguration );
116
117             if ( userManagerChanged )
118             {
119                 log.info( "user managerImpls changed to {} so reload it",
120                           redbackRuntimeConfiguration.getUserManagerImpls() );
121                 userManager.initialize();
122             }
123
124             ldapConnectionFactory.initialize();
125
126             Collection<PasswordRule> passwordRules = applicationContext.getBeansOfType( PasswordRule.class ).values();
127
128             for ( PasswordRule passwordRule : passwordRules )
129             {
130                 passwordRule.initialize();
131             }
132
133             Collection<CookieSettings> cookieSettingsList =
134                 applicationContext.getBeansOfType( CookieSettings.class ).values();
135
136             for ( CookieSettings cookieSettings : cookieSettingsList )
137             {
138                 cookieSettings.initialize();
139             }
140
141             Collection<Authenticator> authenticators =
142                 applicationContext.getBeansOfType( Authenticator.class ).values();
143
144             for ( Authenticator authenticator : authenticators )
145             {
146                 authenticator.initialize();
147             }
148
149             // users cache
150             usersCache.setTimeToIdleSeconds(
151                 redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToIdleSeconds() );
152             usersCache.setTimeToLiveSeconds(
153                 redbackRuntimeConfiguration.getUsersCacheConfiguration().getTimeToLiveSeconds() );
154             usersCache.setMaxElementsInMemory(
155                 redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsInMemory() );
156             usersCache.setMaxElementsOnDisk(
157                 redbackRuntimeConfiguration.getUsersCacheConfiguration().getMaxElementsOnDisk() );
158
159             ldapUserMapper.initialize();
160
161             return Boolean.TRUE;
162         }
163         catch ( AuthenticationException e )
164         {
165             throw new ArchivaRestServiceException( e.getMessage(), e );
166         }
167         catch ( RepositoryAdminException e )
168         {
169             throw new ArchivaRestServiceException( e.getMessage(), e );
170         }
171
172     }
173
174     public List<UserManagerImplementationInformation> getUserManagerImplementationInformations()
175         throws ArchivaRestServiceException
176     {
177
178         Map<String, UserManager> beans = applicationContext.getBeansOfType( UserManager.class );
179
180         if ( beans.isEmpty() )
181         {
182             return Collections.emptyList();
183         }
184
185         List<UserManagerImplementationInformation> informations =
186             new ArrayList<UserManagerImplementationInformation>( beans.size() );
187
188         for ( Map.Entry<String, UserManager> entry : beans.entrySet() )
189         {
190             UserManager userManager = applicationContext.getBean( entry.getKey(), UserManager.class );
191             if ( userManager.isFinalImplementation() )
192             {
193                 UserManagerImplementationInformation information = new UserManagerImplementationInformation();
194                 information.setBeanId( StringUtils.substringAfter( entry.getKey(), "#" ) );
195                 information.setDescriptionKey( userManager.getDescriptionKey() );
196                 information.setReadOnly( userManager.isReadOnly() );
197                 informations.add( information );
198             }
199         }
200
201         return informations;
202     }
203
204     public List<RBACManagerImplementationInformation> getRbacManagerImplementationInformations()
205         throws ArchivaRestServiceException
206     {
207         Map<String, RBACManager> beans = applicationContext.getBeansOfType( RBACManager.class );
208
209         if ( beans.isEmpty() )
210         {
211             return Collections.emptyList();
212         }
213
214         List<RBACManagerImplementationInformation> informations =
215             new ArrayList<RBACManagerImplementationInformation>( beans.size() );
216
217         for ( Map.Entry<String, RBACManager> entry : beans.entrySet() )
218         {
219             UserManager userManager = applicationContext.getBean( entry.getKey(), UserManager.class );
220             if ( userManager.isFinalImplementation() )
221             {
222                 RBACManagerImplementationInformation information = new RBACManagerImplementationInformation();
223                 information.setBeanId( StringUtils.substringAfter( entry.getKey(), "#" ) );
224                 information.setDescriptionKey( userManager.getDescriptionKey() );
225                 information.setReadOnly( userManager.isReadOnly() );
226                 informations.add( information );
227             }
228         }
229
230         return informations;
231     }
232
233     public Boolean checkLdapConnection()
234         throws ArchivaRestServiceException
235     {
236         LdapConnection ldapConnection = null;
237         try
238         {
239             ldapConnection = ldapConnectionFactory.getConnection();
240         }
241         catch ( LdapException e )
242         {
243             log.warn( "fail to get LdapConnection: {}", e.getMessage() );
244             throw new ArchivaRestServiceException( e.getMessage(), e );
245         }
246         finally
247         {
248
249             if ( ldapConnection != null )
250             {
251                 ldapConnection.close();
252             }
253         }
254
255         return Boolean.TRUE;
256     }
257
258     public Boolean checkLdapConnection( LdapConfiguration ldapConfiguration )
259         throws ArchivaRestServiceException
260     {
261         LdapConnection ldapConnection = null;
262         try
263         {
264             LdapConnectionConfiguration ldapConnectionConfiguration =
265                 new LdapConnectionConfiguration( ldapConfiguration.getHostName(), ldapConfiguration.getPort(),
266                                                  ldapConfiguration.getBaseDn(), ldapConfiguration.getContextFactory(),
267                                                  ldapConfiguration.getBindDn(), ldapConfiguration.getPassword(),
268                                                  ldapConfiguration.getAuthenticationMethod(),
269                                                  toProperties( ldapConfiguration.getExtraProperties() ) );
270
271             ldapConnection = ldapConnectionFactory.getConnection( ldapConnectionConfiguration );
272
273             ldapConnection.close();
274
275             // verify groups dn value too
276
277             ldapConnectionConfiguration =
278                 new LdapConnectionConfiguration( ldapConfiguration.getHostName(), ldapConfiguration.getPort(),
279                                                  ldapConfiguration.getBaseGroupsDn(),
280                                                  ldapConfiguration.getContextFactory(), ldapConfiguration.getBindDn(),
281                                                  ldapConfiguration.getPassword(),
282                                                  ldapConfiguration.getAuthenticationMethod(),
283                                                  toProperties( ldapConfiguration.getExtraProperties() ) );
284
285             ldapConnection = ldapConnectionFactory.getConnection( ldapConnectionConfiguration );
286         }
287         catch ( InvalidNameException e )
288         {
289             log.warn( "fail to get LdapConnection: {}", e.getMessage() );
290             throw new ArchivaRestServiceException( e.getMessage(), e );
291         }
292         catch ( LdapException e )
293         {
294             log.warn( "fail to get LdapConnection: {}", e.getMessage() );
295             throw new ArchivaRestServiceException( e.getMessage(), e );
296         }
297         finally
298         {
299
300             if ( ldapConnection != null )
301             {
302                 ldapConnection.close();
303             }
304         }
305
306         return Boolean.TRUE;
307     }
308
309     private Properties toProperties( Map<String, String> map )
310     {
311         Properties properties = new Properties();
312         if ( map == null || map.isEmpty() )
313         {
314             return properties;
315         }
316         for ( Map.Entry<String, String> entry : map.entrySet() )
317         {
318             properties.put( entry.getKey(), entry.getValue() );
319         }
320         return properties;
321     }
322
323 }
324
325