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   *     https://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.ber.Asn1Decoder;
27  import org.apache.directory.api.asn1.ber.tlv.BerValue;
28  import org.apache.directory.api.asn1.util.Asn1Buffer;
29  import org.apache.directory.api.ldap.codec.api.AbstractExtendedOperationFactory;
30  import org.apache.directory.api.ldap.codec.api.ExtendedOperationFactory;
31  import org.apache.directory.api.ldap.codec.api.LdapApiService;
32  import org.apache.directory.api.ldap.extras.extended.certGeneration.CertGenerationRequest;
33  import org.apache.directory.api.ldap.extras.extended.certGeneration.CertGenerationRequestImpl;
34  import org.apache.directory.api.ldap.extras.extended.certGeneration.CertGenerationResponse;
35  import org.apache.directory.api.ldap.extras.extended.certGeneration.CertGenerationResponseImpl;
36  import org.apache.directory.api.ldap.model.message.ExtendedRequest;
37  
38  
39  /**
40   * An {@link ExtendedOperationFactory} for creating certificate generation extended request response 
41   * pairs.
42   *
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44   */
45  public class CertGenerationFactory extends AbstractExtendedOperationFactory
46  {
47      /**
48       * Creates a new instance of CertGenerationFactory.
49       *
50       * @param codec The codec for this factory.
51       */
52      public CertGenerationFactory( LdapApiService codec )
53      {
54          super( codec, CertGenerationRequest.EXTENSION_OID );
55      }
56  
57  
58      /**
59       * {@inheritDoc}
60       */
61      @Override
62      public CertGenerationRequest newRequest()
63      {
64          CertGenerationRequest certGenerationRequest = new CertGenerationRequestImpl();
65  
66          return certGenerationRequest;
67      }
68  
69  
70      /**
71       * {@inheritDoc}
72       */
73      @Override
74      public CertGenerationRequest newRequest( byte[] encodedValue ) throws DecoderException
75      {
76          CertGenerationRequest certGenerationRequest = new CertGenerationRequestImpl();
77          decodeValue( certGenerationRequest, encodedValue );
78  
79          return certGenerationRequest;
80      }
81  
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public CertGenerationResponse newResponse()
88      {
89          return new CertGenerationResponseImpl();
90      }
91  
92  
93      /**
94       * {@inheritDoc}
95       */
96      @Override
97      public void decodeValue( ExtendedRequest extendedRequest, byte[] requestValue ) throws DecoderException
98      {
99          ByteBuffer bb = ByteBuffer.wrap( requestValue );
100         CertGenerationRequestContainer container = new CertGenerationRequestContainer();
101         container.setCertGenerationRequest( ( CertGenerationRequest ) extendedRequest ); 
102         Asn1Decoder.decode( bb, container );
103     }
104 
105 
106     /**
107      * {@inheritDoc}
108      */
109     @Override
110     public void encodeValue( Asn1Buffer buffer, ExtendedRequest extendedRequest )
111     {
112         int start  = buffer.getPos();
113         CertGenerationRequest certGenerationRequest = ( CertGenerationRequest ) extendedRequest;
114         
115         // the key algorithm
116         BerValue.encodeOctetString( buffer, certGenerationRequest.getKeyAlgorithm() );
117         
118         // The subject
119         BerValue.encodeOctetString( buffer, certGenerationRequest.getSubjectDN() );
120 
121         // The issuer
122         BerValue.encodeOctetString( buffer, certGenerationRequest.getIssuerDN() );
123         
124         // The target
125         BerValue.encodeOctetString( buffer, certGenerationRequest.getTargetDN() );
126         
127         // The sequence
128         BerValue.encodeSequence( buffer, start );
129     }
130 }