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.util.Strings;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  
40  /**
41   * The action used to store the ModifyDnRequest new Superior
42   * <pre>
43   * ModifyDNRequest ::= [APPLICATION 12] SEQUENCE { ...
44   *     ...
45   *     newSuperior [0] LDAPDN OPTIONAL }
46   * </pre>
47   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
48   */
49  public class StoreModifyDnRequestNewSuperior extends GrammarAction<LdapMessageContainer<ModifyDnRequest>>
50  {
51      /** The logger */
52      private static final Logger LOG = LoggerFactory.getLogger( StoreModifyDnRequestNewSuperior.class );
53  
54      /**
55       * Instantiates a new action.
56       */
57      public StoreModifyDnRequestNewSuperior()
58      {
59          super( "Store new superior" );
60      }
61  
62  
63      /**
64       * {@inheritDoc}
65       */
66      @Override
67      public void action( LdapMessageContainer<ModifyDnRequest> container ) throws DecoderException
68      {
69          ModifyDnRequest modifyDnRequest = container.getMessage();
70  
71          // Get the Value and store it in the modifyDNRequest
72          TLV tlv = container.getCurrentTLV();
73  
74          // We have to handle the special case of a 0 length matched
75          // Dn
76          if ( tlv.getLength() == 0 )
77          {
78  
79              if ( modifyDnRequest.getDeleteOldRdn() )
80              {
81                  // This will generate a PROTOCOL_ERROR
82                  throw new DecoderException( I18n.err( I18n.ERR_05128_NULL_SUPERIOR ) );
83              }
84              else
85              {
86                  if ( LOG.isWarnEnabled() )
87                  {
88                      LOG.warn( I18n.msg( I18n.MSG_05139_NULL_NEW_SUPERIOR ) );
89                  }
90              }
91  
92              modifyDnRequest.setNewSuperior( Dn.EMPTY_DN );
93          }
94          else
95          {
96              byte[] dnBytes = tlv.getValue().getData();
97              String dnStr = Strings.utf8ToString( dnBytes );
98  
99              try
100             {
101                 DnFactory dnFactory = container.getDnFactory();
102                 Dn newSuperior;
103                 
104                 if ( dnFactory == null )
105                 {
106                     newSuperior = new Dn( dnStr );
107                 }
108                 else
109                 {
110                     newSuperior = dnFactory.create( dnStr );
111                 }
112 
113                 modifyDnRequest.setNewSuperior( newSuperior );
114             }
115             catch ( LdapInvalidDnException ine )
116             {
117                 String msg = I18n.err( I18n.ERR_05129_INVALID_NEW_SUPERIOR, dnStr, Strings.dumpBytes( dnBytes ) );
118                 LOG.error( I18n.err( I18n.ERR_05114_ERROR_MESSAGE, msg, ine.getMessage() ) );
119 
120                 ModifyDnResponseImpl response = new ModifyDnResponseImpl( modifyDnRequest.getMessageId() );
121                 throw new ResponseCarryingException( msg, response, ResultCodeEnum.INVALID_DN_SYNTAX,
122                     modifyDnRequest.getName(), ine );
123             }
124         }
125 
126         // We can have an END transition
127         container.setGrammarEndAllowed( true );
128 
129         if ( LOG.isDebugEnabled() )
130         {
131             LOG.debug( I18n.msg( I18n.MSG_05140_NEW_SUPERIOR_DN, modifyDnRequest.getNewSuperior() ) );
132         }
133     }
134 }