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.api.ldap.codec.actions.ldapResult;
21  
22  
23  import org.apache.directory.api.asn1.DecoderException;
24  import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
25  import org.apache.directory.api.asn1.ber.tlv.TLV;
26  import org.apache.directory.api.i18n.I18n;
27  import org.apache.directory.api.ldap.codec.api.LdapMessageContainer;
28  import org.apache.directory.api.ldap.codec.api.MessageDecorator;
29  import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
30  import org.apache.directory.api.ldap.model.message.LdapResult;
31  import org.apache.directory.api.ldap.model.message.Message;
32  import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
33  import org.apache.directory.api.ldap.model.message.ResultResponse;
34  import org.apache.directory.api.ldap.model.name.Dn;
35  import org.apache.directory.api.util.Strings;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  
40  /**
41   * The action used to set the LdapResult matched Dn.
42   *
43   * <pre>
44   * LDAPResult ::= SEQUENCE {
45   *     ...
46   *     matchedDN LDAPDN,
47   *     ...
48   * </pre>
49   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
50   */
51  public class StoreMatchedDN extends GrammarAction<LdapMessageContainer<MessageDecorator<? extends Message>>>
52  {
53      /** The logger */
54      private static final Logger LOG = LoggerFactory.getLogger( StoreMatchedDN.class );
55  
56      /** Speedup for logs */
57      private static final boolean IS_DEBUG = LOG.isDebugEnabled();
58  
59  
60      /**
61       * Instantiates a new matched dn action.
62       */
63      public StoreMatchedDN()
64      {
65          super( "Store matched Dn" );
66      }
67  
68  
69      /**
70       * {@inheritDoc}
71       */
72      public void action( LdapMessageContainer<MessageDecorator<? extends Message>> container ) throws DecoderException
73      {
74          // Get the Value and store it in the BindResponse
75          TLV tlv = container.getCurrentTLV();
76          Dn matchedDn = null;
77          ResultCodeEnum resultCode = null;
78  
79          ResultResponse response = ( ResultResponse ) container.getMessage();
80          LdapResult ldapResult = response.getLdapResult();
81          resultCode = ldapResult.getResultCode();
82  
83          // We have to handle the special case of a 0 length matched
84          // Dn
85          if ( tlv.getLength() == 0 )
86          {
87              matchedDn = Dn.EMPTY_DN;
88          }
89          else
90          {
91              // A not null matchedDn is valid for resultCodes
92              // NoSuchObject, AliasProblem, InvalidDNSyntax and
93              // AliasDreferencingProblem.
94  
95              switch ( resultCode )
96              {
97                  case NO_SUCH_OBJECT:
98                  case ALIAS_PROBLEM:
99                  case INVALID_DN_SYNTAX:
100                 case ALIAS_DEREFERENCING_PROBLEM:
101                     byte[] dnBytes = tlv.getValue().getData();
102                     String dnStr = Strings.utf8ToString( dnBytes );
103 
104                     try
105                     {
106                         matchedDn = new Dn( dnStr );
107                     }
108                     catch ( LdapInvalidDnException ine )
109                     {
110                         // This is for the client side. We will never decode LdapResult on the server
111                         String msg = I18n.err( I18n.ERR_04013, dnStr, Strings.dumpBytes( dnBytes ), ine
112                             .getLocalizedMessage() );
113                         LOG.error( msg );
114 
115                         throw new DecoderException( I18n.err( I18n.ERR_04014, ine.getLocalizedMessage() ) );
116                     }
117 
118                     break;
119 
120                 default:
121                     LOG.warn( "The matched Dn should not be set when the result code is one of NoSuchObject,"
122                         + " AliasProblem, InvalidDNSyntax or AliasDreferencingProblem" );
123 
124                     matchedDn = Dn.EMPTY_DN;
125                     break;
126             }
127         }
128 
129         if ( IS_DEBUG )
130         {
131             LOG.debug( "The matchedDn is " + matchedDn );
132         }
133 
134         ldapResult.setMatchedDn( matchedDn );
135     }
136 }