Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */ 
 
 package org.apache.naming.resources;
 
 import java.net.URL;
 import java.util.Date;
 import java.util.Map;
 
 
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(s):
Remy Maucherat
Version:
$Revision: 1416 $
 
 public class DirContextURLConnection 
     extends URLConnection {
     
     
     // ----------------------------------------------------------- Constructors
     
     
     public DirContextURLConnection(DirContext contextURL url) {
         super(url);
         if (context == null)
             throw new IllegalArgumentException
                 ("Directory context can't be null");
         if (....) {
             this. = new JndiPermission(url.toString());
     }
         this. = context;
     }
     
     
     // ----------------------------------------------------- Instance Variables
     
    
    
Directory context.
 
     protected DirContext context;
    
    
    
Associated resource.
 
     protected Resource resource;
    
    
    
Associated DirContext.
 
     protected DirContext collection;
    
    
    
Other unknown object.
 
     protected Object object;
    
    
    
Attributes.
    protected Attributes attributes;
    
    
    
Date.
    protected long date;
    
    
    
Permission
    protected Permission permission;

    
    
Local path.
    protected String localPath;
    
    // ------------------------------------------------------------- Properties
    
    
    
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:
java.io.IOException Object not found
    public void connect()
        throws IOException {
        
        if (!) {
            
            try {
                 = System.currentTimeMillis();
                String path = getURL().getFile();
                if ( instanceof ProxyDirContext) {
                    ProxyDirContext proxyDirContext = 
                        (ProxyDirContext;
                    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());
                        }
                    }
                }
                 = path;
                 = .lookup(path);
                 = .getAttributes(path);
                if ( instanceof Resource)
                     = (Resource;
                if ( instanceof DirContext)
                     = (DirContext;
            } catch (NamingException e) {
                // Object not found
            }
            
             = true;
            
        }
        
    }
    
    
    
Return the content length value.
    public int getContentLength() {
    }
    
    
    
Return the content type value.
    public String getContentType() {
    }
    
    
    
Return the last modified date.
    public long getDate() {
        return ;
    }
    
    
    
Return the last modified date.
    public long getLastModified() {
        if (!) {
            // Try to connect (silently)
            try {
                connect();
            } catch (IOException e) {
            }
        }
        if ( == null)
            return 0;
        Attribute lastModified = 
        if (lastModified != null) {
            try {
                Date lmDate = (DatelastModified.get();
                return lmDate.getTime();
            } catch (Exception e) {
            }
        }
        return 0;
    }
    
    protected String getHeaderValueAsString(Object headerValue) {
        if (headerValue == nullreturn null;
        if (headerValue instanceof Date) {
            // return date strings (ie Last-Modified) in HTTP format, rather
            // than Java format
            return FastHttpDateFormat.formatDate(((Date)headerValue).getTime(), null);
        }
        return headerValue.toString();
    }


    
Returns an unmodifiable Map of the header fields.
    public Map getHeaderFields() {
      if (!) {
          // Try to connect (silently)
          try {
              connect();
          } catch (IOException e) {
          }
      }
      if ( == null)
          return (.);
      HashMap headerFields = new HashMap(.size());
      NamingEnumeration attributeEnum = .getIDs();
      try {
          while (attributeEnum.hasMore()) {
              String attributeID = (String)attributeEnum.next();
              Attribute attribute = .get(attributeID);
              if (attribute == nullcontinue;
              ArrayList attributeValueList = new ArrayList(attribute.size());
              NamingEnumeration attributeValues = attribute.getAll();
              while (attributeValues.hasMore()) {
                  Object attrValue = attributeValues.next();
                  attributeValueList.add(getHeaderValueAsString(attrValue));
              }
              attributeValueList.trimToSize(); // should be a no-op if attribute.size() didn't lie
              headerFields.put(attributeID, Collections.unmodifiableList(attributeValueList));
          }
      } catch (NamingException ne) {
            // Shouldn't happen
      }
      return Collections.unmodifiableMap(headerFields);
    }
    
    
    
Returns the name of the specified header field.
    public String getHeaderField(String name) {
        if (!) {
            // Try to connect (silently)
            try {
                connect();
            } catch (IOException e) {
            }
        }
        
        if ( == null)
            return (null);
        NamingEnumeration attributeEnum = .getIDs();
        try {
            while (attributeEnum.hasMore()) {
                String attributeID = (String)attributeEnum.next();
                if (attributeID.equalsIgnoreCase(name)) {
                    Attribute attribute = .get(attributeID);
                    if (attribute == nullreturn null;
                    return getHeaderValueAsString(attribute.get(attribute.size()-1));
                }
            }
        } catch (NamingException ne) {
            // Shouldn't happen
        }
        return (null);
        
    }
    
    
    
Get object content.
    public Object getContent()
        throws IOException {
        
        if (!)
            connect();
        
        if ( != null)
            return getInputStream();
        if ( != null)
            return ;
        if ( != null)
            return ;
        
        throw new FileNotFoundException();
        
    }
    
    
    
Get object content.
    public Object getContent(Class[] classes)
        throws IOException {
        
        Object object = getContent();
        
        for (int i = 0; i < classes.lengthi++) {
            if (classes[i].isInstance(object))
                return object;
        }
        
        return null;
        
    }
    
    
    
Get input stream.
    public InputStream getInputStream() 
        throws IOException {
        
        if (!)
            connect();
        
        if ( == null) {
            throw new FileNotFoundException();
        } else {
            // Reopen resource
            try {
                 = (Resource.lookup();
            } catch (NamingException e) {
            }
        }
        
        return (.streamContent());
        
    }
    
    
    
Get the Permission for this URL
    public Permission getPermission() {
        return ;
    }
    // --------------------------------------------------------- Public Methods
    
    
    
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.
    public Enumeration list()
        throws IOException {
        
        if (!) {
            connect();
        }
        
        if (( == null) && ( == null)) {
            throw new FileNotFoundException();
        }
        
        Vector result = new Vector();
        
        if ( != null) {
            try {
                NamingEnumeration enumeration = .list();
                while (enumeration.hasMoreElements()) {
                    NameClassPair ncp = (NameClassPairenumeration.nextElement();
                    result.addElement(ncp.getName());
                }
            } catch (NamingException e) {
                // Unexpected exception
                throw new FileNotFoundException();
            }
        }
        
        return result.elements();
        
    }
    
    
New to GrepCode? Check out our FAQ X