View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
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   * A Decorator for certificate generation extended request.
42   *
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
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      /** stores the length of the request*/
53      private int requestLength = 0;
54  
55  
56      /**
57       * Creates a new instance of CertGenerationRequestDecorator.
58       * 
59       * @param codec The LDAP Service to use
60       * @param decoratedMessage The certificate generation request
61       */
62      public CertGenerationRequestDecorator( LdapApiService codec, CertGenerationRequest decoratedMessage )
63      {
64          super( codec, decoratedMessage );
65          certGenerationRequest = decoratedMessage;
66      }
67  
68  
69      /**
70       * @return The certificate generation request
71       */
72      public CertGenerationRequest getCertGenerationRequest()
73      {
74          return certGenerationRequest;
75      }
76  
77  
78      /**
79       * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
136      */
137     @Override
138     public CertGenerationResponse getResultResponse()
139     {
140         return ( CertGenerationResponse ) getDecorated().getResultResponse();
141     }
142 
143 
144     /**
145      * {@inheritDoc}
146      */
147     @Override
148     public String getTargetDN()
149     {
150         return getDecorated().getTargetDN();
151     }
152 
153 
154     /**
155      * {@inheritDoc}
156      */
157     @Override
158     public void setTargetDN( String targetDN )
159     {
160         getDecorated().setTargetDN( targetDN );
161     }
162 
163 
164     /**
165      * {@inheritDoc}
166      */
167     @Override
168     public String getIssuerDN()
169     {
170         return getDecorated().getIssuerDN();
171     }
172 
173 
174     /**
175      * {@inheritDoc}
176      */
177     @Override
178     public void setIssuerDN( String issuerDN )
179     {
180         getDecorated().setIssuerDN( issuerDN );
181     }
182 
183 
184     /**
185      * {@inheritDoc}
186      */
187     @Override
188     public String getSubjectDN()
189     {
190         return getDecorated().getSubjectDN();
191     }
192 
193 
194     /**
195      * {@inheritDoc}
196      */
197     @Override
198     public void setSubjectDN( String subjectDN )
199     {
200         getDecorated().setSubjectDN( subjectDN );
201     }
202 
203 
204     /**
205      * {@inheritDoc}
206      */
207     @Override
208     public String getKeyAlgorithm()
209     {
210         return getDecorated().getKeyAlgorithm();
211     }
212 
213 
214     /**
215      * {@inheritDoc}
216      */
217     @Override
218     public void setKeyAlgorithm( String keyAlgorithm )
219     {
220         getDecorated().setKeyAlgorithm( keyAlgorithm );
221     }
222 
223 
224     /**
225      * Compute the CertGenerationRequest length 
226      * 
227      * <pre>
228      * 0x30 L1 
229      *   | 
230      *   +--&gt; 0x04 LL target DN
231      *   +--&gt; 0x04 LL issuer DN
232      *   +--&gt; 0x04 LL subject DN
233      *   +--&gt; 0x04 LL key algorithm
234      * </pre>
235      */
236     /* no qualifier */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      * Encodes the CertGenerationRequest extended operation.
256      * 
257      * @return A ByteBuffer that contains the encoded PDU
258      * @throws org.apache.directory.api.asn1.EncoderException If anything goes wrong.
259      */
260     /* no qualifier */ByteBuffer encodeInternal() throws EncoderException
261     {
262         // Allocate the bytes buffer.
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 }