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.del;
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.DeleteRequest;
31  import org.apache.directory.api.ldap.model.message.DeleteRequestImpl;
32  import org.apache.directory.api.ldap.model.message.DeleteResponseImpl;
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.DnFactory;
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 initialize the DelRequest.
43   * <pre>
44   * LdapMessage ::= ... DelRequest ...
45   * delRequest ::= [APPLICATION 10] LDAPDN
46   *
47   * We store the Dn to bve deleted into the DelRequest object
48   * </pre>
49   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
50   */
51  public class InitDelRequest extends GrammarAction<LdapMessageContainer<DeleteRequest>>
52  {
53      /** The logger */
54      private static final Logger LOG = LoggerFactory.getLogger( InitDelRequest.class );
55  
56      /**
57       * Instantiates a new action.
58       */
59      public InitDelRequest()
60      {
61          super( "Delete Request initialization" );
62      }
63  
64  
65      /**
66       * {@inheritDoc}
67       */
68      @Override
69      public void action( LdapMessageContainer<DeleteRequest> container ) throws DecoderException
70      {
71          // Create the DeleteRequest LdapMessage instance and store it in the container
72          DeleteRequest delRequest = new DeleteRequestImpl();
73          delRequest.setMessageId( container.getMessageId() );
74          container.setMessage( delRequest );
75  
76          // And store the Dn into it
77          // Get the Value and store it in the DelRequest
78          TLV tlv = container.getCurrentTLV();
79  
80          // We have to handle the special case of a 0 length matchedDN
81          if ( tlv.getLength() == 0 )
82          {
83              // This will generate a PROTOCOL_ERROR
84              throw new DecoderException( I18n.err( I18n.ERR_05119_NULL_ENTRY ) );
85          }
86          else
87          {
88              byte[] dnBytes = tlv.getValue().getData();
89              String dnStr = Strings.utf8ToString( dnBytes );
90  
91              try
92              {
93                  DnFactory dnFactory = container.getDnFactory();
94                  Dn entryDn;
95                  
96                  if ( dnFactory == null )
97                  {
98                      entryDn = new Dn( dnStr );
99                  }
100                 else
101                 {
102                     entryDn = dnFactory.create( dnStr );
103                 }
104 
105                 delRequest.setName( entryDn );
106             }
107             catch ( LdapInvalidDnException ine )
108             {
109                 String msg = I18n.err( I18n.ERR_05120_INVALID_DELETE_DN, dnStr, 
110                     Strings.dumpBytes( dnBytes ), ine.getLocalizedMessage() );
111                 LOG.error( msg );
112 
113                 DeleteResponseImpl response = new DeleteResponseImpl( delRequest.getMessageId() );
114                 throw new ResponseCarryingException( msg, response, ResultCodeEnum.INVALID_DN_SYNTAX,
115                     Dn.EMPTY_DN, ine );
116             }
117         }
118 
119         if ( LOG.isDebugEnabled() )
120         {
121             LOG.debug( I18n.msg( I18n.MSG_05124_DELETING_DN, delRequest.getName() ) );
122         }
123 
124         // We can have an END transition
125         container.setGrammarEndAllowed( true );
126     }
127 }