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.LdapInvalidDnException;
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.ResultCodeEnum;
033import org.apache.directory.api.ldap.model.message.ResultResponse;
034import org.apache.directory.api.ldap.model.name.Dn;
035import org.apache.directory.api.util.Strings;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039
040/**
041 * The action used to set the LdapResult matched Dn.
042 *
043 * <pre>
044 * LDAPResult ::= SEQUENCE {
045 *     ...
046 *     matchedDN LDAPDN,
047 *     ...
048 * </pre>
049 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
050 */
051public class StoreMatchedDN extends GrammarAction<LdapMessageContainer<MessageDecorator<? extends Message>>>
052{
053    /** The logger */
054    private static final Logger LOG = LoggerFactory.getLogger( StoreMatchedDN.class );
055
056    /** Speedup for logs */
057    private static final boolean IS_DEBUG = LOG.isDebugEnabled();
058
059
060    /**
061     * Instantiates a new matched dn action.
062     */
063    public StoreMatchedDN()
064    {
065        super( "Store matched Dn" );
066    }
067
068
069    /**
070     * {@inheritDoc}
071     */
072    public void action( LdapMessageContainer<MessageDecorator<? extends Message>> container ) throws DecoderException
073    {
074        // Get the Value and store it in the BindResponse
075        TLV tlv = container.getCurrentTLV();
076        Dn matchedDn;
077        ResultCodeEnum resultCode;
078
079        ResultResponse response = ( ResultResponse ) container.getMessage();
080        LdapResult ldapResult = response.getLdapResult();
081        resultCode = ldapResult.getResultCode();
082
083        // We have to handle the special case of a 0 length matched
084        // Dn
085        if ( tlv.getLength() == 0 )
086        {
087            matchedDn = Dn.EMPTY_DN;
088        }
089        else
090        {
091            // A not null matchedDn is valid for resultCodes
092            // NoSuchObject, AliasProblem, InvalidDNSyntax and
093            // AliasDreferencingProblem.
094
095            switch ( resultCode )
096            {
097                case NO_SUCH_OBJECT:
098                case ALIAS_PROBLEM:
099                case INVALID_DN_SYNTAX:
100                case ALIAS_DEREFERENCING_PROBLEM:
101                    byte[] dnBytes = tlv.getValue().getData();
102                    String dnStr = Strings.utf8ToString( dnBytes );
103
104                    try
105                    {
106                        matchedDn = new Dn( dnStr );
107                    }
108                    catch ( LdapInvalidDnException ine )
109                    {
110                        // This is for the client side. We will never decode LdapResult on the server
111                        String msg = I18n.err( I18n.ERR_04013, dnStr, Strings.dumpBytes( dnBytes ), ine
112                            .getLocalizedMessage() );
113                        LOG.error( msg );
114
115                        throw new DecoderException( I18n.err( I18n.ERR_04014, ine.getLocalizedMessage() ), ine );
116                    }
117
118                    break;
119
120                default:
121                    LOG.warn( "The matched Dn should not be set when the result code is not one of NoSuchObject,"
122                        + " AliasProblem, InvalidDNSyntax or AliasDreferencingProblem" );
123
124                    matchedDn = Dn.EMPTY_DN;
125                    break;
126            }
127        }
128
129        if ( IS_DEBUG )
130        {
131            LOG.debug( "The matchedDn is " + matchedDn );
132        }
133
134        ldapResult.setMatchedDn( matchedDn );
135    }
136}