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.ldap.model.name.Rdn;
36  import org.apache.directory.api.util.Strings;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  
41  /**
42   * The action used to store the ModifyDnRequest new RDN
43   * <pre>
44   * ModifyDNRequest ::= [APPLICATION 12] SEQUENCE { ...
45   *     ...
46   *     newrdn  RelativeRDN,
47   *     ...
48   *
49   * RelativeRDN :: LDAPString
50   * </pre>
51   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
52   */
53  public class StoreModifyDnRequestNewRdn extends GrammarAction<LdapMessageContainer<ModifyDnRequestDecorator>>
54  {
55      /** The logger */
56      private static final Logger LOG = LoggerFactory.getLogger( StoreModifyDnRequestNewRdn.class );
57  
58      /** Speedup for logs */
59      private static final boolean IS_DEBUG = LOG.isDebugEnabled();
60  
61  
62      /**
63       * Instantiates a new action.
64       */
65      public StoreModifyDnRequestNewRdn()
66      {
67          super( "Store ModifyDN request new RDN" );
68      }
69  
70  
71      /**
72       * {@inheritDoc}
73       */
74      public void action( LdapMessageContainer<ModifyDnRequestDecorator> container ) throws DecoderException
75      {
76          ModifyDnRequest modifyDnRequest = container.getMessage();
77  
78          // Get the Value and store it in the modifyDNRequest
79          TLV tlv = container.getCurrentTLV();
80  
81          // We have to handle the special case of a 0 length matched
82          // newDN
83          Rdn newRdn = null;
84  
85          if ( tlv.getLength() == 0 )
86          {
87              String msg = I18n.err( I18n.ERR_04090 );
88              LOG.error( msg );
89  
90              ModifyDnResponseImpl response = new ModifyDnResponseImpl( modifyDnRequest.getMessageId() );
91              throw new ResponseCarryingException( msg, response, ResultCodeEnum.INVALID_DN_SYNTAX,
92                  modifyDnRequest.getName(), null );
93          }
94          else
95          {
96              byte[] dnBytes = tlv.getValue().getData();
97              String dnStr = Strings.utf8ToString( dnBytes );
98  
99              try
100             {
101                 Dn dn = new Dn( dnStr );
102                 newRdn = dn.getRdn( dn.size() - 1 );
103             }
104             catch ( LdapInvalidDnException ine )
105             {
106                 String msg = "Invalid new Rdn given : " + dnStr + " (" + Strings.dumpBytes( dnBytes )
107                     + ") is invalid";
108                 LOG.error( "{} : {}", msg, ine.getMessage() );
109 
110                 ModifyDnResponseImpl response = new ModifyDnResponseImpl( modifyDnRequest.getMessageId() );
111                 throw new ResponseCarryingException( msg, response, ResultCodeEnum.INVALID_DN_SYNTAX,
112                     modifyDnRequest.getName(), ine );
113             }
114 
115             modifyDnRequest.setNewRdn( newRdn );
116         }
117 
118         if ( IS_DEBUG )
119         {
120             LOG.debug( "Modifying with new Rdn {}", newRdn );
121         }
122     }
123 }