1 package mircoders.servlet;
8 import java.lang.reflect.*;
9 import javax.servlet.*;
10 import javax.servlet.http.*;
12 import freemarker.template.*;
13 import com.oreilly.servlet.multipart.*;
14 import com.oreilly.servlet.*;
23 import mircoders.entity.*;
24 import mircoders.storage.*;
25 import mircoders.module.*;
26 import mircoders.producer.*;
29 * ServletModuleOpenIndy -
30 * is the open-access-servlet, which is responsible for
31 * adding comments to articles &
32 * open-postings to the newswire
37 public class ServletModuleOpenIndy extends ServletModule
40 private String commentFormTemplate, commentFormDoneTemplate, commentFormDupeTemplate;
41 private String postingFormTemplate, postingFormDoneTemplate, postingFormDupeTemplate;
42 private ModuleContent contentModule;
43 private ModuleImages imageModule;
44 private ModuleTopics themenModule;
45 private String directOp ="yes";
47 // Singelton / Kontruktor
48 private static ServletModuleOpenIndy instance = new ServletModuleOpenIndy();
49 public static ServletModule getInstance() { return instance; }
51 private ServletModuleOpenIndy() {
53 theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("ServletModule.OpenIndy.Logfile"));
54 commentFormTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentTemplate");
55 commentFormDoneTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentDoneTemplate");
56 commentFormDupeTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentDupeTemplate");
57 postingFormTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingTemplate");
58 postingFormDoneTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingDoneTemplate");
59 postingFormDupeTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingDupeTemplate");
60 directOp = MirConfig.getProp("DirectOpenposting").toLowerCase();
61 mainModule = new ModuleComment(DatabaseComment.getInstance());
62 contentModule = new ModuleContent(DatabaseContent.getInstance());
63 themenModule = new ModuleTopics(DatabaseTopics.getInstance());
64 imageModule = new ModuleImages(DatabaseImages.getInstance());
65 defaultAction="addposting";
67 catch (StorageObjectException e) {
68 theLog.printError("servletmoduleopenindy could not be initialized");
74 * Method for making a comment
77 public void addcomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
79 String aid = req.getParameter("aid"); // the article id the comment will belong to
80 if (aid!=null && !aid.equals(""))
82 SimpleHash mergeData = new SimpleHash();
84 mergeData.put("aid", aid);
85 deliver(req, res, mergeData, commentFormTemplate);
87 else throw new ServletModuleException("aid not set!");
91 * Method for inserting a comment into the Database and delivering
92 * the commentDone Page
95 public void inscomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
97 String aid = req.getParameter("to_media"); // the article id the comment will belong to
98 if (aid!=null && !aid.equals(""))
100 // ok, collecting data from form
102 HashMap withValues = getIntersectingValues(req, DatabaseComment.getInstance());
104 //no html in comments(for now)
105 for (Iterator i=withValues.keySet().iterator(); i.hasNext(); ){
106 String k=(String)i.next();
107 String v=(String)withValues.get(k);
109 withValues.put(k,StringUtil.removeHTMLTags(v));
111 withValues.put("is_published","1");
113 // inserting into database
114 String id = mainModule.add(withValues);
115 theLog.printDebugInfo("id: "+id);
116 //insert was not successfull
118 deliver(req, res, new SimpleHash(), commentFormDupeTemplate);
121 // producing new page
122 new ProducerContent().handle(null, null, true, false, aid);
125 int exitValue = Helper.rsync();
126 theLog.printDebugInfo("rsync:"+exitValue);
128 // redirecting to url
129 // should implement back to article
130 SimpleHash mergeData = new SimpleHash();
131 deliver(req, res, mergeData, commentFormDoneTemplate);
133 catch (StorageObjectException e) { throw new ServletModuleException(e.toString());}
134 catch (ModuleException e) { throw new ServletModuleException(e.toString());}
137 else throw new ServletModuleException("aid not set!");
142 * Method for delivering the form-Page for open posting
145 public void addposting(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
147 SimpleHash mergeData = new SimpleHash();
148 String numOfMedia = req.getParameter("medianum");
149 if(numOfMedia==null||numOfMedia.equals("")){
153 int mediaNum = Integer.parseInt(numOfMedia);
154 SimpleList mediaFields = new SimpleList();
155 for(int i =0; i<mediaNum;i++){
156 Integer mNum = new Integer(i+1);
157 mediaFields.add(mNum.toString());
159 mergeData.put("medianum",numOfMedia);
160 mergeData.put("mediafields",mediaFields);
161 mergeData.put("themenPopupData", themenModule.getTopicsAsSimpleList());
164 /** @todo popups missing */
166 mergeData.put("languagePopUpData",DatabaseLanguage.getInstance().getPopupData());
167 } catch (Exception e) {
168 theLog.printError("languagePopUpData failed");
170 deliver(req, res, mergeData, postingFormTemplate);
174 * Method for inserting an open posting into the Database and delivering
175 * the postingDone Page
178 public void insposting(HttpServletRequest req, HttpServletResponse res)
179 throws ServletModuleException
181 SimpleHash mergeData = new SimpleHash();
182 boolean setMedia=false;
185 WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
187 HashMap withValues = mp.getParameters();
189 // call the routines that escape html
191 for (Iterator i=withValues.keySet().iterator(); i.hasNext(); ){
192 String k=(String)i.next();
193 String v=(String)withValues.get(k);
195 if (k.equals("content_data")){
196 //this doesn't quite work yet, so for now, all html goes
197 //withValues.put(k,StringUtil.approveHTMLTags(v));
198 //withValues.put(k,StringUtil.removeHTMLTags(v));
200 withValues.put(k,StringUtil.removeHTMLTags(v));
205 withValues.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
206 withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
207 withValues.put("is_produced", "0");
208 // op-articles are immediatly published
209 withValues.put("is_published","1");
210 // if op direct article-type == newswire
211 if (directOp.equals("yes")) withValues.put("to_article_type","1");
213 // owner is openposting user
214 withValues.put("to_publisher","1");
215 if (withValues.get("creator").toString().equals(""))
216 withValues.put("creator","Anonym");
218 // inserting content into database
219 String cid = contentModule.add(withValues);
220 theLog.printDebugInfo("id: "+cid);
221 //insert was not successfull
223 //How do we know that it was not succesful cause of a
224 //dupe, what if it failed cause of "No space left on device"?
225 //Or is there something I am missing? Wouldn't it be better
226 //to have an explicit dupe check and then insert? I have no
227 //idea what I am talking about. this comment is in case
228 //I forget to explicitely ask. -mh
229 deliver(req, res, mergeData, postingFormDupeTemplate);
232 String[] to_topicsArr = mp.getParameterValues("to_topic");
233 if (to_topicsArr != null && to_topicsArr.length > 0) {
235 DatabaseContentToTopics.getInstance().setTopics(cid,to_topicsArr);
236 theLog.printError("setting content_x_topic success");
237 } catch (Exception e) {
238 theLog.printError("setting content_x_topic failed");
242 // if op contains uploaddata
245 for(Iterator it = mp.requestList.iterator(); it.hasNext();){
246 MpRequest mpReq = (MpRequest)it.next();
247 String fileName = mpReq.getFilename();
249 //This is just a temporary way to get the content-type via
250 //the .extension , we need to use a magic method, by looking
251 //at the header (first few bytes) of the file.
252 //the Oreilly method sucks cause it relies on the
253 //content-type the client browser sends and that's
254 //too often application-octet stream. -mh
255 String contentType = FileUtil.guessContentTypeFromName(fileName);
256 HashMap mediaValues = new HashMap();
258 theLog.printError("CONTENT TYPE IS: "+contentType);
260 //The map file should be Mir/content-types.properties, it's the
261 //default Sun Java file+ some entries that it did not have.
262 //so if you support a new media type you have to make sure that
263 //it is in this file -mh
264 if ((contentType==null) || (contentType=="application/octet-stream")) {
265 throw new ServletModuleException("ModuleException: One or more files of unrecognized types");
268 String mediaTitle=(String)withValues.get("media_title"+i);
271 if (mediaTitle==null)
272 mediaTitle = (String)withValues.get("title");
274 mediaValues.put("title", mediaTitle);
275 mediaValues.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
276 mediaValues.put("to_publisher", "1"); // op user
277 mediaValues.put("to_media_folder", "7"); // op media_folder
278 mediaValues.put("is_produced", "0");
279 mediaValues.put("is_published","1");
281 //the where clause to find the media_type entry
282 //from the content-type.
283 //we use the media type entry to lookup the
284 //media Handler/Storage classes
285 String wc = " mime_type='"+contentType+"'";
287 EntityList mediaTypesList = DatabaseMediaType.getInstance().selectByWhereClause(wc);
289 String mediaTypeId = null;
290 String mediaStorageName = null;
291 String mediaHandlerName = null;
293 //if we found an entry matching the
294 //content-type int the table.
295 if (mediaTypesList.size() > 0) {
296 //get the class names from the media_type table.
297 mediaTypeId = mediaTypesList.elementAt(0).getId();
298 mediaStorageName = mediaTypesList.elementAt(0).getValue("tablename");
299 mediaHandlerName = mediaTypesList.elementAt(0).getValue("classname");
300 mediaValues.put("to_media_type",mediaTypeId);
302 //load the classes via reflection
305 Class mediaStorageClass = Class.forName("mircoders.storage.Database"+mediaStorageName);
306 Method m = mediaStorageClass.getMethod("getInstance", null);
307 Database mediaStorage = (Database)m.invoke(null, null);
308 Entity mediaEnt = (Entity)mediaStorage.getEntityClass().newInstance();
309 mediaEnt.setStorage(mediaStorage);
310 mediaEnt.setValues(mediaValues);
311 mediaId = mediaEnt.insert();
313 Class mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
314 MirMedia mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
315 //save and store the media data/metadata
316 mediaHandler.set(mpReq.getMedia(), mediaEnt,mediaTypesList.elementAt(0));
318 //were done with mpReq at this point, dereference it.
319 //as it contains mucho mem. -mh 01.10.2001
323 new ProducerMedia().handle(null, null, false, false, mediaId);
325 } catch (Exception e) {
326 theLog.printError("setting uploaded_media failed: "+e.toString());
329 //we got this far, associate the media to the article
331 DatabaseContentToMedia.getInstance().addMedia(cid,mediaId);
332 theLog.printError("setting content_x_media success");
333 } catch (Exception e) {
334 theLog.printError("setting content_x_media failed");
338 theLog.printDebugInfo("Wrong file uploaded!: " + fileName);
339 throw new ServletModuleException("ModuleException: One or more files of unrecognized types");
340 } // end if-else mediaTypesList.size() > 0
342 } //end for Iterator...
344 //dereference mp. -mh
347 // producing openpostinglist
348 new ProducerOpenPosting().handle(null,null,false,false);
349 // producing new page
350 new ProducerContent().handle(null, null, false, false,cid);
351 //if direct op producing startpage
352 if (directOp.equals("yes")) new ProducerStartPage().handle(null,null);
355 //should be configureable
356 int exitValue = Helper.rsync();
357 theLog.printDebugInfo("rsync: "+exitValue);
360 catch (IOException e) { throw new ServletModuleException("IOException: "+ e.toString());}
361 catch (StorageObjectException e) { throw new ServletModuleException("StorageObjectException" + e.toString());}
362 catch (ModuleException e) { throw new ServletModuleException("ModuleException"+e.toString());}
364 deliver(req, res, mergeData, postingFormDoneTemplate);