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.modifyDnRequest;
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.ResponseCarryingException;
29  import org.apache.directory.api.ldap.codec.decorators.ModifyDnRequestDecorator;
30  import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
31  import org.apache.directory.api.ldap.model.message.ModifyDnRequest;
32  import org.apache.directory.api.ldap.model.message.ModifyDnResponseImpl;
33  import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
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 store the ModifyDnRequest entry name
42   * <pre>
43   * ModifyDNRequest ::= [APPLICATION 12] SEQUENCE { ...
44   *     entry LDAPDN,
45   *     ...
46   * </pre>
47   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
48   */
49  public class StoreModifyDnRequestEntryName extends GrammarAction<LdapMessageContainer<ModifyDnRequestDecorator>>
50  {
51      /** The logger */
52      private static final Logger LOG = LoggerFactory.getLogger( StoreModifyDnRequestEntryName.class );
53  
54      /** Speedup for logs */
55      private static final boolean IS_DEBUG = LOG.isDebugEnabled();
56  
57  
58      /**
59       * Instantiates a new action.
60       */
61      public StoreModifyDnRequestEntryName()
62      {
63          super( "Store ModifyDN request entry Name" );
64      }
65  
66  
67      /**
68       * {@inheritDoc}
69       */
70      public void action( LdapMessageContainer<ModifyDnRequestDecorator> container ) throws DecoderException
71      {
72          ModifyDnRequest modifyDnRequest = container.getMessage();
73  
74          // Get the Value and store it in the modifyDNRequest
75          TLV tlv = container.getCurrentTLV();
76  
77          // We have to handle the special case of a 0 length matched
78          // Dn
79          Dn entry;
80  
81          if ( tlv.getLength() == 0 )
82          {
83              // This will generate a PROTOCOL_ERROR
84              throw new DecoderException( I18n.err( I18n.ERR_04089 ) );
85          }
86          else
87          {
88              byte[] dnBytes = tlv.getValue().getData();
89              String dnStr = Strings.utf8ToString( dnBytes );
90  
91              try
92              {
93                  entry = new Dn( dnStr );
94              }
95              catch ( LdapInvalidDnException ine )
96              {
97                  String msg = "Invalid Dn given : " + dnStr + " (" + Strings.dumpBytes( dnBytes )
98                      + ") is invalid";
99                  LOG.error( "{} : {}", msg, ine.getMessage() );
100 
101                 ModifyDnResponseImpl response = new ModifyDnResponseImpl( modifyDnRequest.getMessageId() );
102                 throw new ResponseCarryingException( msg, response, ResultCodeEnum.INVALID_DN_SYNTAX,
103                     Dn.EMPTY_DN, ine );
104             }
105 
106             modifyDnRequest.setName( entry );
107         }
108 
109         if ( IS_DEBUG )
110         {
111             LOG.debug( "Modifying Dn {}", entry );
112         }
113     }
114 }