bug fix in content type finding logic. this really ought to go in another file
authormh <mh>
Mon, 1 Apr 2002 03:57:52 +0000 (03:57 +0000)
committermh <mh>
Mon, 1 Apr 2002 03:57:52 +0000 (03:57 +0000)
source/mircoders/servlet/ServletModuleOpenIndy.java
source/mircoders/servlet/ServletModuleUploadedMedia.java

index 7c7219d..01fe804 100755 (executable)
@@ -298,7 +298,8 @@ public class ServletModuleOpenIndy extends ServletModule
         
         if (contentType.equals("text/plain") ||
             contentType.equals("application/octet-stream")) {
-          throw new ServletModuleUserException("One or more files of unrecognized types");
+          contentModule.deleteById(cid);
+          _throwBadContentType(fileName, contentType);
         }
 
         String mediaTitle=(String)withValues.get("media_title"+i);
@@ -314,12 +315,6 @@ public class ServletModuleOpenIndy extends ServletModule
         mediaValues.put("is_produced", "0");
         mediaValues.put("is_published","0");
 
-        //the where clause to find the media_type entry
-        //from the content-type.
-        //we use the media type entry to lookup the
-        //media Handler/Storage classes
-        //String wc = " mime_type = '"+contentType+"'";
-
         // @todo this should probably be moved to DatabaseMediaType -mh
         String[] cTypeSplit = StringUtil.split(contentType, "/");
         String wc = " mime_type LIKE '"+cTypeSplit[0]+"%'";
@@ -332,79 +327,86 @@ public class ServletModuleOpenIndy extends ServletModule
         Database mediaStorage;
         ProducerMedia mediaProducer;
  
-        //if we found an entry matching the
+        //if we didn't find an entry matching the
         //content-type int the table.
