001/*
002 * Copyright 2012-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.autoconfigure.ldap;
018
019import java.util.HashMap;
020import java.util.Map;
021
022import org.springframework.boot.context.properties.ConfigurationProperties;
023import org.springframework.core.env.Environment;
024import org.springframework.util.Assert;
025import org.springframework.util.ObjectUtils;
026
027/**
028 * Configuration properties for LDAP.
029 *
030 * @author Eddú Meléndez
031 * @since 1.5.0
032 */
033@ConfigurationProperties(prefix = "spring.ldap")
034public class LdapProperties {
035
036        private static final int DEFAULT_PORT = 389;
037
038        /**
039         * LDAP URLs.
040         */
041        private String[] urls;
042
043        /**
044         * Base suffix from which all operations should originate.
045         */
046        private String base;
047
048        /**
049         * Login user of the LDAP.
050         */
051        private String username;
052
053        /**
054         * Login password of the LDAP.
055         */
056        private String password;
057
058        /**
059         * LDAP specification settings.
060         */
061        private Map<String, String> baseEnvironment = new HashMap<String, String>();
062
063        public String[] getUrls() {
064                return this.urls;
065        }
066
067        public void setUrls(String[] urls) {
068                this.urls = urls;
069        }
070
071        public String getBase() {
072                return this.base;
073        }
074
075        public void setBase(String base) {
076                this.base = base;
077        }
078
079        public String getUsername() {
080                return this.username;
081        }
082
083        public void setUsername(String username) {
084                this.username = username;
085        }
086
087        public String getPassword() {
088                return this.password;
089        }
090
091        public void setPassword(String password) {
092                this.password = password;
093        }
094
095        public Map<String, String> getBaseEnvironment() {
096                return this.baseEnvironment;
097        }
098
099        public void setBaseEnvironment(Map<String, String> baseEnvironment) {
100                this.baseEnvironment = baseEnvironment;
101        }
102
103        public String[] determineUrls(Environment environment) {
104                if (ObjectUtils.isEmpty(this.urls)) {
105                        return new String[] { "ldap://localhost:" + determinePort(environment) };
106                }
107                return this.urls;
108        }
109
110        private int determinePort(Environment environment) {
111                Assert.notNull(environment, "Environment must not be null");
112                String localPort = environment.getProperty("local.ldap.port");
113                if (localPort != null) {
114                        return Integer.valueOf(localPort);
115                }
116                return DEFAULT_PORT;
117        }
118
119}