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,
41 commentFormDupeTemplate;
42 private String postingFormTemplate, postingFormDoneTemplate,
43 postingFormDupeTemplate;
44 private ModuleContent contentModule;
45 private ModuleImages imageModule;
46 private ModuleTopics themenModule;
47 private String directOp ="yes";
49 // Singelton / Kontruktor
50 private static ServletModuleOpenIndy instance = new ServletModuleOpenIndy();
51 public static ServletModule getInstance() { return instance; }
53 private ServletModuleOpenIndy() {
55 theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("ServletModule.OpenIndy.Logfile"));
56 commentFormTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentTemplate");
57 commentFormDoneTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentDoneTemplate");
58 commentFormDupeTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentDupeTemplate");
59 postingFormTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingTemplate");
60 postingFormDoneTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingDoneTemplate");
61 postingFormDupeTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingDupeTemplate");
62 directOp = MirConfig.getProp("DirectOpenposting").toLowerCase();
63 mainModule = new ModuleComment(DatabaseComment.getInstance());
64 contentModule = new ModuleContent(DatabaseContent.getInstance());
65 themenModule = new ModuleTopics(DatabaseTopics.getInstance());
66 imageModule = new ModuleImages(DatabaseImages.getInstance());
67 defaultAction="addposting";
69 catch (StorageObjectException e) {
70 theLog.printError("servletmoduleopenindy could not be initialized");
76 * Method for making a comment
79 public void addcomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
81 String aid = req.getParameter("aid"); // the article id the comment will belong to
82 if (aid!=null && !aid.equals(""))
84 SimpleHash mergeData = new SimpleHash();
86 mergeData.put("aid", aid);
87 deliver(req, res, mergeData, commentFormTemplate);
89 else throw new ServletModuleException("aid not set!");
93 * Method for inserting a comment into the Database and delivering
94 * the commentDone Page
97 public void inscomment(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
99 String aid = req.getParameter("to_media"); // the article id the comment will belong to
100 if (aid!=null && !aid.equals(""))
102 // ok, collecting data from form
104 HashMap withValues = getIntersectingValues(req, DatabaseComment.getInstance());
106 //no html in comments(for now)
107 for (Iterator i=withValues.keySet().iterator(); i.hasNext(); ){
108 String k=(String)i.next();
109 String v=(String)withValues.get(k);
111 withValues.put(k,StringUtil.removeHTMLTags(v));
113 withValues.put("is_published","1");
115 // inserting into database
116 String id = mainModule.add(withValues);
117 theLog.printDebugInfo("id: "+id);
118 //insert was not successfull
120 deliver(req, res, new SimpleHash(), commentFormDupeTemplate);
123 // producing new page
124 new ProducerContent().handle(null, null, true, false, aid);
127 int exitValue = Helper.rsync();
128 theLog.printDebugInfo("rsync:"+exitValue);
130 // redirecting to url
131 // should implement back to article
132 SimpleHash mergeData = new SimpleHash();
133 deliver(req, res, mergeData, commentFormDoneTemplate);
135 catch (StorageObjectException e) { throw new ServletModuleException(e.toString());}
136 catch (ModuleException e) { throw new ServletModuleException(e.toString());}
139 else throw new ServletModuleException("aid not set!");
144 * Method for delivering the form-Page for open posting
147 public void addposting(HttpServletRequest req, HttpServletResponse res)
148 throws ServletModuleException {
149 SimpleHash mergeData = new SimpleHash();
150 String numOfMedia = req.getParameter("medianum");
151 if(numOfMedia==null||numOfMedia.equals("")){
155 int mediaNum = Integer.parseInt(numOfMedia);
156 SimpleList mediaFields = new SimpleList();
157 for(int i =0; i<mediaNum;i++){
158 Integer mNum = new Integer(i+1);
159 mediaFields.add(mNum.toString());
161 mergeData.put("medianum",numOfMedia);
162 mergeData.put("mediafields",mediaFields);
165 SimpleHash extraInfo = new SimpleHash();
166 /** @todo popups missing */
168 SimpleList popUpData = DatabaseLanguage.getInstance().getPopupData();
169 extraInfo.put("languagePopUpData", popUpData );
170 extraInfo.put("themenPopupData", themenModule.getTopicsAsSimpleList());
171 } catch (Exception e) {
172 theLog.printError("languagePopUpData or getTopicslist failed "
174 throw new ServletModuleException("OpenIndy -- failed getting language or topics: "+e.toString());
177 deliver(req, res, mergeData, extraInfo, postingFormTemplate);
181 * Method for inserting an open posting into the Database and delivering
182 * the postingDone Page
185 public void insposting(HttpServletRequest req, HttpServletResponse res)
186 throws ServletModuleException, ServletModuleUserException
188 SimpleHash mergeData = new SimpleHash();
189 boolean setMedia=false;
190 boolean setTopic = false;
193 WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
195 HashMap withValues = mp.getParameters();
197 if ((((String)withValues.get("title")).length() == 0) ||
198 (((String)withValues.get("description")).length() == 0) ||
199 (((String)withValues.get("content_data")).length() == 0))
200 throw new ServletModuleUserException("Missing field");
202 // call the routines that escape html
204 for (Iterator i=withValues.keySet().iterator(); i.hasNext(); ){
205 String k=(String)i.next();
206 String v=(String)withValues.get(k);
208 if (k.equals("content_data")){
209 //this doesn't quite work yet, so for now, all html goes
210 //withValues.put(k,StringUtil.approveHTMLTags(v));
211 //withValues.put(k,StringUtil.removeHTMLTags(v));
213 withValues.put(k,StringUtil.removeHTMLTags(v));
218 withValues.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
219 withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));
220 withValues.put("is_produced", "0");
221 // op-articles are not immediatly published
222 // we don't know that all is good yet (media, title is present, etc..)
223 withValues.put("is_published","0");
224 // if op direct article-type == newswire
225 if (directOp.equals("yes")) withValues.put("to_article_type","1");
227 // owner is openposting user
228 withValues.put("to_publisher","1");
229 if (withValues.get("creator").toString().equals(""))
230 withValues.put("creator","Anonym");
232 // inserting content into database
233 String cid = contentModule.add(withValues);
234 theLog.printDebugInfo("id: "+cid);
235 //insert was not successfull
237 //How do we know that it was not succesful cause of a
238 //dupe, what if it failed cause of "No space left on device"?
239 //Or is there something I am missing? Wouldn't it be better
240 //to have an explicit dupe check and then insert? I have no
241 //idea what I am talking about. this comment is in case
242 //I forget to explicitely ask. -mh
243 deliver(req, res, mergeData, postingFormDupeTemplate);
246 String[] to_topicsArr = mp.getParameterValues("to_topic");
248 if (to_topicsArr != null && to_topicsArr.length > 0) {
250 DatabaseContentToTopics.getInstance().setTopics(cid,to_topicsArr);
252 } catch (Exception e) {
253 theLog.printError("setting content_x_topic failed");
254 contentModule.deleteById(cid);
255 throw new ServletModuleException("smod - openindy :: insposting: setting content_x_topic failed: "+e.toString());
259 // if op contains uploaddata
262 for(Iterator it = mp.requestList.iterator(); it.hasNext();){
263 MpRequest mpReq = (MpRequest)it.next();
264 String fileName = mpReq.getFilename();
266 //get the content-type from what the client browser
267 //sends us. (the "Oreilly method")
268 String contentType = mpReq.getContentType();
270 theLog.printInfo("FROM BROWSER: "+contentType);
272 //if the client browser sent us unknown (text/plain is default)
273 //or if we got application/octet-stream, it's possible that
274 //the browser is in error, better check against the file extension
275 if (contentType.equals("text/plain") ||
276 contentType.equals("application/octet-stream")) {
278 * This is just a temporary way to get the content-type via
279 * the .extension , we could maybe use a magic method, by looking
280 * at the header (first few bytes) of the file. (like the file(1)
282 * The Oreilly method relies on the content-type that the client
283 * browser sends and that sometimes is application-octet stream with
284 * broken/mis-configured browsers.
286 * The map file should be Mir/content-types.properties, it's the
287 * default Sun Java file with some additional entries that it did
288 * not have. So if you support a new media type you have to make
289 * sure that it is in this file -mh
291 contentType = FileUtil.guessContentTypeFromName(fileName);
292 if (contentType==null)
293 contentType = "text/plain"; // rfc1867 says this is the default
295 HashMap mediaValues = new HashMap();
297 theLog.printInfo("CONTENT TYPE IS: "+contentType);
299 if (contentType.equals("text/plain") ||
300 contentType.equals("application/octet-stream")) {
301 contentModule.deleteById(cid);
302 _throwBadContentType(fileName, contentType);
305 String mediaTitle=(String)withValues.get("media_title"+i);
308 if (mediaTitle==null)
309 mediaTitle = (String)withValues.get("title");
311 mediaValues.put("title", mediaTitle);
312 mediaValues.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
313 mediaValues.put("to_publisher", "1"); // op user
314 mediaValues.put("to_media_folder", "7"); // op media_folder
315 mediaValues.put("is_produced", "0");
316 mediaValues.put("is_published","0");
318 // @todo this should probably be moved to DatabaseMediaType -mh
319 String[] cTypeSplit = StringUtil.split(contentType, "/");
320 String wc = " mime_type LIKE '"+cTypeSplit[0]+"%'";
322 DatabaseMediaType mediaTypeStor = DatabaseMediaType.getInstance();
323 EntityList mediaTypesList = mediaTypeStor.selectByWhereClause(wc);
325 String mediaTypeId = null;
326 MirMedia mediaHandler;
327 Database mediaStorage;
328 ProducerMedia mediaProducer;
330 //if we didn't find an entry matching the
331 //content-type int the table.
332 if (mediaTypesList.size() == 0) {
333 contentModule.deleteById(cid);
334 _throwBadContentType(fileName, contentType);
337 Entity mediaType = null;
338 Entity mediaType2 = null;
340 // find out if we an exact content-type match if so take it.
341 // otherwise try to match majortype/*
342 // @todo this should probably be moved to DatabaseMediaType -mh
343 for(int j=0;j<mediaTypesList.size();j++) {
344 if(contentType.equals(
345 mediaTypesList.elementAt(j).getValue("mime_type")))
346 mediaType = mediaTypesList.elementAt(j);
347 else if ((mediaTypesList.elementAt(j).getValue("mime_type")).equals(
348 cTypeSplit[0]+"/*") )
349 mediaType2= mediaTypesList.elementAt(j);
352 if ( (mediaType == null) && (mediaType2 == null) ) {
353 contentModule.deleteById(cid);
354 _throwBadContentType(fileName, contentType);
356 else if( (mediaType == null) && (mediaType2 != null) )
357 mediaType = mediaType2;
359 //get the class names from the media_type table.
360 mediaTypeId = mediaType.getId();
362 // ############### @todo: merge these and the getURL call into one
363 // getURL helper call that just takes the Entity as a parameter
364 // along with media_type
365 mediaHandler = MediaHelper.getHandler(mediaType);
366 mediaStorage = MediaHelper.getStorage(mediaType,
367 "mircoders.storage.Database");
368 Class prodCls = Class.forName("mircoders.producer.Producer"
369 +mediaType.getValue("tablename"));
370 mediaProducer = (ProducerMedia)prodCls.newInstance();
371 } catch (Exception e) {
372 theLog.printError("getting media handler failed: "+e.toString());
373 contentModule.deleteById(cid);
374 throw new ServletModuleException("getting media handler failed: "
378 mediaValues.put("to_media_type",mediaTypeId);
380 //load the classes via reflection
382 Entity mediaEnt = null;
384 mediaEnt = (Entity)mediaStorage.getEntityClass().newInstance();
385 mediaEnt.setStorage(mediaStorage);
386 mediaEnt.setValues(mediaValues);
387 mediaId = mediaEnt.insert();
389 //save and store the media data/metadata
390 mediaHandler.set(mpReq.getMedia(), mediaEnt,
393 //were done with mpReq at this point, dereference it.
394 //as it contains mucho mem. -mh 01.10.2001
397 //we got this far, associate the media to the article
398 mediaEnt.setValueForProperty("is_published","1");
401 mediaProducer.handle(null, null, false, false, mediaId);
402 DatabaseContentToMedia.getInstance().addMedia(cid,mediaId);
403 } catch (Exception e) {
404 theLog.printError("setting media failed: "+e.toString());
405 contentModule.deleteById(cid);
406 throw new ServletModuleException("setting media failed: "
410 } //end for Iterator...
412 //if we're here all is ok...
413 EntityContent contentEnt = (EntityContent)contentModule.getById(cid);
414 contentEnt.setValueForProperty("is_published","1");
418 //dereference mp. -mh
421 // producing openpostinglist
422 new ProducerOpenPosting().handle(null,null,false,false);
423 // producing new page
424 new ProducerContent().handle(null, null, false, false,cid);
425 //if direct op producing startpage
426 if (directOp.equals("yes")) new ProducerStartPage().handle(null,null);
428 //produce the topicPages if set
429 //should be more intelligent
430 //if(setTopic==true) new ProducerTopics().handle(null,null);
433 //should be configureable
434 int exitValue = Helper.rsync();
435 theLog.printDebugInfo("rsync: "+exitValue);
438 catch (IOException e) { throw new ServletModuleException("IOException: "+ e.toString());}
439 catch (StorageObjectException e) { throw new ServletModuleException("StorageObjectException" + e.toString());}
440 catch (ModuleException e) { throw new ServletModuleException("ModuleException"+e.toString());}
442 deliver(req, res, mergeData, postingFormDoneTemplate);
445 private void _throwBadContentType (String fileName, String contentType)
446 throws ServletModuleUserException {
448 theLog.printDebugInfo("Wrong file type uploaded!: " + fileName+" "
450 throw new ServletModuleUserException("The file you uploaded is of the "
451 +"following mime-type: "+contentType
452 +", we do not support this mime-type. "
453 +"Error One or more files of unrecognized type. Sorry");