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.9 2002/12/01 15:05:51 zapata 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 String mediaTitle = (String)mediaValues.get("media_title"+fileNum);
147 if ( (mediaTitle == null) || (mediaTitle.length() == 0)) {
148 // uncomment the next line and comment out the exception throw
149 // if you'd rather just assign missing media titles automatically
150 // mediaTitle="media item "+fileNum;
151 throw new FileHandlerUserException("Missing field: media title "+mediaTitle+fileNum);
154 // TODO: need to add all the extra fields that can be present in the
155 // admin upload form. -mh
156 mediaValues.put("title", mediaTitle);
157 mediaValues.put("date", StringUtil.date2webdbDate(
158 new GregorianCalendar()));
159 mediaValues.put("to_publisher", _user);
160 //mediaValues.put("to_media_folder", "7"); // op media_folder
161 mediaValues.put("is_produced", "0");
163 // icky backwards compatibility code -mh
164 if (_publish == true) {
165 mediaValues.put("is_published", "1");
167 if (!mediaValues.containsKey("is_published"))
168 mediaValues.put("is_published", "0");
171 // @todo this should probably be moved to DatabaseMediaType -mh
172 String[] cTypeSplit = StringUtil.split(contentType, "/");
173 String wc = " mime_type LIKE '"+cTypeSplit[0]+"%'";
175 DatabaseMediaType mediaTypeStor = DatabaseMediaType.getInstance();
176 EntityList mediaTypesList = mediaTypeStor.selectByWhereClause(wc);
178 String mediaTypeId = null;
180 //if we didn't find an entry matching the
181 //content-type int the table.
182 if (mediaTypesList.size() == 0) {
183 _throwBadContentType(fileName, contentType);
186 Entity mediaType = null;
187 Entity mediaType2 = null;
189 // find out if we an exact content-type match if so take it.
190 // otherwise try to match majortype/*
191 // @todo this should probably be moved to DatabaseMediaType -mh
192 for(int j=0;j<mediaTypesList.size();j++) {
193 if(contentType.equals(
194 mediaTypesList.elementAt(j).getValue("mime_type")))
195 mediaType = mediaTypesList.elementAt(j);
196 else if ((mediaTypesList.elementAt(j).getValue("mime_type")).equals(
197 cTypeSplit[0]+"/*") )
198 mediaType2= mediaTypesList.elementAt(j);
201 if ( (mediaType == null) && (mediaType2 == null) ) {
202 _throwBadContentType(fileName, contentType);
203 } else if( (mediaType == null) && (mediaType2 != null) ) {
204 mediaType = mediaType2;
207 //get the class names from the media_type table.
208 mediaTypeId = mediaType.getId();
209 // ############### @todo: merge these and the getURL call into one
210 // getURL helper call that just takes the Entity as a parameter
211 // along with media_type
213 mediaHandler = MediaHelper.getHandler(mediaType);
214 mediaStorage = MediaHelper.getStorage(mediaType,
215 "mircoders.storage.Database");
217 catch (MirMediaException e) {
218 throw new FileHandlerException (e.getMessage());
220 mediaValues.put("to_media_type",mediaTypeId);
222 //load the classes via reflection
224 Entity mediaEnt = null;
226 mediaEnt = (Entity)mediaStorage.getEntityClass().newInstance();
227 if (_produce == true) {
228 Class prodCls = Class.forName("mircoders.producer.Producer"+
229 mediaType.getValue("tablename"));
230 mediaProducer = (ProducerMedia)prodCls.newInstance();
232 } catch (Exception e) {
233 throw new FileHandlerException("Error in MediaRequest: "+e.toString());
236 mediaEnt.setStorage(mediaStorage);
237 mediaEnt.setValues(mediaValues);
238 mediaId = mediaEnt.insert();
240 //save and store the media data/metadata
242 mediaHandler.set(filePart.getInputStream(), mediaEnt, mediaType);
244 catch (MirMediaException e) {
245 throw new FileHandlerException(e.getMessage());
248 if (_produce == true )
249 mediaProducer.handle(null, null, false, false, mediaId);
250 } catch (ModuleException e) {
251 // first try to delete it.. don't catch exception as we've already..
252 try { mediaStorage.delete(mediaId); } catch (Exception e2) {}
253 throw new FileHandlerException("error in MediaRequest: "+e.toString());
256 _returnList.add(mediaEnt);
258 catch (StorageObjectException e) {
259 // first try to delete it.. don't catch exception as we've already..
260 try { mediaStorage.delete(mediaId); } catch (Exception e2) {}
261 throw new FileHandlerException("error in MediaRequest: "+e.toString());
262 } //end try/catch block
264 } // method setFile()
266 private void _throwBadContentType (String fileName, String contentType)
267 throws FileHandlerUserException {
269 //theLog.printDebugInfo("Wrong file type uploaded!: " + fileName+" "
271 throw new FileHandlerUserException("The file you uploaded is of the "
272 +"following mime-type: "+contentType
273 +", we do not support this mime-type. "
274 +"Error One or more files of unrecognized type. Sorry");