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.storedProcedure;
021
022
023import java.util.ArrayList;
024import java.util.List;
025
026import org.apache.directory.api.asn1.ber.tlv.BerValue;
027import org.apache.directory.api.asn1.ber.tlv.IntegerDecoder;
028import org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException;
029import org.apache.directory.api.i18n.I18n;
030import org.apache.directory.api.ldap.model.message.AbstractExtendedRequest;
031import org.apache.directory.api.util.StringConstants;
032import org.apache.directory.api.util.Strings;
033import org.apache.directory.api.util.exception.NotImplementedException;
034
035
036/**
037 * An extended operation requesting the server to execute a stored procedure.
038 * 
039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040 */
041public class StoredProcedureRequestImpl extends AbstractExtendedRequest implements StoredProcedureRequest
042{
043    private String language = "Java";
044
045    private byte[] procedure = StringConstants.EMPTY_BYTES;
046
047    private List<StoredProcedureParameter> parameters = new ArrayList<StoredProcedureParameter>();
048
049    private StoredProcedureResponse response;
050
051
052    /**
053     * Instantiates a new stored procedure request.
054     *
055     * @param messageId the message id
056     */
057    public StoredProcedureRequestImpl( int messageId )
058    {
059        super( messageId );
060        this.setRequestName( EXTENSION_OID );
061    }
062
063
064    /**
065     * Instantiates a new stored procedure request.
066     */
067    public StoredProcedureRequestImpl()
068    {
069        this.setRequestName( EXTENSION_OID );
070    }
071
072
073    /**
074     * Instantiates a new stored procedure request.
075     *
076     * @param messageId the message id
077     * @param procedure the procedure
078     * @param language the language
079     */
080    public StoredProcedureRequestImpl( int messageId, String procedure, String language )
081    {
082        super( messageId );
083        this.setRequestName( EXTENSION_OID );
084        this.language = language;
085        this.procedure = Strings.getBytesUtf8( procedure );
086    }
087
088
089    // -----------------------------------------------------------------------
090    // Parameters of the Extended Request Payload
091    // -----------------------------------------------------------------------
092
093    /**
094     * {@inheritDoc}
095     */
096    public String getLanguage()
097    {
098        return language;
099    }
100
101
102    /**
103     * {@inheritDoc}
104     */
105    public void setLanguage( String language )
106    {
107        this.language = language;
108    }
109
110
111    public byte[] getProcedure()
112    {
113        if ( procedure == null )
114        {
115            return null;
116        }
117
118        final byte[] copy = new byte[procedure.length];
119        System.arraycopy( procedure, 0, copy, 0, procedure.length );
120        return copy;
121    }
122
123
124    public void setProcedure( byte[] procedure )
125    {
126        if ( procedure != null )
127        {
128            this.procedure = new byte[procedure.length];
129            System.arraycopy( procedure, 0, this.procedure, 0, procedure.length );
130        }
131        else
132        {
133            this.procedure = null;
134        }
135    }
136
137
138    public List<StoredProcedureParameter> getParameters()
139    {
140        return parameters;
141    }
142
143
144    public void addParameter( StoredProcedureParameter parameter )
145    {
146        parameters.add( parameter );
147    }
148
149
150    /**
151     * {@inheritDoc}
152     */
153    public void setProcedure( String procedure )
154    {
155        this.procedure = Strings.getBytesUtf8( procedure );
156    }
157
158
159    /**
160     * {@inheritDoc}
161     */
162    public String getProcedureSpecification()
163    {
164        return Strings.utf8ToString( procedure );
165    }
166
167
168    /**
169     * {@inheritDoc}
170     */
171    public int size()
172    {
173        return parameters.size();
174    }
175
176
177    /**
178     * {@inheritDoc}
179     */
180    public Object getParameterType( int index )
181    {
182        if ( !language.equals( "java" ) )
183        {
184            return parameters.get( index ).getType();
185        }
186
187        return getJavaParameterType( index );
188    }
189
190
191    /**
192     * {@inheritDoc}
193     */
194    @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "BC_IMPOSSIBLE_INSTANCEOF",
195        justification = "False positive")
196    public Object getParameterTypeString( int index )
197    {
198        if ( !language.equals( "java" ) )
199        {
200            Object obj = parameters.get( index ).getType();
201            if ( obj instanceof byte[] )
202            {
203                return Strings.utf8ToString( ( byte[] ) obj );
204            }
205        }
206
207        return getJavaParameterType( index );
208    }
209
210
211    /**
212     * {@inheritDoc}
213     */
214    public Class<?> getJavaParameterType( int index )
215    {
216        throw new NotImplementedException( I18n.err( I18n.ERR_04175 ) );
217    }
218
219
220    /**
221     * {@inheritDoc}
222     */
223    public Object getParameterValue( int index )
224    {
225        if ( !language.equals( "java" ) )
226        {
227            return parameters.get( index ).getValue();
228        }
229
230        return getJavaParameterValue( index );
231    }
232
233
234    /**
235     * {@inheritDoc}
236     */
237    @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "BC_IMPOSSIBLE_INSTANCEOF",
238        justification = "False positive")
239    public Object getParameterValueString( int index )
240    {
241        if ( !language.equals( "java" ) )
242        {
243            Object obj = parameters.get( index ).getValue();
244            if ( obj instanceof byte[] )
245            {
246                String str = Strings.utf8ToString( ( byte[] ) obj );
247                String type = ( String ) getParameterTypeString( index );
248
249                if ( type.equals( "int" ) )
250                {
251                    try
252                    {
253                        return IntegerDecoder.parse( new BerValue( ( byte[] ) obj ) );
254                    }
255                    catch ( IntegerDecoderException e )
256                    {
257                        throw new RuntimeException( "Failed to decode INTEGER: " +
258                            Strings.dumpBytes( ( byte[] ) obj ), e );
259                    }
260                }
261                else
262                {
263                    return str;
264                }
265            }
266        }
267
268        return getJavaParameterValue( index );
269    }
270
271
272    /**
273     * {@inheritDoc}
274     */
275    public Object getJavaParameterValue( int index )
276    {
277        throw new NotImplementedException( I18n.err( I18n.ERR_04176 ) );
278    }
279
280
281    /**
282     * {@inheritDoc}
283     */
284    public void addParameter( Object type, Object value )
285    {
286        /**
287         *
288         * FIXME: Why do we check here whether it's Java or not ?
289         * Codec has nothing to do with these details.
290         *
291         if ( ! this.procedure.getLanguage().equals( "java" ) )
292         {
293             StoredProcedureParameter parameter = new StoredProcedureParameter();
294             parameter.setType( ( byte[] ) type );
295             parameter.setValue( ( byte[] ) value );
296             this.procedure.addParameter( parameter );
297         }
298         
299         * Replacing this code with the one below without the conditional check.
300         
301         */
302
303        StoredProcedureParameter parameter = new StoredProcedureParameter();
304        parameter.setType( ( byte[] ) type );
305        parameter.setValue( ( byte[] ) value );
306        parameters.add( parameter );
307
308        // below here try to convert parameters to their appropriate byte[] representations
309
310        /**
311         * FIXME: What is this for?
312         * 
313         * throw new NotImplementedException( "conversion of value to java type not implemented" );
314         */
315    }
316
317
318    @Override
319    /**
320     * {@inheritDoc}
321     */
322    public StoredProcedureResponse getResultResponse()
323    {
324        if ( response == null )
325        {
326            StoredProcedureResponseImpl spr = new StoredProcedureResponseImpl( getMessageId() );
327            spr.setResponseName( EXTENSION_OID );
328            response = spr;
329        }
330
331        return response;
332    }
333}