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 theLog.printDebugInfo("mediaTypesList: "+mediaTypesList.size());
123 if (contentType.equals(
124 mediaTypesList.elementAt(j).getValue("mime_type")))
125 mediaType = mediaTypesList.elementAt(j);
128 // if no exact match, whatever foo/* might match
129 if (mediaType == null)
130 mediaType = mediaTypesList.elementAt(0);
132 // get the class names from the media_type table.
133 mediaTypeId = mediaType.getId();
135 // ############### @todo: merge these and the getURL call into one
136 // getURL helper call that just takes the Entity as a parameter
137 // along with media_type
138 mediaHandler = MediaHelper.getHandler(mediaType);
139 mediaStorage = MediaHelper.getStorage(mediaType,
140 "mircoders.storage.Database");
142 catch (Exception e) {
143 theLog.printError("getting media handler failed: " + e.toString());
144 throw new MirMediaException("getting media handler failed: "
148 parameters.put("to_media_type", mediaTypeId);
149 //load the classes via reflection
150 Entity mediaEnt = null;
152 mediaEnt = (Entity) mediaStorage.getEntityClass().newInstance();
153 mediaEnt.setStorage(mediaStorage);
154 mediaEnt.setValues(parameters);
155 // unfortunatly we have to insert it first because of the way
156 // Image setting works right now. that should change soon. -mh
157 mediaId = mediaEnt.insert();
158 //save and store the media data/metadata
159 mediaHandler.set(mpReq.getMedia(), mediaEnt, mediaType);
161 //were done with mpReq at this point, dereference it.
162 //as it contains mucho mem. -mh 01.10.2001
165 //we got this far, associate the media to the article
166 mediaEnt.setValueForProperty("is_published", "1");
169 catch (Exception e) {
171 mediaStorage.delete(mediaId);
173 catch (Exception e2) {
174 // dont't do anything here as the error was setting the media,
175 // this just means that the entity may not have been inserted yet
177 theLog.printError("setting media failed: " + e.toString());
178 throw new MirMediaException("setting media failed: " + e.toString());
181 _edit(mediaId, req, res);
183 catch (MirMediaException e) {
184 throw new ServletModuleException(
185 "upload -- media handling exception " + e.toString());
187 catch (MirMediaUserException e) {
188 throw new ServletModuleUserException(
191 catch (IOException e) {
192 throw new ServletModuleException("upload -- ioexception " + e.toString());
194 catch (StorageObjectException e) {
195 throw new ServletModuleException("StorageObjectException" + e.toString());
200 public void update(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException {
203 WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
204 HashMap parameters = mp.getParameters();
206 EntityUsers user = _getUser(req);
207 parameters.put("to_publisher", user.getId());
208 parameters.put("is_produced", "0");
209 if (!parameters.containsKey("is_published"))
210 parameters.put("is_published", "0");
212 String id = mainModule.set(parameters);
213 theLog.printError("media ID" + id);
216 catch (IOException e) {
217 throw new ServletModuleException("upload -- ioexception " + e.toString());
219 catch (ModuleException e) {
220 throw new ServletModuleException("upload -- moduleexception " + e.toString());
226 public void list(HttpServletRequest req, HttpServletResponse res)
227 throws ServletModuleException {
228 // Parameter auswerten
229 SimpleHash mergeData = new SimpleHash();
230 SimpleHash popups = new SimpleHash();
232 String query_text = req.getParameter("query_text");
233 mergeData.put("query_text", query_text);
234 if (query_text != null) mergeData.put("query_text_encoded", URLEncoder.encode(query_text));
235 String query_field = req.getParameter("query_field");
236 mergeData.put("query_field", query_field);
237 String query_is_published = req.getParameter("query_is_published");
238 mergeData.put("query_is_published", query_is_published);
239 String query_media_folder = req.getParameter("query_media_folder");
240 mergeData.put("query_media_folder", query_media_folder);
241 String offset = req.getParameter("offset");
242 if (offset == null || offset.equals("")) offset = "0";
243 mergeData.put("offset", offset);
245 String order = req.getParameter("order");
246 if (order == null) order = "webdb_lastchange desc";
248 // if in connection mode to content
249 String cid = req.getParameter("cid");
250 mergeData.put("cid", cid);
254 String whereClause = "";
255 boolean isFirst = true;
256 if (query_text != null && !query_text.equalsIgnoreCase("")) {
257 whereClause += "lower(" + query_field + ") like lower('%" + query_text + "%')";
260 if (query_is_published != null && !query_is_published.equals("")) {
261 if (isFirst == false) whereClause += " and ";
262 whereClause += "is_published='" + query_is_published + "'";
265 if (query_media_folder != null && !query_media_folder.equals("")) {
266 if (isFirst == false) whereClause += " and ";
267 whereClause += "to_media_folder='" + query_media_folder + "'";
269 //theLog.printDebugInfo("sql-whereclause: " + whereClause + " order: " + order + " offset: " + offset);
271 // fetch und ausliefern
273 if (query_text != null || query_is_published != null || query_media_folder != null) {
274 EntityList theList = mainModule.getByWhereClause(whereClause, order, (new Integer(offset)).intValue(), 10);
275 if (theList != null) {
276 mergeData.put("contentlist", theList);
277 if (theList.getOrder() != null) {
278 mergeData.put("order", theList.getOrder());
279 mergeData.put("order_encoded", URLEncoder.encode(theList.getOrder()));
281 mergeData.put("count", (new Integer(theList.getCount())).toString());
282 mergeData.put("from", (new Integer(theList.getFrom())).toString());
283 mergeData.put("to", (new Integer(theList.getTo())).toString());
284 if (theList.hasNextBatch())
285 mergeData.put("next", (new Integer(theList.getNextBatch())).toString());
286 if (theList.hasPrevBatch())
287 mergeData.put("prev", (new Integer(theList.getPrevBatch())).toString());
291 popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
293 deliver(req, res, mergeData, popups, templateListString);
295 catch (ModuleException e) {
296 throw new ServletModuleException(e.toString());
298 catch (Exception e) {
299 throw new ServletModuleException(e.toString());
304 public void add(HttpServletRequest req, HttpServletResponse res)
305 throws ServletModuleException {
307 SimpleHash mergeData = new SimpleHash();
308 mergeData.put("new", "1");
309 SimpleHash popups = new SimpleHash();
310 popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
311 deliver(req, res, mergeData, popups, templateObjektString);
313 catch (Exception e) {
314 throw new ServletModuleException(e.toString());
318 public void edit(HttpServletRequest req, HttpServletResponse res)
319 throws ServletModuleException {
320 String idParam = req.getParameter("id");
321 _edit(idParam, req, res);
324 private void _edit(String idParam, HttpServletRequest req, HttpServletResponse res)
325 throws ServletModuleException {
326 if (idParam != null && !idParam.equals("")) {
328 SimpleHash popups = new SimpleHash();
329 popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
330 deliver(req, res, mainModule.getById(idParam), popups,
331 templateObjektString);
333 catch (ModuleException e) {
334 throw new ServletModuleException(e.toString());
336 catch (StorageObjectException e) {
337 throw new ServletModuleException(e.toString());
341 throw new ServletModuleException("ServletmoduleUploadedMedia :: _edit without id");
346 /** @todo should be in ServletModule.java */
347 private EntityUsers _getUser(HttpServletRequest req) {
348 HttpSession session = req.getSession(false);
349 return (EntityUsers) session.getAttribute("login.uid");