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