Author: markt
Date: Sat Apr  5 11:27:32 2008
New Revision: 645157

URL: http://svn.apache.org/viewvc?rev=645157&view=rev
Log:
Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=44611
DirContextURLConnection has bugs:
1) Does not implement the getHeaderFields() method
2) It's implementation of getHeaderField(String name) is case sensitive.
3) It returns an empty string "", rather than null, for header values which
don't exist.
Patch provided by Chris Hubick.

Modified:
    tomcat/tc6.0.x/trunk/STATUS.txt
    
tomcat/tc6.0.x/trunk/java/org/apache/naming/resources/DirContextURLConnection.java
    
tomcat/tc6.0.x/trunk/java/org/apache/naming/resources/ResourceAttributes.java
    tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml

Modified: tomcat/tc6.0.x/trunk/STATUS.txt
URL: 
http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/STATUS.txt?rev=645157&r1=645156&r2=645157&view=diff
==============================================================================
--- tomcat/tc6.0.x/trunk/STATUS.txt (original)
+++ tomcat/tc6.0.x/trunk/STATUS.txt Sat Apr  5 11:27:32 2008
@@ -79,17 +79,6 @@
   +1: markt, fhanik
   -1:
 
-* Fix issues with DirContextURLConnection has bugs:
-  1) Does not implement the getHeaderFields() method
-  2) It's implementation of getHeaderField(String name) is case sensitive.
-  3) It returns an empty string "", rather than null, for header values which
-     don't exist.
-  https://issues.apache.org/bugzilla/show_bug.cgi?id=44611
-  Patch provided by Chris Hubick
-  http://svn.apache.org/viewvc?rev=640584&view=rev
-  +1: markt, fhanik, remm (I wonder where these methods are used)
-  -1:
-
 * Document missing secret attributes
   https://issues.apache.org/bugzilla/show_bug.cgi?id=44715
   http://svn.apache.org/viewvc?rev=643497&view=rev

Modified: 
tomcat/tc6.0.x/trunk/java/org/apache/naming/resources/DirContextURLConnection.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/java/org/apache/naming/resources/DirContextURLConnection.java?rev=645157&r1=645156&r2=645157&view=diff
==============================================================================
--- 
tomcat/tc6.0.x/trunk/java/org/apache/naming/resources/DirContextURLConnection.java
 (original)
+++ 
tomcat/tc6.0.x/trunk/java/org/apache/naming/resources/DirContextURLConnection.java
 Sat Apr  5 11:27:32 2008
@@ -23,8 +23,12 @@
 import java.io.InputStream;
 import java.io.FileNotFoundException;
 import java.security.Permission;
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Date;
 import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Vector;
 import javax.naming.NamingException;
 import javax.naming.NamingEnumeration;
@@ -217,6 +221,46 @@
         return 0;
     }
     
