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