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.delRequest;
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.DeleteRequestDecorator;
30  import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
31  import org.apache.directory.api.ldap.model.message.DeleteRequest;
32  import org.apache.directory.api.ldap.model.message.DeleteRequestImpl;
33  import org.apache.directory.api.ldap.model.message.DeleteResponseImpl;
34  import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
35  import org.apache.directory.api.ldap.model.name.Dn;
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<DeleteRequestDecorator>>
52  {
53      /** The logger */
54      private static final Logger LOG = LoggerFactory.getLogger( InitDelRequest.class );
55  
56      /** Speedup for logs */
57      private static final boolean IS_DEBUG = LOG.isDebugEnabled();
58  
59  
60      /**
61       * Instantiates a new action.
62       */
63      public InitDelRequest()
64      {
65          super( "Delete Request initialization" );
66      }
67  
68  
69      /**
70       * {@inheritDoc}
71       */
72      public void action( LdapMessageContainer<DeleteRequestDecorator> container ) throws DecoderException
73      {
74          // Create the DeleteRequest LdapMessage instance and store it in the container
75          DeleteRequest internaldelRequest = new DeleteRequestImpl();
76          internaldelRequest.setMessageId( container.getMessageId() );
77          DeleteRequestDecorator delRequest = new DeleteRequestDecorator(
78              container.getLdapCodecService(), internaldelRequest );
79          container.setMessage( delRequest );
80  
81          // And store the Dn into it
82          // Get the Value and store it in the DelRequest
83          TLV tlv = container.getCurrentTLV();
84  
85          // We have to handle the special case of a 0 length matched
86          // Dn
87          Dn entry;
88  
89          if ( tlv.getLength() == 0 )
90          {
91              // This will generate a PROTOCOL_ERROR
92              throw new DecoderException( I18n.err( I18n.ERR_04073 ) );
93          }
94          else
95          {
96              byte[] dnBytes = tlv.getValue().getData();
97              String dnStr = Strings.utf8ToString( dnBytes );
98  
99              try
100             {
101                 entry = new Dn( dnStr );
102             }
103             catch ( LdapInvalidDnException ine )
104             {
105                 String msg = I18n.err( I18n.ERR_04074, dnStr, Strings.dumpBytes( dnBytes ), ine
106                     .getLocalizedMessage() );
107                 LOG.error( msg );
108 
109                 DeleteResponseImpl response = new DeleteResponseImpl( delRequest.getMessageId() );
110                 throw new ResponseCarryingException( msg, response, ResultCodeEnum.INVALID_DN_SYNTAX,
111                     Dn.EMPTY_DN, ine );
112             }
113 
114             delRequest.setName( entry );
115         }
116 
117         // We can have an END transition
118         container.setGrammarEndAllowed( true );
119 
120         if ( IS_DEBUG )
121         {
122             LOG.debug( "Deleting Dn {}", entry );
123         }
124     }
125 }