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.extras.extended.ads_impl.whoAmI;
21
22
23 import org.apache.directory.api.asn1.DecoderException;
24 import org.apache.directory.api.asn1.ber.grammar.AbstractGrammar;
25 import org.apache.directory.api.asn1.ber.grammar.Grammar;
26 import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
27 import org.apache.directory.api.asn1.ber.grammar.GrammarTransition;
28 import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
29 import org.apache.directory.api.ldap.codec.api.LdapApiServiceFactory;
30 import org.apache.directory.api.ldap.extras.extended.whoAmI.WhoAmIResponseImpl;
31 import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
32 import org.apache.directory.api.ldap.model.name.Dn;
33 import org.apache.directory.api.util.Strings;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class WhoAmIResponseGrammar extends AbstractGrammar<WhoAmIResponseContainer>
51 {
52
53 private static final Logger LOG = LoggerFactory.getLogger( WhoAmIResponseGrammar.class );
54
55
56 static final boolean IS_DEBUG = LOG.isDebugEnabled();
57
58
59 private static Grammar<WhoAmIResponseContainer> instance = new WhoAmIResponseGrammar();
60
61
62
63
64
65 @SuppressWarnings("unchecked")
66 public WhoAmIResponseGrammar()
67 {
68 setName( WhoAmIResponseGrammar.class.getName() );
69
70
71 super.transitions = new GrammarTransition[WhoAmIResponseStatesEnum.LAST_WHO_AM_I_RESPONSE_STATE
72 .ordinal()][256];
73
74
75
76
77
78
79
80
81 super.transitions[WhoAmIResponseStatesEnum.START_STATE.ordinal()][UniversalTag.OCTET_STRING.getValue()] =
82 new GrammarTransition<WhoAmIResponseContainer>(
83 WhoAmIResponseStatesEnum.START_STATE,
84 WhoAmIResponseStatesEnum.AUTHZ_ID_RESPONSE_STATE,
85 UniversalTag.OCTET_STRING.getValue(), new GrammarAction<WhoAmIResponseContainer>(
86 "Store AuthzId" )
87 {
88 public void action( WhoAmIResponseContainer container ) throws DecoderException
89 {
90 WhoAmIResponseDecorator whoAmIResponse = new WhoAmIResponseDecorator(
91 LdapApiServiceFactory.getSingleton(), new WhoAmIResponseImpl() );
92 container.setWhoAmIResponse( whoAmIResponse );
93
94 byte[] data = container.getCurrentTLV().getValue().getData();
95
96 if ( data != null )
97 {
98 switch ( data.length )
99 {
100 case 0:
101
102 case 1:
103
104 String msg = "authzId too short. Must starts with either u: or dn:";
105 LOG.error( msg );
106 throw new DecoderException( msg );
107
108 case 2 :
109 if ( ( data[0] == 'u' ) && ( data[1] == ':' ) )
110 {
111 whoAmIResponse.setAuthzId( data );
112 whoAmIResponse.setUserId( Strings.utf8ToString( data, 3, data.length - 3 ) );
113 }
114 else
115 {
116 msg = "authzId Must starts with either u: or dn:, it starts with " + Strings.utf8ToString( data );
117 LOG.error( msg );
118 throw new DecoderException( msg );
119 }
120
121 break;
122
123 default :
124 switch ( data[0] )
125 {
126 case 'u' :
127 if ( data[1] == ':' )
128 {
129 whoAmIResponse.setAuthzId( data );
130 whoAmIResponse.setUserId( Strings.utf8ToString( data, 3, data.length - 3 ) );
131 }
132 else
133 {
134 msg = "authzId Must starts with either u: or dn:, it starts with " + Strings.utf8ToString( data );
135 LOG.error( msg );
136 throw new DecoderException( msg );
137 }
138
139 break;
140
141 case 'd' :
142 if ( ( data[1] == 'n' ) && ( data[2] == ':' ) )
143 {
144
145 if ( Dn.isValid( Strings.utf8ToString( data, 3, data.length - 3 ) ) )
146 {
147 whoAmIResponse.setAuthzId( data );
148
149 try
150 {
151 whoAmIResponse.setDn( new Dn( Strings.utf8ToString( data, 3, data.length - 3 ) ) );
152 }
153 catch ( LdapInvalidDnException e )
154 {
155
156 }
157 }
158 else
159 {
160 msg = "authzId Must starts with either u: or dn:, it starts with " + Strings.utf8ToString( data );
161 LOG.error( msg );
162 throw new DecoderException( msg );
163 }
164 }
165 else
166 {
167 msg = "authzId Must starts with either u: or dn:, it starts with " + Strings.utf8ToString( data );
168 LOG.error( msg );
169 throw new DecoderException( msg );
170 }
171
172 break;
173
174 default :
175 msg = "authzId Must starts with either u: or dn:, it starts with " + Strings.utf8ToString( data );
176 LOG.error( msg );
177 throw new DecoderException( msg );
178 }
179
180 break;
181 }
182 }
183 else
184 {
185 whoAmIResponse.setAuthzId( null );
186 }
187
188
189 container.setGrammarEndAllowed( true );
190 }
191 } );
192 }
193
194
195
196
197
198
199
200 public static Grammar<WhoAmIResponseContainer> getInstance()
201 {
202 return instance;
203 }
204 }