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.certGeneration;
21
22
23 import java.nio.ByteBuffer;
24
25 import org.apache.directory.api.asn1.DecoderException;
26 import org.apache.directory.api.asn1.EncoderException;
27 import org.apache.directory.api.asn1.ber.tlv.BerValue;
28 import org.apache.directory.api.asn1.ber.tlv.TLV;
29 import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
30 import org.apache.directory.api.i18n.I18n;
31 import org.apache.directory.api.ldap.codec.api.ExtendedRequestDecorator;
32 import org.apache.directory.api.ldap.codec.api.LdapApiService;
33 import org.apache.directory.api.ldap.extras.extended.certGeneration.CertGenerationRequest;
34 import org.apache.directory.api.ldap.extras.extended.certGeneration.CertGenerationResponse;
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 public class CertGenerationRequestDecorator extends ExtendedRequestDecorator<CertGenerationRequest>
46 implements CertGenerationRequest
47 {
48 private static final Logger LOG = LoggerFactory.getLogger( CertGenerationRequestDecorator.class );
49
50 private CertGenerationRequest certGenerationRequest;
51
52
53 private int requestLength = 0;
54
55
56
57
58
59
60
61
62 public CertGenerationRequestDecorator( LdapApiService codec, CertGenerationRequest decoratedMessage )
63 {
64 super( codec, decoratedMessage );
65 certGenerationRequest = decoratedMessage;
66 }
67
68
69
70
71
72 public CertGenerationRequest getCertGenerationRequest()
73 {
74 return certGenerationRequest;
75 }
76
77
78
79
80
81 @Override
82 public void setRequestValue( byte[] requestValue )
83 {
84 CertGenerationDecoder decoder = new CertGenerationDecoder();
85
86 try
87 {
88 certGenerationRequest = decoder.decode( requestValue );
89
90 if ( requestValue != null )
91 {
92 this.requestValue = new byte[requestValue.length];
93 System.arraycopy( requestValue, 0, this.requestValue, 0, requestValue.length );
94 }
95 else
96 {
97 this.requestValue = null;
98 }
99 }
100 catch ( DecoderException e )
101 {
102 LOG.error( I18n.err( I18n.ERR_04165 ), e );
103 throw new RuntimeException( e );
104 }
105 }
106
107
108
109
110
111 @Override
112 public byte[] getRequestValue()
113 {
114 if ( requestValue == null )
115 {
116 try
117 {
118 requestValue = encodeInternal().array();
119 }
120 catch ( EncoderException e )
121 {
122 LOG.error( I18n.err( I18n.ERR_04167 ), e );
123 throw new RuntimeException( e );
124 }
125 }
126
127 final byte[] copy = new byte[requestValue.length];
128 System.arraycopy( requestValue, 0, copy, 0, requestValue.length );
129
130 return copy;
131 }
132
133
134
135
136
137 @Override
138 public CertGenerationResponse getResultResponse()
139 {
140 return ( CertGenerationResponse ) getDecorated().getResultResponse();
141 }
142
143
144
145
146
147 @Override
148 public String getTargetDN()
149 {
150 return getDecorated().getTargetDN();
151 }
152
153
154
155
156
157 @Override
158 public void setTargetDN( String targetDN )
159 {
160 getDecorated().setTargetDN( targetDN );
161 }
162
163
164
165
166
167 @Override
168 public String getIssuerDN()
169 {
170 return getDecorated().getIssuerDN();
171 }
172
173
174
175
176
177 @Override
178 public void setIssuerDN( String issuerDN )
179 {
180 getDecorated().setIssuerDN( issuerDN );
181 }
182
183
184
185
186
187 @Override
188 public String getSubjectDN()
189 {
190 return getDecorated().getSubjectDN();
191 }
192
193
194
195
196
197 @Override
198 public void setSubjectDN( String subjectDN )
199 {
200 getDecorated().setSubjectDN( subjectDN );
201 }
202
203
204
205
206
207 @Override
208 public String getKeyAlgorithm()
209 {
210 return getDecorated().getKeyAlgorithm();
211 }
212
213
214
215
216
217 @Override
218 public void setKeyAlgorithm( String keyAlgorithm )
219 {
220 getDecorated().setKeyAlgorithm( keyAlgorithm );
221 }
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236 int computeLengthInternal()
237 {
238 int len = Strings.getBytesUtf8( certGenerationRequest.getTargetDN() ).length;
239 requestLength = 1 + TLV.getNbBytes( len ) + len;
240
241 len = Strings.getBytesUtf8( certGenerationRequest.getIssuerDN() ).length;
242 requestLength += 1 + TLV.getNbBytes( len ) + len;
243
244 len = Strings.getBytesUtf8( certGenerationRequest.getSubjectDN() ).length;
245 requestLength += 1 + TLV.getNbBytes( len ) + len;
246
247 len = Strings.getBytesUtf8( certGenerationRequest.getKeyAlgorithm() ).length;
248 requestLength += 1 + TLV.getNbBytes( len ) + len;
249
250 return 1 + TLV.getNbBytes( requestLength ) + requestLength;
251 }
252
253
254
255
256
257
258
259
260 ByteBuffer encodeInternal() throws EncoderException
261 {
262
263 ByteBuffer bb = ByteBuffer.allocate( computeLengthInternal() );
264
265 bb.put( UniversalTag.SEQUENCE.getValue() );
266 bb.put( TLV.getBytes( requestLength ) );
267
268 BerValue.encode( bb, certGenerationRequest.getTargetDN() );
269 BerValue.encode( bb, certGenerationRequest.getIssuerDN() );
270 BerValue.encode( bb, certGenerationRequest.getSubjectDN() );
271 BerValue.encode( bb, certGenerationRequest.getKeyAlgorithm() );
272
273 return bb;
274 }
275 }