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.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.codec.api.MessageDecorator;
29  import org.apache.directory.api.ldap.model.exception.LdapURLEncodingException;
30  import org.apache.directory.api.ldap.model.message.LdapResult;
31  import org.apache.directory.api.ldap.model.message.Message;
32  import org.apache.directory.api.ldap.model.message.Referral;
33  import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
34  import org.apache.directory.api.ldap.model.message.ResultResponse;
35  import org.apache.directory.api.ldap.model.url.LdapUrl;
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 add a referral to a LdapTresult
43   * <pre>
44   * Referral ::= SEQUENCE SIZE (1..MAX) OF uri URI (RFC 4511)
45   * URI ::= LDAPString
46   * </pre>
47   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
48   */
49  public class AddReferral extends GrammarAction<LdapMessageContainer<MessageDecorator<? extends Message>>>
50  {
51      /** The logger */
52      private static final Logger LOG = LoggerFactory.getLogger( AddReferral.class );
53  
54      /** Speedup for logs */
55      private static final boolean IS_DEBUG = LOG.isDebugEnabled();
56  
57  
58      /**
59       * Instantiates a new referral action.
60       */
61      public AddReferral()
62      {
63          super( "Add a referral" );
64      }
65  
66  
67      /**
68       * {@inheritDoc}
69       */
70      public void action( LdapMessageContainer<MessageDecorator<? extends Message>> container ) throws DecoderException
71      {
72          TLV tlv = container.getCurrentTLV();
73  
74          Message response = container.getMessage();
75          LdapResult ldapResult = ( ( ResultResponse ) response ).getLdapResult();
76          Referral referral = ldapResult.getReferral();
77  
78          if ( tlv.getLength() == 0 )
79          {
80              referral.addLdapUrl( "" );
81          }
82          else
83          {
84              if ( ldapResult.getResultCode() == ResultCodeEnum.REFERRAL )
85              {
86                  try
87                  {
88                      String url = Strings.utf8ToString( tlv.getValue().getData() );
89                      referral.addLdapUrl( new LdapUrl( url ).toString() );
90                  }
91                  catch ( LdapURLEncodingException luee )
92                  {
93                      String badUrl = Strings.utf8ToString( tlv.getValue().getData() );
94                      LOG.error( I18n.err( I18n.ERR_04015, badUrl, luee.getMessage() ) );
95                      throw new DecoderException( I18n.err( I18n.ERR_04016, luee.getMessage() ) );
96                  }
97              }
98              else
99              {
100                 LOG.warn( "The Referral error message is not allowed when havind an error code no equals to REFERRAL" );
101                 referral.addLdapUrl( LdapUrl.EMPTY_URL.toString() );
102             }
103         }
104 
105         if ( IS_DEBUG )
106         {
107             StringBuffer sb = new StringBuffer();
108             boolean isFirst = true;
109 
110             for ( String url : ldapResult.getReferral().getLdapUrls() )
111             {
112                 if ( isFirst )
113                 {
114                     isFirst = false;
115                 }
116                 else
117                 {
118                     sb.append( ", " );
119                 }
120 
121                 sb.append( url );
122             }
123 
124             LOG.debug( "The referral error message is set to " + sb.toString() );
125         }
126 
127         // We can have an END transition
128         container.setGrammarEndAllowed( true );
129     }
130 }