View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.ldap.handlers.request;
21  
22  
23  import org.apache.directory.api.ldap.model.message.CompareRequest;
24  import org.apache.directory.api.ldap.model.message.CompareResponse;
25  import org.apache.directory.api.ldap.model.message.LdapResult;
26  import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
27  import org.apache.directory.server.ldap.LdapSession;
28  import org.apache.directory.server.ldap.handlers.LdapRequestHandler;
29  
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  
34  /**
35   * A single reply MessageReceived handler for {@link org.apache.directory.api.ldap.model.message.CompareRequest}s.
36   *
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   */
39  public class CompareRequestHandler extends LdapRequestHandler<CompareRequest>
40  {
41      private static final Logger LOG = LoggerFactory.getLogger( CompareRequestHandler.class );
42  
43      /**
44       * {@inheritDoc}
45       */
46      public void handle( LdapSession session, CompareRequest compareRequest )
47      {
48          LOG.debug( "Handling compare request while ignoring referrals: {}", compareRequest );
49          
50          CompareResponse compareResponse = ( CompareResponse ) compareRequest.getResultResponse();
51          
52          LdapResult result = compareRequest.getResultResponse().getLdapResult();
53  
54          try
55          {
56              if ( session.getCoreSession().compare( compareRequest ) )
57              {
58                  result.setResultCode( ResultCodeEnum.COMPARE_TRUE );
59              }
60              else
61              {
62                  result.setResultCode( ResultCodeEnum.COMPARE_FALSE );
63              }
64  
65              result.setMatchedDn( compareRequest.getName() );
66              session.getIoSession().write( compareResponse );
67          }
68          catch ( Exception e )
69          {
70              handleException( session, compareRequest, compareResponse, e );
71          }
72      }
73  }