+
+    /**
+     * Returns an unmodifiable Map of the header fields.
+     */
+    public Map getHeaderFields() {
+
+      if (!connected) {
+          // Try to connect (silently)
+          try {
+              connect();
+          } catch (IOException e) {
+          }
+      }
+
+      if (attributes == null)
+          return (Collections.EMPTY_MAP);
+
+      HashMap headerFields = new HashMap(attributes.size());
+      NamingEnumeration attributeEnum = attributes.getIDs();
+      try {
+          while (attributeEnum.hasMore()) {
+              String attributeID = (String)attributeEnum.next();
+              Attribute attribute = attributes.get(attributeID);
+              if (attribute == null) continue;
+              ArrayList attributeValueList = new ArrayList(attribute.size());
+              NamingEnumeration attributeValues = attribute.getAll();
+              while (attributeValues.hasMore()) {
+                  attributeValueList.add(attributeValues.next().toString());
+              }
+              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.
@@ -234,11 +278,18 @@
         if (attributes == null)
             return (null);
 
-        Attribute attribute = attributes.get(name);
+        NamingEnumeration attributeEnum = attributes.getIDs();
         try {
-            return attribute.get().toString();
-        } catch (Exception e) {
-            // Shouldn't happen, unless the attribute has no value
+            while (attributeEnum.hasMore()) {
+                String attributeID = (String)attributeEnum.next();
+                if (attributeID.equalsIgnoreCase(name)) {
+                    Attribute attribute = attributes.get(attributeID);
+                    if (attribute == null) return null;
+                    return attribute.get(attribute.size()-1).toString();
+                }
+            }
+        } catch (NamingException ne) {
+            // Shouldn't happen
         }
 
         return (null);

Modified: 
tomcat/tc6.0.x/trunk/java/org/apache/naming/resources/ResourceAttributes.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/java/org/apache/naming/resources/ResourceAttributes.java?rev=645157&r1=645156&r2=645157&view=diff
==============================================================================
--- 
tomcat/tc6.0.x/trunk/java/org/apache/naming/resources/ResourceAttributes.java 
(original)
+++ 
tomcat/tc6.0.x/trunk/java/org/apache/naming/resources/ResourceAttributes.java 
Sat Apr  5 11:27:32 2008
@@ -263,7 +263,7 @@
      */
     public boolean isCollection() {
         if (attributes != null) {
-            return (getResourceType().equals(COLLECTION_TYPE));
+            return (COLLECTION_TYPE.equals(getResourceType()));
         } else {
             return (collection);
         }
@@ -683,7 +683,7 @@
             if (collection)
                 result = COLLECTION_TYPE;
             else
-                result = "";
+                result = null;
         }
         return result;
     }
@@ -775,28 +775,41 @@
     public Attribute get(String attrID) {
         if (attributes == null) {
             if (attrID.equals(CREATION_DATE)) {
-                return new BasicAttribute(CREATION_DATE, getCreationDate());
+                Date creationDate = getCreationDate();
+                if (creationDate == null) return null;
+                return new BasicAttribute(CREATION_DATE, creationDate);
             } else if (attrID.equals(ALTERNATE_CREATION_DATE)) {
-                return new BasicAttribute(ALTERNATE_CREATION_DATE, 
-                                          getCreationDate());
+                Date creationDate = getCreationDate();
+                if (creationDate == null) return null;
+                return new BasicAttribute(ALTERNATE_CREATION_DATE, 
creationDate);
             } else if (attrID.equals(LAST_MODIFIED)) {
-                return new BasicAttribute(LAST_MODIFIED, 
-                                          getLastModifiedDate());
+                Date lastModifiedDate = getLastModifiedDate();
+                if (lastModifiedDate == null) return null;
+                return new BasicAttribute(LAST_MODIFIED, lastModifiedDate);
             } else if (attrID.equals(ALTERNATE_LAST_MODIFIED)) {
-                return new BasicAttribute(ALTERNATE_LAST_MODIFIED,
-                                          getLastModifiedDate());
+                Date lastModifiedDate = getLastModifiedDate();
+                if (lastModifiedDate == null) return null;
+                return new BasicAttribute(ALTERNATE_LAST_MODIFIED, 
lastModifiedDate);
             } else if (attrID.equals(NAME)) {
-                return new BasicAttribute(NAME, getName());
+                String name = getName();
+                if (name == null) return null;
+                return new BasicAttribute(NAME, name);
             } else if (attrID.equals(TYPE)) {
-                return new BasicAttribute(TYPE, getResourceType());
+                String resourceType = getResourceType();
+                if (resourceType == null) return null;
+                return new BasicAttribute(TYPE, resourceType);
             } else if (attrID.equals(ALTERNATE_TYPE)) {
-                return new BasicAttribute(ALTERNATE_TYPE, getResourceType());
+                String resourceType = getResourceType();
+                if (resourceType == null) return null;
+                return new BasicAttribute(ALTERNATE_TYPE, resourceType);
             } else if (attrID.equals(CONTENT_LENGTH)) {
-                return new BasicAttribute(CONTENT_LENGTH, 
-                                          new Long(getContentLength()));
+                long contentLength = getContentLength();
+                if (contentLength < 0) return null;
+                return new BasicAttribute(CONTENT_LENGTH, new 
Long(contentLength));
             } else if (attrID.equals(ALTERNATE_CONTENT_LENGTH)) {
-                return new BasicAttribute(ALTERNATE_CONTENT_LENGTH, 
-                                          new Long(getContentLength()));
+              long contentLength = getContentLength();
+              if (contentLength < 0) return null;
+              return new BasicAttribute(ALTERNATE_CONTENT_LENGTH, new 
Long(contentLength));
             }
         } else {
             return attributes.get(attrID);
@@ -851,15 +864,35 @@
     public NamingEnumeration getAll() {
         if (attributes == null) {
             Vector attributes = new Vector();
-            attributes.addElement(new BasicAttribute
-                                  (CREATION_DATE, getCreationDate()));
-            attributes.addElement(new BasicAttribute
-                                  (LAST_MODIFIED, getLastModifiedDate()));
-            attributes.addElement(new BasicAttribute(NAME, getName()));
-            attributes.addElement(new BasicAttribute(TYPE, getResourceType()));
-            attributes.addElement
-                (new BasicAttribute(CONTENT_LENGTH, 
-                                    new Long(getContentLength())));
+            Date creationDate = getCreationDate();
+            if (creationDate != null) {
+                attributes.addElement(new BasicAttribute
+                                      (CREATION_DATE, creationDate));
+                attributes.addElement(new BasicAttribute
+                                      (ALTERNATE_CREATION_DATE, creationDate));
+            }
+            Date lastModifiedDate = getLastModifiedDate();
+            if (lastModifiedDate != null) {
+                attributes.addElement(new BasicAttribute
+                                      (LAST_MODIFIED, lastModifiedDate));
+                attributes.addElement(new BasicAttribute
+                                      (ALTERNATE_LAST_MODIFIED, 
lastModifiedDate));
+            }
+            String name = getName();
+            if (name != null) {
+                attributes.addElement(new BasicAttribute(NAME, name));
+            }
+            String resourceType = getResourceType();
+            if (resourceType != null) {
+                attributes.addElement(new BasicAttribute(TYPE, resourceType));
+                attributes.addElement(new BasicAttribute(ALTERNATE_TYPE, 
resourceType));
+            }
+            long contentLength = getContentLength();
+            if (contentLength >= 0) {
+                Long contentLengthLong = new Long(contentLength);
+                attributes.addElement(new BasicAttribute(CONTENT_LENGTH, 
contentLengthLong));
+                attributes.addElement(new 
BasicAttribute(ALTERNATE_CONTENT_LENGTH, contentLengthLong));
+            }
             return new RecyclableNamingEnumeration(attributes);
         } else {
             return attributes.getAll();
@@ -873,11 +906,29 @@
     public NamingEnumeration getIDs() {
         if (attributes == null) {
             Vector attributeIDs = new Vector();
-            attributeIDs.addElement(CREATION_DATE);
-            attributeIDs.addElement(LAST_MODIFIED);
-            attributeIDs.addElement(NAME);
-            attributeIDs.addElement(TYPE);
-            attributeIDs.addElement(CONTENT_LENGTH);
+            Date creationDate = getCreationDate();
+            if (creationDate != null) {
+                attributeIDs.addElement(CREATION_DATE);
+                attributeIDs.addElement(ALTERNATE_CREATION_DATE);
+            }
+            Date lastModifiedDate = getLastModifiedDate();
+            if (lastModifiedDate != null) {
+                attributeIDs.addElement(LAST_MODIFIED);
+                attributeIDs.addElement(ALTERNATE_LAST_MODIFIED);
+            }
+            if (getName() != null) {
+                attributeIDs.addElement(NAME);
+            }
+            String resourceType = getResourceType();
+            if (resourceType != null) {
+                attributeIDs.addElement(TYPE);
+                attributeIDs.addElement(ALTERNATE_TYPE);
+            }
+            long contentLength = getContentLength();
+            if (contentLength >= 0) {
+                attributeIDs.addElement(CONTENT_LENGTH);
+                attributeIDs.addElement(ALTERNATE_CONTENT_LENGTH);
+            }
             return new RecyclableNamingEnumeration(attributeIDs);
         } else {
             return attributes.getIDs();
@@ -890,7 +941,13 @@
      */
     public int size() {
         if (attributes == null) {
-            return 5;
+            int size = 0;
+            if (getCreationDate() != null) size += 2;
+            if (getLastModifiedDate() != null) size += 2;
+            if (getName() != null) size++;
+            if (getResourceType() != null) size += 2;
+            if (getContentLength() >= 0) size += 2;
+            return size;
         } else {
             return attributes.size();
         }

Modified: tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml
URL: 
http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml?rev=645157&r1=645156&r2=645157&view=diff
==============================================================================
--- tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/tc6.0.x/trunk/webapps/docs/changelog.xml Sat Apr  5 11:27:32 2008
@@ -65,6 +65,12 @@
         Vista. (markt)
       </fix>
       <fix>
+        <bug>44611</bug>: DirContextURLConnection didn't implement
+        getHeaderFields(), getHeaderField(String name) was case sensitive and
+        returned "" rather than null for header values that did not exist. 
Patch
+        provided by Chris Hubick. (markt)
+      </fix>
+      <fix>
         <bug>44673</bug>: ServletInputStream is no longer readable once closed.
         (markt)
       </fix>



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to