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.shared.ldap.extras.extended.ads_impl.certGeneration;
021
022
023import org.apache.directory.shared.asn1.DecoderException;
024import org.apache.directory.shared.asn1.EncoderException;
025import org.apache.directory.shared.i18n.I18n;
026import org.apache.directory.shared.ldap.codec.api.ExtendedRequestDecorator;
027import org.apache.directory.shared.ldap.codec.api.LdapApiService;
028import org.apache.directory.shared.ldap.extras.extended.CertGenerationRequest;
029import org.apache.directory.shared.ldap.extras.extended.CertGenerationResponse;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032
033
034/**
035 * A Decorator for CancelRequests.
036 *
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039public class CertGenerationRequestDecorator 
040    extends ExtendedRequestDecorator<CertGenerationRequest,CertGenerationResponse> 
041    implements CertGenerationRequest
042{
043    private static final Logger LOG = LoggerFactory.getLogger( CertGenerationRequestDecorator.class );
044    
045
046    private CertGenerationObject certGenObj;
047
048
049    public CertGenerationRequestDecorator( LdapApiService codec, CertGenerationRequest decoratedMessage )
050    {
051        super( codec, decoratedMessage );
052        certGenObj = new CertGenerationObject( decoratedMessage );
053    }
054
055    
056    public CertGenerationObject getCertGenerationObject()
057    {
058        return certGenObj;
059    }
060
061    
062    /**
063     * {@inheritDoc}
064     */
065    @Override
066    public void setRequestValue( byte[] requestValue )
067    {
068        CertGenerationDecoder decoder = new CertGenerationDecoder();
069
070        try
071        {
072            certGenObj = ( CertGenerationObject ) decoder.decode( requestValue );
073
074            if ( requestValue != null )
075            {
076                this.requestValue = new byte[requestValue.length];
077                System.arraycopy( requestValue, 0, this.requestValue, 0, requestValue.length );
078            }
079            else
080            {
081                this.requestValue = null;
082            }
083        }
084        catch ( DecoderException e )
085        {
086            LOG.error( I18n.err( I18n.ERR_04165 ), e );
087            throw new RuntimeException( e );
088        }
089    }
090
091
092    /**
093     * {@inheritDoc}
094     */
095    @Override
096    public byte[] getRequestValue()
097    {
098        if ( requestValue == null )
099        {
100            try
101            {
102                requestValue = certGenObj.encode().array();
103            }
104            catch ( EncoderException e )
105            {
106                LOG.error( I18n.err( I18n.ERR_04167 ), e );
107                throw new RuntimeException( e );
108            }
109        }
110
111        if ( requestValue == null )
112        {
113            return null;
114        }
115
116        final byte[] copy = new byte[requestValue.length];
117        System.arraycopy( requestValue, 0, copy, 0, requestValue.length );
118        return copy;
119    }
120
121
122    /**
123     * {@inheritDoc}
124     */
125    @Override
126    public CertGenerationResponse getResultResponse()
127    {
128        return getDecorated().getResultResponse();
129    }
130    
131
132    /**
133     * {@inheritDoc}
134     */
135    public String getTargetDN()
136    {
137        return getDecorated().getTargetDN();
138    }
139
140
141    /**
142     * {@inheritDoc}
143     */
144    public void setTargetDN( String targetDN )
145    {
146        getDecorated().setTargetDN( targetDN );
147    }
148
149
150    /**
151     * {@inheritDoc}
152     */
153    public String getIssuerDN()
154    {
155        return getDecorated().getIssuerDN();
156    }
157
158
159    /**
160     * {@inheritDoc}
161     */
162    public void setIssuerDN( String issuerDN )
163    {
164        getDecorated().setIssuerDN( issuerDN );
165    }
166
167
168    /**
169     * {@inheritDoc}
170     */
171    public String getSubjectDN()
172    {
173        return getDecorated().getSubjectDN();
174    }
175
176
177    /**
178     * {@inheritDoc}
179     */
180    public void setSubjectDN( String subjectDN )
181    {
182        getDecorated().setSubjectDN( subjectDN );
183    }
184
185
186    /**
187     * {@inheritDoc}
188     */
189    public String getKeyAlgorithm()
190    {
191        return getDecorated().getKeyAlgorithm();
192    }
193
194
195    /**
196     * {@inheritDoc}
197     */
198    public void setKeyAlgorithm( String keyAlgorithm )
199    {
200        getDecorated().setKeyAlgorithm( keyAlgorithm );
201    }
202}