001/*
002 *   Licensed to the Apache Software Foundation (ASF) under one
003 *   or more contributor license agreements.  See the NOTICE file
004 *   distributed with this work for additional information
005 *   regarding copyright ownership.  The ASF licenses this file
006 *   to you under the Apache License, Version 2.0 (the
007 *   "License"); you may not use this file except in compliance
008 *   with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *   Unless required by applicable law or agreed to in writing,
013 *   software distributed under the License is distributed on an
014 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *   KIND, either express or implied.  See the License for the
016 *   specific language governing permissions and limitations
017 *   under the License.
018 *
019 */
020package org.apache.directory.api.ldap.extras.extended.ads_impl.certGeneration;
021
022
023import java.nio.ByteBuffer;
024
025import org.apache.directory.api.asn1.DecoderException;
026import org.apache.directory.api.asn1.ber.Asn1Decoder;
027import org.apache.directory.api.asn1.ber.tlv.BerValue;
028import org.apache.directory.api.asn1.util.Asn1Buffer;
029import org.apache.directory.api.ldap.codec.api.AbstractExtendedOperationFactory;
030import org.apache.directory.api.ldap.codec.api.ExtendedOperationFactory;
031import org.apache.directory.api.ldap.codec.api.LdapApiService;
032import org.apache.directory.api.ldap.extras.extended.certGeneration.CertGenerationRequest;
033import org.apache.directory.api.ldap.extras.extended.certGeneration.CertGenerationRequestImpl;
034import org.apache.directory.api.ldap.extras.extended.certGeneration.CertGenerationResponse;
035import org.apache.directory.api.ldap.extras.extended.certGeneration.CertGenerationResponseImpl;
036import org.apache.directory.api.ldap.model.message.ExtendedRequest;
037
038
039/**
040 * An {@link ExtendedOperationFactory} for creating certificate generation extended request response 
041 * pairs.
042 *
043 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
044 */
045public class CertGenerationFactory extends AbstractExtendedOperationFactory
046{
047    /**
048     * Creates a new instance of CertGenerationFactory.
049     *
050     * @param codec The codec for this factory.
051     */
052    public CertGenerationFactory( LdapApiService codec )
053    {
054        super( codec, CertGenerationRequest.EXTENSION_OID );
055    }
056
057
058    /**
059     * {@inheritDoc}
060     */
061    @Override
062    public CertGenerationRequest newRequest()
063    {
064        CertGenerationRequest certGenerationRequest = new CertGenerationRequestImpl();
065
066        return certGenerationRequest;
067    }
068
069
070    /**
071     * {@inheritDoc}
072     */
073    @Override
074    public CertGenerationRequest newRequest( byte[] encodedValue ) throws DecoderException
075    {
076        CertGenerationRequest certGenerationRequest = new CertGenerationRequestImpl();
077        decodeValue( certGenerationRequest, encodedValue );
078
079        return certGenerationRequest;
080    }
081
082
083    /**
084     * {@inheritDoc}
085     */
086    @Override
087    public CertGenerationResponse newResponse()
088    {
089        return new CertGenerationResponseImpl();
090    }
091
092
093    /**
094     * {@inheritDoc}
095     */
096    @Override
097    public void decodeValue( ExtendedRequest extendedRequest, byte[] requestValue ) throws DecoderException
098    {
099        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}