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 freemarker.template.SimpleHash;
46 import freemarker.template.SimpleList;
48 import mir.config.MirPropertiesConfiguration;
49 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
50 import mir.entity.Entity;
51 import mir.entity.EntityList;
52 import mir.log.LoggerWrapper;
53 import mir.media.MediaHelper;
54 import mir.media.MirMedia;
55 import mir.misc.FileHandler;
56 import mir.misc.WebdbMultipartRequest;
57 import mir.servlet.ServletModule;
58 import mir.servlet.ServletModuleExc;
59 import mir.servlet.ServletModuleFailure;
60 import mir.servlet.ServletModuleUserExc;
61 import mir.storage.StorageObjectFailure;
62 import mir.util.ExceptionFunctions;
63 import mircoders.entity.EntityContent;
64 import mircoders.entity.EntityUploadedMedia;
65 import mircoders.entity.EntityUsers;
66 import mircoders.media.MediaRequest;
67 import mircoders.media.UnsupportedMediaFormatExc;
68 import mircoders.storage.DatabaseContent;
69 import mircoders.storage.DatabaseMediafolder;
72 * ServletModuleBilder -
73 * liefert HTML fuer Bilder
75 * @version $Id: ServletModuleUploadedMedia.java,v 1.22 2003/03/09 05:52:13 zapata Exp $
76 * @author RK, the mir-coders group
79 public abstract class ServletModuleUploadedMedia
80 extends mir.servlet.ServletModule {
82 //private static DatabaseRights dbRights;
84 public static ServletModule getInstance() {
88 public ServletModuleUploadedMedia() {
90 logger = new LoggerWrapper("ServletModule.UploadedMedia");
93 public void insert(HttpServletRequest req, HttpServletResponse res)
94 throws ServletModuleExc, ServletModuleUserExc {
96 EntityUsers user = _getUser(req);
97 MediaRequest mediaReq = new MediaRequest(user.getId(), false);
98 WebdbMultipartRequest mp = new WebdbMultipartRequest(req, (FileHandler)mediaReq);
99 EntityList mediaList = mediaReq.getEntityList();
100 String articleid = (String) mp.getParameters().get("articleid");
102 if (articleid!=null) {
103 EntityContent entContent = (EntityContent) DatabaseContent.getInstance().selectById(articleid);
107 while (mediaList.hasNext()) {
108 entContent.attach( ( (EntityUploadedMedia) mediaList.next()).getId());
112 ((ServletModuleContent) ServletModuleContent.getInstance())._showObject(articleid, req, res);
117 SimpleHash mergeData = new SimpleHash();
118 SimpleHash popups = new SimpleHash();
119 mergeData.put("contentlist", mediaList);
120 if (mediaList.getOrder() != null) {
121 mergeData.put("order", mediaList.getOrder());
122 mergeData.put("order_encoded", URLEncoder.encode(mediaList.getOrder()));
124 mergeData.put("count", (new Integer(mediaList.getCount())).toString());
125 mergeData.put("from", (new Integer(mediaList.getFrom())).toString());
126 mergeData.put("to", (new Integer(mediaList.getTo())).toString());
127 if (mediaList.hasNextBatch())
128 mergeData.put("next", (new Integer(mediaList.getNextBatch())).toString());
129 if (mediaList.hasPrevBatch())
130 mergeData.put("prev", (new Integer(mediaList.getPrevBatch())).toString());
132 popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
134 deliver(req, res, mergeData, popups, templateListString);
136 catch (Throwable t) {
137 Throwable cause = ExceptionFunctions.traceCauseException(t);
139 if (cause instanceof UnsupportedMediaFormatExc) {
140 throw new ServletModuleUserExc("media.error.unsupportedformat", new String[] {});
142 throw new ServletModuleFailure("ServletModuleUploadedMedia.insert: " + t.toString(), t);
146 public void update(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc {
149 EntityUsers user = _getUser(req);
150 WebdbMultipartRequest mp = new WebdbMultipartRequest(req, null);
151 Map parameters = mp.getParameters();
153 parameters.put("to_publisher", user.getId());
154 parameters.put("is_produced", "0");
155 if (!parameters.containsKey("is_published"))
156 parameters.put("is_published", "0");
158 String id = mainModule.set(parameters);
159 logger.debug("update: media ID = " + id);
162 catch (Throwable e) {
163 throw new ServletModuleFailure("upload -- exception " + e.toString(), e);
169 public void list(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc {
170 // Parameter auswerten
171 SimpleHash mergeData = new SimpleHash();
172 SimpleHash popups = new SimpleHash();
174 String query_text = req.getParameter("query_text");
175 mergeData.put("query_text", query_text);
176 if (query_text != null) mergeData.put("query_text_encoded", URLEncoder.encode(query_text));
177 String query_field = req.getParameter("query_field");
178 mergeData.put("query_field", query_field);
179 String query_is_published = req.getParameter("query_is_published");
180 mergeData.put("query_is_published", query_is_published);
181 String query_media_folder = req.getParameter("query_media_folder");
182 mergeData.put("query_media_folder", query_media_folder);
183 String offset = req.getParameter("offset");
184 if (offset == null || offset.equals("")) offset = "0";
185 mergeData.put("offset", offset);
187 String order = req.getParameter("order");
188 if (order == null || order.equals("")) order = "webdb_lastchange desc";
190 // if in connection mode to content
191 String cid = req.getParameter("cid");
192 mergeData.put("cid", cid);
196 String whereClause = "";
197 boolean isFirst = true;
198 if (query_text != null && !query_text.equalsIgnoreCase("")) {
199 whereClause += "lower(" + query_field + ") like lower('%" + query_text + "%')";
202 if (query_is_published != null && !query_is_published.equals("")) {
203 if (isFirst == false) whereClause += " and ";
204 whereClause += "is_published='" + query_is_published + "'";
207 if (query_media_folder != null && !query_media_folder.equals("")) {
208 if (isFirst == false) whereClause += " and ";
209 whereClause += "to_media_folder='" + query_media_folder + "'";
211 //theLog.printDebugInfo("sql-whereclause: " + whereClause + " order: " + order + " offset: " + offset);
215 if (query_text != null || query_is_published != null || query_media_folder != null) {
216 EntityList theList = mainModule.getByWhereClause(whereClause, order, (new Integer(offset)).intValue(), 10);
217 if (theList != null) {
218 mergeData.put("contentlist", theList);
219 if (theList.getOrder() != null) {
220 mergeData.put("order", theList.getOrder());
221 mergeData.put("order_encoded", URLEncoder.encode(theList.getOrder()));
223 mergeData.put("count", (new Integer(theList.getCount())).toString());
224 mergeData.put("from", (new Integer(theList.getFrom())).toString());
225 mergeData.put("to", (new Integer(theList.getTo())).toString());
226 if (theList.hasNextBatch())
227 mergeData.put("next", (new Integer(theList.getNextBatch())).toString());
228 if (theList.hasPrevBatch())
229 mergeData.put("prev", (new Integer(theList.getPrevBatch())).toString());
232 popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
234 deliver(req, res, mergeData, popups, templateListString);
236 catch (Throwable e) {
237 throw new ServletModuleFailure(e);
242 public void add(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc {
244 SimpleHash mergeData = new SimpleHash();
245 SimpleHash popups = new SimpleHash();
246 String maxMedia = MirPropertiesConfiguration.instance().getString("ServletModule.OpenIndy.MaxMediaUploadItems");
247 String numOfMedia = req.getParameter("medianum");
249 mergeData.put("new", "1");
250 mergeData.put("articleid", req.getParameter("articleid"));
252 popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
254 if (numOfMedia==null || numOfMedia.equals("")) {
257 else if(Integer.parseInt(numOfMedia) > Integer.parseInt(maxMedia)) {
258 numOfMedia = maxMedia;
261 int mediaNum = Integer.parseInt(numOfMedia);
262 SimpleList mediaFields = new SimpleList();
263 for(int i =0; i<mediaNum;i++){
264 Integer mNum = new Integer(i+1);
265 mediaFields.add(mNum.toString());
267 mergeData.put("medianum",numOfMedia);
268 mergeData.put("mediafields",mediaFields);
269 deliver(req, res, mergeData, popups, templateObjektString);
271 catch (Exception e) {
272 throw new ServletModuleFailure(e);
276 public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc {
277 String idParam = req.getParameter("id");
278 _edit(idParam, req, res);
281 private void _edit(String idParam, HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc {
282 if (idParam != null && !idParam.equals("")) {
284 SimpleHash popups = new SimpleHash();
285 popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
286 deliver(req, res, mainModule.getById(idParam), popups,
287 templateObjektString);
289 catch (Throwable e) {
290 throw new ServletModuleFailure(e);
294 throw new ServletModuleExc("ServletmoduleUploadedMedia :: _edit without id");
299 /** @todo should be in ServletModule.java */
300 private EntityUsers _getUser(HttpServletRequest req) {
301 HttpSession session = req.getSession(false);
302 return (EntityUsers) session.getAttribute("login.uid");
305 public void getMedia(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc {
306 String idParam = req.getParameter("id");
307 if (idParam!=null && !idParam.equals("")) {
309 EntityUploadedMedia ent = (EntityUploadedMedia)mainModule.getById(idParam);
310 Entity mediaType = ent.getMediaType();
311 MirMedia mediaHandler;
313 ServletContext ctx = MirPropertiesConfiguration.getContext();
314 String fName = ent.getId()+"."+mediaType.getValue("name");
316 mediaHandler = MediaHelper.getHandler(mediaType);
317 InputStream in = mediaHandler.getMedia(ent, mediaType);
319 res.setContentType(ctx.getMimeType(fName));
320 //important that before calling this res.getWriter was not called first
321 ServletOutputStream out = res.getOutputStream();
324 byte[] buf = new byte[8 * 1024];
325 while((read = in.read(buf)) != -1) {
326 out.write(buf, 0, read);
331 catch (Throwable e) {
332 throw new ServletModuleFailure(e);
335 else logger.error("id not specified.");
336 // no exception allowed
339 public void getIcon(HttpServletRequest req, HttpServletResponse res) throws ServletModuleExc
341 String idParam = req.getParameter("id");
342 if (idParam!=null && !idParam.equals("")) {
344 EntityUploadedMedia ent = (EntityUploadedMedia)mainModule.getById(idParam);
345 Entity mediaType = ent.getMediaType();
346 MirMedia mediaHandler;
348 ServletContext ctx = MirPropertiesConfiguration.getContext();
349 String fName = ent.getId()+"."+mediaType.getValue("name");
351 mediaHandler = MediaHelper.getHandler(mediaType);
352 InputStream in = mediaHandler.getIcon(ent);
354 res.setContentType(ctx.getMimeType(fName));
355 //important that before calling this res.getWriter was not called first
356 ServletOutputStream out = res.getOutputStream();
359 byte[] buf = new byte[8 * 1024];
360 while((read = in.read(buf)) != -1) {
361 out.write(buf, 0, read);
367 catch (Throwable e) {
368 throw new ServletModuleFailure(e);
371 else logger.error("getIcon: id not specified.");
372 // no exception allowed