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-2016 ForgeRock AS. 016 */ 017package org.opends.admin.ads.util; 018 019import java.util.Collections; 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 final String ldapUrl; 042 private final 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 @Override 075 public int hashCode() 076 { 077 return (type+ldapUrl.toLowerCase()).hashCode(); 078 } 079 080 @Override 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 * Commodity method that returns a PreferredConnection object with the 098 * information on a given InitialLdapContext. 099 * @param conn the connection we retrieve the information from. 100 * @return a preferred connection object. 101 */ 102 private static PreferredConnection getPreferredConnection(ConnectionWrapper conn) 103 { 104 InitialLdapContext ctx = conn.getLdapContext(); 105 String ldapUrl = ConnectionUtils.getLdapUrl(ctx); 106 PreferredConnection.Type type; 107 if (ConnectionUtils.isStartTLS(ctx)) 108 { 109 type = PreferredConnection.Type.START_TLS; 110 } 111 else if (ConnectionUtils.isSSL(ctx)) 112 { 113 type = PreferredConnection.Type.LDAPS; 114 } 115 else 116 { 117 type = PreferredConnection.Type.LDAP; 118 } 119 return new PreferredConnection(ldapUrl, type); 120 } 121 122 /** 123 * Commodity method that generates a list of preferred connection (of just 124 * one) with the information on a given InitialLdapContext. 125 * @param conn the connection we retrieve the information from. 126 * @return a list containing the preferred connection object. 127 */ 128 public static Set<PreferredConnection> getPreferredConnections(ConnectionWrapper conn) 129 { 130 return Collections.singleton(getPreferredConnection(conn)); 131 } 132}