-        if (mediaTypesList.size() > 0) {
-          Entity mediaType = null;
-          
-          // find out if we an exact content-type match if so take it.
-          // otherwise just use the first one.
-          // @todo this should probably be moved to DatabaseMediaType -mh
-          for(int j=0;j<mediaTypesList.size();j++) {
-            if(contentType.equals(
-                            mediaTypesList.elementAt(j).getValue("mime_type")))
-              mediaType = mediaTypesList.elementAt(j);
-          }
-
-          if( mediaType == null )
-            mediaType = mediaTypesList.elementAt(0);
-                
-          //get the class names from the media_type table.
-          mediaTypeId = mediaType.getId();
-          try {
-            // ############### @todo: merge these and the getURL call into one
-            // getURL helper call that just takes the Entity as a parameter
-            // along with media_type
-            mediaHandler = MediaHelper.getHandler(mediaType);
-            mediaStorage = MediaHelper.getStorage(mediaType,
-                                                "mircoders.storage.Database");
-            Class prodCls = Class.forName("mircoders.producer.Producer"
-                                              +mediaType.getValue("tablename"));
-            mediaProducer = (ProducerMedia)prodCls.newInstance();
-          } catch (Exception e) {
-            theLog.printError("getting media handler failed: "+e.toString());
-            contentModule.deleteById(cid);
-            throw new ServletModuleException("getting media handler failed: "
-                                            +e.toString());
-          }
-
-          mediaValues.put("to_media_type",mediaTypeId);
-
-          //load the classes via reflection
-          String MediaId;
-          Entity mediaEnt = null;
-          try {
-            mediaEnt = (Entity)mediaStorage.getEntityClass().newInstance();
-            mediaEnt.setStorage(mediaStorage);
-            mediaEnt.setValues(mediaValues);
-            mediaId = mediaEnt.insert();
-
-            //save and store the media data/metadata
-            mediaHandler.set(mpReq.getMedia(), mediaEnt,
-                            mediaType);
-
-            //were done with mpReq at this point, dereference it.
-            //as it contains mucho mem. -mh 01.10.2001
-            mpReq=null;
-            
-            //we got this far, associate the media to the article
-            mediaEnt.setValueForProperty("is_published","1");
-            mediaEnt.update();
-            //produce it
-            mediaProducer.handle(null, null, false, false, mediaId);
-            DatabaseContentToMedia.getInstance().addMedia(cid,mediaId);
-          } catch (Exception e) {
-            theLog.printError("setting media failed: "+e.toString());
-            contentModule.deleteById(cid);
-            throw new ServletModuleException("setting media failed: "+e.toString());
-          }
+        if (mediaTypesList.size() == 0) {
+          contentModule.deleteById(cid);
+          _throwBadContentType(fileName, contentType);
+        }
 
-        } else {
+        Entity mediaType = null;
+        Entity mediaType2 = null;
+        
+        // find out if we an exact content-type match if so take it.
+        // otherwise try to match majortype/*
+        // @todo this should probably be moved to DatabaseMediaType -mh
+        for(int j=0;j<mediaTypesList.size();j++) {
+          if(contentType.equals(
+                mediaTypesList.elementAt(j).getValue("mime_type")))
+            mediaType = mediaTypesList.elementAt(j);
+          else if ((mediaTypesList.elementAt(j).getValue("mime_type")).equals(
+                    cTypeSplit[0]+"/*") )
+            mediaType2= mediaTypesList.elementAt(j); 
+        } 
+
+        if ( (mediaType == null) && (mediaType2 == null) ) {
+          contentModule.deleteById(cid);
+          _throwBadContentType(fileName, contentType);
+        }
+        else if( (mediaType == null) && (mediaType2 != null) )
+          mediaType = mediaType2;
+
+        //get the class names from the media_type table.
+        mediaTypeId = mediaType.getId();
+        try {
+          // ############### @todo: merge these and the getURL call into one
+          // getURL helper call that just takes the Entity as a parameter
+          // along with media_type
+          mediaHandler = MediaHelper.getHandler(mediaType);
+          mediaStorage = MediaHelper.getStorage(mediaType,
+                                              "mircoders.storage.Database");
+          Class prodCls = Class.forName("mircoders.producer.Producer"
+                                            +mediaType.getValue("tablename"));
+          mediaProducer = (ProducerMedia)prodCls.newInstance();
+        } catch (Exception e) {
+          theLog.printError("getting media handler failed: "+e.toString());
           contentModule.deleteById(cid);
-          theLog.printDebugInfo("Wrong file type uploaded!: " + fileName);
-          throw new ServletModuleUserException("One or more files of unrecognized types");
-        } // end if-else mediaTypesList.size() > 0
+          throw new ServletModuleException("getting media handler failed: "
+                                          +e.toString());
+        }
+
+        mediaValues.put("to_media_type",mediaTypeId);
+
+        //load the classes via reflection
+        String MediaId;
+        Entity mediaEnt = null;
+        try {
+          mediaEnt = (Entity)mediaStorage.getEntityClass().newInstance();
+          mediaEnt.setStorage(mediaStorage);
+          mediaEnt.setValues(mediaValues);
+          mediaId = mediaEnt.insert();
+
+          //save and store the media data/metadata
+          mediaHandler.set(mpReq.getMedia(), mediaEnt,
+                          mediaType);
+
+          //were done with mpReq at this point, dereference it.
+          //as it contains mucho mem. -mh 01.10.2001
+          mpReq=null;
           
+          //we got this far, associate the media to the article
+          mediaEnt.setValueForProperty("is_published","1");
+          mediaEnt.update();
+          //produce it
+          mediaProducer.handle(null, null, false, false, mediaId);
+          DatabaseContentToMedia.getInstance().addMedia(cid,mediaId);
+        } catch (Exception e) {
+          theLog.printError("setting media failed: "+e.toString());
+          contentModule.deleteById(cid);
+          throw new ServletModuleException("setting media failed: "
+                                            +e.toString());
+        }
+
       } //end for Iterator...
 
       //if we're here all is ok...
