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