organizing imports
[mir.git] / source / mircoders / media / MediaHandlerGeneric.java
index e31af29..ecc7fe9 100755 (executable)
-/*\r
- * Copyright (C) 2001, 2002  The Mir-coders group\r
- *\r
- * This file is part of Mir.\r
- *\r
- * Mir is free software; you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation; either version 2 of the License, or\r
- * (at your option) any later version.\r
- *\r
- * Mir is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with Mir; if not, write to the Free Software\r
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
- *\r
- * In addition, as a special exception, The Mir-coders gives permission to link\r
- * the code of this program with the com.oreilly.servlet library, any library\r
- * licensed under the Apache Software License, The Sun (tm) Java Advanced\r
- * Imaging library (JAI), The Sun JIMI library (or with modified versions of\r
- * the above that use the same license as the above), and distribute linked\r
- * combinations including the two.  You must obey the GNU General Public\r
- * License in all respects for all of the code used other than the above\r
- * mentioned libraries.  If you modify this file, you may extend this exception\r
- * to your version of the file, but you are not obligated to do so.  If you do\r
- * not wish to do so, delete this exception statement from your version.\r
- */\r
-\r
-package  mircoders.media;\r
-\r
-import java.io.File;\r
-import java.io.FileInputStream;\r
-import java.io.IOException;\r
-import java.io.InputStream;\r
-\r
-import mir.config.MirPropertiesConfiguration;\r
-import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;\r
-import mir.entity.Entity;\r
-import mir.log.LoggerWrapper;\r
-import mir.media.MirMedia;\r
-import mir.media.MediaExc;\r
-import mir.media.MediaFailure;\r
-import mir.misc.FileUtil;\r
-import mir.misc.StringUtil;\r
-import freemarker.template.SimpleList;\r
-\r
-\r
-/**\r
- * This is the Generic MediaHandler. It stores the media data on\r
- * the filesystem and keeps basic metadata  (size, type...) in the\r
- * DB. Usually only representation needs to be overridden.\r
- * See the MediaHandlerAudio class to see an example of how one\r
- * could override it.\r
- * <p>\r
- * Most media handlers should override this class.\r
- * <p>\r
- * In theory, it could be used to handle miscellaneous media that\r
- * we don't have entered in the media_type table, (like RTF documents,\r
- * PS, PDF, etc..)\r
- * <p>\r
- * Of course it implements the MirMedia interface.\r
- *\r
- * @see mir.media.MirMedia\r
- * @author mh <mh@nadir.org>\r
- * @version $Id: MediaHandlerGeneric.java,v 1.17 2003/03/09 03:53:11 zapata Exp $\r
- */\r
-\r
-public class MediaHandlerGeneric implements MirMedia\r
-{\r
-    protected static MirPropertiesConfiguration configuration;\r
-    protected static String imageHost;\r
-    protected static String imageRoot;\r
-\r
-    protected LoggerWrapper logger;\r
-\r
-    static {\r
-      try {\r
-        configuration = MirPropertiesConfiguration.instance();\r
-      }\r
-      catch (PropertiesConfigExc e) {\r
-      }\r
-      imageHost = configuration.getString("Producer.Image.Host");\r
-      imageRoot = configuration.getString("Producer.ImageRoot");\r
-    }\r
-\r
-    public MediaHandlerGeneric() {\r
-      logger = new LoggerWrapper("Media.Generic");\r
-    }\r
-\r
-    public void set (InputStream in, Entity ent, Entity mediaTypeEnt ) throws MediaExc, MediaFailure {\r
-      String ext = mediaTypeEnt.getValue("name");\r
-      String mediaFname = ent.getId() + "." + ext;\r
-      String date = ent.getValue("date");\r
-      String datePath = StringUtil.webdbDate2path(date);\r
-      try {\r
-        long size = FileUtil.write(getStoragePath() + File.separator + datePath +\r
-                                   File.separator + mediaFname, in);\r
-        ent.setValueForProperty("publish_path", datePath + mediaFname);\r
-        ent.setValueForProperty("size", new Long(size).toString());\r
-        ent.update();\r
-      }\r
-      catch (Throwable e) {\r
-        logger.error("MediaHandlerGeneric.set: " + e.toString());\r
-        throw new MediaFailure(e);\r
-      }\r
-    }\r
-\r
-    public void produce (Entity ent, Entity mediaTypeEnt ) throws MediaExc, MediaFailure {\r
-      //check first if the media file exist since produced\r
-      //location is also the storage location\r
-\r
-      String date = ent.getValue("date");\r
-      String datePath = StringUtil.webdbDate2path(date);\r
-      String relPath = datePath+ent.getId()+"."+mediaTypeEnt.getValue("name");\r
-      String fname = getStoragePath()+relPath;\r
-      if(! new File(fname).exists())\r
-        throw new MediaExc("error in MirMedia.produce(): " + relPath + " does not exist!");\r
-    }\r
-\r
-    public InputStream getMedia (Entity ent, Entity mediaTypeEnt) throws MediaExc, MediaFailure {\r
-      String publishPath = ent.getValue("publish_path");\r
-      String fname = getStoragePath()+publishPath;\r
-      File f = new File(fname);\r
-      if(! f.exists())\r
-        throw new MediaExc("error in MirMedia.getMedia(): " + fname + " does not exist!");\r
-\r
-      FileInputStream inputStream;\r
-      try {\r
-        inputStream = new FileInputStream(f);\r
-      }\r
-      catch (Throwable e) {\r
-        throw new MediaFailure("MediaHandlerGeneric.getMedia(): " + e.toString(), e);\r
-      }\r
-\r
-      return inputStream;\r
-    }\r
-\r
-    public InputStream getIcon (Entity ent) throws MediaExc, MediaFailure {\r
-        return null;\r
-    }\r
-\r
-    public String getStoragePath()\r
-    {\r
-        return configuration.getString("Producer.Media.Path");\r
-    }\r
-\r
-    public String getIconStoragePath()\r
-    {\r
-        return configuration.getString("Producer.Image.IconPath");\r
-    }\r
-\r
-    public String getPublishHost()\r
-    {\r
-        return StringUtil.removeSlash(configuration.getString("Producer.Media.Host"));\r
-    }\r
-\r
-    public String getTinyIconName()\r
-    {\r
-        return configuration.getString("Producer.Icon.TinyText");\r
-    }\r
-\r
-    public String getBigIconName()\r
-    {\r
-        return configuration.getString("Producer.Icon.BigText");\r
-    }\r
-\r
-    public String getIconAltName()\r
-    {\r
-        return "Generic media";\r
-    }\r
-\r
-    public SimpleList getURL(Entity ent, Entity mediaTypeEnt)\r
-    {\r
-      SimpleList theList = new SimpleList();\r
-      theList.add(ent);\r
-      return theList;\r
-    }\r
-\r
-    public boolean isVideo()\r
-    {\r
-      return false;\r
-    }\r
-\r
-    public boolean isAudio()\r
-    {\r
-      return false;\r
-    }\r
-\r
-    public boolean isImage()\r
-    {\r
-      return false;\r
-    }\r
-\r
-    public String getDescr( Entity mediaType)\r
-    {\r
-      return mediaType.getValue("mime_type");\r
-    }\r
-\r
-}\r
-\r
-\r
-\r
+/*
+ * Copyright (C) 2001, 2002 The Mir-coders group
+ *
+ * This file is part of Mir.
+ *
+ * Mir is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Mir is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Mir; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * In addition, as a special exception, The Mir-coders gives permission to link
+ * the code of this program with  any library licensed under the Apache Software License,
+ * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
+ * (or with modified versions of the above that use the same license as the above),
+ * and distribute linked combinations including the two.  You must obey the
+ * GNU General Public License in all respects for all of the code used other than
+ * the above mentioned libraries.  If you modify this file, you may extend this
+ * exception to your version of the file, but you are not obligated to do so.
+ * If you do not wish to do so, delete this exception statement from your version.
+ */
+package  mircoders.media;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import javax.servlet.ServletContext;
+
+import freemarker.template.SimpleList;
+
+import mir.config.MirPropertiesConfiguration;
+import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
+import mir.entity.Entity;
+import mir.log.LoggerWrapper;
+import mir.media.MediaExc;
+import mir.media.MediaFailure;
+import mir.media.MirMedia;
+import mir.misc.FileUtil;
+import mir.misc.StringUtil;
+
+
+/**
+ * This is the Generic MediaHandler. It stores the media data on
+ * the filesystem and keeps basic metadata  (size, type...) in the
+ * DB. Usually only representation needs to be overridden.
+ * See the MediaHandlerAudio class to see an example of how one
+ * could override it.
+ * <p>
+ * Most media handlers should override this class.
+ * <p>
+ * In theory, it could be used to handle miscellaneous media that
+ * we don't have entered in the media_type table, (like RTF documents,
+ * PS, PDF, etc..)
+ * <p>
+ * Of course it implements the MirMedia interface.
+ *
+ * @see mir.media.MirMedia
+ * @author mh <mh@nadir.org>
+ * @version $Id: MediaHandlerGeneric.java,v 1.21 2003/06/15 22:59:21 idfx Exp $
+ */
+
+public class MediaHandlerGeneric implements MirMedia
+{
+    protected static MirPropertiesConfiguration configuration;
+    protected static String imageHost;
+    protected static String imageRoot;
+
+    protected LoggerWrapper logger;
+
+    static {
+      try {
+        configuration = MirPropertiesConfiguration.instance();
+      }
+      catch (PropertiesConfigExc e) {
+      }
+      imageHost = configuration.getString("Producer.Image.Host");
+      imageRoot = configuration.getString("Producer.ImageRoot");
+    }
+
+    public MediaHandlerGeneric() {
+      logger = new LoggerWrapper("Media.Generic");
+    }
+
+    public void set (InputStream in, Entity ent, Entity mediaTypeEnt ) throws MediaExc, MediaFailure {
+      String ext = mediaTypeEnt.getValue("name");
+      String mediaFname = ent.getId() + "." + ext;
+      String date = ent.getValue("date");
+      String datePath = StringUtil.webdbDate2path(date);
+      try {
+        long size = FileUtil.write(getStoragePath() + File.separator + datePath +
+                                   File.separator + mediaFname, in);
+        ent.setValueForProperty("publish_path", datePath + mediaFname);
+        ent.setValueForProperty("size", new Long(size).toString());
+        ent.update();
+      }
+      catch (Throwable e) {
+        logger.error("MediaHandlerGeneric.set: " + e.toString());
+        throw new MediaFailure(e);
+      }
+    }
+
+    public void produce (Entity ent, Entity mediaTypeEnt ) throws MediaExc, MediaFailure {
+      //check first if the media file exist since produced
+      //location is also the storage location
+
+      String date = ent.getValue("date");
+      String datePath = StringUtil.webdbDate2path(date);
+      String relPath = datePath+ent.getId()+"."+mediaTypeEnt.getValue("name");
+      String fname = getStoragePath()+relPath;
+      if(! new File(fname).exists())
+        throw new MediaExc("error in MirMedia.produce(): " + relPath + " does not exist!");
+    }
+
+    public InputStream getMedia (Entity ent, Entity mediaTypeEnt) throws MediaExc, MediaFailure {
+      String publishPath = ent.getValue("publish_path");
+      String fname = getStoragePath()+publishPath;
+      File f = new File(fname);
+      if(! f.exists())
+        throw new MediaExc("error in MirMedia.getMedia(): " + fname + " does not exist!");
+
+      FileInputStream inputStream;
+      try {
+        inputStream = new FileInputStream(f);
+      }
+      catch (Throwable e) {
+        throw new MediaFailure("MediaHandlerGeneric.getMedia(): " + e.toString(), e);
+      }
+
+      return inputStream;
+    }
+
+    public InputStream getIcon (Entity ent) throws MediaExc, MediaFailure {
+      return null;
+    }
+
+    public String getIconMimeType (Entity aMediaEntity, Entity aMediaType) throws MediaExc, MediaFailure {
+      ServletContext servletContext = MirPropertiesConfiguration.getContext();
+      String fileName = aMediaEntity.getId()+"."+aMediaType.getValue("name");
+
+      return servletContext.getMimeType(fileName);
+    };
+
+    public String getStoragePath()
+    {
+        return configuration.getString("Producer.Media.Path");
+    }
+
+    public String getIconStoragePath()
+    {
+        return configuration.getString("Producer.Image.IconPath");
+    }
+
+    public String getPublishHost()
+    {
+        return StringUtil.removeSlash(configuration.getString("Producer.Media.Host"));
+    }
+
+    public String getTinyIconName()
+    {
+        return configuration.getString("Producer.Icon.TinyText");
+    }
+
+    public String getBigIconName()
+    {
+        return configuration.getString("Producer.Icon.BigText");
+    }
+
+    public String getIconAltName()
+    {
+        return "Generic media";
+    }
+
+    public SimpleList getURL(Entity ent, Entity mediaTypeEnt)
+    {
+      SimpleList theList = new SimpleList();
+      theList.add(ent);
+      return theList;
+    }
+
+    public boolean isVideo()
+    {
+      return false;
+    }
+
+    public boolean isAudio()
+    {
+      return false;
+    }
+
+    public boolean isImage()
+    {
+      return false;
+    }
+
+    public String getDescr( Entity mediaType)
+    {
+      return mediaType.getValue("mime_type");
+    }
+
+}
+
+
+