LdapSpringClient.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.utils.transport.ldap;

  21. import java.net.URI;
  22. import java.util.List;

  23. import javax.naming.directory.Attributes;
  24. import javax.naming.ldap.LdapName;

  25. import org.springframework.ldap.core.LdapTemplate;
  26. import org.springframework.ldap.core.support.LdapContextSource;
  27. import org.springframework.ldap.query.LdapQueryBuilder;

  28. /**
  29.  * Implementazione spring di un client ldap
  30.  *
  31.  * @author Tommaso Burlon (tommaso.burlon@link.it)
  32.  * @author $Author$  
  33.  * @version $Rev$, $Date$
  34.  *
  35.  */
  36. public class LdapSpringClient implements LdapClientInterface{

  37.     private LdapContextSource context;
  38.     private LdapTemplate template;
  39.    
  40.     public LdapSpringClient() {
  41.         this.context = new LdapContextSource();
  42.         this.template = new LdapTemplate();
  43.        
  44.         this.template.setContextSource(null);
  45.     }
  46.    
  47.     private LdapTemplate getTemplate() {
  48.         if (this.template.getContextSource() == null) {
  49.             this.context.afterPropertiesSet();
  50.             this.template.setContextSource(this.context);
  51.         }
  52.         return this.template;
  53.     }
  54.    
  55.     @Override
  56.     public List<Attributes> search(LdapQuery query) {
  57.         LdapTemplate ldapTemplate = this.getTemplate();
  58.         LdapQueryBuilder queryBuilder = LdapQueryBuilder
  59.                 .query();
  60.        
  61.         if (query.getLimit() != null)
  62.             queryBuilder.countLimit(query.getLimit());
  63.        
  64.         if (!query.getAttributes().isEmpty())
  65.             queryBuilder.attributes(query.getAttributes().toArray(new String[0]));
  66.        
  67.         if (query.getBase() != null)
  68.             queryBuilder.base(query.getBase());
  69.        
  70.         return ldapTemplate.search(
  71.                 queryBuilder.filter(query.getFilter().toString()),
  72.                 (Attributes attrs) -> attrs);
  73.     }

  74.     @Override
  75.     public LdapClientInterface base(LdapName base) {
  76.         this.context.setBase(base.toString());
  77.         return this;
  78.     }

  79.     @Override
  80.     public LdapClientInterface uri(URI uri) {
  81.         this.context.setUrl(uri.toString());
  82.         return this;
  83.     }
  84.    
  85.     @Override
  86.     public LdapClientInterface username(LdapName username) {
  87.         this.context.setUserDn(username.toString());
  88.         return this;
  89.     }
  90.    
  91.     @Override
  92.     public LdapClientInterface password(String password) {
  93.         this.context.setPassword(password);
  94.         return this;
  95.     }

  96. }