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.model.exception.LdapURLEncodingException;
29 import org.apache.directory.api.ldap.model.message.LdapResult;
30 import org.apache.directory.api.ldap.model.message.Message;
31 import org.apache.directory.api.ldap.model.message.Referral;
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.url.LdapUrl;
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 public class AddReferral extends GrammarAction<LdapMessageContainer<Message>>
49 {
50
51 private static final Logger LOG = LoggerFactory.getLogger( AddReferral.class );
52
53
54
55
56
57 public AddReferral()
58 {
59 super( "Add a referral" );
60 }
61
62
63
64
65
66 @Override
67 public void action( LdapMessageContainer<Message> container ) throws DecoderException
68 {
69 TLV tlv = container.getCurrentTLV();
70
71 Message response = container.getMessage();
72 LdapResult ldapResult = ( ( ResultResponse ) response ).getLdapResult();
73 Referral referral = ldapResult.getReferral();
74
75 if ( tlv.getLength() == 0 )
76 {
77 referral.addLdapUrl( "" );
78 }
79 else
80 {
81 if ( ldapResult.getResultCode() == ResultCodeEnum.REFERRAL )
82 {
83 try
84 {
85 String url = Strings.utf8ToString( tlv.getValue().getData() );
86 referral.addLdapUrl( new LdapUrl( url ).toString() );
87 }
88 catch ( LdapURLEncodingException luee )
89 {
90 String badUrl = Strings.utf8ToString( tlv.getValue().getData() );
91 LOG.error( I18n.err( I18n.ERR_05103_INVALID_URL, badUrl, luee.getMessage() ) );
92 throw new DecoderException( I18n.err( I18n.ERR_05104_INVALID_URL, luee.getMessage() ), luee );
93 }
94 }
95 else
96 {
97 if ( LOG.isWarnEnabled() )
98 {
99 LOG.warn( I18n.msg( I18n.MSG_05103_REFERRAL_ERROR_MESSAGE_NOT_ALLOWED ) );
100 }
101
102 referral.addLdapUrl( LdapUrl.EMPTY_URL.toString() );
103 }
104 }
105
106 if ( LOG.isDebugEnabled() )
107 {
108 StringBuilder sb = new StringBuilder();
109 boolean isFirst = true;
110
111 for ( String url : ldapResult.getReferral().getLdapUrls() )
112 {
113 if ( isFirst )
114 {
115 isFirst = false;
116 }
117 else
118 {
119 sb.append( ", " );
120 }
121
122 sb.append( url );
123 }
124
125 if ( LOG.isDebugEnabled() )
126 {
127 LOG.debug( I18n.msg( I18n.MSG_05104_REFERRAL_ERROR_MESSAGE_SET_TO, sb.toString() ) );
128 }
129 }
130
131
132 container.setGrammarEndAllowed( true );
133 }
134 }