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