FileDocCategorySizeDatePackage
DirContextURLConnection.javaAPI DocGlassfish v2 API10748Fri May 04 22:33:00 BST 2007org.apache.naming.resources

DirContextURLConnection

public class DirContextURLConnection extends URLConnection
Connection to a JNDI directory context.

Note: All the object attribute names are the WebDAV names, not the HTTP names, so this class overrides some methods from URLConnection to do the queries using the right names. Content handler is also not used; the content is directly returned.

author
Remy Maucherat
version
$Revision: 1.4 $

Fields Summary
protected DirContext
context
Directory context.
protected org.apache.naming.resources.Resource
resource
Associated resource.
protected DirContext
collection
Associated DirContext.
protected Object
object
Other unknown object.
protected Attributes
attributes
Attributes.
protected long
date
Date.
protected Permission
permission
Permission
public static final boolean
IS_SECURITY_ENABLED
Is the Java SecurityManager enabled?
Constructors Summary
public DirContextURLConnection(DirContext context, URL url)

        super(url);
        if (context == null)
            throw new IllegalArgumentException
                ("Directory context can't be null");
        if (IS_SECURITY_ENABLED) {
            this.permission = new JndiPermission(url.toString());
	}
        this.context = context;
    
Methods Summary
public voidconnect()
Connect to the DirContext, and retrive the bound object, as well as its attributes. If no object is bound with the name specified in the URL, then an IOException is thrown.

throws
IOException Object not found



    // ------------------------------------------------------------- Properties
    
    
                                              
      
          
        
        if (!connected) {
            
            try {
                date = System.currentTimeMillis();
                String path = getURL().getFile();
                if (context instanceof ProxyDirContext) {
                    ProxyDirContext proxyDirContext = 
                        (ProxyDirContext) context;
                    String hostName = proxyDirContext.getHostName();
                    String contextName = proxyDirContext.getContextName();
                    if (hostName != null) {
                        if (!path.startsWith("/" + hostName + "/"))
                            return;
                        path = path.substring(hostName.length()+ 1);
                    }
                    if (contextName != null) {
                        if (!path.startsWith(contextName + "/")) {
                            return;
                        } else {
                            path = path.substring(contextName.length());
                        }
                    }
                }
                object = context.lookup(path);
                attributes = context.getAttributes(path);
                if (object instanceof Resource)
                    resource = (Resource) object;
                if (object instanceof DirContext)
                    collection = (DirContext) object;
            } catch (NamingException e) {
                // Object not found
            }
            
            connected = true;
            
        }
        
    
public java.lang.ObjectgetContent()
Get object content.

        
        if (!connected)
            connect();
        
        if (resource != null)
            return getInputStream();
        if (collection != null)
            return collection;
        if (object != null)
            return object;
        
        throw new FileNotFoundException();
        
    
public java.lang.ObjectgetContent(java.lang.Class[] classes)
Get object content.

        
        Object object = getContent();
        
        for (int i = 0; i < classes.length; i++) {
            if (classes[i].isInstance(object))
                return object;
        }
        
        return null;
        
    
public intgetContentLength()
Return the content length value.

        return getHeaderFieldInt(ResourceAttributes.CONTENT_LENGTH, -1);
    
public java.lang.StringgetContentType()
Return the content type value.

        return getHeaderField(ResourceAttributes.CONTENT_TYPE);
    
public longgetDate()
Return the last modified date.

        return date;
    
public java.lang.StringgetHeaderField(java.lang.String name)
Returns the name of the specified header field.


        if (!connected) {
            // Try to connect (silently)
            try {
                connect();
            } catch (IOException e) {
            }
        }
        
        if (attributes == null)
            return (null);

        Attribute attribute = attributes.get(name);
        try {
            return attribute.get().toString();
        } catch (Exception e) {
            // Shouldn't happen, unless the attribute has no value
        }

        return (null);
        
    
public java.io.InputStreamgetInputStream()
Get input stream.

        
        if (!connected)
            connect();
        
        if (resource == null) {
            throw new FileNotFoundException();
        } else {
            // Reopen resource
            try {
                resource = (Resource) context.lookup(getURL().getFile());
            } catch (NamingException e) {
            }
        }
        
        return (resource.streamContent());
        
    
public longgetLastModified()
Return the last modified date.


        if (!connected) {
            // Try to connect (silently)
            try {
                connect();
            } catch (IOException e) {
            }
        }

        if (attributes == null)
            return 0;

        Attribute lastModified = 
            attributes.get(ResourceAttributes.LAST_MODIFIED);
        if (lastModified != null) {
            try {
                Date lmDate = (Date) lastModified.get();
                return lmDate.getTime();
            } catch (Exception e) {
            }
        }

        return 0;
    
public java.security.PermissiongetPermission()
Get the Permission for this URL


        return permission;
    
public java.util.Enumerationlist()
List children of this collection. The names given are relative to this URI's path. The full uri of the children is then : path + "/" + name.

        
        if (!connected) {
            connect();
        }
        
        if ((resource == null) && (collection == null)) {
            throw new FileNotFoundException();
        }
        
        Vector result = new Vector();
        
        if (collection != null) {
            try {
                NamingEnumeration enumeration = context.list(getURL().getFile());
                while (enumeration.hasMoreElements()) {
                    NameClassPair ncp = (NameClassPair) enumeration.nextElement();
                    result.addElement(ncp.getName());
                }
            } catch (NamingException e) {
                // Unexpected exception
                throw new FileNotFoundException();
            }
        }
        
        return result.elements();