001package org.apache.archiva.redback.rest.services; 002/* 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 */ 020 021import org.apache.archiva.redback.common.ldap.MappingException; 022import org.apache.archiva.redback.common.ldap.connection.LdapConnection; 023import org.apache.archiva.redback.common.ldap.connection.LdapConnectionFactory; 024import org.apache.archiva.redback.common.ldap.connection.LdapException; 025import org.apache.archiva.redback.common.ldap.role.LdapRoleMapper; 026import org.apache.archiva.redback.common.ldap.role.LdapRoleMapperConfiguration; 027import org.apache.archiva.redback.rest.api.model.ActionStatus; 028import org.apache.archiva.redback.rest.api.model.LdapGroupMapping; 029import org.apache.archiva.redback.rest.api.model.LdapGroupMappingUpdateRequest; 030import org.apache.archiva.redback.rest.api.model.StringList; 031import org.apache.archiva.redback.rest.api.services.LdapGroupMappingService; 032import org.apache.archiva.redback.rest.api.services.RedbackServiceException; 033import org.slf4j.Logger; 034import org.slf4j.LoggerFactory; 035import org.springframework.stereotype.Service; 036 037import javax.inject.Inject; 038import javax.inject.Named; 039import javax.naming.NamingException; 040import javax.naming.directory.DirContext; 041import java.util.ArrayList; 042import java.util.Collection; 043import java.util.List; 044import java.util.Map; 045 046/** 047 * @deprecated Use new API version {@link org.apache.archiva.redback.rest.services.v2.DefaultGroupService} 048 * @author Olivier Lamy 049 * @since 2.1 050 */ 051@Deprecated 052@Service("ldapGroupMappingService#rest") 053public class DefaultLdapGroupMappingService 054 implements LdapGroupMappingService 055{ 056 private final Logger log = LoggerFactory.getLogger( getClass() ); 057 058 @Inject 059 @Named(value = "ldapRoleMapper#default") 060 private LdapRoleMapper ldapRoleMapper; 061 062 @Inject 063 @Named(value = "ldapRoleMapperConfiguration#default") 064 private LdapRoleMapperConfiguration ldapRoleMapperConfiguration; 065 066 @Inject 067 @Named(value = "ldapConnectionFactory#configurable") 068 private LdapConnectionFactory ldapConnectionFactory; 069 070 public StringList getLdapGroups() 071 throws RedbackServiceException 072 { 073 LdapConnection ldapConnection = null; 074 075 DirContext context = null; 076 077 try 078 { 079 ldapConnection = ldapConnectionFactory.getConnection(); 080 context = ldapConnection.getDirContext(); 081 return new StringList( ldapRoleMapper.getAllGroups( context ) ); 082 } 083 catch ( LdapException | MappingException e ) 084 { 085 log.error( e.getMessage(), e ); 086 throw new RedbackServiceException( e.getMessage() ); 087 } 088 finally 089 { 090 closeContext( context ); 091 closeLdapConnection( ldapConnection ); 092 } 093 } 094 095 public List<LdapGroupMapping> getLdapGroupMappings() 096 throws RedbackServiceException 097 { 098 try 099 { 100 Map<String, Collection<String>> map = ldapRoleMapperConfiguration.getLdapGroupMappings(); 101 List<LdapGroupMapping> ldapGroupMappings = new ArrayList<>( map.size( ) ); 102 for ( Map.Entry<String, Collection<String>> entry : map.entrySet() ) 103 { 104 LdapGroupMapping ldapGroupMapping = new LdapGroupMapping( entry.getKey(), entry.getValue() ); 105 ldapGroupMappings.add( ldapGroupMapping ); 106 } 107 108 return ldapGroupMappings; 109 } 110 catch ( MappingException e ) 111 { 112 log.error( e.getMessage(), e ); 113 throw new RedbackServiceException( e.getMessage() ); 114 } 115 } 116 117 public ActionStatus addLdapGroupMapping( LdapGroupMapping ldapGroupMapping ) 118 throws RedbackServiceException 119 { 120 try 121 { 122 ldapRoleMapperConfiguration.addLdapMapping( ldapGroupMapping.getGroup(), 123 new ArrayList<>( ldapGroupMapping.getRoleNames() ) ); 124 } 125 catch ( MappingException e ) 126 { 127 log.error( e.getMessage(), e ); 128 throw new RedbackServiceException( e.getMessage() ); 129 } 130 return ActionStatus.SUCCESS; 131 } 132 133 public ActionStatus removeLdapGroupMapping( String group ) 134 throws RedbackServiceException 135 { 136 try 137 { 138 ldapRoleMapperConfiguration.removeLdapMapping( group ); 139 } 140 catch ( MappingException e ) 141 { 142 log.error( e.getMessage(), e ); 143 throw new RedbackServiceException( e.getMessage() ); 144 } 145 return ActionStatus.SUCCESS; 146 } 147 148 public ActionStatus updateLdapGroupMapping( LdapGroupMappingUpdateRequest ldapGroupMappingUpdateRequest ) 149 throws RedbackServiceException 150 { 151 try 152 { 153 for ( LdapGroupMapping ldapGroupMapping : ldapGroupMappingUpdateRequest.getLdapGroupMapping() ) 154 { 155 ldapRoleMapperConfiguration.updateLdapMapping( ldapGroupMapping.getGroup(), 156 new ArrayList<>( ldapGroupMapping.getRoleNames() ) ); 157 } 158 } 159 catch ( MappingException e ) 160 { 161 log.error( e.getMessage(), e ); 162 throw new RedbackServiceException( e.getMessage() ); 163 } 164 return ActionStatus.SUCCESS; 165 } 166 167 //------------------ 168 // utils 169 //------------------ 170 171 protected void closeLdapConnection( LdapConnection ldapConnection ) 172 { 173 if ( ldapConnection != null ) 174 { 175 try 176 { 177 ldapConnection.close(); 178 } 179 catch ( NamingException e ) 180 { 181 log.error( "Could not close connection: {}", e.getMessage( ), e ); 182 } 183 } 184 } 185 186 protected void closeContext( DirContext context ) 187 { 188 if ( context != null ) 189 { 190 try 191 { 192 context.close(); 193 } 194 catch ( NamingException e ) 195 { 196 log.warn( "skip issue closing context: {}", e.getMessage() ); 197 } 198 } 199 } 200}