use ServletContext.getMimeType() as fallback to find media mime-type from .extension...
authormh <mh>
Tue, 2 Apr 2002 22:58:30 +0000 (22:58 +0000)
committermh <mh>
Tue, 2 Apr 2002 22:58:30 +0000 (22:58 +0000)
etc/web.xml
source/mir/misc/FileUtil.java
source/mircoders/servlet/ServletModuleOpenIndy.java
source/mircoders/servlet/ServletModuleUploadedMedia.java

index 91dbf46..001d9a0 100755 (executable)
         <url-pattern>/OutputMir</url-pattern>
     </servlet-mapping>
 
+    <mime-mapping>
+      <extension>
+        mp3 
+      </extension>
+      <mime-type>
+        audio/x-mp3
+      </mime-type>
+    </mime-mapping>
+    <mime-mapping>
+      <extension>
+        ra
+      </extension>
+      <mime-type>
+        audio/vnd.rn-realaudio
+      </mime-type>
+    </mime-mapping>
+    <mime-mapping>
+      <extension>
+        rm
+      </extension>
+      <mime-type>
+        application/vnd.rn-realmedia
+      </mime-type>
+    </mime-mapping>
+    <mime-mapping>
+      <extension>
+        mov
+      </extension>
+      <mime-type>
+        video/quicktime 
+      </mime-type>
+    </mime-mapping>
+    <mime-mapping>
+      <extension>
+        mpg
+      </extension>
+      <mime-type>
+        video/mpeg
+      </mime-type>
+    </mime-mapping>
+    <mime-mapping>
+      <extension>
+        avi
+      </extension>
+      <mime-type>
+        video/x-msvideo
+      </mime-type>
+    </mime-mapping>
+    <mime-mapping>
+      <extension>
+        asf
+      </extension>
+      <mime-type>
+        video/x-ms-asf
+      </mime-type>
+    </mime-mapping>
+    <mime-mapping>
+      <extension>
+        pdf
+      </extension>
+      <mime-type>
+        application/pdf
+      </mime-type>
+    </mime-mapping>
+
+
 
     <taglib>
         <taglib-uri>
index e655879..ced0d66 100755 (executable)
@@ -13,25 +13,21 @@ import  java.net.*;
 import  freemarker.template.*;
 import  mir.entity.*;
 import  mir.storage.*;
-import javax.servlet.http.*;
 
