X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmircoders%2Flocalizer%2Fbasic%2FMirBasicPostingSessionHandler.java;h=786fd6268b1f32c6b68fffec067427e9a6fa3e0a;hb=04bf29aa1f1abf43e96d0272b2a286f0c1f9fea8;hp=921eb09953ec8a0b1b0999d6ab3a52fe5c47f480;hpb=a459f111d85598df56c6ab711cec11632676b39c;p=mir.git diff --git a/source/mircoders/localizer/basic/MirBasicPostingSessionHandler.java b/source/mircoders/localizer/basic/MirBasicPostingSessionHandler.java index 921eb099..786fd626 100755 --- a/source/mircoders/localizer/basic/MirBasicPostingSessionHandler.java +++ b/source/mircoders/localizer/basic/MirBasicPostingSessionHandler.java @@ -18,18 +18,17 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * In addition, as a special exception, The Mir-coders gives permission to link - * the code of this program with any library licensed under the Apache Software License, - * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library - * (or with modified versions of the above that use the same license as the above), - * and distribute linked combinations including the two. You must obey the - * GNU General Public License in all respects for all of the code used other than - * the above mentioned libraries. If you modify this file, you may extend this - * exception to your version of the file, but you are not obligated to do so. + * the code of this program with any library licensed under the Apache Software License, + * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library + * (or with modified versions of the above that use the same license as the above), + * and distribute linked combinations including the two. You must obey the + * GNU General Public License in all respects for all of the code used other than + * the above mentioned libraries. If you modify this file, you may extend this + * exception to your version of the file, but you are not obligated to do so. * If you do not wish to do so, delete this exception statement from your version. */ package mircoders.localizer.basic; -import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -38,7 +37,6 @@ import java.util.Random; import java.util.Vector; import mir.config.MirPropertiesConfiguration; -import mir.entity.Entity; import mir.log.LoggerWrapper; import mir.session.Request; import mir.session.Response; @@ -47,15 +45,11 @@ import mir.session.SessionExc; import mir.session.SessionFailure; import mir.session.SessionHandler; import mir.session.UploadedFile; +import mir.session.ValidationError; import mir.storage.StorageObject; import mir.util.ExceptionFunctions; -import mircoders.entity.EntityComment; import mircoders.global.MirGlobal; -import mircoders.media.MediaUploadProcessor; -import mircoders.module.ModuleComment; -import mircoders.storage.DatabaseComment; -import mircoders.storage.DatabaseCommentToMedia; -import mircoders.storage.DatabaseContent; +import mircoders.module.ModuleMediaType; /** * @@ -67,11 +61,15 @@ import mircoders.storage.DatabaseContent; * @version 1.0 */ -public class MirBasicPostingSessionHandler implements SessionHandler { +public abstract class MirBasicPostingSessionHandler implements SessionHandler { protected LoggerWrapper logger; protected MirPropertiesConfiguration configuration; - protected ModuleComment commentModule; - protected DatabaseCommentToMedia commentToMedia = DatabaseCommentToMedia.getInstance(); + + private String normalResponseGenerator; + private String dupeResponseGenerator; + private String unsupportedMediaTypeResponseGenerator; + private String finalResponseGenerator; + public MirBasicPostingSessionHandler() { logger = new LoggerWrapper("Localizer.OpenPosting"); @@ -83,288 +81,233 @@ public class MirBasicPostingSessionHandler implements SessionHandler { throw new RuntimeException("Cannot load configuration: " + t.toString()); } - commentModule= new ModuleComment(DatabaseComment.getInstance()); + } + + protected void setNormalResponseGenerator(String aGenerator) { + normalResponseGenerator = aGenerator; + } + + protected void setResponseGenerators(String aNormalResponseGenerator, String aDupeResponseGenerator, + String anUnsupportedMediaTypeResponseGenerator, String aFinalResponseGenerator) { + setNormalResponseGenerator(aNormalResponseGenerator); + dupeResponseGenerator = aDupeResponseGenerator; + unsupportedMediaTypeResponseGenerator = anUnsupportedMediaTypeResponseGenerator; + finalResponseGenerator = aFinalResponseGenerator; } public void processRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure { - if (aSession.getAttribute("initialRequest")==null) { - initialRequest(aRequest, aSession, aResponse); - aSession.setAttribute("initialRequest", "no"); + if (MirGlobal.abuse().getOpenPostingDisabled()) { + makeOpenPostingDisabledResponse(aRequest, aSession, aResponse); + aSession.terminate(); } else { - subsequentRequest(aRequest, aSession, aResponse); + if (aSession.getAttribute("initialRequest") == null) { + initialRequest(aRequest, aSession, aResponse); + aSession.setAttribute("initialRequest", "no"); + } + else { + subsequentRequest(aRequest, aSession, aResponse); + } } }; - protected Map getIntersectingValues(Request aRequest, StorageObject aStorage) throws SessionExc, SessionFailure { - Map result = new HashMap(); - - Iterator i = aStorage.getFields().iterator(); - - while (i.hasNext()) { - String fieldName = (String) i.next(); - Object value = aRequest.getParameter(fieldName); - if (value != null) - result.put(fieldName, value); - } - - return result; + protected void initialRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure { + initializeSession(aRequest, aSession); + initializeResponseData(aRequest, aSession, aResponse); + makeInitialResponse(aRequest, aSession, aResponse); } + public void subsequentRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure { + try { + try { + List validationErrors = new Vector(); - protected String generateOnetimePassword() { - Random r = new Random(); - int random = r.nextInt(); - - long l = System.currentTimeMillis(); - - l = (l*l*l*l)/random; - if (l<0) - l = l * -1; - - String returnString = ""+l; + if (!shouldProcessRequest(aRequest, aSession, validationErrors)) { + initializeResponseData(aRequest, aSession, aResponse); + makeResponse(aRequest, aSession, aResponse, validationErrors); + } + else { + preProcessRequest(aRequest, aSession); + Iterator i = aRequest.getUploadedFiles().iterator(); + while (i.hasNext()) { + processUploadedFile(aRequest, aSession, (UploadedFile) i.next()); + } + postProcessRequest(aRequest, aSession); + initializeResponseData(aRequest, aSession, aResponse); + makeFinalResponse(aRequest, aSession, aResponse); + aSession.terminate(); + } + } + catch (Throwable t) { + initializeResponseData(aRequest, aSession, aResponse); + makeErrorResponse(aRequest, aSession, aResponse, t); + aSession.terminate(); + } + } + catch (Throwable t) { + aSession.terminate(); - return returnString.substring(5); + throw new SessionFailure(t); + } } - protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure { + protected void initializeSession(Request aRequest, Session aSession) throws SessionExc, SessionFailure { if (MirGlobal.abuse().getOpenPostingPassword()) { String password = (String) aSession.getAttribute("password"); if (password==null) { password = generateOnetimePassword(); aSession.setAttribute("password", password); } - aResponse.setResponseValue("password", password); } else { - aResponse.setResponseValue("password", null); aSession.deleteAttribute("password"); } - aResponse.setResponseValue("errors", null); - }; - - protected void initialRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure{ - Iterator i = DatabaseComment.getInstance().getFields().iterator(); - while (i.hasNext()) { - aResponse.setResponseValue( (String) i.next(), null); - } - - String articleId = aRequest.getParameter("to_media"); + logger.debug("referrer = " + aRequest.getHeader("Referer")); - if (articleId == null) - throw new SessionExc("MirBasicPostingSessionHandler.initialRequest: article id not set!"); - - aSession.setAttribute("to_media", articleId); + aSession.setAttribute("referer", aRequest.getHeader("Referer")); + } - initializeResponseData(aRequest, aSession, aResponse); + protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure { + int nrMediaItems = configuration.getInt("ServletModule.OpenIndy.DefaultMediaUploadItems", 5); + if (aSession.getAttribute("nrmediaitems")!=null) { + nrMediaItems = ((Integer) aSession.getAttribute("nrmediaitems")).intValue(); + } try { - aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.EditTemplate")); + nrMediaItems = Math.min(configuration.getInt("ServletModule.OpenIndy.MaxMediaUploadItems"), Integer.parseInt(aRequest.getParameter("nrmediaitems"))); } - catch (Throwable e) { - throw new SessionFailure("Can't get configuration: " + e.getMessage(), e); + catch (Throwable t) { } + aSession.setAttribute("nrmediaitems", new Integer(nrMediaItems)); - } + List mediaItems = new Vector(); + int i=0; - protected boolean testFieldEntered(Request aRequest, String aFieldName, String anErrorMessageResource, List aValidationResults) { - Object value = aRequest.getParameter(aFieldName); - if (value==null || !(value instanceof String) || ((String) value).trim().length()==0) { - logger.debug(" missing field " + aFieldName + " value = " + value); - aValidationResults.add(new ValidationError(aFieldName, anErrorMessageResource)); - return false; + while (i0) { - returnValidationErrors(aRequest, aSession, aResponse, validationErrors); - } - else { -// finish(aRequest, aSession, aResponse); + protected void makeInitialResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure { + aResponse.setResponseGenerator(normalResponseGenerator); + }; - EntityComment comment = (EntityComment) commentModule.createNew (); -// comment.setValues(getIntersectingValues(aRequest, )); + protected void makeResponse(Request aRequest, Session aSession, Response aResponse, List anErrors) throws SessionExc, SessionFailure { + aResponse.setResponseValue("errors", anErrors); + aResponse.setResponseGenerator(normalResponseGenerator); + }; - finishComment(aRequest, aSession, comment); + protected void makeFinalResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure { + aResponse.setResponseGenerator(finalResponseGenerator); + }; - String id = comment.insert(); - if(id==null){ - afterDuplicateCommentPosting(aRequest, aSession, aResponse, comment); - logger.info("Dupe comment rejected"); - aSession.terminate(); - } - else { - i = aRequest.getUploadedFiles().iterator(); - while (i.hasNext()) { - UploadedFile file = (UploadedFile) i.next(); - processMediaFile(aRequest, aSession, comment, file); - } + protected void makeErrorResponse(Request aRequest, Session aSession, Response aResponse, Throwable anError) throws SessionExc, SessionFailure { + anError.printStackTrace(); + Throwable rootCause = ExceptionFunctions.traceCauseException(anError); - afterCommentPosting(aRequest, aSession, aResponse, comment); - MirGlobal.abuse().checkComment(comment, aRequest, null); - MirGlobal.localizer().openPostings().afterCommentPosting(comment); - logger.info("Comment posted"); - aSession.terminate(); - } - } + if (rootCause instanceof DuplicatePostingExc) + aResponse.setResponseGenerator(dupeResponseGenerator); + if (rootCause instanceof ModuleMediaType.UnsupportedMimeTypeExc) { + aResponse.setResponseValue("mimetype", ((ModuleMediaType.UnsupportedMimeTypeExc) rootCause).getMimeType()); + aResponse.setResponseGenerator(unsupportedMediaTypeResponseGenerator); } - catch (Throwable t) { - ExceptionFunctions.traceCauseException(t).printStackTrace(); - - throw new SessionFailure("MirBasicPostingSessionHandler.subsequentRequest: " + t.getMessage(), t); + else { + aResponse.setResponseValue("errorstring", anError.getMessage()); + aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.ErrorTemplate")); } - } - - public void initializeCommentPosting(Request aRequest, Session aSession, Response aResponse) throws SessionFailure, SessionExc { - String articleId = aRequest.getParameter("to_media"); - if (articleId==null) - articleId = aRequest.getParameter("aid"); + }; - if (articleId==null) - throw new SessionExc("initializeCommentPosting: article id not set!"); + protected void makeOpenPostingDisabledResponse(Request aRequest, Session aSession, Response aResponse) { + aResponse.setResponseGenerator(configuration.getString("ServletModule.OpenIndy.PostingDisabledTemplate")); + } - aSession.setAttribute("to_media", articleId); - processCommentPosting(aRequest, aSession, aResponse); + protected void preProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure { }; - - public void returnValidationErrors(Request aRequest, Session aSession, Response aResponse, List aValidationErrors) throws SessionFailure, SessionExc { - aResponse.setResponseValue("errors", aValidationErrors); - aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.EditTemplate")); + public void processUploadedFile(Request aRequest, Session aSession, UploadedFile aFile) throws SessionExc, SessionFailure { + }; + protected void postProcessRequest(Request aRequest, Session aSession) throws SessionExc, SessionFailure { }; - public void processCommentPosting(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure { - if (MirGlobal.abuse().getOpenPostingPassword()) { - String password = generateOnetimePassword(); - aSession.setAttribute("password", password); - aResponse.setResponseValue("password", password); - aResponse.setResponseValue("passwd", password); - } + protected boolean shouldProcessRequest(Request aRequest, Session aSession, List aValidationErrors) throws SessionExc, SessionFailure { + if (aRequest.getParameter("post")==null) + return false; else { - aResponse.setResponseValue("password", null); + validate(aValidationErrors, aRequest, aSession); + return (aValidationErrors == null || aValidationErrors.size() == 0); } + } - aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.EditTemplate")); - }; + protected void validate(List aResults, Request aRequest, Session aSession) throws SessionExc, SessionFailure { + String password = (String) aSession.getAttribute("password"); - public void processMediaFile(Request aRequest, Session aSession, EntityComment aComment, UploadedFile aFile) throws SessionExc, SessionFailure { - try { - Entity mediaItem = MediaUploadProcessor.processMediaUpload(aFile, new HashMap()); - finishMedia(aRequest, aSession, aFile, mediaItem); - mediaItem.update(); - commentToMedia.addMedia(aComment.getId(), mediaItem.getId()); - } - catch (Throwable t) { - throw new SessionFailure(t); + if (password!=null) { + String submittedPassword= aRequest.getParameter("password").trim(); + + if (!password.equals(submittedPassword)) { + aResults.add(new ValidationError("password", "passwordmismatch")); + } } } - public void finishMedia(Request aRequest, Session aSession, UploadedFile aFile, Entity aMedia) throws SessionExc, SessionFailure { - } - public void finishComment(Request aRequest, Session aSession, EntityComment aComment) throws SessionExc, SessionFailure { - if (aSession.getAttribute("to_media") == null) - throw new SessionExc("missing to_media"); + /** + * Method to generate a one-time password + * + * @return a password, to be used once + */ - aComment.setValueForProperty("is_published", "1"); - aComment.setValueForProperty("to_comment_status", "1"); - aComment.setValueForProperty("is_html","0"); - aComment.setValueForProperty("to_media", (String) aSession.getAttribute("to_media")); - }; + protected String generateOnetimePassword() { + Random r = new Random(); + int random = r.nextInt(); - public void addMedia(Request aRequest, Session aSession, EntityComment aComment) throws SessionExc, SessionFailure { - } + long l = System.currentTimeMillis(); - public void afterCommentPosting(Request aRequest, Session aSession, Response aResponse, EntityComment aComment) { - DatabaseContent.getInstance().setUnproduced("id=" + aComment.getValue("to_media")); - aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.DoneTemplate")); - }; + l = (l*l*l*l)/random; + if (l<0) + l = l * -1; - public void afterDuplicateCommentPosting(Request aRequest, Session aSession, Response aResponse, EntityComment aComment) { - aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.DupeTemplate")); - }; + String returnString = ""+l; - public class ValidationError { - private String field; - private String message; - private List parameters; + return returnString.substring(5); + } - public ValidationError(String aField, String aMessage) { - this (aField, aMessage, new String[] {}); - } - public ValidationError(String aField, String aMessage, Object aParameter) { - this (aField, aMessage, new Object[] {aParameter}); - } + /** + * + * @param aRequest + * @param aStorage + * @return + * @throws SessionExc + * @throws SessionFailure + */ - public ValidationError(String aField, String aMessage, Object[] aParameters) { - field = aField; - message = aMessage; - parameters = Arrays.asList(aParameters); - } + protected static final Map getIntersectingValues(Request aRequest, StorageObject aStorage) throws SessionExc, SessionFailure { + Map result = new HashMap(); - public String getMessage() { - return message; - } + Iterator i = aStorage.getFields().iterator(); - public String getField() { - return field; + while (i.hasNext()) { + String fieldName = (String) i.next(); + Object value = aRequest.getParameter(fieldName); + if (value != null) + result.put(fieldName, value); } - public List getParameters() { - return parameters; - } + return result; } - + protected static class DuplicatePostingExc extends SessionExc { + public DuplicatePostingExc(String aMessage) { + super(aMessage); + } + } }