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.extendedResponse;
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.asn1.util.Oid;
27  import org.apache.directory.api.i18n.I18n;
28  import org.apache.directory.api.ldap.codec.api.ExtendedResponseDecorator;
29  import org.apache.directory.api.ldap.codec.api.LdapApiServiceFactory;
30  import org.apache.directory.api.ldap.codec.api.LdapMessageContainer;
31  import org.apache.directory.api.ldap.codec.decorators.LdapResultDecorator;
32  import org.apache.directory.api.ldap.model.message.ExtendedResponse;
33  import org.apache.directory.api.util.Strings;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  
38  /**
39   * The action used to store a Response Name to an ExtendedResponse
40   * <pre>
41   * LdapMessage ::= ... ExtendedResponse ...
42   * ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
43   *     COMPONENTS OF LDAPResult,
44   *     responseName   [10] LDAPOID OPTIONAL,
45   *     ...
46   * </pre>
47   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
48   */
49  public class StoreExtendedResponseName extends GrammarAction<LdapMessageContainer<ExtendedResponseDecorator<?>>>
50  {
51      /** The logger */
52      private static final Logger LOG = LoggerFactory.getLogger( StoreExtendedResponseName.class );
53  
54      /** Speedup for logs */
55      private static final boolean IS_DEBUG = LOG.isDebugEnabled();
56  
57  
58      /**
59       * Instantiates a new response name action.
60       */
61      public StoreExtendedResponseName()
62      {
63          super( "Store response name" );
64      }
65  
66  
67      /**
68       * {@inheritDoc}
69       */
70      public void action( LdapMessageContainer<ExtendedResponseDecorator<?>> container ) throws DecoderException
71      {
72          // We can allocate the ExtendedResponse Object
73          ExtendedResponse extendedResponse = null;
74  
75          // Get the Value and store it in the ExtendedResponse
76          TLV tlv = container.getCurrentTLV();
77  
78          // We have to handle the special case of a 0 length matched
79          // OID
80          if ( tlv.getLength() == 0 )
81          {
82              String msg = I18n.err( I18n.ERR_04017 );
83              LOG.error( msg );
84              throw new DecoderException( msg );
85          }
86          else
87          {
88              String responseName = Oid.fromString( Strings.asciiBytesToString( tlv.getValue().getData() ) )
89                  .toString();
90  
91              extendedResponse = LdapApiServiceFactory.getSingleton().newExtendedResponse( responseName,
92                  container.getMessageId(), null );
93              
94              ((ExtendedResponseDecorator<?>)extendedResponse).setLdapResult( ((LdapResultDecorator)(container.getMessage().getLdapResult() ) ) );
95              container.setMessage( LdapApiServiceFactory.getSingleton().decorate( extendedResponse ) );
96          }
97  
98          // We can have an END transition
99          container.setGrammarEndAllowed( true );
100 
101         if ( IS_DEBUG )
102         {
103             LOG.debug( "OID read : {}", extendedResponse.getResponseName() );
104         }
105     }
106 }