+import javax.servlet.http.*;
+import javax.servlet.*;
 
 /**
  * Hilfsklasse zum Mergen von Template und Daten
  */
 public final class FileUtil {
 
-  private static boolean fileNameMapLoaded = false;
-  private static FileNameMap fileNameMap;
   private static String producerStorageRoot;
 
   //
   // Initialisierung
 
   static {
-    System.setProperty("content.types.user.table", MirConfig.getProp("Home")+
-                        "content-types.properties");
-    fileNameMap = sun.net.www.MimeTable.loadTable();
     producerStorageRoot = MirConfig.getProp("Producer.StorageRoot");
   }
 
@@ -40,7 +36,7 @@ public final class FileUtil {
    */
   private FileUtil () {
   }
-       
+
   public static boolean write(String filename, byte[] in)
     throws IOException {
 
@@ -100,33 +96,5 @@ public final class FileUtil {
     }
   }
 
-  private static FileNameMap getFileNameMap() {
-    if ((fileNameMap == null) && !fileNameMapLoaded) {
-      fileNameMap = sun.net.www.MimeTable.loadTable();
-      fileNameMapLoaded = true;
-    }
-
-    return new FileNameMap() {
-      private FileNameMap map = fileNameMap;
-      public String getContentTypeFor(String fileName) {
-        return map.getContentTypeFor(fileName);
-      }
-    };
-  }
-
-  public static void setFileNameMap(FileNameMap map) {
-    fileNameMap = map;
-  }
-
-  public static String guessContentTypeFromName(String fname) {
-    String contentType = null;
-                  
-    contentType = getFileNameMap().getContentTypeFor(fname);
-                                  
-    return contentType;
-  }
-
-
  
-  
 }
index 01fe804..6483ef4 100755 (executable)
@@ -274,23 +274,36 @@ public class ServletModuleOpenIndy extends ServletModule
         //the browser is in error, better check against the file extension
         if (contentType.equals("text/plain") ||
             contentType.equals("application/octet-stream")) {
-            /**
-             * This is just a temporary way to get the content-type via
-             * the .extension , we could maybe use a magic method, by looking
-             * at the header (first few bytes) of the file. (like the file(1)
-             * command).
-             * The Oreilly method  relies on the content-type that the client
-             * browser sends and that sometimes is application-octet stream with
-             * broken/mis-configured browsers.
-             *
-             * The map file should be Mir/content-types.properties, it's the
-             * default Sun Java file with some additional entries that it did
-             * not have. So if you support a new media type you have to make
-             * sure that it is in this file -mh
-             */
-            contentType = FileUtil.guessContentTypeFromName(fileName);
-            if (contentType==null)
-                contentType = "text/plain"; // rfc1867 says this is the default
+          /**
+           * Fallback to finding the mime-type through the standard ServletApi
+           * ServletContext getMimeType() method.
+           *
+           * This is a way to get the content-type via the .extension,
+           * we could maybe use a magic method as an additional method of
+           * figuring out the content-type, by looking at the header (first
+           * few bytes) of the file. (like the file(1) command). We could 
+           * also call the "file" command through Runtime. This is an
+           * option that I almost prefer as it is already implemented and
+           * exists with an up-to-date map on most modern Unix like systems.
+           * I haven't found a really nice implementation of the magic method
+           * in pure java yet.
+           *
+           * The first method we try thought is the "Oreilly method". It
+           * relies on the content-type that the client browser sends and 
+           * that sometimes is application-octet stream with
+           * broken/mis-configured browsers.
+           *
+           * The map file we use for the extensions is the standard web-app
+           * deployment descriptor file (web.xml). See Mir's web.xml or see
+           * your Servlet containers (most likely Tomcat) documentation.
+           * So if you support a new media type you have to make sure that 
+           * it is in this file -mh
+           */
+          ServletContext ctx =
+            (ServletContext)MirConfig.getPropAsObject("ServletContext");
+          contentType = ctx.getMimeType(fileName);
+          if (contentType==null)
+            contentType = "text/plain"; // rfc1867 says this is the default
         }
         HashMap mediaValues = new HashMap();
 
index c6d74f3..fd66221 100755 (executable)
@@ -7,7 +7,7 @@ import mir.media.MediaHelper;
 import mir.media.MirMedia;
 import mir.media.MirMediaException;
 import mir.media.MirMediaUserException;
-import mir.misc.FileUtil;
+import mir.misc.MirConfig;
 import mir.misc.MpRequest;
 import mir.misc.StringUtil;
 import mir.misc.WebdbMultipartRequest;
@@ -24,6 +24,8 @@ import mircoders.storage.DatabaseMediafolder;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
+import javax.servlet.ServletContext;
+
 import java.io.IOException;
 import java.net.URLEncoder;
 import java.util.GregorianCalendar;
@@ -70,20 +72,33 @@ public abstract class ServletModuleUploadedMedia
       if (contentType.equals("text/plain") ||
               contentType.equals("application/octet-stream")) {
         /**
-         * This is just a temporary way to get the content-type via
-         * the .extension , we could maybe use a magic method, by looking
-         * at the header (first few bytes) of the file. (like the file(1)
-         * command).
-         * The Oreilly method  relies on the content-type that the client
-         * browser sends and that sometimes is application-octet stream with
+         * Fallback to finding the mime-type through the standard ServletApi
+         * ServletContext getMimeType() method.
+         *
+         * This is a way to get the content-type via the .extension,
+         * we could maybe use a magic method as an additional method of
+         * figuring out the content-type, by looking at the header (first
+         * few bytes) of the file. (like the file(1) command). We could 
+         * also call the "file" command through Runtime. This is an
+         * option that I almost prefer as it is already implemented and
+         * exists with an up-to-date map on most modern Unix like systems.
+         * I haven't found a really nice implementation of the magic method
+         * in pure java yet.
+         *
+         * The first method we try thought is the "Oreilly method". It
+         * relies on the content-type that the client browser sends and 
+         * that sometimes is application-octet stream with
          * broken/mis-configured browsers.
          *
-         * The map file should be Mir/content-types.properties, it's the
-         * default Sun Java file with some additional entries that it did
-         * not have. So if you support a new media type you have to make
-         * sure that it is in this file -mh
+         * The map file we use for the extensions is the standard web-app
+         * deployment descriptor file (web.xml). See Mir's web.xml or see
+         * your Servlet containers (most likely Tomcat) documentation.
+         * So if you support a new media type you have to make sure that 
+         * it is in this file -mh
          */
-        contentType = FileUtil.guessContentTypeFromName(fileName);
+        ServletContext ctx =
+          (ServletContext)MirConfig.getPropAsObject("ServletContext");
+        contentType = ctx.getMimeType(fileName);
         if (contentType == null)
           contentType = "text/plain"; // rfc1867 says this is the default
       }