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.controls.proxiedauthz;
21
22
23 import java.nio.ByteBuffer;
24
25 import org.apache.directory.api.asn1.Asn1Object;
26 import org.apache.directory.api.asn1.DecoderException;
27 import org.apache.directory.api.asn1.EncoderException;
28 import org.apache.directory.api.asn1.ber.tlv.BerValue;
29 import org.apache.directory.api.i18n.I18n;
30 import org.apache.directory.api.ldap.codec.api.ControlDecorator;
31 import org.apache.directory.api.ldap.codec.api.LdapApiService;
32 import org.apache.directory.api.ldap.model.message.controls.ProxiedAuthz;
33 import org.apache.directory.api.ldap.model.message.controls.ProxiedAuthzImpl;
34 import org.apache.directory.api.util.Strings;
35
36
37
38
39
40
41
42
43
44 public class ProxiedAuthzDecorator extends ControlDecorator<ProxiedAuthz> implements ProxiedAuthz
45 {
46
47 private byte[] authzIdBytes = null;
48
49
50
51
52
53
54
55
56 public ProxiedAuthzDecorator( LdapApiService codec )
57 {
58 super( codec, new ProxiedAuthzImpl() );
59 }
60
61
62
63
64
65
66
67
68
69 public ProxiedAuthzDecorator( LdapApiService codec, ProxiedAuthz control )
70 {
71 super( codec, control );
72 }
73
74
75
76
77
78
79
80 private ProxiedAuthz getProxiedAuthz()
81 {
82 return getDecorated();
83 }
84
85
86
87
88
89
90
91
92
93
94 @Override
95 public int computeLength()
96 {
97 int valueLength = 0;
98
99 if ( getAuthzId() != null )
100 {
101 authzIdBytes = Strings.getBytesUtf8( getAuthzId() );
102 valueLength = authzIdBytes.length;
103 }
104
105 return valueLength;
106 }
107
108
109
110
111
112
113
114
115
116 @Override
117 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
118 {
119 if ( buffer == null )
120 {
121 throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
122 }
123
124 if ( getAuthzId() != null )
125 {
126 buffer.put( authzIdBytes );
127 }
128
129 return buffer;
130 }
131
132
133
134
135
136 @Override
137 public byte[] getValue()
138 {
139 if ( value == null )
140 {
141 try
142 {
143 computeLength();
144 ByteBuffer buffer = ByteBuffer.allocate( valueLength );
145
146 if ( authzIdBytes != null )
147 {
148 BerValue.encode( buffer, authzIdBytes );
149 }
150 else
151 {
152 BerValue.encode( buffer, Strings.EMPTY_BYTES );
153 }
154
155 value = buffer.array();
156 }
157 catch ( Exception e )
158 {
159 return null;
160 }
161 }
162
163 return value;
164 }
165
166
167
168
169
170 @Override
171 public String getAuthzId()
172 {
173 return getProxiedAuthz().getAuthzId();
174 }
175
176
177
178
179
180 @Override
181 public void setAuthzId( String authzId )
182 {
183 getProxiedAuthz().setAuthzId( authzId );
184 }
185
186
187
188
189
190 @Override
191 public Asn1Object decode( byte[] controlBytes ) throws DecoderException
192 {
193 getProxiedAuthz().setAuthzId( Strings.utf8ToString( controlBytes ) );
194
195 return this;
196 }
197 }