directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Directory SandBox > Implementing a simple interceptor
Date Sat, 06 Feb 2010 19:58:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/5/_/styles/combined.css?spaceKey=DIRxSBOX&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/DIRxSBOX/Implementing+a+simple+interceptor">Implementing
a simple interceptor</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~szoerner">Stefan
Zoerner</a>
    </h4>
     
          <br/>
     <div class="notificationGreySide">
         <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><b>Be
Careful</b><br /><p>Work in progress. Any feedback highly appreciated!</p></td></tr></table></div>

<h1><a name="Implementingasimpleinterceptor-Implementingasimpleinterceptor"></a>Implementing
a simple interceptor</h1>

<p>The following is for developers who plan to implement their own interceptors in order
to extend or modify the functionality of Apache Directory Server. It contains a simple example
as a starting point.</p>

<div>
<ul>
    <li><a href='#Implementingasimpleinterceptor-Whatexactlyisaninterceptor%3F'>What
exactly is an interceptor?</a></li>
    <li><a href='#Implementingasimpleinterceptor-Passwordhash.Asimpleinterceptor'>Password
hash. A simple interceptor</a></li>
<ul>
    <li><a href='#Implementingasimpleinterceptor-Thesources'>The sources</a></li>
    <li><a href='#Implementingasimpleinterceptor-ImplementingtheclassPasswordHashInterceptor'>Implementing
the class PasswordHashInterceptor</a></li>
    <li><a href='#Implementingasimpleinterceptor-Usingtheinterceptor'>Using the
interceptor</a></li>
    <li><a href='#Implementingasimpleinterceptor-Verification'>Verification</a></li>
</ul>
</ul></div>

<h2><a name="Implementingasimpleinterceptor-Whatexactlyisaninterceptor%3F"></a>What
exactly is an interceptor?</h2>

<p>An interceptor filters method calls performed on on the DefaultPartitionNexus just
like Servlet filters do. The ApacheDS configuration contains a chain of filters performing
several tasks. In order to illustrate this, here is the list of interceptors from the default
server configuration of ApacheDS 1.5.5</p>

<ul>
	<li>org.apache.directory.server.core.normalization.NormalizationInterceptor</li>
	<li>org.apache.directory.server.core.authn.AuthenticationInterceptor</li>
	<li>org.apache.directory.server.core.referral.ReferralInterceptor</li>
	<li>org.apache.directory.server.core.authz.AciAuthorizationInterceptor</li>
	<li>org.apache.directory.server.core.authz.DefaultAuthorizationInterceptor</li>
	<li>org.apache.directory.server.core.exception.ExceptionInterceptor</li>
	<li>org.apache.directory.server.core.changelog.ChangeLogInterceptor</li>
	<li>org.apache.directory.server.core.operational.OperationalAttributeInterceptor</li>
	<li>org.apache.directory.server.core.schema.SchemaInterceptor</li>
	<li>org.apache.directory.server.core.subtree.SubentryInterceptor</li>
	<li>org.apache.directory.server.core.collective.CollectiveAttributeInterceptor</li>
	<li>org.apache.directory.server.core.event.EventInterceptor</li>
	<li>org.apache.directory.server.core.trigger.TriggerInterceptor</li>
	<li>org.apache.directory.server.core.journal.JournalInterceptor</li>
</ul>


<p>Interceptors should usually pass the control of current invocation to the next interceptor
by calling an appropriate method on NextInterceptor.  The flow control is returned when the
next  interceptor's filter method returns. You can therefore implement pre-, post-, around-
invocation handler by how you place the statement.</p>

<p>Interceptors are a powerful way to extend and modify the server behavior. But be
warned. A mistakenly written interceptor may lead to a dis-functional or corrupt server. </p>

<h2><a name="Implementingasimpleinterceptor-Passwordhash.Asimpleinterceptor"></a>Password
hash. A simple interceptor</h2>

<p>In order to demonstrate how to write an interceptor, here is a simple but realistic
example. The following requirement should be fulfilled by an interceptor.</p>

<ul>
	<li>no user passwords should be stored in clear text in the directory</li>
	<li>If a userpassword is set by an LDAP client in clear text, a message digest algorithm
should be applied to the value, and the one-way encrypted value should be stored</li>
	<li>the algorithm should be applied if new entries are created or existing entries
are modified (hence modify and add operations will be intercepted)</li>
	<li>If the value given by the client is already provided in hashed form, nothing happens</li>
</ul>


<h3><a name="Implementingasimpleinterceptor-Thesources"></a>The sources</h3>

<p>Currently, the sources are checked in here</p>
<ul>
	<li><a href="http://svn.apache.org/repos/asf/directory/sandbox/szoerner/passwordHashInterceptor"
rel="nofollow">http://svn.apache.org/repos/asf/directory/sandbox/szoerner/passwordHashInterceptor</a></li>
</ul>


<p>In order to build it, simply check it out and type "mvn install". </p>

<h3><a name="Implementingasimpleinterceptor-ImplementingtheclassPasswordHashInterceptor"></a>Implementing
the class PasswordHashInterceptor</h3>

<p>The following UML class diagram depicts the structure of the little example.</p>

<p><img src="/confluence/download/attachments/13271374/passwordHashInterceptor_UML.png"
align="absmiddle" border="0" /></p>

<h3><a name="Implementingasimpleinterceptor-Usingtheinterceptor"></a>Using
the interceptor</h3>

<h4><a name="Implementingasimpleinterceptor-Embeddedmode"></a>Embedded mode</h4>


<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">package</span> org.apache.directory.samples.interceptor.pwdhash;

<span class="code-keyword">import</span> java.util.List;

<span class="code-keyword">import</span> org.apache.directory.server.core.DefaultDirectoryService;
<span class="code-keyword">import</span> org.apache.directory.server.core.DirectoryService;
<span class="code-keyword">import</span> org.apache.directory.server.core.interceptor.Interceptor;
<span class="code-keyword">import</span> org.apache.directory.server.ldap.LdapServer;
<span class="code-keyword">import</span> org.apache.directory.server.protocol.shared.transport.TcpTransport;

/**
 * Main class which starts an embedded server with the interceptor added to the
 * chain.
 */
<span class="code-keyword">public</span> class Main {

    <span class="code-keyword">public</span> <span class="code-keyword">static</span>
void main(<span class="code-object">String</span>[] args) <span class="code-keyword">throws</span>
Exception {

        DirectoryService directoryService = <span class="code-keyword">new</span>
DefaultDirectoryService();
        directoryService.setShutdownHookEnabled(<span class="code-keyword">true</span>);

        LdapServer ldapServer = <span class="code-keyword">new</span> LdapServer();
        ldapServer.setDirectoryService(directoryService);
        ldapServer.setAllowAnonymousAccess(<span class="code-keyword">true</span>);

        List&lt;Interceptor&gt; is = directoryService.getInterceptors();
        is.add(<span class="code-keyword">new</span> PasswordHashInterceptor());
        directoryService.setInterceptors(is);

        TcpTransport ldapTransport = <span class="code-keyword">new</span> TcpTransport(10389);
        ldapServer.setTransports(ldapTransport);

        directoryService.startup();
        ldapServer.start();
    }
}
</pre>
</div></div>

<h4><a name="Implementingasimpleinterceptor-Addingittoaserver.xmlfile"></a>Adding
it to a server.xml file</h4>

<h3><a name="Implementingasimpleinterceptor-Verification"></a>Verification</h3>
     </div>
     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/DIRxSBOX/Implementing+a+simple+interceptor">View
Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=13271374&revisedVersion=12&originalVersion=11">View
Change</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message