camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > LDAP
Date Mon, 15 Nov 2010 08:31:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1810/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/LDAP">LDAP</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
        <br/>
                         <h4>Changes (3)</h4>
                                 
    
<div id="page-diffs">
            <table class="diff" cellpadding="0" cellspacing="0">
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br>Maven users will need to
add the following dependency to their {{pom.xml}} for this component: <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words">{code<span
class="diff-added-chars"style="background-color: #dfd;">:xml</span>}</span>
<br></td></tr>
            <tr><td class="diff-unchanged" >&lt;dependency&gt; <br>
   &lt;groupId&gt;org.apache.camel&lt;/groupId&gt; <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br>h3. Options <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">
</span> <span class="diff-added-words"style="background-color: #dfd;">{div:class=confluenceTableSmall}</span>
<br></td></tr>
            <tr><td class="diff-unchanged" >|| Name || Default Value || Description
|| <br>| {{base}} | {{ou=system}} | The base DN for searches. | <br>| {{scope}}
| {{subtree}} | Specifies how deeply to search the tree of entries, starting at the base DN.
 Value can be {{object}}, {{onelevel}}, or {{subtree}}. | <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">|pageSize
|	no paging used | *Camel 2.6:* When specified the ldap module uses paging to retrieve all
results (most LDAP Servers throw an exception when trying to retrieve more than 1000 entries
in one query). To be able to use this a LdapContext (subclass of DirContext) has to be passed
in as ldapServerBean (otherwise an exception is thrown)   | <br>|returnedAttributes|
depends on LDAP Server (could be all or none) | *Camel 2.6:* Comma-separated list of attributes
that should be set in each entry of the result | <br>{div} <br></td></tr>
            <tr><td class="diff-unchanged" > <br>h3. Result <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
        </table>
</div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="LDAP-LDAPComponent"></a>LDAP Component</h2>

<p>The <b>ldap</b> component allows you to perform searches in LDAP servers
using filters as the message payload.<br/>
This component uses standard JNDI (<tt>javax.naming</tt> package) to access the
server. </p>

<p>Maven users will need to add the following dependency to their <tt>pom.xml</tt>
for this component:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;dependency&gt;</span>
    <span class="code-tag">&lt;groupId&gt;</span>org.apache.camel<span
class="code-tag">&lt;/groupId&gt;</span>
    <span class="code-tag">&lt;artifactId&gt;</span>camel-ldap<span
class="code-tag">&lt;/artifactId&gt;</span>
    <span class="code-tag">&lt;version&gt;</span>x.x.x<span class="code-tag">&lt;/version&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- use the same
version as your Camel core version --&gt;</span></span>
<span class="code-tag">&lt;/dependency&gt;</span>
</pre>
</div></div>

<h3><a name="LDAP-URIformat"></a>URI format</h3>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
ldap:ldapServerBean[?options]
</pre>
</div></div>

<p>The <em>ldapServerBean</em> portion of the URI refers to a <a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/naming/directory/DirContext.html"
class="external-link" rel="nofollow">DirContext</a> bean in the registry. The LDAP
component only supports producer endpoints, which means that an <tt>ldap</tt>
URI cannot appear in the <tt>from</tt> at the start of a route.</p>

<p>You can append query options to the URI in the following format, <tt>?option=value&amp;option=value&amp;...</tt></p>

<h3><a name="LDAP-Options"></a>Options</h3>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Default Value </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>base</tt> </td>
<td class='confluenceTd'> <tt>ou=system</tt> </td>
<td class='confluenceTd'> The base DN for searches. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>scope</tt> </td>
<td class='confluenceTd'> <tt>subtree</tt> </td>
<td class='confluenceTd'> Specifies how deeply to search the tree of entries, starting
at the base DN.  Value can be <tt>object</tt>, <tt>onelevel</tt>,
or <tt>subtree</tt>. </td>
</tr>
<tr>
<td class='confluenceTd'>pageSize </td>
<td class='confluenceTd'>	no paging used </td>
<td class='confluenceTd'> <b>Camel 2.6:</b> When specified the ldap module
uses paging to retrieve all results (most LDAP Servers throw an exception when trying to retrieve
more than 1000 entries in one query). To be able to use this a LdapContext (subclass of DirContext)
has to be passed in as ldapServerBean (otherwise an exception is thrown)   </td>
</tr>
<tr>
<td class='confluenceTd'>returnedAttributes</td>
<td class='confluenceTd'> depends on LDAP Server (could be all or none) </td>
<td class='confluenceTd'> <b>Camel 2.6:</b> Comma-separated list of attributes
that should be set in each entry of the result </td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="LDAP-Result"></a>Result</h3>
<p>The result is returned in the Out body as a <tt>ArrayList&lt;javax.naming.directory.SearchResult&gt;</tt>
object. </p>

<h3><a name="LDAP-DirContext"></a>DirContext</h3>

<p>The URI, <tt>ldap:ldapserver</tt>, references a Spring bean with the
ID, <tt>ldapserver</tt>. The <tt>ldapserver</tt> bean may be defined
as follows:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&lt;bean id=<span class="code-quote">"ldapserver"</span> class=<span class="code-quote">"javax.naming.directory.InitialDirContext"</span>
scope=<span class="code-quote">"prototype"</span>&gt;
  &lt;constructor-arg&gt;
    &lt;props&gt;
      &lt;prop key=<span class="code-quote">"java.naming.factory.initial"</span>&gt;com.sun.jndi.ldap.LdapCtxFactory&lt;/prop&gt;
      &lt;prop key=<span class="code-quote">"java.naming.provider.url"</span>&gt;ldap:<span
