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