001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.naming.util;
018:
019: import java.text.ParseException;
020: import java.text.SimpleDateFormat;
021: import java.util.Date;
022: import java.util.Locale;
023:
024: import javax.naming.NamingException;
025: import javax.naming.directory.Attribute;
026: import javax.naming.directory.Attributes;
027:
028: // import org.apache.naming.resources.Resource;
029: // import org.apache.naming.resources.ResourceAttributes;
030:
031: /**
032: * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
033: * @author Costin Manolache
034: */
035: public class AttributeHelper {
036: /**
037: * Content length.
038: */
039: public static final String CONTENT_LENGTH = "getcontentlength";
040: public static final String ALTERNATE_CONTENT_LENGTH = "content-length";
041:
042: /**
043: * MIME type of the content.
044: */
045: public static final String CONTENT_TYPE = "getcontenttype";
046: public static final String ALTERNATE_TYPE = "content-type";
047:
048: /**
049: * Last modification date. XXX Use standard LDAP att name
050: */
051: public static final String LAST_MODIFIED = "getlastmodified";
052: public static final String ALTERNATE_LAST_MODIFIED = "last-modified";
053: /**
054: * Date formats using for Date parsing.
055: */
056: protected static final SimpleDateFormat formats[] = {
057: new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz",
058: Locale.US),
059: new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy",
060: Locale.US),
061: new SimpleDateFormat("EEEEEE, dd-MMM-yy HH:mm:ss zzz",
062: Locale.US),
063: new SimpleDateFormat("EEE MMMM d HH:mm:ss yyyy", Locale.US) };
064:
065: /**
066: * Get content length.
067: *
068: * @return content length value
069: */
070: public static long getContentLength(Attributes attributes) {
071: long contentLength = -1;
072: if (contentLength != -1L)
073: return contentLength;
074: if (attributes != null) {
075: Attribute attribute = attributes.get(CONTENT_LENGTH);
076: if (attribute == null)
077: attribute = attributes.get(ALTERNATE_CONTENT_LENGTH);
078: if (attribute != null) {
079: try {
080: Object value = attribute.get();
081: if (value instanceof Long) {
082: contentLength = ((Long) value).longValue();
083: } else {
084: try {
085: contentLength = Long.parseLong(value
086: .toString());
087: } catch (NumberFormatException e) {
088: ; // Ignore
089: }
090: }
091: } catch (NamingException e) {
092: ; // No value for the attribute
093: }
094: }
095: }
096: return contentLength;
097: }
098:
099: /**
100: * Return the content type value.
101: */
102: public static String getContentType(Attributes attributes) {
103: Attribute attribute = attributes.get(CONTENT_TYPE);
104: if (attribute == null)
105: return null;
106:
107: try {
108: String s = attribute.get().toString();
109: return s;
110: } catch (Exception e) {
111: // Shouldn't happen, unless the attribute has no value
112: }
113: return null;
114: }
115:
116: /** Find the last modified of an entry. It's using various common
117: * attribute names, and support Long, Date, String att values.
118: */
119: public static long getLastModified(Attributes attributes) {
120: long lastModified = -1;
121: Date lastModifiedDate;
122:
123: Attribute attribute = attributes.get(LAST_MODIFIED);
124: if (attribute == null)
125: attribute = attributes.get(ALTERNATE_LAST_MODIFIED);
126:
127: if (attribute != null) {
128: try {
129: Object value = attribute.get();
130: if (value instanceof Long) {
131: lastModified = ((Long) value).longValue();
132: } else if (value instanceof Date) {
133: lastModified = ((Date) value).getTime();
134: lastModifiedDate = (Date) value;
135: } else {
136: String lastModifiedDateValue = value.toString();
137: Date result = null;
138: // Parsing the HTTP Date
139: for (int i = 0; (result == null)
140: && (i < formats.length); i++) {
141: try {
142: result = formats[i]
143: .parse(lastModifiedDateValue);
144: } catch (ParseException e) {
145: ;
146: }
147: }
148: if (result != null) {
149: lastModified = result.getTime();
150: lastModifiedDate = result;
151: }
152: }
153: } catch (NamingException e) {
154: ; // No value for the attribute
155: }
156: }
157: return lastModified;
158: }
159:
160: }
|