KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > naming > util > AttributeHelper


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.naming.util;
18
19 import java.text.ParseException JavaDoc;
20 import java.text.SimpleDateFormat JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.Locale JavaDoc;
23
24 import javax.naming.NamingException JavaDoc;
25 import javax.naming.directory.Attribute JavaDoc;
26 import javax.naming.directory.Attributes JavaDoc;
27 // import org.apache.naming.resources.Resource;
28
// import org.apache.naming.resources.ResourceAttributes;
29

30 /**
31  * @author <a HREF="mailto:remm@apache.org">Remy Maucherat</a>
32  * @author Costin Manolache
33  */

34 public class AttributeHelper
35 {
36     /**
37      * Content length.
38      */

39     public static final String JavaDoc CONTENT_LENGTH = "getcontentlength";
40     public static final String JavaDoc ALTERNATE_CONTENT_LENGTH = "content-length";
41
42     /**
43      * MIME type of the content.
44      */

45     public static final String JavaDoc CONTENT_TYPE = "getcontenttype";
46     public static final String JavaDoc ALTERNATE_TYPE = "content-type";
47     
48
49     /**
50      * Last modification date. XXX Use standard LDAP att name
51      */

52     public static final String JavaDoc LAST_MODIFIED = "getlastmodified";
53     public static final String JavaDoc ALTERNATE_LAST_MODIFIED = "last-modified";
54     /**
55      * Date formats using for Date parsing.
56      */

57     protected static final SimpleDateFormat JavaDoc formats[] = {
58         new SimpleDateFormat JavaDoc("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US),
59         new SimpleDateFormat JavaDoc("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US),
60         new SimpleDateFormat JavaDoc("EEEEEE, dd-MMM-yy HH:mm:ss zzz", Locale.US),
61         new SimpleDateFormat JavaDoc("EEE MMMM d HH:mm:ss yyyy", Locale.US)
62     };
63
64
65     /**
66      * Get content length.
67      *
68      * @return content length value
69      */

70     public static long getContentLength(Attributes JavaDoc attributes) {
71         long contentLength=-1;
72         if (contentLength != -1L)
73             return contentLength;
74         if (attributes != null) {
75             Attribute JavaDoc attribute = attributes.get(CONTENT_LENGTH);
76             if( attribute==null )
77                 attribute=attributes.get(ALTERNATE_CONTENT_LENGTH );
78             if (attribute != null) {
79                 try {
80                     Object JavaDoc value = attribute.get();
81                     if (value instanceof Long JavaDoc) {
82                         contentLength = ((Long JavaDoc) value).longValue();
83                     } else {
84                         try {
85                             contentLength = Long.parseLong(value.toString());
86                         } catch (NumberFormatException JavaDoc e) {
87                             ; // Ignore
88
}
89                     }
90                 } catch (NamingException JavaDoc e) {
91                     ; // No value for the attribute
92
}
93             }
94         }
95         return contentLength;
96     }
97     
98     /**
99      * Return the content type value.
100      */

101     public static String JavaDoc getContentType(Attributes JavaDoc attributes) {
102         Attribute JavaDoc attribute = attributes.get(CONTENT_TYPE);
103         if( attribute == null ) return null;
104         
105         try {
106             String JavaDoc s= attribute.get().toString();
107             return s;
108         } catch (Exception JavaDoc e) {
109             // Shouldn't happen, unless the attribute has no value
110
}
111         return null;
112     }
113     
114     /** Find the last modified of an entry. It's using various common
115      * attribute names, and support Long, Date, String att values.
116      */

117     public static long getLastModified( Attributes JavaDoc attributes ) {
118         long lastModified=-1;
119         Date JavaDoc lastModifiedDate;
120         
121         Attribute JavaDoc attribute = attributes.get(LAST_MODIFIED);
122         if( attribute==null )
123             attribute=attributes.get(ALTERNATE_LAST_MODIFIED);
124         
125         if (attribute != null) {
126             try {
127                 Object JavaDoc value = attribute.get();
128                 if (value instanceof Long JavaDoc) {
129                     lastModified = ((Long JavaDoc) value).longValue();
130                 } else if (value instanceof Date JavaDoc) {
131                     lastModified = ((Date JavaDoc) value).getTime();
132                     lastModifiedDate = (Date JavaDoc) value;
133                 } else {
134                     String JavaDoc lastModifiedDateValue = value.toString();
135                     Date JavaDoc result = null;
136                     // Parsing the HTTP Date
137
for (int i = 0; (result == null) &&
138                              (i < formats.length); i++) {
139                         try {
140                             result =
141                                 formats[i].parse(lastModifiedDateValue);
142                         } catch (ParseException JavaDoc e) {
143                             ;
144                         }
145                     }
146                     if (result != null) {
147                         lastModified = result.getTime();
148                         lastModifiedDate = result;
149                     }
150                 }
151             } catch (NamingException JavaDoc e) {
152                 ; // No value for the attribute
153
}
154         }
155         return lastModified;
156     }
157         
158 }
159
Popular Tags