001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2008-2009 Sun Microsystems, Inc.
025 *      Portions Copyright 2013-2015 ForgeRock AS.
026 */
027package org.opends.admin.ads.util;
028
029import java.util.LinkedHashSet;
030import java.util.Set;
031
032import javax.naming.ldap.InitialLdapContext;
033
034/**
035 * A simple class that is used to be able to specify which URL and connection
036 * type to use when we connect to a server.
037 */
038public class PreferredConnection
039{
040  /** The type of the connection. */
041  public enum Type
042  {
043    /** LDAP connection. */
044    LDAP,
045    /** LDAPS connection. */
046    LDAPS,
047    /** Start TLS connection. */
048    START_TLS
049  }
050
051  private String ldapUrl;
052  private Type type;
053
054  /**
055   * The constructor of the PreferredConnection.
056   * @param ldapUrl the LDAP URL to connect to the server.
057   * @param type the type of connection to be used to connect (required to
058   * differentiate StartTLS and regular LDAP).
059   */
060  public PreferredConnection(String ldapUrl, Type type)
061  {
062    this.ldapUrl = ldapUrl;
063    this.type = type;
064  }
065
066  /**
067   * Returns the LDAP URL to be used.
068   * @return the LDAP URL to be used.
069   */
070  public String getLDAPURL()
071  {
072    return ldapUrl;
073  }
074
075  /**
076   * Returns the type of the connection.
077   * @return the type of the connection.
078   */
079  public Type getType()
080  {
081    return type;
082  }
083
084  /** {@inheritDoc} */
085  public int hashCode()
086  {
087    return (type+ldapUrl.toLowerCase()).hashCode();
088  }
089
090  /** {@inheritDoc} */
091  public boolean equals(Object o)
092  {
093    if (this == o)
094    {
095      return true;
096    }
097    if (o instanceof PreferredConnection)
098    {
099      PreferredConnection p = (PreferredConnection)o;
100      return type == p.getType()
101          && ldapUrl.equalsIgnoreCase(p.getLDAPURL());
102    }
103    return false;
104  }
105
106
107  /**
108   * Commodity method that returns a PreferredConnection object with the
109   * information on a given InitialLdapContext.
110   * @param ctx the connection we retrieve the information from.
111   * @return a preferred connection object.
112   */
113  public static PreferredConnection getPreferredConnection(
114      InitialLdapContext ctx)
115  {
116    String ldapUrl = ConnectionUtils.getLdapUrl(ctx);
117    PreferredConnection.Type type;
118    if (ConnectionUtils.isStartTLS(ctx))
119    {
120      type = PreferredConnection.Type.START_TLS;
121    }
122    else if (ConnectionUtils.isSSL(ctx))
123    {
124      type = PreferredConnection.Type.LDAPS;
125    }
126    else
127    {
128      type = PreferredConnection.Type.LDAP;
129    }
130    return new PreferredConnection(ldapUrl, type);
131  }
132
133
134
135  /**
136   * Commodity method that generates a list of preferred connection (of just
137   * one) with the information on a given InitialLdapContext.
138   * @param ctx the connection we retrieve the information from.
139   * @return a list containing the preferred connection object.
140   */
141  public static Set<PreferredConnection> getPreferredConnections(
142      InitialLdapContext ctx)
143  {
144    PreferredConnection cnx = PreferredConnection.getPreferredConnection(ctx);
145    Set<PreferredConnection> returnValue = new LinkedHashSet<>();
146    returnValue.add(cnx);
147    return returnValue;
148  }
149}