LdapUtility.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.net.URISyntaxException;
  23. import java.net.URLEncoder;
  24. import java.nio.charset.StandardCharsets;
  25. import java.text.ParseException;
  26. import java.util.ArrayList;
  27. import java.util.List;
  28. import java.util.Objects;

  29. import javax.naming.InvalidNameException;
  30. import javax.naming.ldap.LdapName;

  31. /**
  32.  * Classe utility per ldap
  33.  *
  34.  * @author Tommaso Burlon (tommaso.burlon@link.it)
  35.  * @author $Author$  
  36.  * @version $Rev$, $Date$
  37.  *
  38.  */
  39. public class LdapUtility {
  40.    
  41.     private LdapUtility() {}
  42.    
  43.     public static LdapName getBaseFromURI(URI uri) throws InvalidNameException {
  44.         String[] paths = uri.getPath().split("/");
  45.         String base = paths.length > 0 ? paths[paths.length - 1] : "";
  46.         return new LdapName(base);
  47.     }
  48.    
  49.     public static String[] getAttributesFromURI(URI uri) {
  50.         String[] params = uri.getQuery().split("\\?");
  51.         for (String param : params)
  52.             if (!param.isEmpty() && param.charAt(0) != '(')
  53.                 return param.split(",");
  54.         return new String[0];
  55.     }
  56.    
  57.     public static URI getBaseUrlFromURI(String uri) throws URISyntaxException {
  58.         return getBaseUrlFromURI(new URI(uri));
  59.     }
  60.     public static URI getBaseUrlFromURI(URI uri) throws URISyntaxException {
  61.         String scheme = Objects.requireNonNullElse(uri.getScheme(), "ldap");
  62.         int port = Objects.requireNonNullElse(uri.getPort(), scheme.equals("ldaps") ? 636 : 389);
  63.         return new URI(scheme + "://" + uri.getHost() + ":" + port);
  64.     }
  65.    
  66.     public static LdapFilter getFilterFromURI(URI uri) throws ParseException {
  67.         String[] params = uri.getQuery().split("\\?");
  68.         for (String param : params)
  69.             if (!param.isEmpty() && param.charAt(0) == '(')
  70.                 return LdapFilter.parse(param);
  71.         return LdapFilter.isPresent("cn");
  72.     }
  73.    
  74.     public static LdapQuery getQueryFromURI(String uri) throws InvalidNameException, ParseException, URISyntaxException {
  75.         return getQueryFromURI(new URI(uri));
  76.     }
  77.     public static LdapQuery getQueryFromURI(URI uri) throws InvalidNameException, ParseException {
  78.         return new LdapQuery()
  79.                 .attributes(LdapUtility.getAttributesFromURI(uri))
  80.                 .filter(LdapUtility.getFilterFromURI(uri))
  81.                 .base(LdapUtility.getBaseFromURI(uri));
  82.     }
  83.    
  84.     public static URI getURIFromQuery(URI baseUrl, LdapQuery query) throws URISyntaxException {
  85.         StringBuilder url = new StringBuilder(getBaseUrlFromURI(baseUrl).toString());
  86.         if (query.getBase() != null)
  87.             url.append("/").append(URLEncoder.encode(query.getBase().toString(), StandardCharsets.UTF_8));
  88.         if (query.getAttributes() != null && !query.getAttributes().isEmpty()) {
  89.             url.append("?");
  90.             List<String> encodeAttr = new ArrayList<>();
  91.             for (String attr : query.getAttributes())
  92.                 encodeAttr.add(URLEncoder.encode(attr, StandardCharsets.UTF_8));
  93.             url.append(String.join(",", encodeAttr));
  94.         }
  95.         if (query.getFilter() != null && !query.getFilter().isAbsoluteTrueFilter())
  96.             url.append("?").append(URLEncoder.encode(query.getFilter().toString(), StandardCharsets.UTF_8));
  97.         return new URI(url.toString());
  98.     }
  99. }