-/*\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.util.GregorianCalendar;\r
-import java.util.Iterator;\r
-import java.util.Map;\r
-\r
-import javax.servlet.ServletContext;\r
-\r
-import mir.config.MirPropertiesConfiguration;\r
-import mir.entity.Entity;\r
-import mir.entity.EntityList;\r
-import mir.log.LoggerWrapper;\r
-import mir.media.MediaHelper;\r
-import mir.media.MirMedia;\r
-\r
-import mir.misc.FileHandler;\r
-import mir.misc.StringUtil;\r
-import mir.storage.Database;\r
-import mir.storage.StorageObjectExc;\r
-import mir.storage.StorageObjectFailure;\r
-import mircoders.storage.DatabaseMediaType;\r
-\r
-import com.oreilly.servlet.multipart.FilePart;\r
-\r
-\r
-/*\r
- * MediaRequest.java -\r
- * Takes an HTTPServletRequest from a mutltipart form and finds the files\r
- * uploaded via the com.oreilly.servlet.multipart package. Finally the\r
- * appropriate media objects are set.\r
- *\r
- * @author mh\r
- * @version $Id: MediaRequest.java,v 1.17 2003/03/09 03:53:11 zapata Exp $\r
- *\r
- */\r
-\r
-public class MediaRequest implements FileHandler\r
-{\r
- private String _user;\r
- private EntityList _returnList = new EntityList();\r
- private boolean _publish;\r
- private LoggerWrapper logger;\r
-\r
- public MediaRequest(String user, boolean publish) {\r
- _user = user;\r
- _publish = publish;\r
- logger = new LoggerWrapper("Media.Request");\r
- }\r
-\r
- public EntityList getEntityList() {\r
- return _returnList;\r
- }\r
-\r
- /*\r
- * parses the files in the uploaded media and creates media Entity's out of\r
- * them. Produces them if the "produce" argument is true. The "publish"\r
- * parameter determines if it should publish per default in the case where no\r
- * is_published parameter (from the upload form) is supplied. (for backwards\r
- * compatibility.)\r
- */\r
- public void setFile(FilePart filePart, int fileNum, Map mediaValues) throws FileHandlerExc, FileHandlerFailure {\r
-\r
- String mediaId=null;\r
- MirMedia mediaHandler;\r
- Database mediaStorage = null;\r
-\r
- try {\r
- String fileName = filePart.getFileName();\r
-\r
- //get the content-type from what the client browser\r
- //sends us. (the "Oreilly method")\r
- String contentType = filePart.getContentType();\r
-\r
- //theLog.printInfo("FROM BROWSER: "+contentType);\r
-\r
- //if the client browser sent us unknown (text/plain is default)\r
- //or if we got application/octet-stream, it's possible that\r
- //the browser is in error, better check against the file extension\r
- if (contentType.equals("text/plain") ||\r
- contentType.equals("application/octet-stream")) {\r
- /**\r
- * Fallback to finding the mime-type through the standard ServletApi\r
- * ServletContext getMimeType() method.\r
- *\r
- * This is a way to get the content-type via the .extension,\r
- * we could maybe use a magic method as an additional method of\r
- * figuring out the content-type, by looking at the header (first\r
- * few bytes) of the file. (like the file(1) command). We could\r
- * also call the "file" command through Runtime. This is an\r
- * option that I almost prefer as it is already implemented and\r
- * exists with an up-to-date map on most modern Unix like systems.\r
- * I haven't found a really nice implementation of the magic method\r
- * in pure java yet.\r
- *\r
- * The first method we try thought is the "Oreilly method". It\r
- * relies on the content-type that the client browser sends and\r
- * that sometimes is application-octet stream with\r
- * broken/mis-configured browsers.\r
- *\r
- * The map file we use for the extensions is the standard web-app\r
- * deployment descriptor file (web.xml). See Mir's web.xml or see\r
- * your Servlet containers (most likely Tomcat) documentation.\r
- * So if you support a new media type you have to make sure that\r
- * it is in this file -mh\r
- */\r
- ServletContext ctx = MirPropertiesConfiguration.getContext();\r
- contentType = ctx.getMimeType(fileName);\r
- if (contentType==null)\r
- contentType = "text/plain"; // rfc1867 says this is the default\r
- }\r
- //theLog.printInfo("CONTENT TYPE IS: "+contentType);\r
-\r
- if (contentType.equals("text/plain") ||\r
- contentType.equals("application/octet-stream")) {\r
- _throwBadContentType(fileName, contentType);\r
- }\r
-\r
- // call the routines that escape html\r
- for (Iterator i=mediaValues.keySet().iterator(); i.hasNext(); ){\r
- String k=(String)i.next();\r
- String v=(String)mediaValues.get(k);\r
-\r
- if (k.equals("description")) {\r
- String tmp = StringUtil.deleteForbiddenTags(v);\r
- mediaValues.put(k,StringUtil.deleteHTMLTableTags(tmp));\r
- } else {\r
- //we don't want people fucking with the author/title, etc..\r
- mediaValues.put(k,StringUtil.removeHTMLTags(v));\r
- }\r
-\r
- }\r
-\r
- String mediaTitle = (String)mediaValues.get("media_title"+fileNum);\r
- if ( (mediaTitle == null) || (mediaTitle.length() == 0)) {\r
- // uncomment the next line and comment out the exception throw\r
- // if you'd rather just assign missing media titles automatically\r
- // mediaTitle="media item "+fileNum;\r
-// throw new FileHandlerUserException("Missing field: media title "+mediaTitle+fileNum);\r
- }\r
-\r
- // TODO: need to add all the extra fields that can be present in the\r
- // admin upload form. -mh\r
- mediaValues.put("title", mediaTitle);\r
- mediaValues.put("date", StringUtil.date2webdbDate(\r
- new GregorianCalendar()));\r
- mediaValues.put("to_publisher", _user);\r
- //mediaValues.put("to_media_folder", "7"); // op media_folder\r
- mediaValues.put("is_produced", "0");\r
-\r
- // icky backwards compatibility code -mh\r
- if (_publish == true) {\r
- mediaValues.put("is_published", "1");\r
- } else {\r
- if (!mediaValues.containsKey("is_published"))\r
- mediaValues.put("is_published", "0");\r
- }\r
-\r
- // @todo this should probably be moved to DatabaseMediaType -mh\r
- String[] cTypeSplit = StringUtil.split(contentType, "/");\r
- String wc = " mime_type LIKE '"+cTypeSplit[0]+"%'";\r
-\r
- DatabaseMediaType mediaTypeStor = DatabaseMediaType.getInstance();\r
- EntityList mediaTypesList = mediaTypeStor.selectByWhereClause(wc);\r
-\r
- String mediaTypeId = null;\r
-\r
- //if we didn't find an entry matching the\r
- //content-type int the table.\r
- if (mediaTypesList.size() == 0) {\r
- _throwBadContentType(fileName, contentType);\r
- }\r
-\r
- Entity mediaType = null;\r
- Entity mediaType2 = null;\r
-\r
- // find out if we an exact content-type match if so take it.\r
- // otherwise try to match majortype/*\r
- // @todo this should probably be moved to DatabaseMediaType -mh\r
- for(int j=0;j<mediaTypesList.size();j++) {\r
- if(contentType.equals(\r
- mediaTypesList.elementAt(j).getValue("mime_type")))\r
- mediaType = mediaTypesList.elementAt(j);\r
- else if ((mediaTypesList.elementAt(j).getValue("mime_type")).equals(\r
- cTypeSplit[0]+"/*") )\r
- mediaType2= mediaTypesList.elementAt(j);\r
- }\r
-\r
- if ( (mediaType == null) && (mediaType2 == null) ) {\r
- _throwBadContentType(fileName, contentType);\r
- } else if( (mediaType == null) && (mediaType2 != null) ) {\r
- mediaType = mediaType2;\r
- }\r
-\r
- //get the class names from the media_type table.\r
- mediaTypeId = mediaType.getId();\r
- // ############### @todo: merge these and the getURL call into one\r
- // getURL helper call that just takes the Entity as a parameter\r
- // along with media_type\r
- try {\r
- mediaHandler = MediaHelper.getHandler(mediaType);\r
- mediaStorage = MediaHelper.getStorage(mediaType, "mircoders.storage.Database");\r
- }\r
- catch (Throwable e) {\r
- throw new FileHandlerFailure(e);\r
- }\r
- mediaValues.put("to_media_type",mediaTypeId);\r
-\r
- //load the classes via reflection\r
- String MediaId;\r
- Entity mediaEnt = null;\r
-\r
- try {\r
- mediaEnt = (Entity)mediaStorage.getEntityClass().newInstance();\r
- }\r
- catch (Throwable e) {\r
- throw new FileHandlerFailure("MediaRequest.setFile: "+e.toString(), e);\r
- }\r
-\r
- mediaEnt.setStorage(mediaStorage);\r
- mediaEnt.setValues(mediaValues);\r
- mediaId = mediaEnt.insert();\r
-\r
- //save and store the media data/metadata\r
- try {\r
- mediaHandler.set(filePart.getInputStream(), mediaEnt, mediaType);\r
- }\r
- catch (Throwable e) {\r
- throw new FileHandlerFailure("MediaRequest.setFile: "+e.toString(), e);\r
- }\r
-\r
- _returnList.add(mediaEnt);\r
- }\r
- catch (StorageObjectFailure e) {\r
- // first try to delete it.. don't catch exception as we've already..\r
- try {\r
- mediaStorage.delete(mediaId);\r
- }\r
- catch (Exception e2) {\r
- }\r
- throw new FileHandlerFailure("MediaRequest.setFile: "+e.toString(), e);\r
- }\r
- catch (Throwable e) {\r
- throw new FileHandlerFailure("MediaRequest.setFile: "+e.toString(), e);\r
- } //end try/catch block\r
-\r
- } // method setFile()\r
-\r
- private void _throwBadContentType (String fileName, String contentType) throws FileHandlerExc, FileHandlerFailure {\r
- try {\r
- throw new UnsupportedMediaFormatExc(\r
- "The file you uploaded is of the following mime-type: " + contentType +\r
- ", we do not support this mime-type. "\r
- + "Error One or more files of unrecognized type. Sorry");\r
- }\r
- catch (Throwable t) {\r
- throw new FileHandlerFailure(t);\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 the com.oreilly.servlet library, 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.util.GregorianCalendar;
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.servlet.ServletContext;
+
+import mir.config.MirPropertiesConfiguration;
+import mir.entity.Entity;
+import mir.entity.EntityList;
+import mir.log.LoggerWrapper;
+import mir.media.MediaHelper;
+import mir.media.MirMedia;
+import mir.misc.FileHandler;
+import mir.misc.StringUtil;
+import mir.storage.Database;
+import mir.storage.StorageObjectFailure;
+import mircoders.storage.DatabaseMediaType;
+
+import com.oreilly.servlet.multipart.FilePart;
+
+
+/*
+ * MediaRequest.java -
+ * Takes an HTTPServletRequest from a mutltipart form and finds the files
+ * uploaded via the com.oreilly.servlet.multipart package. Finally the
+ * appropriate media objects are set.
+ *
+ * @author mh
+ * @version $Id: MediaRequest.java,v 1.18 2003/03/09 19:14:21 idfx Exp $
+ *
+ */
+
+public class MediaRequest implements FileHandler
+{
+ private String _user;
+ private EntityList _returnList = new EntityList();
+ private boolean _publish;
+ private LoggerWrapper logger;
+
+ public MediaRequest(String user, boolean publish) {
+ _user = user;
+ _publish = publish;
+ logger = new LoggerWrapper("Media.Request");
+ }
+
+ public EntityList getEntityList() {
+ return _returnList;
+ }
+
+ /*
+ * parses the files in the uploaded media and creates media Entity's out of
+ * them. Produces them if the "produce" argument is true. The "publish"
+ * parameter determines if it should publish per default in the case where no
+ * is_published parameter (from the upload form) is supplied. (for backwards
+ * compatibility.)
+ */
+ public void setFile(FilePart filePart, int fileNum, Map mediaValues) throws FileHandlerExc, FileHandlerFailure {
+
+ String mediaId=null;
+ MirMedia mediaHandler;
+ Database mediaStorage = null;
+
+ try {
+ String fileName = filePart.getFileName();
+
+ //get the content-type from what the client browser
+ //sends us. (the "Oreilly method")
+ String contentType = filePart.getContentType();
+
+ //theLog.printInfo("FROM BROWSER: "+contentType);
+
+ //if the client browser sent us unknown (text/plain is default)
+ //or if we got application/octet-stream, it's possible that
+ //the browser is in error, better check against the file extension
+ if (contentType.equals("text/plain") ||
+ contentType.equals("application/octet-stream")) {
+ /**
+ * 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 = MirPropertiesConfiguration.getContext();
+ contentType = ctx.getMimeType(fileName);
+ if (contentType==null)
+ contentType = "text/plain"; // rfc1867 says this is the default
+ }
+ //theLog.printInfo("CONTENT TYPE IS: "+contentType);
+
+ if (contentType.equals("text/plain") ||
+ contentType.equals("application/octet-stream")) {
+ _throwBadContentType(fileName, contentType);
+ }
+
+ // call the routines that escape html
+ for (Iterator i=mediaValues.keySet().iterator(); i.hasNext(); ){
+ String k=(String)i.next();
+ String v=(String)mediaValues.get(k);
+
+ if (k.equals("description")) {
+ String tmp = StringUtil.deleteForbiddenTags(v);
+ mediaValues.put(k,StringUtil.deleteHTMLTableTags(tmp));
+ } else {
+ //we don't want people fucking with the author/title, etc..
+ mediaValues.put(k,StringUtil.removeHTMLTags(v));
+ }
+
+ }
+
+ String mediaTitle = (String)mediaValues.get("media_title"+fileNum);
+ if ( (mediaTitle == null) || (mediaTitle.length() == 0)) {
+ // uncomment the next line and comment out the exception throw
+ // if you'd rather just assign missing media titles automatically
+ // mediaTitle="media item "+fileNum;
+// throw new FileHandlerUserException("Missing field: media title "+mediaTitle+fileNum);
+ }
+
+ // TODO: need to add all the extra fields that can be present in the
+ // admin upload form. -mh
+ mediaValues.put("title", mediaTitle);
+ mediaValues.put("date", StringUtil.date2webdbDate(
+ new GregorianCalendar()));
+ mediaValues.put("to_publisher", _user);
+ //mediaValues.put("to_media_folder", "7"); // op media_folder
+ mediaValues.put("is_produced", "0");
+
+ // icky backwards compatibility code -mh
+ if (_publish == true) {
+ mediaValues.put("is_published", "1");
+ } else {
+ if (!mediaValues.containsKey("is_published"))
+ mediaValues.put("is_published", "0");
+ }
+
+ // @todo this should probably be moved to DatabaseMediaType -mh
+ String[] cTypeSplit = StringUtil.split(contentType, "/");
+ String wc = " mime_type LIKE '"+cTypeSplit[0]+"%'";
+
+ DatabaseMediaType mediaTypeStor = DatabaseMediaType.getInstance();
+ EntityList mediaTypesList = mediaTypeStor.selectByWhereClause(wc);
+
+ String mediaTypeId = null;
+
+ //if we didn't find an entry matching the
+ //content-type int the table.
+ if (mediaTypesList.size() == 0) {
+ _throwBadContentType(fileName, contentType);
+ }
+
+ 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) ) {
+ _throwBadContentType(fileName, contentType);
+ } else if( (mediaType == null) && (mediaType2 != null) ) {
+ mediaType = mediaType2;
+ }
+
+ //get the class names from the media_type table.
+ mediaTypeId = mediaType.getId();
+ // ############### @todo: merge these and the getURL call into one
+ // getURL helper call that just takes the Entity as a parameter
+ // along with media_type
+ try {
+ mediaHandler = MediaHelper.getHandler(mediaType);
+ mediaStorage = MediaHelper.getStorage(mediaType, "mircoders.storage.Database");
+ }
+ catch (Throwable e) {
+ throw new FileHandlerFailure(e);
+ }
+ mediaValues.put("to_media_type",mediaTypeId);
+
+ //load the classes via reflection
+ String MediaId;
+ Entity mediaEnt = null;
+
+ try {
+ mediaEnt = (Entity)mediaStorage.getEntityClass().newInstance();
+ }
+ catch (Throwable e) {
+ throw new FileHandlerFailure("MediaRequest.setFile: "+e.toString(), e);
+ }
+
+ mediaEnt.setStorage(mediaStorage);
+ mediaEnt.setValues(mediaValues);
+ mediaId = mediaEnt.insert();
+
+ //save and store the media data/metadata
+ try {
+ mediaHandler.set(filePart.getInputStream(), mediaEnt, mediaType);
+ }
+ catch (Throwable e) {
+ throw new FileHandlerFailure("MediaRequest.setFile: "+e.toString(), e);
+ }
+
+ _returnList.add(mediaEnt);
+ }
+ catch (StorageObjectFailure e) {
+ // first try to delete it.. don't catch exception as we've already..
+ try {
+ mediaStorage.delete(mediaId);
+ }
+ catch (Exception e2) {
+ }
+ throw new FileHandlerFailure("MediaRequest.setFile: "+e.toString(), e);
+ }
+ catch (Throwable e) {
+ throw new FileHandlerFailure("MediaRequest.setFile: "+e.toString(), e);
+ } //end try/catch block
+
+ } // method setFile()
+
+ private void _throwBadContentType (String fileName, String contentType) throws FileHandlerExc, FileHandlerFailure {
+ try {
+ throw new UnsupportedMediaFormatExc(
+ "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");
+ }
+ catch (Throwable t) {
+ throw new FileHandlerFailure(t);
+ }
+ }
+
+}
+