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.response.intermediate;
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.IntermediateOperationFactory;
29  import org.apache.directory.api.ldap.codec.api.LdapMessageContainer;
30  import org.apache.directory.api.ldap.model.message.Control;
31  import org.apache.directory.api.ldap.model.message.IntermediateResponse;
32  import org.apache.directory.api.ldap.model.message.LdapResult;
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 IntermediateResponse Name
40   * <pre>
41   * IntermediateResponse ::= [APPLICATION 25] SEQUENCE {
42   *     responseName [0] LDAPOID OPTIONAL,
43   * </pre>
44   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
45   */
46  public class StoreIntermediateResponseName extends GrammarAction<LdapMessageContainer<IntermediateResponse>>
47  {
48      /** The logger */
49      private static final Logger LOG = LoggerFactory.getLogger( StoreIntermediateResponseName.class );
50  
51      /**
52       * Instantiates a new response name action.
53       */
54      public StoreIntermediateResponseName()
55      {
56          super( "Store response name" );
57      }
58  
59  
60      /**
61       * {@inheritDoc}
62       */
63      public void action( LdapMessageContainer<IntermediateResponse> container ) throws DecoderException
64      {
65          // We can get the IntermediateResponse Object
66          IntermediateResponse intermediateResponse = container.getMessage();
67  
68          // Get the Value and store it in the IntermediateResponse
69          TLV tlv = container.getCurrentTLV();
70  
71          // We have to handle the special case of a 0 length matched
72          // OID.
73          if ( tlv.getLength() == 0 )
74          {
75              String msg = I18n.err( I18n.ERR_05122_NULL_NAME );
76              LOG.error( msg );
77              // This will generate a PROTOCOL_ERROR
78              throw new DecoderException( msg );
79          }
80          else
81          {
82              byte[] responseNameBytes = tlv.getValue().getData();
83  
84              // Check if the OID is valid
85              String oidStr = Strings.utf8ToString( responseNameBytes );
86  
87              if ( Oid.isOid( oidStr ) )
88              {
89                  // Get the factory
90                  IntermediateOperationFactory intermediateFactory = 
91                      container.getLdapCodecService().getIntermediateResponseFactories().get( oidStr );
92                  
93                  if ( intermediateFactory != null )
94                  {
95                      // Ok, let's create the new operation, which will replace
96                      // the one created during the init phase
97                      IntermediateResponse newIntermediateResponse = intermediateFactory.newResponse();
98                      newIntermediateResponse.setMessageId( intermediateResponse.getMessageId() );
99                      
100                     // Copy the LdapResult 
101                     LdapResult ldapResult = intermediateResponse.getLdapResult();
102                     newIntermediateResponse.getLdapResult().setDiagnosticMessage( ldapResult.getDiagnosticMessage() );
103                     newIntermediateResponse.getLdapResult().setMatchedDn( ldapResult.getMatchedDn() );
104                     newIntermediateResponse.getLdapResult().setReferral( ldapResult.getReferral() );
105                     newIntermediateResponse.getLdapResult().setResultCode( ldapResult.getResultCode() );
106                     
107                     // Copy the controls
108                     for ( Control control : intermediateResponse.getControls().values() )
109                     {
110                         newIntermediateResponse.addControl( control );
111                     }
112                     
113                     container.setMessage( newIntermediateResponse );
114                     container.setIntermediateFactory( intermediateFactory );
115                 }
116                 else
117                 {
118                     // We simply store the OID in teh existing message
119                     intermediateResponse.setResponseName( oidStr );
120                 }
121             }
122             else
123             {
124                 String msg = I18n.err( I18n.ERR_05133_INTERMEDIATE_RESPONSE_INVALID_OID, Strings.utf8ToString( responseNameBytes ), 
125                     Strings.dumpBytes( responseNameBytes ) );
126                 LOG.error( I18n.err( I18n.ERR_05114_ERROR_MESSAGE, msg, oidStr ) );
127 
128                 // Rethrow the exception, we will get a PROTOCOL_ERROR
129                 throw new DecoderException( msg );
130             }
131         }
132 
133         // We can have an END transition
134         container.setGrammarEndAllowed( true );
135 
136         if ( LOG.isDebugEnabled() )
137         {
138             LOG.debug( I18n.msg( I18n.MSG_05172_OID_READ, intermediateResponse.getResponseName() ) );
139         }
140     }
141 }