1 16 17 package org.apache.taglibs.jndi; 18 19 import javax.servlet.jsp.*; 20 import javax.servlet.jsp.tagext.*; 21 import javax.naming.*; 22 import javax.naming.directory.*; 23 24 import java.io.*; 25 26 31 public class SearchTag extends BodyTagSupport { 32 33 private int scope = PageContext.PAGE_SCOPE; 34 private String attributes; 35 private String attributeSeparator = ","; 36 private SearchControls controls; 37 private DirContext context; 38 private String contextRef; 39 private String filter; 40 private NamingEnumeration nameEnum; 41 private SearchResult currentResult; 42 private String name = ""; 43 private Name nameObject; 44 45 46 public SearchTag() { 47 } 48 49 public void setPageContext(PageContext pc) { 50 controls = new SearchControls(); 51 super.setPageContext(pc); 52 } 53 54 58 public void setScope(String scope) { 59 this.scope = decodeScope(scope); 60 } 61 62 66 public DirContext getContext() { 67 return context; 68 } 69 70 74 public void setContext(DirContext context) { 75 this.context = context; 76 } 77 78 82 public void setContextRef(String contextRef) { 83 this.contextRef = contextRef; 84 } 85 86 90 public String getName() { 91 return name; 92 } 93 94 98 public void setName(String name) { 99 this.name = name; 100 } 101 102 106 public Name getNameObject() { 107 return nameObject; 108 } 109 110 114 public void setNameObject(Name nameObject) { 115 this.nameObject = nameObject; 116 } 117 118 122 public String getFilter() { 123 return filter; 124 } 125 126 130 public void setFilter(String filter) { 131 this.filter = filter; 132 } 133 134 138 public long getCountLimit() { 139 return controls.getCountLimit(); 140 } 141 142 146 public void setCountLimit(long countLimit) { 147 controls.setCountLimit(countLimit); 148 } 149 150 154 public boolean getDerefLink() { 155 return controls.getDerefLinkFlag(); 156 } 157 158 162 public void setDerefLink(boolean derefLink) { 163 controls.setDerefLinkFlag(derefLink); 164 } 165 166 170 public String getAttributes() { 171 return attributes; 172 } 173 174 178 public void setAttributes(String attributes) { 179 this.attributes = attributes; 180 } 181 182 186 public String getAttributeSeparator() { 187 return attributeSeparator; 188 } 189 190 194 public void setAttributeSeparator(String attributeSeparator) { 195 this.attributeSeparator = attributeSeparator; 196 } 197 198 202 public boolean getBindings() { 203 return controls.getReturningObjFlag(); 204 } 205 206 210 public void setBindings(boolean bindings) { 211 controls.setReturningObjFlag(bindings); 212 } 213 214 218 public String getSearchScope() { 219 return decodeSearchScope(controls.getSearchScope()); 220 } 221 222 226 public void setSearchScope(String searchScope) { 227 controls.setSearchScope(decodeSearchScope(searchScope)); 228 } 229 230 234 public int getTimeLimit() { 235 return controls.getTimeLimit(); 236 } 237 238 242 public void setTimeLimit(int timeLimit) { 243 controls.setTimeLimit(timeLimit); 244 } 245 246 public int doStartTag() throws JspException { 247 nameEnum = null; 249 250 if( contextRef != null ) { 251 context = null; 252 Object o = pageContext.findAttribute(contextRef); 253 if (o instanceof DirContext) { 254 context = (DirContext) o; 255 } else if (o instanceof Context) { 256 try { 257 o = ((Context)o).lookup(""); 259 if (o instanceof DirContext) { 260 context = (DirContext) o; 261 } 262 } catch (NamingException ne) { 263 } 265 } 266 } 267 if( context == null ) { 268 throw new JspException("JNDI search tag could not find a context"); 269 } 270 String [] attrs = null; 271 if (attributes != null) { 272 if (attributes.length() == 0) { 273 attrs = new String [0]; 274 } else { 275 int count = 1; 276 int i=0, j=0, size = attributeSeparator.length(); 277 while ((j=attributes.indexOf(attributeSeparator, i)) != -1) { 278 count++; 279 i = j + size; 280 } 281 attrs = new String [count]; 282 count = 0; 283 i = 0; 284 while ((j=attributes.indexOf(attributeSeparator, i)) != -1) { 285 attrs[count] = attributes.substring(i, j); 286 count++; 287 i = j + size; 288 } 289 attrs[count] = attributes.substring(i); 290 } 291 } 292 293 controls.setReturningAttributes(attrs); 294 295 try { 296 if (nameObject != null) { 297 nameEnum = context.search(nameObject, filter, controls); 298 } else { 299 nameEnum = context.search(name, filter, controls); 300 } 301 } catch (NamingException ne) { 302 throw new JspException("JNDI search tag failed: "+ne.getMessage()); 303 } 304 305 if (nameEnum.hasMoreElements()) { 306 return EVAL_BODY_TAG; 307 } else { 308 return SKIP_BODY; 309 } 310 } 311 312 public void doInitBody() { 313 currentResult = (SearchResult) nameEnum.nextElement(); 314 if (getId() != null) { 315 pageContext.setAttribute(getId(), 316 currentResult, scope); 317 } 318 } 319 320 public int doAfterBody() { 321 if (! nameEnum.hasMoreElements()) { 322 return SKIP_BODY; 323 } else { 324 currentResult = (SearchResult) nameEnum.nextElement(); 325 if (getId() != null) { 326 pageContext.setAttribute(getId(), 327 currentResult, scope); 328 } 329 return EVAL_BODY_TAG; 330 } 331 } 332 333 public int doEndTag() throws JspException { 334 try { 335 if (bodyContent != null) { 336 bodyContent.writeOut(pageContext.getOut()); 337 } 338 } catch (IOException ioe) { 339 throw new JspException(ioe.toString()); 340 } 341 try { 342 nameEnum.close(); 343 } catch (NamingException ne) { 344 throw new JspException(ne.toString()); 345 } 346 return EVAL_PAGE; 347 } 348 349 public static int decodeSearchScope(String scope) { 350 if ("object_scope".equalsIgnoreCase(scope) || 352 "object".equalsIgnoreCase(scope)) { 353 return SearchControls.OBJECT_SCOPE; 354 } else if ("onelevel_scope".equalsIgnoreCase(scope) || 355 "onelevel".equalsIgnoreCase(scope)) { 356 return SearchControls.ONELEVEL_SCOPE; 357 } else if ("subtree_scope".equalsIgnoreCase(scope) || 358 "subtree".equalsIgnoreCase(scope)) { 359 return SearchControls.SUBTREE_SCOPE; 360 } else { 361 return -1; 362 } 363 } 364 365 public static String decodeSearchScope(int scope) { 366 switch (scope) { 367 case SearchControls.OBJECT_SCOPE: 368 return "OBJECT_SCOPE"; 369 case SearchControls.ONELEVEL_SCOPE: 370 return "ONELEVEL_SCOPE"; 371 case SearchControls.SUBTREE_SCOPE: 372 return "SUBTREE_SCOPE"; 373 default: 374 return null; 375 } 376 } 377 378 public static int decodeScope(String scope) { 379 if (scope.equalsIgnoreCase("request")) { 380 return PageContext.REQUEST_SCOPE; 381 } else if (scope.equalsIgnoreCase("session")) { 382 return PageContext.SESSION_SCOPE; 383 } else if (scope.equalsIgnoreCase("application")) { 384 return PageContext.APPLICATION_SCOPE; 385 } else { 386 return PageContext.PAGE_SCOPE; 387 } 388 } 389 } 390 | Popular Tags |