1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.LdapInvalidDnException;
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.ResultCodeEnum;
33 import org.apache.directory.api.ldap.model.message.ResultResponse;
34 import org.apache.directory.api.ldap.model.name.Dn;
35 import org.apache.directory.api.util.Strings;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39
40
41
42
43
44
45
46
47
48
49
50
51 public class StoreMatchedDN extends GrammarAction<LdapMessageContainer<MessageDecorator<? extends Message>>>
52 {
53
54 private static final Logger LOG = LoggerFactory.getLogger( StoreMatchedDN.class );
55
56
57 private static final boolean IS_DEBUG = LOG.isDebugEnabled();
58
59
60
61
62
63 public StoreMatchedDN()
64 {
65 super( "Store matched Dn" );
66 }
67
68
69
70
71
72 public void action( LdapMessageContainer<MessageDecorator<? extends Message>> container ) throws DecoderException
73 {
74
75 TLV tlv = container.getCurrentTLV();
76 Dn matchedDn = null;
77 ResultCodeEnum resultCode = null;
78
79 ResultResponse response = ( ResultResponse ) container.getMessage();
80 LdapResult ldapResult = response.getLdapResult();
81 resultCode = ldapResult.getResultCode();
82
83
84
85 if ( tlv.getLength() == 0 )
86 {
87 matchedDn = Dn.EMPTY_DN;
88 }
89 else
90 {
91
92
93
94
95 switch ( resultCode )
96 {
97 case NO_SUCH_OBJECT:
98 case ALIAS_PROBLEM:
99 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
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() ) );
116 }
117
118 break;
119
120 default:
121 LOG.warn( "The matched Dn should not be set when the result code is 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 }