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.ldapResult;
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.model.message.LdapResult;
29  import org.apache.directory.api.ldap.model.message.Message;
30  import org.apache.directory.api.ldap.model.message.Referral;
31  import org.apache.directory.api.ldap.model.message.ReferralImpl;
32  import org.apache.directory.api.ldap.model.message.ResultResponse;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  
37  /**
38   * The action used to init referrals to a LdapResult :
39   * <pre>
40   * LDAPResult ::= SEQUENCE {
41   *     ...
42   *     referral   [3] Referral OPTIONNAL }
43   * </pre>
44   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
45   */
46  public class InitReferrals extends GrammarAction<LdapMessageContainer<Message>>
47  {
48      /** The logger */
49      private static final Logger LOG = LoggerFactory.getLogger( InitReferrals.class );
50  
51      /**
52       * Instantiates a new init referrals action.
53       */
54      public InitReferrals()
55      {
56          super( "Init the referrals list" );
57      }
58  
59  
60      /**
61       * {@inheritDoc}
62       */
63      @Override
64      public void action( LdapMessageContainer<Message> container ) throws DecoderException
65      {
66          TLV tlv = container.getCurrentTLV();
67  
68          // If we hae a Referrals sequence, then it should not be empty
69          if ( tlv.getLength() == 0 )
70          {
71              String msg = I18n.err( I18n.ERR_05105_REFERRAL_MUST_NOT_BE_NULL );
72              LOG.error( msg );
73  
74              // This will generate a PROTOCOL_ERROR
75              throw new DecoderException( msg );
76          }
77  
78          ResultResponse response = ( ResultResponse ) container.getMessage();
79          LdapResult ldapResult = response.getLdapResult();
80  
81          Referral referral = new ReferralImpl();
82          ldapResult.setReferral( referral );
83  
84          if ( LOG.isDebugEnabled() )
85          {
86              LOG.debug( I18n.msg( I18n.MSG_05105_INITIALISNG_REFERRAL_LIST ) );
87          }
88      }
89  }