@@ -440,6 +442,17 @@ public class ServletModuleOpenIndy extends ServletModule
     deliver(req, res, mergeData, postingFormDoneTemplate);
   }
 
+  private void _throwBadContentType (String fileName, String contentType)
+    throws ServletModuleUserException {
+
+    theLog.printDebugInfo("Wrong file type uploaded!: " + fileName+" "
+                          +contentType);
+    throw new ServletModuleUserException("The file you uploaded is of the "
+        +"following mime-type: "+contentType
+        +", we do not support this mime-type. "
+        +"Error One or more files of unrecognized type. Sorry");
+  }
+      
 }
 
 
index f9ebde3..c6d74f3 100755 (executable)
@@ -91,8 +91,7 @@ public abstract class ServletModuleUploadedMedia
 
       if (contentType.equals("text/plain") ||
               contentType.equals("application/octet-stream")) {
-        throw new ServletModuleUserException(
-                "One or more files of unrecognized types");
+        _throwBadContentType(fileName, contentType);
       }
 
       parameters.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
@@ -110,24 +109,29 @@ public abstract class ServletModuleUploadedMedia
       // if we didn't find an entry matching the
       // content-type in the table.
       if (mediaTypesList.size() == 0) {
-        theLog.printDebugInfo("Wrong file type uploaded!: " + fileName);
-        throw new MirMediaUserException("One or more files of unrecognized type");
+        _throwBadContentType(fileName, contentType);
       }
       Entity mediaType = null;
+      Entity mediaType2 = null;
 
       // find out if we an exact content-type match if so take it.
-      // otherwise just use the first one.
+      // otherwise try to match majortype/*
       // @todo this should probably be moved to DatabaseMediaType -mh
-      for (int j = 0; j < mediaTypesList.size(); j++) {
-        theLog.printDebugInfo("mediaTypesList: "+mediaTypesList.size());
-                               if (contentType.equals(
-                mediaTypesList.elementAt(j).getValue("mime_type")))
+      for(int j=0;j<mediaTypesList.size();j++) {
+        if(contentType.equals(
+                        mediaTypesList.elementAt(j).getValue("mime_type")))
           mediaType = mediaTypesList.elementAt(j);
+        else if ((mediaTypesList.elementAt(j).getValue("mime_type")).equals(
+                  contentTypeSplit[0]+"/*"))
+          mediaType2= mediaTypesList.elementAt(j); 
       }
 
-      // if no exact match, whatever foo/* might match
-      if (mediaType == null)
-        mediaType = mediaTypesList.elementAt(0);
+      if ( (mediaType == null) && (mediaType2 == null) ) {
+        _throwBadContentType(fileName, contentType);
+      }
+      else if( (mediaType == null) && (mediaType2 != null) )
+        mediaType = mediaType2;
+
 
       // get the class names from the media_type table.
       mediaTypeId = mediaType.getId();
@@ -184,10 +188,6 @@ public abstract class ServletModuleUploadedMedia
       throw new ServletModuleException(
               "upload -- media handling exception " + e.toString());
     }
-    catch (MirMediaUserException e) {
-      throw new ServletModuleUserException(
-              e.getMsg());
-    }
     catch (IOException e) {
       throw new ServletModuleException("upload -- ioexception " + e.toString());
     }
@@ -197,6 +197,18 @@ public abstract class ServletModuleUploadedMedia
 
   }
 
+  private void _throwBadContentType (String fileName, String contentType)
+    throws ServletModuleUserException {
+
+    theLog.printDebugInfo("Wrong file type uploaded!: " + fileName+" "
+                          +contentType);
+    throw new ServletModuleUserException("The file you uploaded is of the "
+        +"following mime-type: "+contentType
+        +", we do not support this mime-type. "
+        +"Error One or more files of unrecognized type. Sorry");
+  }
+
   public void update(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException {
 
     try {