From f9ea507d9f61aa6db3107551086bdb3fad56d8ba Mon Sep 17 00:00:00 2001 From: mh Date: Tue, 29 Jan 2002 19:57:36 +0000 Subject: [PATCH] a bunch of bufixes and also a new ServletModuleUserException so that we can return a nice error page for user errors. (like forgetting to add a title.) --- source/OpenMir.java | 46 ++++++------- source/config.properties-dist | 1 + source/mir/module/ModuleUserException.java | 20 ++++++ source/mir/servlet/ServletModuleDispatch.java | 15 +++-- source/mir/servlet/ServletModuleUserException.java | 27 ++++++++ source/mircoders/producer/ProducerAll.java | 5 +- source/mircoders/producer/ProducerMedia.java | 16 +++-- source/mircoders/servlet/ServletModuleImages.java | 22 ++----- .../mircoders/servlet/ServletModuleOpenIndy.java | 75 +++++++++++----------- 9 files changed, 136 insertions(+), 91 deletions(-) create mode 100755 source/mir/module/ModuleUserException.java create mode 100755 source/mir/servlet/ServletModuleUserException.java diff --git a/source/OpenMir.java b/source/OpenMir.java index 7ade219a..2002b9f2 100755 --- a/source/OpenMir.java +++ b/source/OpenMir.java @@ -9,7 +9,6 @@ import java.sql.*; import freemarker.template.*; import mir.misc.*; -import mir.xml.XmlConfigurator; import mir.servlet.*; import mircoders.servlet.*; @@ -36,32 +35,6 @@ public class OpenMir extends AbstractServlet { doPost(req,res); } - public void init() throws ServletException { - try { - XmlConfigurator xmlXonfigurator = XmlConfigurator.getInstance(); - xmlXonfigurator.addRequiredTag("/mir/class:mir.storage.DatabaseConfig/property:username"); - xmlXonfigurator.addRequiredTag("/mir/class:mir.storage.DatabaseConfig/property:password"); - xmlXonfigurator.addComesFirstTag("/mir/class:mir.storage.DatabaseConfig/property:password"); - xmlXonfigurator.addComesFirstTag("/mir/class:mir.storage.DatabaseConfig/property:username"); - xmlXonfigurator.addComesFirstTag("/mir/class:mir.storage.DatabaseConfig/property:host"); - xmlXonfigurator.addComesFirstTag("/mir/class:mir.storage.DatabaseConfig/property:adaptor"); - xmlXonfigurator.addComesFirstTag("/mir/class:mir.storage.DatabaseConfig/property:limit"); - xmlXonfigurator.addComesFirstTag("/mir/class:mir.storage.DatabaseConfig/property:poolMax"); - xmlXonfigurator.addComesFirstTag("/mir/class:mir.storage.DatabaseConfig/property:poolMin"); - xmlXonfigurator.addComesFirstTag("/mir/class:mir.storage.DatabaseConfig/property:poolResetTime"); - xmlXonfigurator.addComesFirstTag("/mir/class:mir.storage.DatabaseConfig/property:poolLog"); - xmlXonfigurator.configure(new File(getInitParameter("xml-config"))); - System.err.println("INIT"); - /*try { - MirXmlParser xmlParser = new MirXmlParser(); - xmlParser.parse("/home/heckmann/server.xml");*/ - } catch (Exception e) { - e.printStackTrace(); - throw new UnavailableException ("Error loading Config file: "+e.toString(), 0); - } - System.err.println("DONE INIT"); - } - public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { @@ -83,6 +56,9 @@ public class OpenMir extends AbstractServlet { try { ServletModuleDispatch.dispatch(ServletModuleOpenIndy.getInstance(),req,res); } + catch (ServletModuleUserException e) { + handleUserError(res,res.getWriter(), e.getMsg()); + } catch (ServletModuleException e){ e.printStackTrace(); handleError(res,res.getWriter(), "OpenIndy :: ServletException in Module ServletModule -- " + e.toString()); @@ -92,6 +68,22 @@ public class OpenMir extends AbstractServlet { theLog.printInfo("EXECTIME (ServletModuleOpenIndy): " + sessionConnectTime + " ms"); } + private void handleUserError(HttpServletResponse res,PrintWriter out, String errorString) { + + try { + theLog.printError(errorString); + SimpleHash modelRoot = new SimpleHash(); + modelRoot.put("errorstring", new SimpleScalar(errorString)); + modelRoot.put("date", new SimpleScalar(StringUtil.date2readableDateTime(new GregorianCalendar()))); + HTMLTemplateProcessor.process(res,MirConfig.getProp("Mir.UserErrorTemplate"),modelRoot,out); + out.close(); + } + catch (Exception e) { + System.err.println("Fehler in UserErrorTemplate"); + } + + } + private void handleError(HttpServletResponse res,PrintWriter out, String errorString) { try { diff --git a/source/config.properties-dist b/source/config.properties-dist index 952f2cae..3684fa03 100755 --- a/source/config.properties-dist +++ b/source/config.properties-dist @@ -187,6 +187,7 @@ Entity.Logfile=log/dbentity.log Mir.Logfile=log/mir.log Mir.ErrorTemplate=fehler.template +Mir.UserErrorTemplate=usererror.template ServletModule.Schwerpunkt.ConfirmTemplate=confirm.template ServletModule.Schwerpunkt.ObjektTemplate=schwerpunkt.template diff --git a/source/mir/module/ModuleUserException.java b/source/mir/module/ModuleUserException.java new file mode 100755 index 00000000..e26c5030 --- /dev/null +++ b/source/mir/module/ModuleUserException.java @@ -0,0 +1,20 @@ +package mir.module; + +import java.lang.*; + +/* + * ServletModuleException - + * wird vom ServletModule geschmissen + * + * + * @version 5.7.199 + * @author RK + */ + +public final class ModuleUserException extends Exception +{ + public ModuleUserException(String msg) { + super(msg); + } +} + diff --git a/source/mir/servlet/ServletModuleDispatch.java b/source/mir/servlet/ServletModuleDispatch.java index de5d86dd..472b48e4 100755 --- a/source/mir/servlet/ServletModuleDispatch.java +++ b/source/mir/servlet/ServletModuleDispatch.java @@ -19,7 +19,7 @@ public final class ServletModuleDispatch { static Logfile theLog; static { - theLog = Logfile.getInstance("ServletModuleDispatch"); + theLog = Logfile.getInstance("/tmp/smod.dispatch"); } /** @@ -43,7 +43,7 @@ public final class ServletModuleDispatch { */ public static void dispatch(ServletModule sMod, HttpServletRequest req, - HttpServletResponse res) throws ServletModuleException + HttpServletResponse res) throws ServletModuleException, ServletModuleUserException { //sMod.predeliver(req,res); @@ -66,9 +66,16 @@ public final class ServletModuleDispatch { } catch ( NoSuchMethodException e) { throw new ServletModuleException("no such method!" + e.toString());} catch ( SecurityException e) { throw new ServletModuleException("method not allowed!" + e.toString());} - catch ( InvocationTargetException e) {e.printStackTrace();throw new ServletModuleException("target method exception!" + e.getTargetException().toString());} + catch ( InvocationTargetException e) { + if (e.getTargetException().getClass().getName() == "mir.servlet.ServletModuleUserException") { + throw new ServletModuleUserException(((ServletModuleUserException)e.getTargetException()).getMsg()); + } else { + e.printStackTrace(); + throw new ServletModuleException(e.getTargetException().toString()); + } + } catch ( IllegalAccessException e) { throw new ServletModuleException("illegal method not allowed!" + e.toString());} -// catch ( Exception e ) { throw new ServletModuleException(e.toString()); } + //catch ( ServletModuleException e ) { throw new ServletModuleException(e.toString()); } throw new ServletModuleException("delivery failed! -- "); } diff --git a/source/mir/servlet/ServletModuleUserException.java b/source/mir/servlet/ServletModuleUserException.java new file mode 100755 index 00000000..9b83c960 --- /dev/null +++ b/source/mir/servlet/ServletModuleUserException.java @@ -0,0 +1,27 @@ +package mir.servlet; + +import java.lang.*; + +/* + * ServletModuleException - + * wird vom ServletModule geschmissen + * + * + * @version 28.6.199 + * @author RK + */ + +public final class ServletModuleUserException extends Exception +{ + String msg; + + public ServletModuleUserException(String msg) { + super(msg); + this.msg = msg; + } + + public String getMsg() { + return msg; + } +} + diff --git a/source/mircoders/producer/ProducerAll.java b/source/mircoders/producer/ProducerAll.java index b437b27f..4f21327b 100755 --- a/source/mircoders/producer/ProducerAll.java +++ b/source/mircoders/producer/ProducerAll.java @@ -33,12 +33,13 @@ public class ProducerAll extends Producer{ long sessionConnectTime = 0; long startTime = (new java.util.Date()).getTime(); - new ProducerImages().handle(htmlout, user, force,sync); + //new ProducerImages().handle(htmlout, user, force,sync); + new ProducerMedia().handle(htmlout, user, force,sync); new ProducerStartPage().handle(htmlout, user, force,sync); new ProducerContent().handle(htmlout, user, force,sync); new ProducerOpenPosting().handle(htmlout, user, force,sync); new ProducerTopics().handle(htmlout, user, force,sync); - new ProducerNavigation().handle(htmlout, user, force,sync); + new ProducerNavigation().handle(htmlout, user, force,sync); // Finish sessionConnectTime = new java.util.Date().getTime() - startTime; diff --git a/source/mircoders/producer/ProducerMedia.java b/source/mircoders/producer/ProducerMedia.java index 8a9dea47..93eda600 100755 --- a/source/mircoders/producer/ProducerMedia.java +++ b/source/mircoders/producer/ProducerMedia.java @@ -40,6 +40,7 @@ public class ProducerMedia extends Producer { boolean mediaProduced = false; String whereClause; String mediaHost; + String id2=null; String iconPath; String mediaPath; String iconFilename; @@ -65,6 +66,7 @@ public class ProducerMedia extends Producer { Entity currentMediaType = DatabaseUploadedMedia.getInstance().getMediaType(currentMedia); String mediaHandlerName = currentMediaType.getValue("classname"); + theLog.printError("HANLDER"+mediaHandlerName); Class mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName); MirMedia currentMediaHandler = (MirMedia)mediaHandlerClass.newInstance(); @@ -88,19 +90,23 @@ public class ProducerMedia extends Producer { //now produce the media content + theLog.printError("ABOUT OT MED"+id2); mediaHost = currentMediaHandler.getPublishHost(); mediaPath = currentMediaHandler.getStoragePath(); + id2 = currentMedia.getId(); mediaFilename = currentMedia.getId()+"."+currentMediaType.getValue("name"); //hack: make this a config option. -mh - //productionPath = mediaPath+datePath+"/"+ mediaFilename ; - productionPath = mediaPath+"/"+mediaFilename ; + productionPath = mediaPath+datePath+"/"+ mediaFilename ; + //productionPath = mediaPath+"/"+mediaFilename ; //hack: see above. -mh. - //currentMedia.setValueForProperty("publish_path",datePath+"/"+mediaFilename); - currentMedia.setValueForProperty("publish_path",mediaFilename); + currentMedia.setValueForProperty("publish_path",datePath+"/"+mediaFilename); + //currentMedia.setValueForProperty("publish_path",mediaFilename); currentMedia.setValueForProperty("publish_server", mediaHost); + theLog.printError("ABOUT OT FILE"+id2); mediaProduced = produceFile(productionPath, currentMediaHandler.get(currentMedia, currentMediaType), htmlout, false); + theLog.printError("FILED"+id2); logHTML(htmlout,"media: " + productionPath + ((mediaProduced==true)?" succeded":" ")); // update media-data @@ -111,7 +117,7 @@ public class ProducerMedia extends Producer { } } catch (Exception e) { - theLog.printError("failed in reflection: "+e); + theLog.printError("failed in reflection: "+id+e.toString()); } } diff --git a/source/mircoders/servlet/ServletModuleImages.java b/source/mircoders/servlet/ServletModuleImages.java index e8702f56..dbea216a 100755 --- a/source/mircoders/servlet/ServletModuleImages.java +++ b/source/mircoders/servlet/ServletModuleImages.java @@ -49,7 +49,7 @@ public class ServletModuleImages extends mir.servlet.ServletModule private ServletModuleImages() { - theLog = Logfile.getInstance(this.getClass().getName()); + theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("ServletModule.Bilder.Logfile")); templateListString = MirConfig.getProp("ServletModule.Bilder.ListTemplate"); templateObjektString = MirConfig.getProp("ServletModule.Bilder.ObjektTemplate"); templateConfirmString = MirConfig.getProp("ServletModule.Bilder.ConfirmTemplate"); @@ -160,10 +160,10 @@ public class ServletModuleImages extends mir.servlet.ServletModule try { WebdbMultipartRequest mp = new WebdbMultipartRequest(req); HashMap parameters = mp.getParameters(); - MpRequest mpReq = (MpRequest)mp.requestList.get(0); - byte[] imageData=mpReq.getMedia(); - String fileName=mpReq.getFilename(); - String contentType=mpReq.getContentType(); + //MpRequest mpReq = (MpRequest)mp.requestList.get(0); + //byte[] imageData=mpReq.getMedia(); + //String fileName=mpReq.getFilename(); + //String contentType=mpReq.getContentType(); EntityUsers user = _getUser(req); parameters.put("to_publisher", user.getId()); @@ -172,17 +172,7 @@ public class ServletModuleImages extends mir.servlet.ServletModule parameters.put("is_published","0"); String id = mainModule.set(parameters); - EntityImages entImage = (EntityImages)mainModule.getById(id); - - if (imageData!=null && fileName!=null) { - String fileType = "-1"; - if (contentType.equals("image/jpeg")) fileType="0"; - if (contentType.equals("image/gif")) fileType="1"; - if (fileType != "-1") - entImage.setImage(imageData, fileType); - else - theLog.printError("Wrong file uploaded!"); - } + theLog.printError("Image ID"+id); _edit(id, req, res); } catch (IOException e) { throw new ServletModuleException("upload -- ioexception " + e.toString());} diff --git a/source/mircoders/servlet/ServletModuleOpenIndy.java b/source/mircoders/servlet/ServletModuleOpenIndy.java index ff293544..9c6d5014 100755 --- a/source/mircoders/servlet/ServletModuleOpenIndy.java +++ b/source/mircoders/servlet/ServletModuleOpenIndy.java @@ -36,40 +36,28 @@ import mircoders.producer.*; public class ServletModuleOpenIndy extends ServletModule { + + private String commentFormTemplate, commentFormDoneTemplate, commentFormDupeTemplate; + private String postingFormTemplate, postingFormDoneTemplate, postingFormDupeTemplate; private ModuleContent contentModule; private ModuleImages imageModule; private ModuleTopics themenModule; - - //config values - private static String commentFormTemplate = "open/comment.template"; - private static String commentFormDoneTemplate = "open/comment_done.template"; - private static String commentFormDupeTemplate = "open/comment_dupe.template"; - private static String postingFormTemplate = "open/posting.template"; - private static String postingFormDoneTemplate = "open/posting_done.template"; - private static String postingFormDupeTemplate = "open/posting_dupe.template"; - private static boolean directOp = true; + private String directOp ="yes"; // Singelton / Kontruktor private static ServletModuleOpenIndy instance = new ServletModuleOpenIndy(); public static ServletModule getInstance() { return instance; } - public static void setDirectOp(boolean v) { - directOp=v; - } - -// public static setCommentFormTemplate(String template) throws { - // if - private ServletModuleOpenIndy() { try { - theLog = Logfile.getInstance(this.getClass().getName()); - //commentFormTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentTemplate"); - //commentFormDoneTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentDoneTemplate"); - //commentFormDupeTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentDupeTemplate"); - //postingFormTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingTemplate"); - //postingFormDoneTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingDoneTemplate"); - //postingFormDupeTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingDupeTemplate"); - //directOp = MirConfig.getProp("DirectOpenposting").toLowerCase(); + theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("ServletModule.OpenIndy.Logfile")); + commentFormTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentTemplate"); + commentFormDoneTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentDoneTemplate"); + commentFormDupeTemplate = MirConfig.getProp("ServletModule.OpenIndy.CommentDupeTemplate"); + postingFormTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingTemplate"); + postingFormDoneTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingDoneTemplate"); + postingFormDupeTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingDupeTemplate"); + directOp = MirConfig.getProp("DirectOpenposting").toLowerCase(); mainModule = new ModuleComment(DatabaseComment.getInstance()); contentModule = new ModuleContent(DatabaseContent.getInstance()); themenModule = new ModuleTopics(DatabaseTopics.getInstance()); @@ -188,7 +176,7 @@ public class ServletModuleOpenIndy extends ServletModule */ public void insposting(HttpServletRequest req, HttpServletResponse res) - throws ServletModuleException + throws ServletModuleException, ServletModuleUserException { SimpleHash mergeData = new SimpleHash(); boolean setMedia=false; @@ -197,6 +185,11 @@ public class ServletModuleOpenIndy extends ServletModule WebdbMultipartRequest mp = new WebdbMultipartRequest(req); HashMap withValues = mp.getParameters(); + + if ((((String)withValues.get("title")).length() == 0) || + (((String)withValues.get("description")).length() == 0) || + (((String)withValues.get("content_data")).length() == 0)) + throw new ServletModuleUserException("Missing field"); // call the routines that escape html @@ -217,10 +210,11 @@ public class ServletModuleOpenIndy extends ServletModule withValues.put("date", StringUtil.date2webdbDate(new GregorianCalendar())); withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date"))); withValues.put("is_produced", "0"); - // op-articles are immediatly published - withValues.put("is_published","1"); + // op-articles are not immediatly published + // we don't know that all is good yet (media, title is present, etc..) + withValues.put("is_published","0"); // if op direct article-type == newswire - if (directOp) withValues.put("to_article_type","1"); + if (directOp.equals("yes")) withValues.put("to_article_type","1"); // owner is openposting user withValues.put("to_publisher","1"); @@ -294,7 +288,7 @@ public class ServletModuleOpenIndy extends ServletModule if (contentType.equals("text/plain") || contentType.equals("application/octet-stream")) { - throw new ServletModuleException("ModuleException: One or more files of unrecognized types"); + throw new ServletModuleUserException("One or more files of unrecognized types"); } String mediaTitle=(String)withValues.get("media_title"+i); @@ -308,7 +302,7 @@ public class ServletModuleOpenIndy extends ServletModule mediaValues.put("to_publisher", "1"); // op user mediaValues.put("to_media_folder", "7"); // op media_folder mediaValues.put("is_produced", "0"); - mediaValues.put("is_published","1"); + mediaValues.put("is_published","0"); //the where clause to find the media_type entry //from the content-type. @@ -333,11 +327,12 @@ public class ServletModuleOpenIndy extends ServletModule //load the classes via reflection String MediaId; + Entity mediaEnt = null; try { Class mediaStorageClass = Class.forName("mircoders.storage.Database"+mediaStorageName); Method m = mediaStorageClass.getMethod("getInstance", null); Database mediaStorage = (Database)m.invoke(null, null); - Entity mediaEnt = (Entity)mediaStorage.getEntityClass().newInstance(); + mediaEnt = (Entity)mediaStorage.getEntityClass().newInstance(); mediaEnt.setStorage(mediaStorage); mediaEnt.setValues(mediaValues); mediaId = mediaEnt.insert(); @@ -351,28 +346,35 @@ public class ServletModuleOpenIndy extends ServletModule //as it contains mucho mem. -mh 01.10.2001 mpReq=null; - if(mediaId!=null){ - new ProducerMedia().handle(null, null, false, false, mediaId); - } } catch (Exception e) { theLog.printError("setting uploaded_media failed: "+e.toString()); } //end try-catch //we got this far, associate the media to the article try{ + theLog.printError("ID"+mediaId); DatabaseContentToMedia.getInstance().addMedia(cid,mediaId); + mediaEnt.setValueForProperty("is_published","1"); + mediaEnt.update(); + new ProducerMedia().handle(null,null,false,false,mediaId); theLog.printError("setting content_x_media success"); } catch (Exception e) { theLog.printError("setting content_x_media failed"); } } else { + contentModule.deleteById(cid); theLog.printDebugInfo("Wrong file uploaded!: " + fileName); - throw new ServletModuleException("ModuleException: One or more files of unrecognized types"); + throw new ServletModuleUserException("One or more files of unrecognized types"); } // end if-else mediaTypesList.size() > 0 } //end for Iterator... + //if we're here all is ok... + EntityContent contentEnt = (EntityContent)contentModule.getById(cid); + contentEnt.setValueForProperty("is_published","1"); + contentEnt.update(); + //dereference mp. -mh mp=null; @@ -381,7 +383,7 @@ public class ServletModuleOpenIndy extends ServletModule // producing new page new ProducerContent().handle(null, null, false, false,cid); //if direct op producing startpage - if (directOp) new ProducerStartPage().handle(null,null); + if (directOp.equals("yes")) new ProducerStartPage().handle(null,null); // sync the server //should be configureable @@ -394,7 +396,6 @@ public class ServletModuleOpenIndy extends ServletModule catch (ModuleException e) { throw new ServletModuleException("ModuleException"+e.toString());} deliver(req, res, mergeData, postingFormDoneTemplate); - //System.gc(); } } -- 2.11.0