1 16 17 package org.apache.naming.util; 18 19 import java.text.ParseException ; 20 import java.text.SimpleDateFormat ; 21 import java.util.Date ; 22 import java.util.Locale ; 23 24 import javax.naming.NamingException ; 25 import javax.naming.directory.Attribute ; 26 import javax.naming.directory.Attributes ; 27 30 34 public class AttributeHelper 35 { 36 39 public static final String CONTENT_LENGTH = "getcontentlength"; 40 public static final String ALTERNATE_CONTENT_LENGTH = "content-length"; 41 42 45 public static final String CONTENT_TYPE = "getcontenttype"; 46 public static final String ALTERNATE_TYPE = "content-type"; 47 48 49 52 public static final String LAST_MODIFIED = "getlastmodified"; 53 public static final String ALTERNATE_LAST_MODIFIED = "last-modified"; 54 57 protected static final SimpleDateFormat formats[] = { 58 new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US), 59 new SimpleDateFormat ("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US), 60 new SimpleDateFormat ("EEEEEE, dd-MMM-yy HH:mm:ss zzz", Locale.US), 61 new SimpleDateFormat ("EEE MMMM d HH:mm:ss yyyy", Locale.US) 62 }; 63 64 65 70 public static long getContentLength(Attributes attributes) { 71 long contentLength=-1; 72 if (contentLength != -1L) 73 return contentLength; 74 if (attributes != null) { 75 Attribute attribute = attributes.get(CONTENT_LENGTH); 76 if( attribute==null ) 77 attribute=attributes.get(ALTERNATE_CONTENT_LENGTH ); 78 if (attribute != null) { 79 try { 80 Object value = attribute.get(); 81 if (value instanceof Long ) { 82 contentLength = ((Long ) value).longValue(); 83 } else { 84 try { 85 contentLength = Long.parseLong(value.toString()); 86 } catch (NumberFormatException e) { 87 ; } 89 } 90 } catch (NamingException e) { 91 ; } 93 } 94 } 95 return contentLength; 96 } 97 98 101 public static String getContentType(Attributes attributes) { 102 Attribute attribute = attributes.get(CONTENT_TYPE); 103 if( attribute == null ) return null; 104 105 try { 106 String s= attribute.get().toString(); 107 return s; 108 } catch (Exception e) { 109 } 111 return null; 112 } 113 114 117 public static long getLastModified( Attributes attributes ) { 118 long lastModified=-1; 119 Date lastModifiedDate; 120 121 Attribute 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 value = attribute.get(); 128 if (value instanceof Long ) { 129 lastModified = ((Long ) value).longValue(); 130 } else if (value instanceof Date ) { 131 lastModified = ((Date ) value).getTime(); 132 lastModifiedDate = (Date ) value; 133 } else { 134 String lastModifiedDateValue = value.toString(); 135 Date result = null; 136 for (int i = 0; (result == null) && 138 (i < formats.length); i++) { 139 try { 140 result = 141 formats[i].parse(lastModifiedDateValue); 142 } catch (ParseException e) { 143 ; 144 } 145 } 146 if (result != null) { 147 lastModified = result.getTime(); 148 lastModifiedDate = result; 149 } 150 } 151 } catch (NamingException e) { 152 ; } 154 } 155 return lastModified; 156 } 157 158 } 159 | Popular Tags |