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