001package org.apache.archiva.redback.common.ldap.connection; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import org.apache.commons.lang3.StringUtils; 023 024import javax.naming.InvalidNameException; 025import javax.naming.ldap.LdapName; 026import java.util.ArrayList; 027import java.util.List; 028import java.util.Properties; 029import java.util.TreeMap; 030 031/** 032 * This class contains the configuration for a ldap connection. 033 * 034 * Properties of a ldap connection: 035 * <ul> 036 * <li>Hostname - String, required. 037 * <li>Port - int, not required. If 0 then the default value is used by the ldap driver. 038 * <li>Ssl - boolean, not required. If true then the ldaps will be used. 039 * <li>Base DN - String, required. 040 * <li>Context factory - String, required. 041 * <li>Bind DN - String, not required. 042 * <li>Password - String, not required. 043 * </ul> 044 * Note that both the bind dn and password must be set if any are set. 045 * 046 * @author <a href="mailto:trygvis@inamo.no">trygvis</a> 047 * 048 */ 049public class LdapConnectionConfiguration 050{ 051 private String hostname; 052 053 private int port; 054 055 private boolean ssl; 056 057 private LdapName baseDn; 058 059 private String contextFactory; 060 061 private LdapName bindDn; 062 063 private String password; 064 065 private String authenticationMethod; 066 067 private List<Class<?>> objectFactories; 068 069 private List<Class<?>> stateFactories; 070 071 private Properties extraProperties; 072 073 public LdapConnectionConfiguration() 074 { 075 // no op 076 } 077 078 public LdapConnectionConfiguration( String hostname, int port, String baseDn, String contextFactory, String bindDn, 079 String password, String authenticationMethod, Properties extraProperties ) 080 throws InvalidNameException, LdapException 081 { 082 this.hostname = hostname; 083 this.port = port; 084 085 if ( baseDn != null ) 086 { 087 this.baseDn = new LdapName( baseDn ); 088 } 089 090 if ( bindDn != null ) 091 { 092 this.bindDn = new LdapName( bindDn ); 093 } 094 095 this.contextFactory = contextFactory; 096 097 this.password = password; 098 099 this.authenticationMethod = authenticationMethod; 100 101 this.extraProperties = extraProperties; 102 103 check(); 104 } 105 106 public LdapConnectionConfiguration( String hostname, int port, LdapName baseDn, String contextFactory ) 107 throws LdapException 108 { 109 this.hostname = hostname; 110 111 this.port = port; 112 113 this.baseDn = baseDn; 114 115 this.contextFactory = contextFactory; 116 117 check(); 118 } 119 120 // ---------------------------------------------------------------------- 121 // Accessors 122 // ---------------------------------------------------------------------- 123 124 public String getHostname() 125 { 126 return hostname; 127 } 128 129 public void setHostname( String hostname ) 130 { 131 this.hostname = hostname; 132 } 133 134 public int getPort() 135 { 136 return port; 137 } 138 139 public void setPort( int port ) 140 { 141 this.port = port; 142 } 143 144 public boolean isSsl() 145 { 146 return ssl; 147 } 148 149 public void setSsl( boolean ssl ) 150 { 151 this.ssl = ssl; 152 } 153 154 public LdapName getBaseDn() 155 { 156 return baseDn; 157 } 158 159 public void setBaseDn( LdapName baseDn ) 160 { 161 this.baseDn = baseDn; 162 } 163 164 public void setBaseDn( String baseDn ) 165 throws InvalidNameException 166 { 167 if ( baseDn != null ) 168 { 169 this.baseDn = new LdapName( baseDn ); 170 } 171 } 172 173 public String getContextFactory() 174 { 175 return contextFactory; 176 } 177 178 public void setContextFactory( String contextFactory ) 179 { 180 this.contextFactory = contextFactory; 181 } 182 183 public LdapName getBindDn() 184 { 185 return bindDn; 186 } 187 188 public void setBindDn( LdapName bindDn ) 189 { 190 this.bindDn = bindDn; 191 } 192 193 public void setBindDn( String bindDn ) 194 throws InvalidNameException 195 { 196 if ( bindDn != null ) 197 { 198 this.bindDn = new LdapName( bindDn ); 199 } 200 } 201 202 public String getPassword() 203 { 204 return password; 205 } 206 207 public void setPassword( String password ) 208 { 209 this.password = password; 210 } 211 212 public String getAuthenticationMethod() 213 { 214 return authenticationMethod; 215 } 216 217 public void setAuthenticationMethod( String authenticationMethod ) 218 { 219 this.authenticationMethod = authenticationMethod; 220 } 221 222 public List<Class<?>> getObjectFactories() 223 { 224 if ( objectFactories == null ) 225 { 226 objectFactories = new ArrayList<Class<?>>( 0 ); 227 } 228 229 return objectFactories; 230 } 231 232 public void setObjectFactories( List<Class<?>> objectFactories ) 233 { 234 this.objectFactories = objectFactories; 235 } 236 237 public List<Class<?>> getStateFactories() 238 { 239 if ( stateFactories == null ) 240 { 241 stateFactories = new ArrayList<Class<?>>( 0 ); 242 } 243 244 return stateFactories; 245 } 246 247 public void setStateFactories( List<Class<?>> stateFactories ) 248 { 249 this.stateFactories = stateFactories; 250 } 251 252 public Properties getExtraProperties() 253 { 254 if ( extraProperties == null ) 255 { 256 extraProperties = new Properties(); 257 } 258 259 return extraProperties; 260 } 261 262 public void setExtraProperties( Properties extraProperties ) 263 { 264 this.extraProperties = extraProperties; 265 } 266 267 // ---------------------------------------------------------------------- 268 // 269 // ---------------------------------------------------------------------- 270 271 public void check() 272 throws LdapException 273 { 274 if ( port < 0 || port > 65535 ) 275 { 276 throw new LdapException( "The port must be between 1 and 65535." ); 277 } 278 if ( baseDn == null ) 279 { 280 throw new LdapException( "The base DN must be set." ); 281 } 282 if ( StringUtils.isEmpty( contextFactory ) ) 283 { 284 throw new LdapException( "The context factory must be set." ); 285 } 286 if ( password != null && bindDn == null ) 287 { 288 throw new LdapException( "The password cant be set unless the bind dn is." ); 289 } 290 291 if ( extraProperties == null ) 292 { 293 extraProperties = new Properties(); 294 } 295 } 296 297 // ---------------------------------------------------------------------- 298 // 299 // ---------------------------------------------------------------------- 300 301 public String toString() 302 { 303 return "{LdapConnectionConfiguration: " + 304 "hostname: " + getHostname() + ", " + 305 "port: " + getPort() + ", " + 306 "ssl: " + isSsl() + ", " + 307 "baseDn: " + getBaseDn() + ", " + 308 "contextFactory: " + getContextFactory() + ", " + 309 "bindDn: " + getBindDn() + ", " + 310 // not display password 311 //"password: " + getPassword() + ", " + 312 "authenticationMethod: " + getAuthenticationMethod() + ", " + 313 "objectFactories: " + getObjectFactories() + ", " + 314 "stateFactories: " + getStateFactories() + ", " + 315 "extraProperties: " + new TreeMap<Object, Object>( extraProperties ).toString() + "}"; 316 } 317}