1 package org.apache.archiva.redback.rest.services;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
48
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
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 }