001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.ldap;
018    
019    import java.net.URISyntaxException;
020    
021    import javax.naming.directory.SearchControls;
022    
023    import org.apache.camel.Consumer;
024    import org.apache.camel.Exchange;
025    import org.apache.camel.Processor;
026    import org.apache.camel.Producer;
027    import org.apache.camel.RuntimeCamelException;
028    import org.apache.camel.impl.DefaultEndpoint;
029    
030    /**
031     * Represents an endpoint that synchronously invokes an LDAP server when a producer sends a message to it.
032     *
033     * @version
034     */
035    public class LdapEndpoint<E extends Exchange> extends DefaultEndpoint<E> {
036        public static final String SYSTEM_DN  = "ou=system";
037        public static final String OBJECT_SCOPE = "object";
038        public static final String ONELEVEL_SCOPE = "onelevel";
039        public static final String SUBTREE_SCOPE = "subtree";
040    
041        private String remaining;
042        private String base = SYSTEM_DN;
043        private String scope = SUBTREE_SCOPE;
044    
045        protected LdapEndpoint(String endpointUri, String remaining, LdapComponent component) throws URISyntaxException {
046            super(endpointUri, component);
047            this.remaining = remaining;
048        }
049    
050        public LdapEndpoint(String endpointUri, String remaining) throws URISyntaxException {
051            super(endpointUri);
052            this.remaining = remaining;
053        }
054    
055        public Consumer<E> createConsumer(Processor processor) throws Exception {
056            throw new RuntimeCamelException("An LDAP Consumer would be the LDAP server itself! No such support here");
057        }
058    
059        public Producer<E> createProducer() throws Exception {
060            return new LdapProducer(this, remaining, base, toSearchControlScope(scope));
061        }
062    
063        public boolean isSingleton() {
064            return true;
065        }
066    
067        public String getBase() {
068            return base;
069        }
070    
071        public void setBase(String base) {
072            this.base = base;
073        }
074    
075        public String getScope() {
076            return scope;
077        }
078    
079        public void setScope(String scope) {
080            this.scope = scope;
081        }
082    
083        private int toSearchControlScope(String scope) {
084            if (scope.equalsIgnoreCase(OBJECT_SCOPE)) {
085                return SearchControls.OBJECT_SCOPE;
086            } else if (scope.equalsIgnoreCase(ONELEVEL_SCOPE)) {
087                return SearchControls.ONELEVEL_SCOPE;
088            } else if (scope.equalsIgnoreCase(SUBTREE_SCOPE)) {
089                return SearchControls.SUBTREE_SCOPE;
090            } else {
091                throw new IllegalArgumentException("Invalid search scope \"" + scope
092                    + "\" for LdapEndpoint: " + getEndpointUri());
093            }
094        }
095    }