1 18 19 package org.apache.jmeter.protocol.ldap.sampler; 20 21 import javax.naming.NamingException ; 22 import javax.naming.directory.Attribute ; 23 import javax.naming.directory.BasicAttribute ; 24 import javax.naming.directory.BasicAttributes ; 25 import javax.naming.directory.DirContext ; 26 import javax.naming.directory.ModificationItem ; 27 28 import org.apache.jmeter.config.Argument; 29 import org.apache.jmeter.config.Arguments; 30 import org.apache.jmeter.config.ConfigTestElement; 31 import org.apache.jmeter.config.LoginConfig; 32 import org.apache.jmeter.protocol.ldap.config.LdapConfig; 33 import org.apache.jmeter.samplers.AbstractSampler; 34 import org.apache.jmeter.samplers.Entry; 35 import org.apache.jmeter.samplers.SampleResult; 36 import org.apache.jmeter.testelement.TestElement; 37 import org.apache.jmeter.testelement.property.BooleanProperty; 38 import org.apache.jmeter.testelement.property.PropertyIterator; 39 import org.apache.jmeter.testelement.property.StringProperty; 40 import org.apache.jmeter.testelement.property.TestElementProperty; 41 import org.apache.jorphan.logging.LoggingManager; 42 import org.apache.log.Logger; 43 44 45 54 public class LDAPSampler extends AbstractSampler 55 { 56 transient private static Logger log = LoggingManager.getLoggerForClass(); 57 58 public final static String SERVERNAME = "servername"; 59 public final static String PORT = "port"; 60 public final static String ROOTDN = "rootdn"; 61 public final static String TEST = "test"; 62 public final static String ADD = "add"; 63 public final static String MODIFY = "modify"; 64 public final static String DELETE = "delete"; 65 public final static String SEARCHBASE = "search"; 66 public final static String SEARCHFILTER = "searchfilter"; 67 public final static String USER_DEFINED = "user_defined"; 68 public final static String ARGUMENTS = "arguments"; 69 public final static String BASE_ENTRY_DN = "base_entry_dn"; 70 71 private static int counter=0; 74 75 private boolean searchFoundEntries; 77 public LDAPSampler() 78 { 79 } 80 81 82 public void addCustomTestElement(TestElement element) 83 { 84 if(element instanceof LdapConfig || element instanceof LoginConfig) 85 { 86 mergeIn(element); 87 } 88 } 89 90 95 public String getUsername() 96 { 97 return getPropertyAsString(ConfigTestElement.USERNAME); 98 } 99 100 105 public String getPassword() 106 { 107 return getPropertyAsString(ConfigTestElement.PASSWORD); 108 } 109 110 115 public void setServername(String servername) 116 { 117 setProperty(new StringProperty(SERVERNAME, servername)); 118 } 119 120 125 public void setPort(String port) 126 { 127 setProperty(new StringProperty(PORT, port)); 128 } 129 130 131 136 public String getServername() 137 { 138 return getPropertyAsString(SERVERNAME); 139 } 140 141 146 public String getPort() 147 { 148 return getPropertyAsString(PORT); 149 } 150 151 156 public void setRootdn(String newRootdn) 157 { 158 this.setProperty(ROOTDN,newRootdn); 159 } 160 161 166 public String getRootdn() 167 { 168 return getPropertyAsString(ROOTDN); 169 } 170 171 176 public void setTest(String newTest) 177 { 178 this.setProperty(TEST,newTest); 179 } 180 181 186 public String getTest() 187 { 188 return getPropertyAsString(TEST); 189 } 190 191 196 public void setUserDefinedTest(boolean value) 197 { 198 setProperty(new BooleanProperty(USER_DEFINED, value)); 199 } 200 201 207 public boolean getUserDefinedTest() 208 { 209 return getPropertyAsBoolean(USER_DEFINED); 210 } 211 212 217 public void setBaseEntryDN(String newbaseentry) 218 { 219 setProperty(new StringProperty(BASE_ENTRY_DN, newbaseentry)); 220 } 221 222 227 public String getBaseEntryDN() 228 { 229 return getPropertyAsString(BASE_ENTRY_DN); 230 } 231 232 238 public void setArguments(Arguments value) 239 { 240 setProperty(new TestElementProperty(ARGUMENTS, value)); 241 } 242 243 248 public Arguments getArguments() 249 { 250 return (Arguments) getProperty(ARGUMENTS).getObjectValue(); 251 } 252 253 254 261 private BasicAttributes getUserAttributes() 262 { 263 BasicAttribute basicattribute = new BasicAttribute ("objectclass"); 264 basicattribute.add("top"); 265 basicattribute.add("person"); 266 basicattribute.add("organizationalPerson"); 267 basicattribute.add("inetOrgPerson"); 268 BasicAttributes attrs = new BasicAttributes (true); 269 attrs.put(basicattribute); 270 BasicAttribute attr; 271 PropertyIterator iter = getArguments().iterator(); 272 273 while (iter.hasNext()) 274 { 275 Argument item = (Argument) iter.next().getObjectValue(); 276 attr = getBasicAttribute( item.getName(),item.getValue()); 277 attrs.put(attr); 278 } 279 return attrs; 280 } 281 282 283 290 private ModificationItem [] getUserModAttributes() 291 { 292 ModificationItem [] mods = 293 new ModificationItem [getArguments().getArguments().size()]; 294 BasicAttribute attr; 295 PropertyIterator iter = getArguments().iterator(); 296 int count =0; 297 while (iter.hasNext()) 298 { 299 Argument item = (Argument) iter.next().getObjectValue(); 300 attr = getBasicAttribute( item.getName(),item.getValue()); 301 mods[count] = 302 new ModificationItem (DirContext.REPLACE_ATTRIBUTE, attr); 303 count=+1; 304 } 305 return mods; 306 } 307 308 309 315 private ModificationItem [] getModificationItem() 316 { 317 ModificationItem [] mods = new ModificationItem [2]; 318 Attribute mod0 = new BasicAttribute ("userpassword", 320 "secret"); 321 Attribute mod1 = new BasicAttribute ("mobile", 323 "123-456-1234"); 324 325 mods[0] = new ModificationItem (DirContext.REPLACE_ATTRIBUTE, mod0); 326 mods[1] = new ModificationItem (DirContext.ADD_ATTRIBUTE, mod1); 327 328 return mods; 329 } 330 331 337 private BasicAttributes getBasicAttributes() 338 { 339 BasicAttributes basicattributes = new BasicAttributes (); 340 BasicAttribute basicattribute = new BasicAttribute ("objectclass"); 341 basicattribute.add("top"); 342 basicattribute.add("person"); 343 basicattribute.add("organizationalPerson"); 344 basicattribute.add("inetOrgPerson"); 345 basicattributes.put(basicattribute); 346 String s1 = "User"; 347 String s3 = "Test"; 348 String s5 = "user"; 349 String s6 = "test"; 350 counter+=1; 351 basicattributes.put(new BasicAttribute ("givenname", s1)); 352 basicattributes.put(new BasicAttribute ("sn", s3)); 353 basicattributes.put(new BasicAttribute ("cn","TestUser"+counter)); 354 basicattributes.put(new BasicAttribute ("uid", s5)); 355 basicattributes.put(new BasicAttribute ("userpassword", s6)); 356 setProperty(new StringProperty(ADD,"cn=TestUser"+counter)); 357 return basicattributes; 358 } 359 360 365 private BasicAttribute getBasicAttribute(String name, String value) 366 { 367 BasicAttribute attr = new BasicAttribute (name,value); 368 return attr; 369 } 370 371 376 public String getLabel() 377 { 378 return ( 379 "ldap://" 380 + this.getServername() 381 + ":" 382 + getPort() 383 + "/" 384 + this.getRootdn()); 385 } 386 387 388 394 private void addTest(LdapClient ldap, SampleResult res) 395 throws NamingException 396 { 397 if (getPropertyAsBoolean(USER_DEFINED)) 398 { 399 res.sampleStart(); 400 ldap.createTest( 401 getUserAttributes(), 402 getPropertyAsString(BASE_ENTRY_DN)); 403 res.sampleEnd(); 404 } 405 else 406 { 407 res.sampleStart(); 408 ldap.createTest(getBasicAttributes(), getPropertyAsString(ADD)); 409 res.sampleEnd(); 410 ldap.deleteTest(getPropertyAsString(ADD)); 411 } 412 } 413 414 415 421 private void deleteTest(LdapClient ldap, SampleResult res) 422 throws NamingException 423 { 424 if (!getPropertyAsBoolean(USER_DEFINED)) 425 { 426 ldap.createTest(getBasicAttributes(), getPropertyAsString(ADD)); 427 setProperty(new StringProperty(DELETE, getPropertyAsString(ADD))); 428 } 429 res.sampleStart(); 430 ldap.deleteTest(getPropertyAsString(DELETE)); 431 res.sampleEnd(); 432 } 433 434 440 private void searchTest(LdapClient ldap, SampleResult res) 441 throws NamingException 442 { 443 if (!getPropertyAsBoolean(USER_DEFINED)) 444 { 445 ldap.createTest(getBasicAttributes(), getPropertyAsString(ADD)); 446 setProperty( 447 new StringProperty(SEARCHBASE, getPropertyAsString(ADD))); 448 setProperty( 449 new StringProperty(SEARCHFILTER, getPropertyAsString(ADD))); 450 } 451 res.sampleStart(); 452 searchFoundEntries = ldap.searchTest( 453 getPropertyAsString(SEARCHBASE), 454 getPropertyAsString(SEARCHFILTER)); 455 res.sampleEnd(); 456 if (!getPropertyAsBoolean(USER_DEFINED)) 457 { 458 ldap.deleteTest(getPropertyAsString(ADD)); 459 } 460 } 461 462 468 private void modifyTest(LdapClient ldap, SampleResult res) 469 throws NamingException 470 { 471 if (getPropertyAsBoolean(USER_DEFINED)) 472 { 473 res.sampleStart(); 474 ldap.modifyTest( 475 getUserModAttributes(), 476 getPropertyAsString(BASE_ENTRY_DN)); 477 res.sampleEnd(); 478 } 479 else 480 { 481 ldap.createTest(getBasicAttributes(), getPropertyAsString(ADD)); 482 setProperty(new StringProperty(MODIFY, getPropertyAsString(ADD))); 483 res.sampleStart(); 484 ldap.modifyTest(getModificationItem(), getPropertyAsString(MODIFY)); 485 res.sampleEnd(); 486 ldap.deleteTest(getPropertyAsString(ADD)); 487 } 488 } 489 490 public SampleResult sample(Entry e) 491 { 492 SampleResult res = new SampleResult(); 493 boolean isSuccessful = false; 494 res.setSampleLabel(getLabel()); 495 res.setSamplerData(getPropertyAsString(TEST)); LdapClient ldap = new LdapClient(); 497 498 try 499 { 500 ldap.connect( 501 getServername(), 502 getPort(), 503 getRootdn(), 504 getUsername(), 505 getPassword()); 506 507 if (getPropertyAsString(TEST).equals("add")) 508 { 509 addTest(ldap,res); 510 } 511 else if (getPropertyAsString(TEST).equals("delete")) 512 { 513 deleteTest(ldap,res); 514 } 515 else if (getPropertyAsString(TEST).equals("modify")) 516 { 517 modifyTest(ldap,res); 518 } 519 else if (getPropertyAsString(TEST).equals("search")) 520 { 521 searchTest(ldap,res); 522 } 523 524 if (getPropertyAsString(TEST).equals("search") 526 && !searchFoundEntries ) 527 { 528 res.setResponseCode("201"); res.setResponseMessage("OK - no results"); 530 res.setResponseData("successful - no results".getBytes()); 531 } else { 532 res.setResponseCode("200"); 533 res.setResponseMessage("OK"); 534 res.setResponseData("successful".getBytes()); 535 } 536 res.setDataType(SampleResult.TEXT); 537 isSuccessful = true; 538 ldap.disconnect(); 539 } 540 catch (Exception ex) 541 { 542 log.error("Ldap client - ",ex); 543 res.setResponseCode("500"); res.setResponseMessage(ex.toString()); 548 ldap.disconnect(); 549 isSuccessful = false; 550 } 551 552 res.setSuccessful(isSuccessful); 554 return res; 555 } 556 } | Popular Tags |