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      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       * {@inheritDoc}
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       * {@inheritDoc}
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      * {@inheritDoc}
130      */
131     @Override
132     public CertGenerationResponse getResultResponse()
133     {
134         return ( CertGenerationResponse ) getDecorated().getResultResponse();
135     }
136 
137 
138     /**
139      * {@inheritDoc}
140      */
141     public String getTargetDN()
142     {
143         return getDecorated().getTargetDN();
144     }
145 
146 
147     /**
148      * {@inheritDoc}
149      */
150     public void setTargetDN( String targetDN )
151     {
152         getDecorated().setTargetDN( targetDN );
153     }
154 
155 
156     /**
157      * {@inheritDoc}
158      */
159     public String getIssuerDN()
160     {
161         return getDecorated().getIssuerDN();
162     }
163 
164 
165     /**
166      * {@inheritDoc}
167      */
168     public void setIssuerDN( String issuerDN )
169     {
170         getDecorated().setIssuerDN( issuerDN );
171     }
172 
173 
174     /**
175      * {@inheritDoc}
176      */
177     public String getSubjectDN()
178     {
179         return getDecorated().getSubjectDN();
180     }
181 
182 
183     /**
184      * {@inheritDoc}
185      */
186     public void setSubjectDN( String subjectDN )
187     {
188         getDecorated().setSubjectDN( subjectDN );
189     }
190 
191 
192     /**
193      * {@inheritDoc}
194      */
195     public String getKeyAlgorithm()
196     {
197         return getDecorated().getKeyAlgorithm();
198     }
199 
200 
201     /**
202      * {@inheritDoc}
203      */
204     public void setKeyAlgorithm( String keyAlgorithm )
205     {
206         getDecorated().setKeyAlgorithm( keyAlgorithm );
207     }
208 
209 
210     /**
211      * Compute the CertGenerationRequest length 
212      * 
213      * <pre>
214      * 0x30 L1 
215      *   | 
216      *   +--> 0x04 LL target DN
217      *   +--> 0x04 LL issuer DN
218      *   +--> 0x04 LL subject DN
219      *   +--> 0x04 LL key algorithm
220      * </pre>
221      */
222     /* no qualifier */ 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      * Encodes the CertGenerationRequest extended operation.
242      * 
243      * @return A ByteBuffer that contains the encoded PDU
244      * @throws org.apache.directory.api.asn1.EncoderException If anything goes wrong.
245      */
246     /* no qualifier */ ByteBuffer encodeInternal() throws EncoderException
247     {
248         // Allocate the bytes buffer.
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 }