1 16 17 package org.apache.naming.handler.jndi; 18 19 import java.io.ByteArrayInputStream; 20 import java.io.FileNotFoundException; 21 import java.io.IOException; 22 import java.io.InputStream; 23 import java.net.URL; 24 import java.net.URLConnection; 25 import java.security.Permission; 26 import java.util.Enumeration; 27 import java.util.Vector; 28 29 import javax.naming.NameClassPair; 30 import javax.naming.NamingEnumeration; 31 import javax.naming.NamingException; 32 import javax.naming.directory.Attribute; 33 import javax.naming.directory.Attributes; 34 import javax.naming.directory.DirContext; 35 36 import org.apache.naming.core.JndiPermission; 37 import org.apache.naming.util.AttributeHelper; 38 41 52 public class DirContextURLConnection 53 extends URLConnection 54 { 55 56 57 59 68 public DirContextURLConnection(DirContext context, URL url) { 69 super(url); 70 if (context == null) 71 throw new IllegalArgumentException 72 ("Directory context can't be null"); 73 if (System.getSecurityManager() != null) { 74 this.permission = new JndiPermission(url.toString()); 75 } 76 this.context = context; 77 } 78 79 80 82 83 86 protected DirContext context; 87 88 89 92 protected Object resource; 93 94 95 98 protected DirContext collection; 99 100 101 104 protected Object object; 105 106 107 110 protected Attributes attributes; 111 112 113 116 protected long date; 117 118 119 122 protected Permission permission; 123 124 125 127 128 135 public void connect() 136 throws IOException { 137 138 if (!connected) { 139 140 try { 141 date = System.currentTimeMillis(); 143 String path = getURL().getFile(); 144 145 174 object = context.lookup(path); 175 attributes = context.getAttributes(path); 176 if (object instanceof DirContext) 179 collection = (DirContext) object; 180 else 181 resource=object; 182 } catch (NamingException e) { 183 } 185 186 connected = true; 187 188 } 189 190 } 191 192 193 196 public int getContentLength() { 197 if (!connected) { 198 try { 200 connect(); 201 } catch (IOException e) { 202 } 203 } 204 205 if (attributes == null) 206 return (-1); 207 208 return (int)AttributeHelper.getContentLength( attributes ); 209 } 210 211 214 public String getContentType() { 215 if (!connected) { 216 try { 218 connect(); 219 } catch (IOException e) { 220 } 221 } 222 223 if (attributes == null) 224 return null; 225 226 return AttributeHelper.getContentType(attributes); 227 } 228 229 230 234 public long getDate() { 235 return date; 236 } 237 238 239 242 public long getLastModified() { 243 244 if (!connected) { 245 try { 247 connect(); 248 } catch (IOException e) { 249 } 250 } 251 252 if (attributes == null) 253 return 0; 254 255 return AttributeHelper.getLastModified( attributes ); 256 } 257 258 259 262 public String getHeaderField(String name) { 263 264 if (!connected) { 265 try { 267 connect(); 268 } catch (IOException e) { 269 } 270 } 271 272 if (attributes == null) 273 return (null); 274 275 Attribute attribute = attributes.get(name); 276 try { 277 return attribute.get().toString(); 278 } catch (Exception e) { 279 } 281 282 return (null); 283 284 } 285 286 287 290 public Object getContent() 291 throws IOException { 292 293 if (!connected) 294 connect(); 295 296 if (resource != null) 297 return getInputStream(); 298 if (collection != null) 299 return collection; 300 if (object != null) 301 return object; 302 303 throw new FileNotFoundException(); 304 305 } 306 307 308 311 public Object getContent(Class[] classes) 312 throws IOException { 313 314 Object object = getContent(); 315 316 for (int i = 0; i < classes.length; i++) { 317 if (classes[i].isInstance(object)) 318 return object; 319 } 320 321 return null; 322 323 } 324 325 326 329 public InputStream getInputStream() 330 throws IOException { 331 332 if (!connected) 333 connect(); 334 335 if (resource == null) { 336 throw new FileNotFoundException(); 337 } else { 338 try { 340 resource = context.lookup(getURL().getFile()); 341 } catch (NamingException e) { 342 } 343 } 344 345 return getInputStream(resource); 347 348 } 349 350 353 public static InputStream getInputStream( Object resource ) { 354 if( resource instanceof InputStream ) 355 return (InputStream) resource; 356 357 return new ByteArrayInputStream(resource.toString().getBytes()); 359 } 360 361 364 public Permission getPermission() { 365 366 return permission; 367 } 368 369 370 372 373 377 public Enumeration list() 378 throws IOException { 379 380 if (!connected) { 381 connect(); 382 } 383 384 if ((resource == null) && (collection == null)) { 385 throw new FileNotFoundException(); 386 } 387 388 Vector result = new Vector(); 389 390 if (collection != null) { 391 try { 392 NamingEnumeration enum = context.list(getURL().getFile()); 393 while (enum.hasMoreElements()) { 394 NameClassPair ncp = (NameClassPair) enum.nextElement(); 395 result.addElement(ncp.getName()); 396 } 397 } catch (NamingException e) { 398 throw new FileNotFoundException(); 400 } 401 } 402 403 return result.elements(); 404 405 } 406 407 408 } 409 | Popular Tags |