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.LdapURLEncodingException;
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.Referral;
33 import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
34 import org.apache.directory.api.ldap.model.message.ResultResponse;
35 import org.apache.directory.api.ldap.model.url.LdapUrl;
36 import org.apache.directory.api.util.Strings;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40
41
42
43
44
45
46
47
48
49 public class AddReferral extends GrammarAction<LdapMessageContainer<MessageDecorator<? extends Message>>>
50 {
51
52 private static final Logger LOG = LoggerFactory.getLogger( AddReferral.class );
53
54
55 private static final boolean IS_DEBUG = LOG.isDebugEnabled();
56
57
58
59
60
61 public AddReferral()
62 {
63 super( "Add a referral" );
64 }
65
66
67
68
69
70 public void action( LdapMessageContainer<MessageDecorator<? extends Message>> container ) throws DecoderException
71 {
72 TLV tlv = container.getCurrentTLV();
73
74 Message response = container.getMessage();
75 LdapResult ldapResult = ( ( ResultResponse ) response ).getLdapResult();
76 Referral referral = ldapResult.getReferral();
77
78 if ( tlv.getLength() == 0 )
79 {
80 referral.addLdapUrl( "" );
81 }
82 else
83 {
84 if ( ldapResult.getResultCode() == ResultCodeEnum.REFERRAL )
85 {
86 try
87 {
88 String url = Strings.utf8ToString( tlv.getValue().getData() );
89 referral.addLdapUrl( new LdapUrl( url ).toString() );
90 }
91 catch ( LdapURLEncodingException luee )
92 {
93 String badUrl = Strings.utf8ToString( tlv.getValue().getData() );
94 LOG.error( I18n.err( I18n.ERR_04015, badUrl, luee.getMessage() ) );
95 throw new DecoderException( I18n.err( I18n.ERR_04016, luee.getMessage() ) );
96 }
97 }
98 else
99 {
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 StringBuffer sb = new StringBuffer();
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
128 container.setGrammarEndAllowed( true );
129 }
130 }