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