remm 01/01/13 13:19:13 Modified: catalina/src/share/org/apache/naming/resources ProxyDirContext.java Resource.java ResourceAttributes.java Log: - ProxyDirContext is now way smarter about the types which are returned by the actual DirContext. It will wrap an InputStream inside a Resource object, as well as wrap generic attributes around ResourceAttributes. - Caching is still not implemented. Revision Changes Path 1.2 +37 -16 jakarta-tomcat-4.1/catalina/src/share/org/apache/naming/resources/ProxyDirContext.java Index: ProxyDirContext.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-4.1/catalina/src/share/org/apache/naming/resources/ProxyDirContext.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ProxyDirContext.java 2001/01/10 04:46:49 1.1 +++ ProxyDirContext.java 2001/01/13 21:19:12 1.2 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-tomcat-4.1/catalina/src/share/org/apache/naming/resources/ProxyDirContext.java,v 1.1 2001/01/10 04:46:49 remm Exp $ - * $Revision: 1.1 $ - * $Date: 2001/01/10 04:46:49 $ + * $Header: /home/cvs/jakarta-tomcat-4.1/catalina/src/share/org/apache/naming/resources/ProxyDirContext.java,v 1.2 2001/01/13 21:19:12 remm Exp $ + * $Revision: 1.2 $ + * $Date: 2001/01/13 21:19:12 $ * * ==================================================================== * @@ -65,6 +65,7 @@ package org.apache.naming.resources; import java.util.Hashtable; +import java.io.InputStream; import javax.naming.Context; import javax.naming.Name; import javax.naming.NameParser; @@ -81,7 +82,7 @@ * Proxy Directory Context implementation. * * @author Remy Maucherat - * @version $Revision: 1.1 $ $Date: 2001/01/10 04:46:49 $ + * @version $Revision: 1.2 $ $Date: 2001/01/13 21:19:12 $ */ public class ProxyDirContext implements DirContext { @@ -145,10 +146,11 @@ */ public Object lookup(Name name) throws NamingException { - // Strip the URL header - // Find the appropriate NamingContext according to the current bindings - // Execute the lookup on that context - return dirContext.lookup(parseName(name)); + Object object = dirContext.lookup(parseName(name)); + if (object instanceof InputStream) + return new Resource((InputStream) object); + else + return object; } @@ -161,10 +163,11 @@ */ public Object lookup(String name) throws NamingException { - // Strip the URL header - // Find the appropriate NamingContext according to the current bindings - // Execute the lookup on that context - return dirContext.lookup(parseName(name)); + Object object = dirContext.lookup(parseName(name)); + if (object instanceof InputStream) + return new Resource((InputStream) object); + else + return object; } @@ -650,7 +653,11 @@ */ public Attributes getAttributes(Name name) throws NamingException { - return dirContext.getAttributes(parseName(name)); + Attributes attributes = dirContext.getAttributes(parseName(name)); + if (!(attributes instanceof ResourceAttributes)) { + attributes = new ResourceAttributes(attributes); + } + return attributes; } @@ -663,7 +670,11 @@ */ public Attributes getAttributes(String name) throws NamingException { - return dirContext.getAttributes(parseName(name)); + Attributes attributes = dirContext.getAttributes(parseName(name)); + if (!(attributes instanceof ResourceAttributes)) { + attributes = new ResourceAttributes(attributes); + } + return attributes; } @@ -681,7 +692,12 @@ */ public Attributes getAttributes(Name name, String[] attrIds) throws NamingException { - return dirContext.getAttributes(parseName(name), attrIds); + Attributes attributes = + dirContext.getAttributes(parseName(name), attrIds); + if (!(attributes instanceof ResourceAttributes)) { + attributes = new ResourceAttributes(attributes); + } + return attributes; } @@ -697,7 +713,12 @@ */ public Attributes getAttributes(String name, String[] attrIds) throws NamingException { - return dirContext.getAttributes(parseName(name), attrIds); + Attributes attributes = + dirContext.getAttributes(parseName(name), attrIds); + if (!(attributes instanceof ResourceAttributes)) { + attributes = new ResourceAttributes(attributes); + } + return attributes; } 1.2 +10 -5 jakarta-tomcat-4.1/catalina/src/share/org/apache/naming/resources/Resource.java Index: Resource.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-4.1/catalina/src/share/org/apache/naming/resources/Resource.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- Resource.java 2001/01/10 04:46:49 1.1 +++ Resource.java 2001/01/13 21:19:12 1.2 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-tomcat-4.1/catalina/src/share/org/apache/naming/resources/Resource.java,v 1.1 2001/01/10 04:46:49 remm Exp $ - * $Revision: 1.1 $ - * $Date: 2001/01/10 04:46:49 $ + * $Header: /home/cvs/jakarta-tomcat-4.1/catalina/src/share/org/apache/naming/resources/Resource.java,v 1.2 2001/01/13 21:19:12 remm Exp $ + * $Revision: 1.2 $ + * $Date: 2001/01/13 21:19:12 $ * * ==================================================================== * @@ -71,7 +71,7 @@ * Encapsultes the contents of a resource. * * @author <a href="mailto:[EMAIL PROTECTED]">Remy Maucherat</a> - * @version $Revision: 1.1 $ + * @version $Revision: 1.2 $ */ public class Resource { @@ -79,6 +79,10 @@ // ----------------------------------------------------------- Constructors + public Resource() { + } + + public Resource(InputStream inputStream) { setContent(inputStream); } @@ -125,7 +129,8 @@ * * @return binary content */ - public byte[] getContent() { + public byte[] getContent() + throws IOException { return binaryContent; } 1.2 +100 -31 jakarta-tomcat-4.1/catalina/src/share/org/apache/naming/resources/ResourceAttributes.java Index: ResourceAttributes.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-4.1/catalina/src/share/org/apache/naming/resources/ResourceAttributes.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ResourceAttributes.java 2001/01/10 04:46:49 1.1 +++ ResourceAttributes.java 2001/01/13 21:19:12 1.2 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-tomcat-4.1/catalina/src/share/org/apache/naming/resources/ResourceAttributes.java,v 1.1 2001/01/10 04:46:49 remm Exp $ - * $Revision: 1.1 $ - * $Date: 2001/01/10 04:46:49 $ + * $Header: /home/cvs/jakarta-tomcat-4.1/catalina/src/share/org/apache/naming/resources/ResourceAttributes.java,v 1.2 2001/01/13 21:19:12 remm Exp $ + * $Revision: 1.2 $ + * $Date: 2001/01/13 21:19:12 $ * * ==================================================================== * @@ -83,7 +83,7 @@ * Attributes implementation. * * @author <a href="mailto:[EMAIL PROTECTED]">Remy Maucherat</a> - * @version $Revision: 1.1 $ + * @version $Revision: 1.2 $ */ public final class ResourceAttributes extends BasicAttributes { @@ -100,12 +100,24 @@ /** + * Creation date. + */ + public static final String ALTERNATE_CREATION_DATE = "last-modified"; + + + /** * Last modification date. */ public static final String LAST_MODIFIED = "getlastmodified"; /** + * Last modification date. + */ + public static final String ALTERNATE_LAST_MODIFIED = "last-modified"; + + + /** * Name. */ public static final String NAME = "displayname"; @@ -118,6 +130,12 @@ /** + * Type. + */ + public static final String ALTERNATE_TYPE = "content-type"; + + + /** * Source. */ public static final String SOURCE = "source"; @@ -142,6 +160,12 @@ /** + * Content length. + */ + public static final String ALTERNATE_CONTENT_LENGTH = "content-length"; + + + /** * ETag. */ public static final String ETAG = "getetag"; @@ -182,6 +206,33 @@ } + /** + * Merges with another attribute set. + */ + public ResourceAttributes(Attributes attributes) { + super(false); + // Merging attributes + try { + Enumeration enum = attributes.getAll(); + while (enum.hasMoreElements()) { + Attribute attribute = (Attribute) enum.nextElement(); + // FIXME: Check if it's a protected attribute ? + put(attribute); + } + } catch (Throwable t) { + } + } + + + /** + * Package private constructor (used for caching). + */ + ResourceAttributes(Hashtable attributes) { + super(false); + this.protectedAttributes = attributes; + } + + // ----------------------------------------------------- Instance Variables @@ -205,6 +256,8 @@ public Date getCreationDate() { Attribute creationDate = getProtectedAttribute(CREATION_DATE); if (creationDate == null) + creationDate = getProtectedAttribute(ALTERNATE_CREATION_DATE); + if (creationDate == null) return null; try { if (creationDate.get() instanceof Date) { @@ -238,6 +291,8 @@ public Date getLastModified() { Attribute lastModified = getProtectedAttribute(LAST_MODIFIED); if (lastModified == null) + lastModified = getProtectedAttribute(ALTERNATE_LAST_MODIFIED); + if (lastModified == null) return null; try { if (lastModified.get() instanceof Date) { @@ -270,21 +325,21 @@ */ public long getContentLength() { Attribute contentLength = getProtectedAttribute(CONTENT_LENGTH); - if (contentLength == null) { + if (contentLength == null) + contentLength = getProtectedAttribute(ALTERNATE_CONTENT_LENGTH); + if (contentLength == null) return -1L; - } else { - try { - if (contentLength.get() instanceof Long) { - return ((Long) contentLength.get()).longValue(); - } - if (contentLength.get() instanceof String) { - return (new Long - ((String) contentLength.get())).longValue(); - } - } catch (NamingException e) { + try { + if (contentLength.get() instanceof Long) { + return ((Long) contentLength.get()).longValue(); } - return -1L; + if (contentLength.get() instanceof String) { + return (new Long((String) contentLength.get())).longValue(); + } + } catch (NamingException e) { } + return -1L; + } @@ -292,14 +347,7 @@ * Is collection. */ public boolean isCollection() { - Attribute resourceType = getProtectedAttribute(TYPE); - if (resourceType == null) - return true; - try { - return (resourceType.get().equals(COLLECTION_TYPE)); - } catch (NamingException e) { - return false; - } + return (getResourceType().equals(COLLECTION_TYPE)); } @@ -329,12 +377,12 @@ */ public String getResourceType() { Attribute resourceType = getProtectedAttribute(TYPE); + if (resourceType == null) + resourceType = getProtectedAttribute(ALTERNATE_TYPE); + if (resourceType == null) + return ""; try { - if (resourceType == null) { - return ""; - } else { - return (String) resourceType.get(); - } + return (String) resourceType.get(); } catch (NamingException e) { return ""; } @@ -469,6 +517,23 @@ } + /** + * Get all the attributes in a Hashtable. Will mutate the + * protectedAttributes hashtable. + */ + Hashtable getAttributes() { + Enumeration enum = super.getAll(); + while (enum.hasMoreElements()) { + Attribute attribute = (Attribute) enum.nextElement(); + String id = attribute.getID(); + if (!protectedAttributes.containsKey(id)) { + protectedAttributes.put(id, attribute); + } + } + return protectedAttributes; + } + + // ------------------------------------------------------ Protected Methods @@ -490,12 +555,16 @@ * @param name Attribute name */ protected Attribute getProtectedAttribute(String name) { + Attribute result = null; if (name.startsWith(Constants.DEFAULT_NAMESPACE)) { - return (Attribute) protectedAttributes.get + result = (Attribute) protectedAttributes.get (name.substring(Constants.DEFAULT_NAMESPACE.length())); } else { - return (Attribute) protectedAttributes.get(name); + result = (Attribute) protectedAttributes.get(name); } + if (result != null) + return result; + return super.get(name); } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, email: [EMAIL PROTECTED]