1 47 package com.openedit.page.manage; 48 49 import java.io.File ; 50 import java.io.FileInputStream ; 51 import java.io.InputStream ; 52 import java.util.HashMap ; 53 import java.util.Map ; 54 import java.util.Properties ; 55 import java.util.StringTokenizer ; 56 57 import com.openedit.OpenEditRuntimeException; 58 import com.openedit.util.FileUtils; 59 60 61 67 public class BaseMimeTypeMap implements MimeTypeMap 68 { 69 protected String fieldDefaultMimeType; 70 protected Map fieldInternalMap; 71 protected File fieldRootDirectory; 72 73 public BaseMimeTypeMap() 74 { 75 fieldDefaultMimeType = "application/octet-stream"; 76 } 77 78 81 public void setDefaultMimeType(String inMimeType) 82 { 83 fieldDefaultMimeType = inMimeType; 84 } 85 86 89 public String getDefaultMimeType() 90 { 91 return fieldDefaultMimeType; 92 } 93 94 97 public String getMimeType(String inExtension) 98 { 99 if ( inExtension == null ) 100 { 101 return getDefaultMimeType(); 102 } 103 String mimeType = (String ) get(inExtension.toLowerCase()); 104 105 if (mimeType == null) 106 { 107 mimeType = getDefaultMimeType(); 108 } 109 110 return mimeType; 111 } 112 113 116 public String getPathMimeType(String path) 117 { 118 return getMimeType(getExtension(path)); 119 } 120 121 protected String getExtension(String inPath) 122 { 123 String result = null; 124 125 if (inPath != null) 126 { 127 int index = inPath.lastIndexOf('.'); 128 129 if (index > -1) 130 { 131 result = inPath.substring(index + 1); 132 } 133 } 134 135 return result; 136 } 137 138 public void addMappings(String mimeType, String exts) 139 { 140 String ext; 141 142 for (StringTokenizer st = new StringTokenizer (exts, ","); st.hasMoreTokens();) 143 { 144 ext = st.nextToken(); 145 put(ext, mimeType); 146 } 147 } 148 149 public Map getInternalMap() 150 { 151 if ( fieldInternalMap == null ) 152 { 153 fieldInternalMap = new HashMap (); 154 loadMimeTypes(fieldInternalMap); 155 } 156 return fieldInternalMap; 157 } 158 159 protected void loadMimeTypes(Map inInternalMap) 160 { 161 InputStream in = getClass().getClassLoader().getResourceAsStream("mimetypes.properties"); 163 if ( in == null) 164 { 165 throw new OpenEditRuntimeException("Could not load mimetypes"); 166 } 167 Properties values = new Properties (); 168 try 169 { 170 values.load(in); 171 inInternalMap.putAll(values); 172 } 173 catch ( Exception ex) 174 { 175 throw new OpenEditRuntimeException(ex); 176 } 177 finally 178 { 179 FileUtils.safeClose(in); 180 } 181 File props2 = new File ( getRootDirectory(), "/WEB-INF/mimetypes.properties"); 183 if ( props2.exists()) 184 { 185 values = new Properties (); 186 try 187 { 188 in = new FileInputStream (props2); 189 values.load(in); 190 inInternalMap.putAll(values); 191 } 192 catch ( Exception ex) 193 { 194 throw new OpenEditRuntimeException(ex); 195 } 196 finally 197 { 198 FileUtils.safeClose(in); 199 } 200 } 201 } 202 203 public void setInternalMap( Map internalMap ) 204 { 205 fieldInternalMap = internalMap; 206 } 207 210 public Object get( Object key ) 211 { 212 return getInternalMap().get( key ); 213 } 214 217 public Object put( Object arg0, Object arg1 ) 218 { 219 return getInternalMap().put( arg0, arg1 ); 220 } 221 222 public File getRootDirectory() 223 { 224 return fieldRootDirectory; 225 } 226 227 public void setRootDirectory(File inRootDirectory) 228 { 229 fieldRootDirectory = inRootDirectory; 230 } 231 } 232 | Popular Tags |