This project has retired. For details please refer to its Attic page.
DefaultLdapGroupMappingService xref
View Javadoc

1   package org.apache.archiva.redback.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.redback.common.ldap.MappingException;
22  import org.apache.archiva.redback.common.ldap.connection.DefaultLdapConnection;
23  import org.apache.archiva.redback.common.ldap.connection.LdapConnection;
24  import org.apache.archiva.redback.common.ldap.connection.LdapConnectionFactory;
25  import org.apache.archiva.redback.common.ldap.connection.LdapException;
26  import org.apache.archiva.redback.common.ldap.role.LdapRoleMapper;
27  import org.apache.archiva.redback.common.ldap.role.LdapRoleMapperConfiguration;
28  import org.apache.archiva.redback.rest.api.model.LdapGroupMapping;
29  import org.apache.archiva.redback.rest.api.model.LdapGroupMappingUpdateRequest;
30  import org.apache.archiva.redback.rest.api.model.StringList;
31  import org.apache.archiva.redback.rest.api.services.LdapGroupMappingService;
32  import org.apache.archiva.redback.rest.api.services.RedbackServiceException;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  import org.springframework.stereotype.Service;
36  
37  import javax.inject.Inject;
38  import javax.inject.Named;
39  import javax.naming.NamingException;
40  import javax.naming.directory.DirContext;
41  import java.util.ArrayList;
42  import java.util.Collection;
43  import java.util.List;
44  import java.util.Map;
45  
46  /**
47   * @author Olivier Lamy
48   * @since 2.1
49   */
50  @Service("ldapGroupMappingService#rest")
51  public class DefaultLdapGroupMappingService
52      implements LdapGroupMappingService
53  {
54      private Logger log = LoggerFactory.getLogger( getClass() );
55  
56      @Inject
57      @Named(value = "ldapRoleMapper#default")
58      private LdapRoleMapper ldapRoleMapper;
59  
60      @Inject
61      @Named(value = "ldapRoleMapperConfiguration#default")
62      private LdapRoleMapperConfiguration ldapRoleMapperConfiguration;
63  
64      @Inject
65      @Named(value = "ldapConnectionFactory#configurable")
66      private LdapConnectionFactory ldapConnectionFactory;
67  
68      public StringList getLdapGroups()
69          throws RedbackServiceException
70      {
71          LdapConnection ldapConnection = null;
72  
73          DirContext context = null;
74  
75          try
76          {
77              ldapConnection = ldapConnectionFactory.getConnection();
78              context = ldapConnection.getDirContext();
79              return new StringList( ldapRoleMapper.getAllGroups( context ) );
80          }
81          catch ( LdapException e )
82          {
83              log.error( e.getMessage(), e );
84              throw new RedbackServiceException( e.getMessage() );
85          }
86          catch ( MappingException e )
87          {
88              log.error( e.getMessage(), e );
89              throw new RedbackServiceException( e.getMessage() );
90          }
91          finally
92          {
93              closeContext( context );
94              closeLdapConnection( ldapConnection );
95          }
96      }
97  
98      public List<LdapGroupMapping> getLdapGroupMappings()
99          throws RedbackServiceException
100     {
101         try
102         {
103             Map<String, Collection<String>> map = ldapRoleMapperConfiguration.getLdapGroupMappings();
104             List<LdapGroupMapping> ldapGroupMappings = new ArrayList<LdapGroupMapping>( map.size() );
105             for ( Map.Entry<String, Collection<String>> entry : map.entrySet() )
106             {
107                 LdapGroupMapping ldapGroupMapping = new LdapGroupMapping( entry.getKey(), entry.getValue() );
108                 ldapGroupMappings.add( ldapGroupMapping );
109             }
110 
111             return ldapGroupMappings;
112         }
113         catch ( MappingException e )
114         {
115             log.error( e.getMessage(), e );
116             throw new RedbackServiceException( e.getMessage() );
117         }
118     }
119 
120     public Boolean addLdapGroupMapping( LdapGroupMapping ldapGroupMapping )
121         throws RedbackServiceException
122     {
123         try
124         {
125             ldapRoleMapperConfiguration.addLdapMapping( ldapGroupMapping.getGroup(),
126                                                         new ArrayList( ldapGroupMapping.getRoleNames() ) );
127         }
128         catch ( MappingException e )
129         {
130             log.error( e.getMessage(), e );
131             throw new RedbackServiceException( e.getMessage() );
132         }
133         return Boolean.TRUE;
134     }
135 
136     public Boolean removeLdapGroupMapping( String group )
137         throws RedbackServiceException
138     {
139         try
140         {
141             ldapRoleMapperConfiguration.removeLdapMapping( group );
142         }
143         catch ( MappingException e )
144         {
145             log.error( e.getMessage(), e );
146             throw new RedbackServiceException( e.getMessage() );
147         }
148         return Boolean.TRUE;
149     }
150 
151     public Boolean updateLdapGroupMapping( LdapGroupMappingUpdateRequest ldapGroupMappingUpdateRequest )
152         throws RedbackServiceException
153     {
154         try
155         {
156             for ( LdapGroupMapping ldapGroupMapping : ldapGroupMappingUpdateRequest.getLdapGroupMapping() )
157             {
158                 ldapRoleMapperConfiguration.updateLdapMapping( ldapGroupMapping.getGroup(),
159                                                                new ArrayList( ldapGroupMapping.getRoleNames() ) );
160             }
161         }
162         catch ( MappingException e )
163         {
164             log.error( e.getMessage(), e );
165             throw new RedbackServiceException( e.getMessage() );
166         }
167         return Boolean.TRUE;
168     }
169 
170     //------------------
171     // utils
172     //------------------
173 
174     protected void closeLdapConnection( LdapConnection ldapConnection )
175     {
176         if ( ldapConnection != null )
177         {
178             ldapConnection.close();
179         }
180     }
181 
182     protected void closeContext( DirContext context )
183     {
184         if ( context != null )
185         {
186             try
187             {
188                 context.close();
189             }
190             catch ( NamingException e )
191             {
192                 log.warn( "skip issue closing context: {}", e.getMessage() );
193             }
194         }
195     }
196 }