001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.directory.api.ldap.codec.actions.ldapResult;
021
022
023import org.apache.directory.api.asn1.DecoderException;
024import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
025import org.apache.directory.api.asn1.ber.tlv.TLV;
026import org.apache.directory.api.i18n.I18n;
027import org.apache.directory.api.ldap.codec.api.LdapMessageContainer;
028import org.apache.directory.api.ldap.codec.api.MessageDecorator;
029import org.apache.directory.api.ldap.model.exception.LdapURLEncodingException;
030import org.apache.directory.api.ldap.model.message.LdapResult;
031import org.apache.directory.api.ldap.model.message.Message;
032import org.apache.directory.api.ldap.model.message.Referral;
033import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
034import org.apache.directory.api.ldap.model.message.ResultResponse;
035import org.apache.directory.api.ldap.model.url.LdapUrl;
036import org.apache.directory.api.util.Strings;
037import org.slf4j.Logger;
038import org.slf4j.LoggerFactory;
039
040
041/**
042 * The action used to add a referral to a LdapTresult
043 * <pre>
044 * Referral ::= SEQUENCE SIZE (1..MAX) OF uri URI (RFC 4511)
045 * URI ::= LDAPString
046 * </pre>
047 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
048 */
049public class AddReferral extends GrammarAction<LdapMessageContainer<MessageDecorator<? extends Message>>>
050{
051    /** The logger */
052    private static final Logger LOG = LoggerFactory.getLogger( AddReferral.class );
053
054    /** Speedup for logs */
055    private static final boolean IS_DEBUG = LOG.isDebugEnabled();
056
057
058    /**
059     * Instantiates a new referral action.
060     */
061    public AddReferral()
062    {
063        super( "Add a referral" );
064    }
065
066
067    /**
068     * {@inheritDoc}
069     */
070    public void action( LdapMessageContainer<MessageDecorator<? extends Message>> container ) throws DecoderException
071    {
072        TLV tlv = container.getCurrentTLV();
073
074        Message response = container.getMessage();
075        LdapResult ldapResult = ( ( ResultResponse ) response ).getLdapResult();
076        Referral referral = ldapResult.getReferral();
077
078        if ( tlv.getLength() == 0 )
079        {
080            referral.addLdapUrl( "" );
081        }
082        else
083        {
084            if ( ldapResult.getResultCode() == ResultCodeEnum.REFERRAL )
085            {
086                try
087                {
088                    String url = Strings.utf8ToString( tlv.getValue().getData() );
089                    referral.addLdapUrl( new LdapUrl( url ).toString() );
090                }
091                catch ( LdapURLEncodingException luee )
092                {
093                    String badUrl = Strings.utf8ToString( tlv.getValue().getData() );
094                    LOG.error( I18n.err( I18n.ERR_04015, badUrl, luee.getMessage() ) );
095                    throw new DecoderException( I18n.err( I18n.ERR_04016, luee.getMessage() ), luee );
096                }
097            }
098            else
099            {
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            StringBuilder sb = new StringBuilder();
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}