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.util.ArrayList;
020 import java.util.List;
021 import javax.naming.NamingEnumeration;
022 import javax.naming.directory.DirContext;
023 import javax.naming.directory.SearchControls;
024 import javax.naming.directory.SearchResult;
025
026 import org.apache.camel.Exchange;
027 import org.apache.camel.impl.DefaultExchange;
028 import org.apache.camel.impl.DefaultProducer;
029 import org.apache.commons.logging.Log;
030 import org.apache.commons.logging.LogFactory;
031
032 /**
033 * @version $
034 */
035 public class LdapProducer<E extends Exchange> extends DefaultProducer<DefaultExchange> {
036 private static final transient Log LOG = LogFactory.getLog(LdapProducer.class);
037 private String remaining;
038 private SearchControls controls;
039 private String searchBase;
040
041 public LdapProducer(LdapEndpoint endpoint, String remaining, String base, int scope) throws Exception {
042 super(endpoint);
043
044 this.remaining = remaining;
045 searchBase = base;
046 controls = new SearchControls();
047 controls.setSearchScope(scope);
048 }
049
050 public void process(Exchange exchange) throws Exception {
051 String filter = exchange.getIn().getBody(String.class);
052
053 // Obtain our ldap context. We do this by looking up the context in our registry.
054 // Note though that a new context is expected each time. Therefore if spring is
055 // being used then use prototype="scope". If you do not then you might experience
056 // concurrency issues as InitialContext is not required to support concurrency.
057 // On the other hand if you have a DirContext that is able to support concurrency
058 // then using the default singleton scope is entirely sufficient. Most DirContext
059 // classes will require prototype scope though.
060 DirContext ldapContext = (DirContext) getEndpoint().getCamelContext().getRegistry().lookup(remaining);
061 try {
062 // could throw NamingException
063 List<SearchResult> data = new ArrayList<SearchResult>();
064 NamingEnumeration<SearchResult> namingEnumeration =
065 ldapContext.search(searchBase, filter, getControls());
066
067 while (namingEnumeration.hasMore()) {
068 data.add(namingEnumeration.next());
069 }
070 exchange.getOut().setBody(data);
071 } finally {
072 ldapContext.close();
073 }
074 }
075
076 protected SearchControls getControls() {
077 return controls;
078 }
079 }