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.servlet;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.net.URLEncoder;
37 import java.util.HashMap;
39 import javax.servlet.ServletContext;
40 import javax.servlet.ServletOutputStream;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.http.HttpServletResponse;
43 import javax.servlet.http.HttpSession;
45 import mir.config.MirPropertiesConfiguration;
46 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
47 import mir.entity.Entity;
48 import mir.entity.EntityList;
49 import mir.log.LoggerWrapper;
50 import mir.media.MediaHelper;
51 import mir.media.MirMedia;
52 import mir.misc.FileHandler;
53 import mir.misc.FileHandlerException;
54 import mir.misc.FileHandlerUserException;
55 import mir.misc.WebdbMultipartRequest;
56 import mir.module.ModuleException;
57 import mir.servlet.ServletModule;
58 import mir.servlet.ServletModuleException;
59 import mir.servlet.ServletModuleUserException;
60 import mir.storage.StorageObjectFailure;
61 import mircoders.entity.EntityUploadedMedia;
62 import mircoders.entity.EntityUsers;
63 import mircoders.media.MediaRequest;
64 import mircoders.storage.DatabaseMediafolder;
65 import freemarker.template.SimpleHash;
66 import freemarker.template.SimpleList;
69 * ServletModuleBilder -
70 * liefert HTML fuer Bilder
72 * @version $Id: ServletModuleUploadedMedia.java,v 1.16 2003/02/23 05:00:15 zapata Exp $
73 * @author RK, the mir-coders group
76 public abstract class ServletModuleUploadedMedia
77 extends mir.servlet.ServletModule {
79 //private static DatabaseRights dbRights;
81 public static ServletModule getInstance() {
85 public ServletModuleUploadedMedia() {
87 logger = new LoggerWrapper("ServletModule.UploadedMedia");
90 public void insert(HttpServletRequest req, HttpServletResponse res)
91 throws ServletModuleException, ServletModuleUserException {
93 EntityUsers user = _getUser(req);
94 MediaRequest mediaReq = new MediaRequest(user.getId(), false);
95 WebdbMultipartRequest mp = new WebdbMultipartRequest(req, (FileHandler)mediaReq);
96 EntityList mediaList = mediaReq.getEntityList();
98 SimpleHash mergeData = new SimpleHash();
99 SimpleHash popups = new SimpleHash();
100 mergeData.put("contentlist", mediaList);
101 if (mediaList.getOrder() != null) {
102 mergeData.put("order", mediaList.getOrder());
103 mergeData.put("order_encoded", URLEncoder.encode(mediaList.getOrder()));
105 mergeData.put("count", (new Integer(mediaList.getCount())).toString());
106 mergeData.put("from", (new Integer(mediaList.getFrom())).toString());
107 mergeData.put("to", (new Integer(mediaList.getTo())).toString());
108 if (mediaList.hasNextBatch())
109 mergeData.put("next", (new Integer(mediaList.getNextBatch())).toString());
110 if (mediaList.hasPrevBatch())
111 mergeData.put("prev", (new Integer(mediaList.getPrevBatch())).toString());
113 popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
115 deliver(req, res, mergeData, popups, templateListString);
116 } catch (FileHandlerUserException e) {
117 logger.error("ServletModuleUploadedMedia.insert: " + e.getMessage());
118 throw new ServletModuleUserException(e.getMessage());
119 } catch (FileHandlerException e) {
120 throw new ServletModuleException(
121 "upload -- media handling exception " + e.toString());
122 } catch (StorageObjectFailure e) {
123 throw new ServletModuleException("upload -- storageobjectexception "
125 } catch (IOException e) {
126 throw new ServletModuleException("upload -- ioexception " + e.toString());
127 } catch (PropertiesConfigExc e) {
128 throw new ServletModuleException("upload -- configexception " + e.toString());
132 public void update(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException {
135 EntityUsers user = _getUser(req);
136 WebdbMultipartRequest mp = new WebdbMultipartRequest(req, null);
137 HashMap parameters = mp.getParameters();
139 parameters.put("to_publisher", user.getId());
140 parameters.put("is_produced", "0");
141 if (!parameters.containsKey("is_published"))
142 parameters.put("is_published", "0");
144 String id = mainModule.set(parameters);
145 logger.debug("update: media ID = " + id);
148 catch (IOException e) {
149 throw new ServletModuleException("upload -- ioexception " + e.toString());
151 catch (ModuleException e) {
152 throw new ServletModuleException("upload -- moduleexception " + e.toString());
154 catch (Exception e) {
155 throw new ServletModuleException("upload -- exception " + e.toString());
161 public void list(HttpServletRequest req, HttpServletResponse res)
162 throws ServletModuleException {
163 // Parameter auswerten
164 SimpleHash mergeData = new SimpleHash();
165 SimpleHash popups = new SimpleHash();
167 String query_text = req.getParameter("query_text");
168 mergeData.put("query_text", query_text);
169 if (query_text != null) mergeData.put("query_text_encoded", URLEncoder.encode(query_text));
170 String query_field = req.getParameter("query_field");
171 mergeData.put("query_field", query_field);
172 String query_is_published = req.getParameter("query_is_published");
173 mergeData.put("query_is_published", query_is_published);
174 String query_media_folder = req.getParameter("query_media_folder");
175 mergeData.put("query_media_folder", query_media_folder);
176 String offset = req.getParameter("offset");
177 if (offset == null || offset.equals("")) offset = "0";
178 mergeData.put("offset", offset);
180 String order = req.getParameter("order");
181 if (order == null || order.equals("")) order = "webdb_lastchange desc";
183 // if in connection mode to content
184 String cid = req.getParameter("cid");
185 mergeData.put("cid", cid);
189 String whereClause = "";
190 boolean isFirst = true;
191 if (query_text != null && !query_text.equalsIgnoreCase("")) {
192 whereClause += "lower(" + query_field + ") like lower('%" + query_text + "%')";
195 if (query_is_published != null && !query_is_published.equals("")) {
196 if (isFirst == false) whereClause += " and ";
197 whereClause += "is_published='" + query_is_published + "'";
200 if (query_media_folder != null && !query_media_folder.equals("")) {
201 if (isFirst == false) whereClause += " and ";
202 whereClause += "to_media_folder='" + query_media_folder + "'";
204 //theLog.printDebugInfo("sql-whereclause: " + whereClause + " order: " + order + " offset: " + offset);
208 if (query_text != null || query_is_published != null || query_media_folder != null) {
209 EntityList theList = mainModule.getByWhereClause(whereClause, order, (new Integer(offset)).intValue(), 10);
210 if (theList != null) {
211 mergeData.put("contentlist", theList);
212 if (theList.getOrder() != null) {
213 mergeData.put("order", theList.getOrder());
214 mergeData.put("order_encoded", URLEncoder.encode(theList.getOrder()));
216 mergeData.put("count", (new Integer(theList.getCount())).toString());
217 mergeData.put("from", (new Integer(theList.getFrom())).toString());
218 mergeData.put("to", (new Integer(theList.getTo())).toString());
219 if (theList.hasNextBatch())
220 mergeData.put("next", (new Integer(theList.getNextBatch())).toString());
221 if (theList.hasPrevBatch())
222 mergeData.put("prev", (new Integer(theList.getPrevBatch())).toString());
225 popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
227 deliver(req, res, mergeData, popups, templateListString);
229 catch (ModuleException e) {
230 throw new ServletModuleException(e.toString());
232 catch (Exception e) {
233 throw new ServletModuleException(e.toString());
238 public void add(HttpServletRequest req, HttpServletResponse res)
239 throws ServletModuleException {
241 SimpleHash mergeData = new SimpleHash();
242 mergeData.put("new", "1");
243 SimpleHash popups = new SimpleHash();
244 popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
245 String maxMedia = MirPropertiesConfiguration.instance().getString("ServletModule.OpenIndy.MaxMediaUploadItems");
246 String numOfMedia = req.getParameter("medianum");
247 if(numOfMedia==null||numOfMedia.equals("")){
249 } else if(Integer.parseInt(numOfMedia) > Integer.parseInt(maxMedia)) {
250 numOfMedia = maxMedia;
253 int mediaNum = Integer.parseInt(numOfMedia);
254 SimpleList mediaFields = new SimpleList();
255 for(int i =0; i<mediaNum;i++){
256 Integer mNum = new Integer(i+1);
257 mediaFields.add(mNum.toString());
259 mergeData.put("medianum",numOfMedia);
260 mergeData.put("mediafields",mediaFields);
261 deliver(req, res, mergeData, popups, templateObjektString);
262 } catch (Exception e) {
263 throw new ServletModuleException(e.toString());
267 public void edit(HttpServletRequest req, HttpServletResponse res)
268 throws ServletModuleException {
269 String idParam = req.getParameter("id");
270 _edit(idParam, req, res);
273 private void _edit(String idParam, HttpServletRequest req, HttpServletResponse res)
274 throws ServletModuleException {
275 if (idParam != null && !idParam.equals("")) {
277 SimpleHash popups = new SimpleHash();
278 popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
279 deliver(req, res, mainModule.getById(idParam), popups,
280 templateObjektString);
282 catch (ModuleException e) {
283 throw new ServletModuleException(e.toString());
285 catch (StorageObjectFailure e) {
286 throw new ServletModuleException(e.toString());
290 throw new ServletModuleException("ServletmoduleUploadedMedia :: _edit without id");
295 /** @todo should be in ServletModule.java */
296 private EntityUsers _getUser(HttpServletRequest req) {
297 HttpSession session = req.getSession(false);
298 return (EntityUsers) session.getAttribute("login.uid");
301 public void getMedia(HttpServletRequest req, HttpServletResponse res)
302 throws ServletModuleException
304 String idParam = req.getParameter("id");
305 if (idParam!=null && !idParam.equals("")) {
307 EntityUploadedMedia ent = (EntityUploadedMedia)mainModule.getById(idParam);
308 Entity mediaType = ent.getMediaType();
309 MirMedia mediaHandler;
311 ServletContext ctx = MirPropertiesConfiguration.getContext();
312 String fName = ent.getId()+"."+mediaType.getValue("name");
314 mediaHandler = MediaHelper.getHandler(mediaType);
315 InputStream in = mediaHandler.getMedia(ent, mediaType);
317 res.setContentType(ctx.getMimeType(fName));
318 //important that before calling this res.getWriter was not called first
319 ServletOutputStream out = res.getOutputStream();
322 byte[] buf = new byte[8 * 1024];
323 while((read = in.read(buf)) != -1) {
324 out.write(buf, 0, read);
330 catch (IOException e) {
331 throw new ServletModuleException(e.toString());
333 catch (ModuleException e) {
334 throw new ServletModuleException(e.toString());
336 catch (Exception e) {
337 throw new ServletModuleException(e.toString());
340 else logger.error("id not specified.");
341 // no exception allowed
344 public void getIcon(HttpServletRequest req, HttpServletResponse res)
345 throws ServletModuleException
347 String idParam = req.getParameter("id");
348 if (idParam!=null && !idParam.equals("")) {
350 EntityUploadedMedia ent = (EntityUploadedMedia)mainModule.getById(idParam);
351 Entity mediaType = ent.getMediaType();
352 MirMedia mediaHandler;
354 ServletContext ctx = MirPropertiesConfiguration.getContext();
355 String fName = ent.getId()+"."+mediaType.getValue("name");
357 mediaHandler = MediaHelper.getHandler(mediaType);
358 InputStream in = mediaHandler.getIcon(ent);
360 res.setContentType(ctx.getMimeType(fName));
361 //important that before calling this res.getWriter was not called first
362 ServletOutputStream out = res.getOutputStream();
365 byte[] buf = new byte[8 * 1024];
366 while((read = in.read(buf)) != -1) {
367 out.write(buf, 0, read);
373 catch (IOException e) {
374 throw new ServletModuleException(e.toString());
376 catch (ModuleException e) {
377 throw new ServletModuleException(e.toString());
379 catch (Exception e) {
380 throw new ServletModuleException(e.toString());
383 else logger.error("getIcon: id not specified.");
384 // no exception allowed