X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmircoders%2Flocalizer%2Fbasic%2FMirBasicPostingSessionHandler.java;h=786fd6268b1f32c6b68fffec067427e9a6fa3e0a;hb=04bf29aa1f1abf43e96d0272b2a286f0c1f9fea8;hp=245e6b3a2afa9f0df635d78046afb45cac7cde4b;hpb=87809334c4eaf3955a1cfe609425ad2f07a26351;p=mir.git diff --git a/source/mircoders/localizer/basic/MirBasicPostingSessionHandler.java b/source/mircoders/localizer/basic/MirBasicPostingSessionHandler.java index 245e6b3a..786fd626 100755 --- a/source/mircoders/localizer/basic/MirBasicPostingSessionHandler.java +++ b/source/mircoders/localizer/basic/MirBasicPostingSessionHandler.java @@ -1,37 +1,34 @@ -/* - * Copyright (C) 2001, 2002 The Mir-coders group - * - * This file is part of Mir. - * - * Mir is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * Mir is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Mir; if not, write to the Free Software - * 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 the com.oreilly.servlet library, 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; +/* + * Copyright (C) 2001, 2002 The Mir-coders group + * + * This file is part of Mir. + * + * Mir is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Mir is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Mir; if not, write to the Free Software + * 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. + * If you do not wish to do so, delete this exception statement from your version. + */ +package mircoders.localizer.basic; + import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -40,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; @@ -49,324 +45,269 @@ 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; - -/** - * - *

Title: Experimental session handler for comment postings

- *

Description:

- *

Copyright: Copyright (c) 2003

- *

Company:

- * @author not attributable - * @version 1.0 - */ - -public class MirBasicPostingSessionHandler implements SessionHandler { - protected LoggerWrapper logger; - protected MirPropertiesConfiguration configuration; - protected ModuleComment commentModule; - protected DatabaseCommentToMedia commentToMedia = DatabaseCommentToMedia.getInstance(); - - public MirBasicPostingSessionHandler() { - logger = new LoggerWrapper("Localizer.OpenPosting"); - try { - configuration = MirPropertiesConfiguration.instance(); - } - catch (Throwable t) { - logger.fatal("Cannot load configuration: " + t.toString()); - - throw new RuntimeException("Cannot load configuration: " + t.toString()); - } - commentModule= new ModuleComment(DatabaseComment.getInstance()); - } - - 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"); - } - 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 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; - - return returnString.substring(5); - } - - protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) 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"); - - if (articleId == null) - throw new SessionExc("MirBasicPostingSessionHandler.initialRequest: article id not set!"); - - aSession.setAttribute("to_media", articleId); - - initializeResponseData(aRequest, aSession, aResponse); - - try { - aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.EditTemplate")); - } - catch (Throwable e) { - throw new SessionFailure("Can't get configuration: " + e.getMessage(), e); - } - - } - - 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; - } - else - return true; - } - - protected boolean testFieldIsNumeric(Request aRequest, String aFieldName, String anErrorMessageResource, List aValidationResults) { - Object value = aRequest.getParameter(aFieldName); - if (value!=null) { - try { - Integer.parseInt((String) value); - return true; - } - catch (Throwable t) { - logger.debug(" field not numeric: " + aFieldName + " value = " + value); - aValidationResults.add(new ValidationError(aFieldName, anErrorMessageResource)); - return false; - } - } - return true; - } - - public void validate(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure { - - } - - public List validate(Request aRequest, Session aSession) throws SessionExc, SessionFailure { - List result = new Vector(); - - testFieldEntered(aRequest, "title", "validationerror.missing", result); - testFieldEntered(aRequest, "description", "validationerror.missing", result); - testFieldEntered(aRequest, "creator", "validationerror.missing", result); - - return result; - } - - public void subsequentRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure { - try { - Map commentFields = new HashMap(); - - Iterator i = DatabaseContent.getInstance().getFields().iterator(); - while (i.hasNext()) { - String field = (String) i.next(); - aResponse.setResponseValue(field, aRequest.getParameter(field)); - if (aRequest.getParameter(field)!=null) { - commentFields.put(field, aRequest.getParameter(field)); - } - } - - initializeResponseData(aRequest, aSession, aResponse); - - List validationErrors = validate(aRequest, aSession); - - if (validationErrors != null && validationErrors.size()>0) { - returnValidationErrors(aRequest, aSession, aResponse, validationErrors); - } - else { -// finish(aRequest, aSession, aResponse); - - EntityComment comment = (EntityComment) commentModule.createNew (); -// comment.setValues(getIntersectingValues(aRequest, )); - - finishComment(aRequest, aSession, comment); - - 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); - } - - afterCommentPosting(aRequest, aSession, aResponse, comment); - MirGlobal.abuse().checkComment(comment, aRequest, null); - MirGlobal.localizer().openPostings().afterCommentPosting(comment); - logger.info("Comment posted"); - aSession.terminate(); - } - } - } - catch (Throwable t) { - ExceptionFunctions.traceCauseException(t).printStackTrace(); - - throw new SessionFailure("MirBasicPostingSessionHandler.subsequentRequest: " + t.getMessage(), t); - } - } - - 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!"); - - aSession.setAttribute("to_media", articleId); - processCommentPosting(aRequest, aSession, aResponse); - }; - - 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 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); - } - else { - aResponse.setResponseValue("password", null); - } - - aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.EditTemplate")); - }; - - 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); - } - } - - 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"); - - 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")); - }; - - public void addMedia(Request aRequest, Session aSession, EntityComment aComment) throws SessionExc, SessionFailure { - } - - 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")); - }; - - public void afterDuplicateCommentPosting(Request aRequest, Session aSession, Response aResponse, EntityComment aComment) { - aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.comment.DupeTemplate")); - }; - - public class ValidationError { - private String field; - private String message; - private List parameters; - - 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}); - } - - public ValidationError(String aField, String aMessage, Object[] aParameters) { - field = aField; - message = aMessage; - parameters = Arrays.asList(aParameters); - } - - public String getMessage() { - return message; - } - - public String getField() { - return field; - } - - public List getParameters() { - return parameters; - } - } - - - -} +import mircoders.module.ModuleMediaType; + +/** + * + *

Title: Experimental session handler for comment postings

+ *

Description:

+ *

Copyright: Copyright (c) 2003

+ *

Company:

+ * @author not attributable + * @version 1.0 + */ + +public abstract class MirBasicPostingSessionHandler implements SessionHandler { + protected LoggerWrapper logger; + protected MirPropertiesConfiguration configuration; + + private String normalResponseGenerator; + private String dupeResponseGenerator; + private String unsupportedMediaTypeResponseGenerator; + private String finalResponseGenerator; + + + public MirBasicPostingSessionHandler() { + logger = new LoggerWrapper("Localizer.OpenPosting"); + try { + configuration = MirPropertiesConfiguration.instance(); + } + catch (Throwable t) { + logger.fatal("Cannot load configuration: " + t.toString()); + + throw new RuntimeException("Cannot load configuration: " + t.toString()); + } + } + + 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 (MirGlobal.abuse().getOpenPostingDisabled()) { + makeOpenPostingDisabledResponse(aRequest, aSession, aResponse); + aSession.terminate(); + } + else { + if (aSession.getAttribute("initialRequest") == null) { + initialRequest(aRequest, aSession, aResponse); + aSession.setAttribute("initialRequest", "no"); + } + else { + subsequentRequest(aRequest, aSession, aResponse); + } + } + }; + + 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(); + + 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(); + + throw new SessionFailure(t); + } + } + + 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); + } + } + else { + aSession.deleteAttribute("password"); + } + + logger.debug("referrer = " + aRequest.getHeader("Referer")); + + aSession.setAttribute("referer", aRequest.getHeader("Referer")); + } + + 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 { + nrMediaItems = Math.min(configuration.getInt("ServletModule.OpenIndy.MaxMediaUploadItems"), Integer.parseInt(aRequest.getParameter("nrmediaitems"))); + } + catch (Throwable t) { + } + aSession.setAttribute("nrmediaitems", new Integer(nrMediaItems)); + + List mediaItems = new Vector(); + int i=0; + + while (i