class="code-comment">//localhost:10389&lt;/prop&gt;
</span>      &lt;prop key=<span class="code-quote">"java.naming.security.authentication"</span>&gt;none&lt;/prop&gt;
    &lt;/props&gt;
  &lt;/constructor-arg&gt;
&lt;/bean&gt;
</pre>
</div></div>

<p>The preceding example declares a regular Sun based LDAP <tt>DirContext</tt>
that connects anonymously to a locally hosted LDAP server.</p>

<div class='panelMacro'><table class='noteMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/warning.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><tt>DirContext</tt>
objects are <b>not</b> required to support concurrency by contract. It is therefore
important that the directory context is declared with the setting, <tt>scope="prototype"</tt>,
in the <tt>bean</tt> definition or that the context supports concurrency. In the
Spring framework, <tt>prototype</tt> scoped objects are instantiated each time
they are looked up.</td></tr></table></div>

<div class='panelMacro'><table class='noteMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/warning.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td>Camel 1.6.1 and Camel 2.0 include
a <a href="https://issues.apache.org/activemq/browse/CAMEL-1583?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&amp;focusedCommentId=51503"
class="external-link" rel="nofollow">fix</a> to support concurrency for LDAP producers.
<em>ldapServerBean</em> contexts are now looked up each time a request is sent
to the LDAP server. In addition, the contexts are released as soon as the producer completes.</td></tr></table></div>

<h3><a name="LDAP-Samples"></a>Samples</h3>

<p>Following on from the Spring configuration above, the code sample below sends an
LDAP request to filter search a group for a member. The Common Name is then extracted from
the response.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
ProducerTemplate&lt;Exchange&gt; template = exchange
  .getContext().createProducerTemplate();

Collection&lt;?&gt; results = (Collection&lt;?&gt;) (template
  .sendBody(
    <span class="code-quote">"ldap:ldapserver?base=ou=mygroup,ou=groups,ou=system"</span>,
    <span class="code-quote">"(member=uid=huntc,ou=users,ou=system)"</span>));

<span class="code-keyword">if</span> (results.size() &gt; 0) {
  <span class="code-comment">// Extract what we need from the device's profile
</span>
  Iterator&lt;?&gt; resultIter = results.iterator();
  SearchResult searchResult = (SearchResult) resultIter
      .next();
  Attributes attributes = searchResult
      .getAttributes();
  Attribute deviceCNAttr = attributes.get(<span class="code-quote">"cn"</span>);
  <span class="code-object">String</span> deviceCN = (<span class="code-object">String</span>)
deviceCNAttr.get();

  ...
</pre>
</div></div>

<p>If no specific filter is required - for example, you just need to look up a single
entry - specify a wildcard filter expression. For example, if the LDAP entry has a Common
Name, use a filter expression like:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
(cn=*)
</pre>
</div></div>

<h4><a name="LDAP-Bindingusingcredentials"></a>Binding using credentials</h4>

<p>A Camel end user donated this sample code he used to bind to the ldap server using
credentials.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
Properties props = <span class="code-keyword">new</span> Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY, <span class="code-quote">"com.sun.jndi.ldap.LdapCtxFactory"</span>);
props.setProperty(Context.PROVIDER_URL, <span class="code-quote">"ldap:<span class="code-comment">//localhost:389"</span>);
</span>props.setProperty(Context.URL_PKG_PREFIXES, <span class="code-quote">"com.sun.jndi.url"</span>);
props.setProperty(Context.REFERRAL, <span class="code-quote">"ignore"</span>);
props.setProperty(Context.SECURITY_AUTHENTICATION, <span class="code-quote">"simple"</span>);
props.setProperty(Context.SECURITY_PRINCIPAL, <span class="code-quote">"cn=Manager"</span>);
props.setProperty(Context.SECURITY_CREDENTIALS, <span class="code-quote">"secret"</span>);

SimpleRegistry reg = <span class="code-keyword">new</span> SimpleRegistry();
reg.put(<span class="code-quote">"myldap"</span>, <span class="code-keyword">new</span>
InitialLdapContext(props, <span class="code-keyword">null</span>));

CamelContext context = <span class="code-keyword">new</span> DefaultCamelContext(reg);
context.addRoutes(
    <span class="code-keyword">new</span> RouteBuilder() {
        <span class="code-keyword">public</span> void configure() <span class="code-keyword">throws</span>
Exception { 
            from(<span class="code-quote">"direct:start"</span>).to(<span class="code-quote">"ldap:myldap?base=ou=test"</span>);
        }
    }
);
context.start();

ProducerTemplate template = context.createProducerTemplate();

Endpoint endpoint = context.getEndpoint(<span class="code-quote">"direct:start"</span>);
Exchange exchange = endpoint.createExchange();
exchange.getIn().setBody(<span class="code-quote">"(uid=test)"</span>);
Exchange out = template.send(endpoint, exchange);

Collection&lt;SearchResult&gt; data = out.getOut().getBody(Collection.class);
<span class="code-keyword">assert</span> data != <span class="code-keyword">null</span>;
<span class="code-keyword">assert</span> !data.isEmpty();

<span class="code-object">System</span>.out.println(out.getOut().getBody());

context.stop();
</pre>
</div></div>

<h3><a name="LDAP-SeeAlso"></a>See Also</h3>
<ul>
	<li><a href="/confluence/display/CAMEL/Configuring+Camel" title="Configuring Camel">Configuring
Camel</a></li>
	<li><a href="/confluence/display/CAMEL/Component" title="Component">Component</a></li>
	<li><a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a></li>
	<li><a href="/confluence/display/CAMEL/Getting+Started" title="Getting Started">Getting
Started</a></li>
</ul>

    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/LDAP">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=95798&revisedVersion=11&originalVersion=10">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/LDAP?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message