2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with the com.oreilly.servlet library, any library
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24 * the above that use the same license as the above), and distribute linked
25 * combinations including the two. You must obey the GNU General Public
26 * License in all respects for all of the code used other than the above
27 * mentioned libraries. If you modify this file, you may extend this exception
28 * to your version of the file, but you are not obligated to do so. If you do
29 * not wish to do so, delete this exception statement from your version.
32 package mircoders.media;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.ServletContext;
40 import com.oreilly.servlet.multipart.FilePart;
42 import mircoders.storage.DatabaseMediaType;
43 import mircoders.producer.ProducerMedia;
44 import mir.storage.StorageObjectException;
45 import mir.storage.Database;
46 import mir.module.ModuleException;
53 * Takes an HTTPServletRequest from a mutltipart form and finds the files
54 * uploaded via the com.oreilly.servlet.multipart package. Finally the
55 * appropriate media objects are set.
58 * @version $Id: MediaRequest.java,v 1.1.2.5 2002/12/13 05:52:24 mh Exp $
62 public class MediaRequest implements FileHandler
66 EntityList _returnList = new EntityList();
67 boolean _produce, _publish;
69 public MediaRequest(String user, boolean produce, boolean publish) {
75 public EntityList getEntityList() {
80 * parses the files in the uploaded media and creates media Entity's out of
81 * them. Produces them if the "produce" argument is true. The "publish"
82 * parameter determines if it should publish per default in the case where no
83 * is_published parameter (from the upload form) is supplied. (for backwards
86 public void setFile(FilePart filePart, int fileNum, HashMap mediaValues)
87 throws FileHandlerException, FileHandlerUserException {
90 MirMedia mediaHandler;
91 Database mediaStorage = null;
92 ProducerMedia mediaProducer = null;
95 String fileName = filePart.getFileName();
97 //get the content-type from what the client browser
98 //sends us. (the "Oreilly method")
99 String contentType = filePart.getContentType();
101 //theLog.printInfo("FROM BROWSER: "+contentType);
103 //if the client browser sent us unknown (text/plain is default)
104 //or if we got application/octet-stream, it's possible that
105 //the browser is in error, better check against the file extension
106 if (contentType.equals("text/plain") ||
107 contentType.equals("application/octet-stream")) {
109 * Fallback to finding the mime-type through the standard ServletApi
110 * ServletContext getMimeType() method.
112 * This is a way to get the content-type via the .extension,
113 * we could maybe use a magic method as an additional method of
114 * figuring out the content-type, by looking at the header (first
115 * few bytes) of the file. (like the file(1) command). We could
116 * also call the "file" command through Runtime. This is an
117 * option that I almost prefer as it is already implemented and
118 * exists with an up-to-date map on most modern Unix like systems.
119 * I haven't found a really nice implementation of the magic method
122 * The first method we try thought is the "Oreilly method". It
123 * relies on the content-type that the client browser sends and
124 * that sometimes is application-octet stream with
125 * broken/mis-configured browsers.
127 * The map file we use for the extensions is the standard web-app
128 * deployment descriptor file (web.xml). See Mir's web.xml or see
129 * your Servlet containers (most likely Tomcat) documentation.
130 * So if you support a new media type you have to make sure that
131 * it is in this file -mh
134 (ServletContext)MirConfig.getPropAsObject("ServletContext");
135 contentType = ctx.getMimeType(fileName);
136 if (contentType==null)
137 contentType = "text/plain"; // rfc1867 says this is the default
139 //theLog.printInfo("CONTENT TYPE IS: "+contentType);
141 if (contentType.equals("text/plain") ||
142 contentType.equals("application/octet-stream")) {
143 _throwBadContentType(fileName, contentType);
146 // call the routines that escape html
147 for (Iterator i=mediaValues.keySet().iterator(); i.hasNext(); ){
148 String k=(String)i.next();
149 String v=(String)mediaValues.get(k);
151 if (k.equals("description")) {
152 String tmp = StringUtil.deleteForbiddenTags(v);
153 mediaValues.put(k,StringUtil.deleteHTMLTableTags(tmp));
155 //we don't want people fucking with the author/title, etc..
156 mediaValues.put(k,StringUtil.removeHTMLTags(v));
161 String mediaTitle = (String)mediaValues.get("media_title"+fileNum);
162 if ( (mediaTitle == null) || (mediaTitle.length() == 0))
163 throw new FileHandlerUserException("Missing field: media title "+mediaTitle+fileNum);
165 // TODO: need to add all the extra fields that can be present in the
166 // admin upload form. -mh
167 mediaValues.put("title", mediaTitle);
168 mediaValues.put("date", StringUtil.date2webdbDate(
169 new GregorianCalendar()));
170 mediaValues.put("to_publisher", _user);
171 //mediaValues.put("to_media_folder", "7"); // op media_folder
172 mediaValues.put("is_produced", "0");
174 // icky backwards compatibility code -mh
175 if (_publish == true) {
176 mediaValues.put("is_published", "1");
178 if (!mediaValues.containsKey("is_published"))
179 mediaValues.put("is_published", "0");
182 // @todo this should probably be moved to DatabaseMediaType -mh
183 String[] cTypeSplit = StringUtil.split(contentType, "/");
184 String wc = " mime_type LIKE '"+cTypeSplit[0]+"%'";
186 DatabaseMediaType mediaTypeStor = DatabaseMediaType.getInstance();
187 EntityList mediaTypesList = mediaTypeStor.selectByWhereClause(wc);
189 String mediaTypeId = null;
191 //if we didn't find an entry matching the
192 //content-type int the table.
193 if (mediaTypesList.size() == 0) {
194 _throwBadContentType(fileName, contentType);
197 Entity mediaType = null;
198 Entity mediaType2 = null;
200 // find out if we an exact content-type match if so take it.
201 // otherwise try to match majortype/*
202 // @todo this should probably be moved to DatabaseMediaType -mh
203 for(int j=0;j<mediaTypesList.size();j++) {
204 if(contentType.equals(
205 mediaTypesList.elementAt(j).getValue("mime_type")))
206 mediaType = mediaTypesList.elementAt(j);
207 else if ((mediaTypesList.elementAt(j).getValue("mime_type")).equals(
208 cTypeSplit[0]+"/*") )
209 mediaType2= mediaTypesList.elementAt(j);
212 if ( (mediaType == null) && (mediaType2 == null) ) {
213 _throwBadContentType(fileName, contentType);
214 } else if( (mediaType == null) && (mediaType2 != null) ) {
215 mediaType = mediaType2;
218 //get the class names from the media_type table.
219 mediaTypeId = mediaType.getId();
220 // ############### @todo: merge these and the getURL call into one
221 // getURL helper call that just takes the Entity as a parameter
222 // along with media_type
224 mediaHandler = MediaHelper.getHandler(mediaType);
225 mediaStorage = MediaHelper.getStorage(mediaType,
226 "mircoders.storage.Database");
227 } catch (MirMediaException e) {
228 throw new FileHandlerException (e.getMsg());
230 mediaValues.put("to_media_type",mediaTypeId);
232 //load the classes via reflection
234 Entity mediaEnt = null;
236 mediaEnt = (Entity)mediaStorage.getEntityClass().newInstance();
237 if (_produce == true) {
238 Class prodCls = Class.forName("mircoders.producer.Producer"+
239 mediaType.getValue("tablename"));
240 mediaProducer = (ProducerMedia)prodCls.newInstance();
242 } catch (Exception e) {
243 throw new FileHandlerException("Error in MediaRequest: "+e.toString());
246 mediaEnt.setStorage(mediaStorage);
247 mediaEnt.setValues(mediaValues);
248 mediaId = mediaEnt.insert();
250 //save and store the media data/metadata
252 mediaHandler.set(filePart.getInputStream(), mediaEnt, mediaType);
253 } catch (MirMediaException e) {
254 throw new FileHandlerException(e.getMsg());
257 if (_produce == true )
258 mediaProducer.handle(null, null, false, false, mediaId);
259 } catch (ModuleException e) {
260 // first try to delete it.. don't catch exception as we've already..
261 try { mediaStorage.delete(mediaId); } catch (Exception e2) {}
262 throw new FileHandlerException("error in MediaRequest: "+e.toString());
265 _returnList.add(mediaEnt);
266 } catch (StorageObjectException e) {
267 // first try to delete it.. don't catch exception as we've already..
268 try { mediaStorage.delete(mediaId); } catch (Exception e2) {}
269 throw new FileHandlerException("error in MediaRequest: "+e.toString());
270 } //end try/catch block
272 } // method setFile()
274 private void _throwBadContentType (String fileName, String contentType)
275 throws FileHandlerUserException {
277 //theLog.printDebugInfo("Wrong file type uploaded!: " + fileName+" "
279 throw new FileHandlerUserException("The file you uploaded is of the "
280 +"following mime-type: "+contentType
281 +", we do not support this mime-type. "
282 +"Error One or more files of unrecognized type. Sorry");