Author: remy.maucherat(a)jboss.com
Date: 2008-03-31 09:51:46 -0400 (Mon, 31 Mar 2008)
New Revision: 570
Modified:
trunk/java/org/apache/naming/resources/DirContextURLConnection.java
trunk/java/org/apache/naming/resources/ResourceAttributes.java
trunk/webapps/docs/changelog.xml
Log:
- 44611: Implement DirContextURLConnection.getHeaderFields(), fix case
sensitivity problem and return null for header values which don't exist.
Modified: trunk/java/org/apache/naming/resources/DirContextURLConnection.java
===================================================================
--- trunk/java/org/apache/naming/resources/DirContextURLConnection.java 2008-03-31
13:42:42 UTC (rev 569)
+++ trunk/java/org/apache/naming/resources/DirContextURLConnection.java 2008-03-31
13:51:46 UTC (rev 570)
@@ -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;
@@ -61,7 +65,7 @@
("Directory context can't be null");
if (org.apache.naming.Constants.IS_SECURITY_ENABLED) {
this.permission = new JndiPermission(url.toString());
- }
+ }
this.context = context;
}
@@ -217,7 +221,47 @@
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: trunk/java/org/apache/naming/resources/ResourceAttributes.java
===================================================================
--- trunk/java/org/apache/naming/resources/ResourceAttributes.java 2008-03-31 13:42:42 UTC
(rev 569)
+++ trunk/java/org/apache/naming/resources/ResourceAttributes.java 2008-03-31 13:51:46 UTC
(rev 570)
@@ -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: trunk/webapps/docs/changelog.xml
===================================================================
--- trunk/webapps/docs/changelog.xml 2008-03-31 13:42:42 UTC (rev 569)
+++ trunk/webapps/docs/changelog.xml 2008-03-31 13:51:46 UTC (rev 570)
@@ -48,6 +48,10 @@
<fix>
<bug>44673</bug>: Fix ServletInputStream still readable when closed.
(markt)
</fix>
+ <fix>
+ <bug>44611</bug>: Implement
DirContextURLConnection.getHeaderFields(), fix case
+ sensitivity problem and return null for header values which don't exist.
(markt)
+ </fix>
</changelog>
</subsection>
<subsection name="Coyote">