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.request.modifydn;
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.model.exception.LdapInvalidDnException;
30  import org.apache.directory.api.ldap.model.message.ModifyDnRequest;
31  import org.apache.directory.api.ldap.model.message.ModifyDnResponseImpl;
32  import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
33  import org.apache.directory.api.ldap.model.name.Dn;
34  import org.apache.directory.api.ldap.model.name.DnFactory;
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<ModifyDnRequest>>
54  {
55      /** The logger */
56      private static final Logger LOG = LoggerFactory.getLogger( StoreModifyDnRequestNewRdn.class );
57  
58      /**
59       * Instantiates a new action.
60       */
61      public StoreModifyDnRequestNewRdn()
62      {
63          super( "Store ModifyDN request new RDN" );
64      }
65  
66  
67      /**
68       * {@inheritDoc}
69       */
70      @Override
71      public void action( LdapMessageContainer<ModifyDnRequest> container ) throws DecoderException
72      {
73          ModifyDnRequest modifyDnRequest = container.getMessage();
74  
75          // Get the Value and store it in the modifyDNRequest
76          TLV tlv = container.getCurrentTLV();
77  
78          // We have to handle the special case of a 0 length matched
79          // newDN
80          if ( tlv.getLength() == 0 )
81          {
82              String msg = I18n.err( I18n.ERR_05126_RDN_MUST_NOT_BE_NULL );
83              LOG.error( msg );
84  
85              ModifyDnResponseImpl response = new ModifyDnResponseImpl( modifyDnRequest.getMessageId() );
86              throw new ResponseCarryingException( msg, response, ResultCodeEnum.INVALID_DN_SYNTAX,
87                  modifyDnRequest.getName(), null );
88          }
89          else
90          {
91              byte[] dnBytes = tlv.getValue().getData();
92              String dnStr = Strings.utf8ToString( dnBytes );
93  
94              try
95              {
96                  DnFactory dnFactory = container.getDnFactory();
97                  Dn entryDn;
98                  
99                  if ( dnFactory == null )
100                 {
101                     entryDn = new Dn( dnStr );
102                 }
103                 else
104                 {
105                     entryDn = dnFactory.create( dnStr );
106                 }
107                 
108                 Rdn newRdn = entryDn.getRdn( entryDn.size() - 1 );
109                 modifyDnRequest.setNewRdn( newRdn );
110             }
111             catch ( LdapInvalidDnException ine )
112             {
113                 String msg = I18n.err( I18n.ERR_05127_INVALID_NEW_RDN, dnStr, Strings.dumpBytes( dnBytes ) );
114                 LOG.error( I18n.err( I18n.ERR_05114_ERROR_MESSAGE, msg, ine.getMessage() ) );
115 
116                 ModifyDnResponseImpl response = new ModifyDnResponseImpl( modifyDnRequest.getMessageId() );
117                 throw new ResponseCarryingException( msg, response, ResultCodeEnum.INVALID_DN_SYNTAX,
118                     modifyDnRequest.getName(), ine );
119             }
120         }
121 
122         if ( LOG.isDebugEnabled() )
123         {
124             LOG.debug( I18n.msg( I18n.MSG_05138_MODIFYING_WITH_NEW_RDN, modifyDnRequest.getNewRdn() ) );
125         }
126     }
127 }