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;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.ServletContext;
39 import mircoders.storage.DatabaseMediaType;
40 import mircoders.producer.ProducerMedia;
41 import mir.storage.StorageObjectException;
42 import mir.storage.Database;
43 import mir.module.ModuleException;
50 * Takes an HTTPServletRequest from a mutltipart form and finds the files
51 * uploaded via the com.oreilly.servlet.multipart package. Finally the
52 * appropriate media objects are set.
54 * @author $Author: mh $
55 * @version $Revision: 1.1.2.1 $
57 * $Log: MediaRequest.java,v $
58 * Revision 1.1.2.1 2002/09/01 21:31:43 mh
61 * Revision 1.1 2002/07/21 22:38:39 mh
62 * parses a multipart request's files and makes media Entity's out of them. Basically the old code from insposting() in ServletModuleOpenIndy
67 public class MediaRequest
70 WebdbMultipartRequest _mp;
73 public MediaRequest(WebdbMultipartRequest mPreq, String user) {
79 * parses the files in the uploaded media and creates media Entity's out of
80 * them. Produces them if the "produce" argument is true. The "publish"
81 * parameter determines if it should publish per default in the case where no
82 * is_published parameter (from the upload form) is supplied. (for backwards
85 public EntityList getMedia(boolean produce, boolean publish)
86 throws MirMediaException,
87 MirMediaUserException {
89 HashMap mediaValues = _mp.getParameters();
90 EntityList returnList = new EntityList();
91 MirMedia mediaHandler;
92 Database mediaStorage = null;
93 ProducerMedia mediaProducer = null;
96 for(Iterator it = _mp.requestList.iterator(); it.hasNext();){
98 MpRequest mpReq = (MpRequest)it.next();
99 String fileName = mpReq.getFilename();
101 //get the content-type from what the client browser
102 //sends us. (the "Oreilly method")
103 String contentType = mpReq.getContentType();
105 //theLog.printInfo("FROM BROWSER: "+contentType);
107 //if the client browser sent us unknown (text/plain is default)
108 //or if we got application/octet-stream, it's possible that
109 //the browser is in error, better check against the file extension
110 if (contentType.equals("text/plain") ||
111 contentType.equals("application/octet-stream")) {
113 * Fallback to finding the mime-type through the standard ServletApi
114 * ServletContext getMimeType() method.
116 * This is a way to get the content-type via the .extension,
117 * we could maybe use a magic method as an additional method of
118 * figuring out the content-type, by looking at the header (first
119 * few bytes) of the file. (like the file(1) command). We could
120 * also call the "file" command through Runtime. This is an
121 * option that I almost prefer as it is already implemented and
122 * exists with an up-to-date map on most modern Unix like systems.
123 * I haven't found a really nice implementation of the magic method
126 * The first method we try thought is the "Oreilly method". It
127 * relies on the content-type that the client browser sends and
128 * that sometimes is application-octet stream with
129 * broken/mis-configured browsers.
131 * The map file we use for the extensions is the standard web-app
132 * deployment descriptor file (web.xml). See Mir's web.xml or see
133 * your Servlet containers (most likely Tomcat) documentation.
134 * So if you support a new media type you have to make sure that
135 * it is in this file -mh
138 (ServletContext)MirConfig.getPropAsObject("ServletContext");
139 contentType = ctx.getMimeType(fileName);
140 if (contentType==null)
141 contentType = "text/plain"; // rfc1867 says this is the default
143 //theLog.printInfo("CONTENT TYPE IS: "+contentType);
145 if (contentType.equals("text/plain") ||
146 contentType.equals("application/octet-stream")) {
147 _throwBadContentType(fileName, contentType);
150 String mediaTitle = (String)mediaValues.get("media_title"+i);
152 if ( (mediaTitle == null) || (mediaTitle.length() == 0))
153 throw new MirMediaUserException("Missing field: media title");
155 // TODO: need to add all the extra fields that can be present in the
156 // admin upload form. -mh
157 mediaValues.put("title", mediaTitle);
158 mediaValues.put("date", StringUtil.date2webdbDate(
159 new GregorianCalendar()));
160 mediaValues.put("to_publisher", _user);
161 //mediaValues.put("to_media_folder", "7"); // op media_folder
162 mediaValues.put("is_produced", "0");
164 // icky backwards compatibility code -mh
165 if (publish == true) {
166 mediaValues.put("is_published", "1");
168 if (!mediaValues.containsKey("is_published"))
169 mediaValues.put("is_published", "0");
172 // @todo this should probably be moved to DatabaseMediaType -mh
173 String[] cTypeSplit = StringUtil.split(contentType, "/");
174 String wc = " mime_type LIKE '"+cTypeSplit[0]+"%'";
176 DatabaseMediaType mediaTypeStor = DatabaseMediaType.getInstance();
177 EntityList mediaTypesList = mediaTypeStor.selectByWhereClause(wc);
179 String mediaTypeId = null;
181 //if we didn't find an entry matching the
182 //content-type int the table.
183 if (mediaTypesList.size() == 0) {
184 _throwBadContentType(fileName, contentType);
187 Entity mediaType = null;
188 Entity mediaType2 = null;
190 // find out if we an exact content-type match if so take it.
191 // otherwise try to match majortype/*
192 // @todo this should probably be moved to DatabaseMediaType -mh
193 for(int j=0;j<mediaTypesList.size();j++) {
194 if(contentType.equals(
195 mediaTypesList.elementAt(j).getValue("mime_type")))
196 mediaType = mediaTypesList.elementAt(j);
197 else if ((mediaTypesList.elementAt(j).getValue("mime_type")).equals(
198 cTypeSplit[0]+"/*") )
199 mediaType2= mediaTypesList.elementAt(j);
202 if ( (mediaType == null) && (mediaType2 == null) ) {
203 _throwBadContentType(fileName, contentType);
205 else if( (mediaType == null) && (mediaType2 != null) )
206 mediaType = mediaType2;
208 //get the class names from the media_type table.
209 mediaTypeId = mediaType.getId();
210 // ############### @todo: merge these and the getURL call into one
211 // getURL helper call that just takes the Entity as a parameter
212 // along with media_type
213 mediaHandler = MediaHelper.getHandler(mediaType);
214 mediaStorage = MediaHelper.getStorage(mediaType,
215 "mircoders.storage.Database");
216 mediaValues.put("to_media_type",mediaTypeId);
218 //load the classes via reflection
220 Entity mediaEnt = null;
222 mediaEnt = (Entity)mediaStorage.getEntityClass().newInstance();
223 if (produce == true) {
224 Class prodCls = Class.forName("mircoders.producer.Producer"+
225 mediaType.getValue("tablename"));
226 mediaProducer = (ProducerMedia)prodCls.newInstance();
228 } catch (Exception e) {
229 throw new MirMediaException("Error in MediaRequest: "+e.toString());
232 mediaEnt.setStorage(mediaStorage);
233 mediaEnt.setValues(mediaValues);
234 mediaId = mediaEnt.insert();
236 //save and store the media data/metadata
237 mediaHandler.set(mpReq.getMedia(), mediaEnt,
240 if (produce == true )
241 mediaProducer.handle(null, null, false, false, mediaId);
242 } catch (ModuleException e) {
243 // first try to delete it.. don't catch exception as we've already..
244 try { mediaStorage.delete(mediaId); } catch (Exception e2) {}
245 throw new MirMediaException("error in MediaRequest: "+e.toString());
248 returnList.add(mediaEnt);
249 } catch (StorageObjectException e) {
250 // first try to delete it.. don't catch exception as we've already..
251 try { mediaStorage.delete(mediaId); } catch (Exception e2) {}
252 throw new MirMediaException("error in MediaRequest: "+e.toString());
253 } //end try/catch block
255 } //end for Iterator...
257 } // method getMedia()
259 private void _throwBadContentType (String fileName, String contentType)
260 throws MirMediaUserException {
262 //theLog.printDebugInfo("Wrong file type uploaded!: " + fileName+" "
264 throw new MirMediaUserException("The file you uploaded is of the "
265 +"following mime-type: "+contentType
266 +", we do not support this mime-type. "
267 +"Error One or more files of unrecognized type. Sorry");