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;
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.servlet.ServletModule;
57 import mir.servlet.ServletModuleExc;
58 import mir.servlet.ServletModuleFailure;
59 import mir.servlet.ServletModuleUserExc;
60 import mir.storage.StorageObjectFailure;
61 import mircoders.entity.EntityContent;
62 import mircoders.entity.EntityUploadedMedia;
63 import mircoders.entity.EntityUsers;
64 import mircoders.media.MediaRequest;
65 import mircoders.storage.DatabaseContent;
66 import mircoders.storage.DatabaseMediafolder;
67 import freemarker.template.SimpleHash;
68 import freemarker.template.SimpleList;
71 * ServletModuleBilder -
72 * liefert HTML fuer Bilder
74 * @version $Id: ServletModuleUploadedMedia.java,v 1.20 2003/03/08 17:18:19 idfx Exp $
75 * @author RK, the mir-coders group
78 public abstract class ServletModuleUploadedMedia
79 extends mir.servlet.ServletModule {
81 //private static DatabaseRights dbRights;
83 public static ServletModule getInstance() {
87 public ServletModuleUploadedMedia() {
89 logger = new LoggerWrapper("ServletModule.UploadedMedia");
92 public void insert(HttpServletRequest req, HttpServletResponse res)
93 throws ServletModuleExc, ServletModuleUserExc {
95 EntityUsers user = _getUser(req);
96 MediaRequest mediaReq = new MediaRequest(user.getId(), false);
97 WebdbMultipartRequest mp = new WebdbMultipartRequest(req, (FileHandler)mediaReq);
98 EntityList mediaList = mediaReq.getEntityList();
99 String articleid = (String) mp.getParameters().get("articleid");
101 if (articleid!=null) {
102 EntityContent entContent = (EntityContent) DatabaseContent.getInstance().selectById(articleid);
106 while (mediaList.hasNext()) {
107 entContent.attach( ( (EntityUploadedMedia) mediaList.next()).getId());
111 ((ServletModuleContent) ServletModuleContent.getInstance())._showObject(articleid, req, res);
116 SimpleHash mergeData = new SimpleHash();
117 SimpleHash popups = new SimpleHash();
118 mergeData.put("contentlist", mediaList);
119 if (mediaList.getOrder() != null) {
120 mergeData.put("order", mediaList.getOrder());
121 mergeData.put("order_encoded", URLEncoder.encode(mediaList.getOrder()));
123 mergeData.put("count", (new Integer(mediaList.getCount())).toString());
124 mergeData.put("from", (new Integer(mediaList.getFrom())).toString());
125 mergeData.put("to", (new Integer(mediaList.getTo())).toString());
126 if (mediaList.hasNextBatch())
127 mergeData.put("next", (new Integer(mediaList.getNextBatch())).toString());
128 if (mediaList.hasPrevBatch())
129 mergeData.put("prev", (new Integer(mediaList.getPrevBatch())).toString());
131 popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
133 deliver(req, res, mergeData, popups, templateListString);
135 catch (FileHandlerUserException e) {
136 logger.error("ServletModuleUploadedMedia.insert: " + e.getMessage());
137 throw new ServletModuleUserExc(e.getMessage());
139 catch (FileHandlerException e) {
140 throw new ServletModuleFailure("upload -- media handling exception " + e.toString(), e);
142 catch (StorageObjectFailure e) {
143 throw new ServletModuleFailure("upload -- storageobjectexception " + e.toString(), e);
145 catch (IOException e) {
146 throw new ServletModuleFailure("upload -- ioexception " + e.toString(), e);
148 catch (PropertiesConfigExc e) {
149 throw new ServletModuleFailure("upload -- configexception " + e.toString(), e);
151 catch (Throwable t) {
152 throw new ServletModuleFailure("upload -- exception " + t.toString(), t);
156 public void update(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc {
159 EntityUsers user = _getUser(req);
160 WebdbMultipartRequest mp = new WebdbMultipartRequest(req, null);
161 Map parameters = mp.getParameters();
163 parameters.put("to_publisher", user.getId());
164 parameters.put("is_produced", "0");
165 if (!parameters.containsKey("is_published"))
166 parameters.put("is_published", "0");
168 String id = mainModule.set(parameters);
169 logger.debug("update: media ID = " + id);
172 catch (Throwable e) {
173 throw new ServletModuleFailure("upload -- exception " + e.toString(), e);
179 public void list(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc {
180 // Parameter auswerten
181 SimpleHash mergeData = new SimpleHash();
182 SimpleHash popups = new SimpleHash();
184 String query_text = req.getParameter("query_text");
185 mergeData.put("query_text", query_text);
186 if (query_text != null) mergeData.put("query_text_encoded", URLEncoder.encode(query_text));
187 String query_field = req.getParameter("query_field");
188 mergeData.put("query_field", query_field);
189 String query_is_published = req.getParameter("query_is_published");
190 mergeData.put("query_is_published", query_is_published);
191 String query_media_folder = req.getParameter("query_media_folder");
192 mergeData.put("query_media_folder", query_media_folder);
193 String offset = req.getParameter("offset");
194 if (offset == null || offset.equals("")) offset = "0";
195 mergeData.put("offset", offset);
197 String order = req.getParameter("order");
198 if (order == null || order.equals("")) order = "webdb_lastchange desc";
200 // if in connection mode to content
201 String cid = req.getParameter("cid");
202 mergeData.put("cid", cid);
206 String whereClause = "";
207 boolean isFirst = true;
208 if (query_text != null && !query_text.equalsIgnoreCase("")) {
209 whereClause += "lower(" + query_field + ") like lower('%" + query_text + "%')";
212 if (query_is_published != null && !query_is_published.equals("")) {
213 if (isFirst == false) whereClause += " and ";
214 whereClause += "is_published='" + query_is_published + "'";
217 if (query_media_folder != null && !query_media_folder.equals("")) {
218 if (isFirst == false) whereClause += " and ";
219 whereClause += "to_media_folder='" + query_media_folder + "'";
221 //theLog.printDebugInfo("sql-whereclause: " + whereClause + " order: " + order + " offset: " + offset);
225 if (query_text != null || query_is_published != null || query_media_folder != null) {
226 EntityList theList = mainModule.getByWhereClause(whereClause, order, (new Integer(offset)).intValue(), 10);
227 if (theList != null) {
228 mergeData.put("contentlist", theList);
229 if (theList.getOrder() != null) {
230 mergeData.put("order", theList.getOrder());
231 mergeData.put("order_encoded", URLEncoder.encode(theList.getOrder()));
233 mergeData.put("count", (new Integer(theList.getCount())).toString());
234 mergeData.put("from", (new Integer(theList.getFrom())).toString());
235 mergeData.put("to", (new Integer(theList.getTo())).toString());
236 if (theList.hasNextBatch())
237 mergeData.put("next", (new Integer(theList.getNextBatch())).toString());
238 if (theList.hasPrevBatch())
239 mergeData.put("prev", (new Integer(theList.getPrevBatch())).toString());
242 popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
244 deliver(req, res, mergeData, popups, templateListString);
246 catch (Throwable e) {
247 throw new ServletModuleFailure(e);
252 public void add(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc {
254 SimpleHash mergeData = new SimpleHash();
255 SimpleHash popups = new SimpleHash();
256 String maxMedia = MirPropertiesConfiguration.instance().getString("ServletModule.OpenIndy.MaxMediaUploadItems");
257 String numOfMedia = req.getParameter("medianum");
259 mergeData.put("new", "1");
260 mergeData.put("articleid", req.getParameter("articleid"));
262 popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
264 if (numOfMedia==null || numOfMedia.equals("")) {
267 else if(Integer.parseInt(numOfMedia) > Integer.parseInt(maxMedia)) {
268 numOfMedia = maxMedia;
271 int mediaNum = Integer.parseInt(numOfMedia);
272 SimpleList mediaFields = new SimpleList();
273 for(int i =0; i<mediaNum;i++){
274 Integer mNum = new Integer(i+1);
275 mediaFields.add(mNum.toString());
277 mergeData.put("medianum",numOfMedia);
278 mergeData.put("mediafields",mediaFields);
279 deliver(req, res, mergeData, popups, templateObjektString);
281 catch (Exception e) {
282 throw new ServletModuleFailure(e);
286 public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc {
287 String idParam = req.getParameter("id");
288 _edit(idParam, req, res);
291 private void _edit(String idParam, HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc {
292 if (idParam != null && !idParam.equals("")) {
294 SimpleHash popups = new SimpleHash();
295 popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
296 deliver(req, res, mainModule.getById(idParam), popups,
297 templateObjektString);
299 catch (Throwable e) {
300 throw new ServletModuleFailure(e);
304 throw new ServletModuleExc("ServletmoduleUploadedMedia :: _edit without id");
309 /** @todo should be in ServletModule.java */
310 private EntityUsers _getUser(HttpServletRequest req) {
311 HttpSession session = req.getSession(false);
312 return (EntityUsers) session.getAttribute("login.uid");
315 public void getMedia(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
317 String idParam = req.getParameter("id");
318 if (idParam!=null && !idParam.equals("")) {
320 EntityUploadedMedia ent = (EntityUploadedMedia)mainModule.getById(idParam);
321 Entity mediaType = ent.getMediaType();
322 MirMedia mediaHandler;
324 ServletContext ctx = MirPropertiesConfiguration.getContext();
325 String fName = ent.getId()+"."+mediaType.getValue("name");
327 mediaHandler = MediaHelper.getHandler(mediaType);
328 InputStream in = mediaHandler.getMedia(ent, mediaType);
330 res.setContentType(ctx.getMimeType(fName));
331 //important that before calling this res.getWriter was not called first
332 ServletOutputStream out = res.getOutputStream();
335 byte[] buf = new byte[8 * 1024];
336 while((read = in.read(buf)) != -1) {
337 out.write(buf, 0, read);
343 catch (Throwable e) {
344 throw new ServletModuleFailure(e);
347 else logger.error("id not specified.");
348 // no exception allowed
351 public void getIcon(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
353 String idParam = req.getParameter("id");
354 if (idParam!=null && !idParam.equals("")) {
356 EntityUploadedMedia ent = (EntityUploadedMedia)mainModule.getById(idParam);
357 Entity mediaType = ent.getMediaType();
358 MirMedia mediaHandler;
360 ServletContext ctx = MirPropertiesConfiguration.getContext();
361 String fName = ent.getId()+"."+mediaType.getValue("name");
363 mediaHandler = MediaHelper.getHandler(mediaType);
364 InputStream in = mediaHandler.getIcon(ent);
366 res.setContentType(ctx.getMimeType(fName));
367 //important that before calling this res.getWriter was not called first
368 ServletOutputStream out = res.getOutputStream();
371 byte[] buf = new byte[8 * 1024];
372 while((read = in.read(buf)) != -1) {
373 out.write(buf, 0, read);
379 catch (Throwable e) {
380 throw new ServletModuleFailure(e);
383 else logger.error("getIcon: id not specified.");
384 // no exception allowed