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   *    http://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.storedProcedure;
21  
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.directory.api.asn1.ber.tlv.BerValue;
27  import org.apache.directory.api.asn1.ber.tlv.IntegerDecoder;
28  import org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException;
29  import org.apache.directory.api.i18n.I18n;
30  import org.apache.directory.api.ldap.model.message.AbstractExtendedRequest;
31  import org.apache.directory.api.util.StringConstants;
32  import org.apache.directory.api.util.Strings;
33  import org.apache.directory.api.util.exception.NotImplementedException;
34  
35  
36  /**
37   * An extended operation requesting the server to execute a stored procedure.
38   * 
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   */
41  public class StoredProcedureRequestImpl extends AbstractExtendedRequest implements StoredProcedureRequest
42  {
43      private String language = "Java";
44  
45      private byte[] procedure = StringConstants.EMPTY_BYTES;
46  
47      private List<StoredProcedureParameter> parameters = new ArrayList<StoredProcedureParameter>();
48  
49      private StoredProcedureResponse response;
50  
51  
52      /**
53       * Instantiates a new stored procedure request.
54       *
55       * @param messageId the message id
56       */
57      public StoredProcedureRequestImpl( int messageId )
58      {
59          super( messageId );
60          this.setRequestName( EXTENSION_OID );
61      }
62  
63  
64      /**
65       * Instantiates a new stored procedure request.
66       */
67      public StoredProcedureRequestImpl()
68      {
69          this.setRequestName( EXTENSION_OID );
70      }
71  
72  
73      /**
74       * Instantiates a new stored procedure request.
75       *
76       * @param messageId the message id
77       * @param procedure the procedure
78       * @param language the language
79       */
80      public StoredProcedureRequestImpl( int messageId, String procedure, String language )
81      {
82          super( messageId );
83          this.setRequestName( EXTENSION_OID );
84          this.language = language;
85          this.procedure = Strings.getBytesUtf8( procedure );
86      }
87  
88  
89      // -----------------------------------------------------------------------
90      // Parameters of the Extended Request Payload
91      // -----------------------------------------------------------------------
92  
93      /**
94       * {@inheritDoc}
95       */
96      public String getLanguage()
97      {
98          return language;
99      }
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 }