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.storedProcedure;
021
022
023import org.apache.directory.shared.asn1.DecoderException; 
024import org.apache.directory.shared.ldap.codec.api.ExtendedRequestDecorator;
025import org.apache.directory.shared.ldap.codec.api.ExtendedRequestFactory;
026import org.apache.directory.shared.ldap.codec.api.ExtendedResponseDecorator;
027import org.apache.directory.shared.ldap.codec.api.LdapApiService;
028import org.apache.directory.shared.ldap.extras.extended.StoredProcedureRequest;
029import org.apache.directory.shared.ldap.extras.extended.StoredProcedureResponse;
030import org.apache.directory.shared.ldap.extras.extended.StoredProcedureResponseImpl;
031import org.apache.directory.shared.ldap.model.message.ExtendedRequest;
032import org.apache.directory.shared.ldap.model.message.ExtendedResponse;
033
034
035/**
036 * An {@link ExtendedRequestFactory} for creating cancel extended request response 
037 * pairs.
038 *
039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040 */
041public class StoredProcedureFactory implements ExtendedRequestFactory<StoredProcedureRequest, StoredProcedureResponse>
042{
043    private LdapApiService codec;
044    
045    
046    /**
047     * Creates a new instance of StoredProcedureFactory.
048     *
049     * @param codec
050     */
051    public StoredProcedureFactory( LdapApiService codec )
052    {
053        this.codec = codec;
054    }
055    
056    
057    /**
058     * {@inheritDoc}
059     */
060    public String getOid()
061    {
062        return StoredProcedureRequest.EXTENSION_OID;
063    }
064
065    
066    /**
067     * {@inheritDoc}
068     */
069    public StoredProcedureRequest newRequest()
070    {
071        return new StoredProcedureRequestDecorator( codec );
072    }
073
074
075    /**
076     * {@inheritDoc}
077     */
078    public StoredProcedureResponse newResponse( byte[] encodedValue ) throws DecoderException
079    {
080        StoredProcedureResponseDecorator response = new StoredProcedureResponseDecorator( codec, new StoredProcedureResponseImpl() );
081        response.setResponseValue( encodedValue );
082        return response;
083    }
084
085
086    /**
087     * {@inheritDoc}
088     */
089    public StoredProcedureRequest newRequest( byte[] value )
090    {
091        StoredProcedureRequestDecorator req = new StoredProcedureRequestDecorator( codec );
092        
093        if ( value != null )
094        {
095            req.setRequestValue( value );
096        }
097        return req;
098    }
099
100
101    /**
102     * {@inheritDoc}
103     */
104    public ExtendedRequestDecorator<StoredProcedureRequest, StoredProcedureResponse> decorate(
105        ExtendedRequest<?> modelRequest )
106    {
107        if ( modelRequest instanceof StoredProcedureRequestDecorator )
108        {
109            return ( StoredProcedureRequestDecorator ) modelRequest;
110        }
111        
112        
113        return new StoredProcedureRequestDecorator( codec, ( StoredProcedureRequest ) modelRequest );
114    }
115
116
117    /**
118     * {@inheritDoc}
119     */
120    public ExtendedResponseDecorator<StoredProcedureResponse> decorate( ExtendedResponse decoratedMessage )
121    {
122        if ( decoratedMessage instanceof StoredProcedureResponseDecorator )
123        {
124            return ( StoredProcedureResponseDecorator ) decoratedMessage;
125        }
126        
127        return new StoredProcedureResponseDecorator( codec, ( StoredProcedureResponse ) decoratedMessage );
128    }
129}