1 package mircoders.servlet;
3 import freemarker.template.SimpleHash;
4 import mir.entity.Entity;
5 import mir.entity.EntityList;
6 import mir.media.MediaHelper;
7 import mir.media.MirMedia;
8 import mir.media.MirMediaException;
9 import mir.media.MirMediaUserException;
10 import mir.misc.FileUtil;
11 import mir.misc.MpRequest;
12 import mir.misc.StringUtil;
13 import mir.misc.WebdbMultipartRequest;
14 import mir.module.ModuleException;
15 import mir.servlet.ServletModule;
16 import mir.servlet.ServletModuleException;
17 import mir.servlet.ServletModuleUserException;
18 import mir.storage.Database;
19 import mir.storage.StorageObjectException;
20 import mircoders.entity.EntityUsers;
21 import mircoders.storage.DatabaseMediaType;
22 import mircoders.storage.DatabaseMediafolder;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26 import javax.servlet.http.HttpSession;
27 import java.io.IOException;
28 import java.net.URLEncoder;
29 import java.util.GregorianCalendar;
30 import java.util.HashMap;
33 * ServletModuleBilder -
34 * liefert HTML fuer Bilder
40 public abstract class ServletModuleUploadedMedia
41 extends mir.servlet.ServletModule {
43 //private static DatabaseRights dbRights;
45 public static ServletModule getInstance() {
49 public void insert(HttpServletRequest req, HttpServletResponse res)
50 throws ServletModuleException, ServletModuleUserException {
52 WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
53 HashMap parameters = mp.getParameters();
54 EntityUsers user = _getUser(req);
55 String mediaId = null;
56 MpRequest mpReq = (MpRequest) mp.requestList.get(0);
57 String mediaTypeId; //= null;
58 MirMedia mediaHandler;
59 Database mediaStorage;
61 // get the content-type from what the client browser
62 // sends us. (the "Oreilly method")
63 String contentType = mpReq.getContentType();
64 String fileName = mpReq.getFilename();
65 theLog.printInfo("CONTENT-TYPE FROM BROWSER: " + contentType);
67 // if the client browser sent us unknown (text/plain is default)
68 // or if we got application/octet-stream, it's possible that
69 // the browser is in error, better check against the file extension
70 if (contentType.equals("text/plain") ||
71 contentType.equals("application/octet-stream")) {
73 * This is just a temporary way to get the content-type via
74 * the .extension , we could maybe use a magic method, by looking
75 * at the header (first few bytes) of the file. (like the file(1)
77 * The Oreilly method relies on the content-type that the client
78 * browser sends and that sometimes is application-octet stream with
79 * broken/mis-configured browsers.
81 * The map file should be Mir/content-types.properties, it's the
82 * default Sun Java file with some additional entries that it did
83 * not have. So if you support a new media type you have to make
84 * sure that it is in this file -mh
86 contentType = FileUtil.guessContentTypeFromName(fileName);
87 if (contentType == null)
88 contentType = "text/plain"; // rfc1867 says this is the default
90 theLog.printInfo("CONTENT TYPE IS: " + contentType);
92 if (contentType.equals("text/plain") ||
93 contentType.equals("application/octet-stream")) {
94 throw new ServletModuleUserException(
95 "One or more files of unrecognized types");
98 parameters.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
99 parameters.put("to_publisher", user.getId());
101 //the where clause to find the media_type entry from the content-type.
102 //we use the media type entry to lookup the media Handler/Storage classes
103 // @todo this should probably be moved to DatabaseMediaType or
104 // somewhere else appropriate -mh
105 String[] contentTypeSplit = StringUtil.split(contentType, "/");
106 String wc = " mime_type LIKE '" + contentTypeSplit[0] + "%'";
107 DatabaseMediaType mediaTypeStor = DatabaseMediaType.getInstance();
108 EntityList mediaTypesList = mediaTypeStor.selectByWhereClause(wc);
110 // if we didn't find an entry matching the
111 // content-type in the table.
112 if (mediaTypesList.size() == 0) {
113 theLog.printDebugInfo("Wrong file type uploaded!: " + fileName);
114 throw new MirMediaUserException("One or more files of unrecognized type");
116 Entity mediaType = null;
118 // find out if we an exact content-type match if so take it.
119 // otherwise just use the first one.
120 // @todo this should probably be moved to DatabaseMediaType -mh
121 for (int j = 0; j < mediaTypesList.size(); j++) {
122 if (contentType.equals(
123 mediaTypesList.elementAt(j).getValue("mime_type")))
124 mediaType = mediaTypesList.elementAt(j);
127 // if no exact match, whatever foo/* might match
128 if (mediaType == null)
129 mediaType = mediaTypesList.elementAt(0);
131 // get the class names from the media_type table.
132 mediaTypeId = mediaType.getId();
134 // ############### @todo: merge these and the getURL call into one
135 // getURL helper call that just takes the Entity as a parameter
136 // along with media_type
137 mediaHandler = MediaHelper.getHandler(mediaType);
138 mediaStorage = MediaHelper.getStorage(mediaType,
139 "mircoders.storage.Database");
141 catch (Exception e) {
142 theLog.printError("getting media handler failed: " + e.toString());
143 throw new MirMediaException("getting media handler failed: "
147 parameters.put("to_media_type", mediaTypeId);
148 //load the classes via reflection
149 Entity mediaEnt = null;
151 mediaEnt = (Entity) mediaStorage.getEntityClass().newInstance();
152 mediaEnt.setStorage(mediaStorage);
153 mediaEnt.setValues(parameters);
154 // unfortunatly we have to insert it first because of the way
155 // Image setting works right now. that should change soon. -mh
156 mediaId = mediaEnt.insert();
157 //save and store the media data/metadata
158 mediaHandler.set(mpReq.getMedia(), mediaEnt, mediaType);
160 //were done with mpReq at this point, dereference it.
161 //as it contains mucho mem. -mh 01.10.2001
164 //we got this far, associate the media to the article
165 mediaEnt.setValueForProperty("is_published", "1");
168 catch (Exception e) {
170 mediaStorage.delete(mediaId);
172 catch (Exception e2) {
173 // dont't do anything here as the error was setting the media,
174 // this just means that the entity may not have been inserted yet
176 theLog.printError("setting media failed: " + e.toString());
177 throw new MirMediaException("setting media failed: " + e.toString());
180 _edit(mediaId, req, res);
182 catch (MirMediaException e) {
183 throw new ServletModuleException(
184 "upload -- media handling exception " + e.toString());
186 catch (MirMediaUserException e) {
187 throw new ServletModuleUserException(
190 catch (IOException e) {
191 throw new ServletModuleException("upload -- ioexception " + e.toString());
193 catch (StorageObjectException e) {
194 throw new ServletModuleException("StorageObjectException" + e.toString());
199 public void update(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException {
202 WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
203 HashMap parameters = mp.getParameters();
205 EntityUsers user = _getUser(req);
206 parameters.put("to_publisher", user.getId());
207 parameters.put("is_produced", "0");
208 if (!parameters.containsKey("is_published"))
209 parameters.put("is_published", "0");
211 String id = mainModule.set(parameters);
212 theLog.printError("media ID" + id);
215 catch (IOException e) {
216 throw new ServletModuleException("upload -- ioexception " + e.toString());
218 catch (ModuleException e) {
219 throw new ServletModuleException("upload -- moduleexception " + e.toString());
225 public void list(HttpServletRequest req, HttpServletResponse res)
226 throws ServletModuleException {
227 // Parameter auswerten
228 SimpleHash mergeData = new SimpleHash();
229 SimpleHash popups = new SimpleHash();
231 String query_text = req.getParameter("query_text");
232 mergeData.put("query_text", query_text);
233 if (query_text != null) mergeData.put("query_text_encoded", URLEncoder.encode(query_text));
234 String query_field = req.getParameter("query_field");
235 mergeData.put("query_field", query_field);
236 String query_is_published = req.getParameter("query_is_published");
237 mergeData.put("query_is_published", query_is_published);
238 String query_media_folder = req.getParameter("query_media_folder");
239 mergeData.put("query_media_folder", query_media_folder);
240 String offset = req.getParameter("offset");
241 if (offset == null || offset.equals("")) offset = "0";
242 mergeData.put("offset", offset);
244 String order = req.getParameter("order");
245 if (order == null) order = "webdb_lastchange desc";
247 // if in connection mode to content
248 String cid = req.getParameter("cid");
249 mergeData.put("cid", cid);
253 String whereClause = "";
254 boolean isFirst = true;
255 if (query_text != null && !query_text.equalsIgnoreCase("")) {
256 whereClause += "lower(" + query_field + ") like lower('%" + query_text + "%')";
259 if (query_is_published != null && !query_is_published.equals("")) {
260 if (isFirst == false) whereClause += " and ";
261 whereClause += "is_published='" + query_is_published + "'";
264 if (query_media_folder != null && !query_media_folder.equals("")) {
265 if (isFirst == false) whereClause += " and ";
266 whereClause += "to_media_folder='" + query_media_folder + "'";
268 //theLog.printDebugInfo("sql-whereclause: " + whereClause + " order: " + order + " offset: " + offset);
270 // fetch und ausliefern
272 if (query_text != null || query_is_published != null || query_media_folder != null) {
273 EntityList theList = mainModule.getByWhereClause(whereClause, order, (new Integer(offset)).intValue(), 10);
274 if (theList != null) {
275 mergeData.put("contentlist", theList);
276 if (theList.getOrder() != null) {
277 mergeData.put("order", theList.getOrder());
278 mergeData.put("order_encoded", URLEncoder.encode(theList.getOrder()));
280 mergeData.put("count", (new Integer(theList.getCount())).toString());
281 mergeData.put("from", (new Integer(theList.getFrom())).toString());
282 mergeData.put("to", (new Integer(theList.getTo())).toString());
283 if (theList.hasNextBatch())
284 mergeData.put("next", (new Integer(theList.getNextBatch())).toString());
285 if (theList.hasPrevBatch())
286 mergeData.put("prev", (new Integer(theList.getPrevBatch())).toString());
290 popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
292 deliver(req, res, mergeData, popups, templateListString);
294 catch (ModuleException e) {
295 throw new ServletModuleException(e.toString());
297 catch (Exception e) {
298 throw new ServletModuleException(e.toString());
303 public void add(HttpServletRequest req, HttpServletResponse res)
304 throws ServletModuleException {
306 SimpleHash mergeData = new SimpleHash();
307 mergeData.put("new", "1");
308 SimpleHash popups = new SimpleHash();
309 popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
310 deliver(req, res, mergeData, popups, templateObjektString);
312 catch (Exception e) {
313 throw new ServletModuleException(e.toString());
317 public void edit(HttpServletRequest req, HttpServletResponse res)
318 throws ServletModuleException {
319 String idParam = req.getParameter("id");
320 _edit(idParam, req, res);
323 private void _edit(String idParam, HttpServletRequest req, HttpServletResponse res)
324 throws ServletModuleException {
325 if (idParam != null && !idParam.equals("")) {
327 SimpleHash popups = new SimpleHash();
328 popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
329 deliver(req, res, mainModule.getById(idParam), popups,
330 templateObjektString);
332 catch (ModuleException e) {
333 throw new ServletModuleException(e.toString());
335 catch (StorageObjectException e) {
336 throw new ServletModuleException(e.toString());
340 throw new ServletModuleException("ServletmoduleUploadedMedia :: _edit without id");
345 /** @todo should be in ServletModule.java */
346 private EntityUsers _getUser(HttpServletRequest req) {
347 HttpSession session = req.getSession(false);
348 return (EntityUsers) session.getAttribute("login.uid");