X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmircoders%2Flocalizer%2Fbasic%2FMirBasicPostingSessionHandler.java;h=df23dedc7a1679b3b77b65b359fe4bf2a09a0a38;hb=bd42ea79e70f65a59814d5ade0a296a22399c9ae;hp=921eb09953ec8a0b1b0999d6ab3a52fe5c47f480;hpb=84272ce4d6d778cb73bdd2785eb72420187052f5;p=mir.git diff --git a/source/mircoders/localizer/basic/MirBasicPostingSessionHandler.java b/source/mircoders/localizer/basic/MirBasicPostingSessionHandler.java index 921eb099..df23dedc 100755 --- a/source/mircoders/localizer/basic/MirBasicPostingSessionHandler.java +++ b/source/mircoders/localizer/basic/MirBasicPostingSessionHandler.java @@ -18,13 +18,13 @@ * 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; @@ -35,7 +35,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Random; -import java.util.Vector; +import java.util.*; import mir.config.MirPropertiesConfiguration; import mir.entity.Entity; @@ -67,11 +67,9 @@ 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(); public MirBasicPostingSessionHandler() { logger = new LoggerWrapper("Localizer.OpenPosting"); @@ -83,7 +81,6 @@ public class MirBasicPostingSessionHandler implements SessionHandler { 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 { @@ -96,242 +93,129 @@ public class MirBasicPostingSessionHandler implements SessionHandler { } }; - 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 { - 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; + try { + List validationErrors = new Vector(); - 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); - - 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; + aSession.setAttribute("referer", aRequest.getHeader("Referer")); + aSession.setAttribute("nrmediaitems", + new Integer(configuration.getInt("ServletModule.OpenIndy.DefaultMediaUploadItems"))); } - 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); - } + protected void initializeResponseData(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure { + int nrMediaItems = ((Integer) aSession.getAttribute("nrmediaitems")).intValue(); + List mediaItems = new Vector(); + int i=0; - afterCommentPosting(aRequest, aSession, aResponse, comment); - MirGlobal.abuse().checkComment(comment, aRequest, null); - MirGlobal.localizer().openPostings().afterCommentPosting(comment); - logger.info("Comment posted"); - aSession.terminate(); - } - } + while (iTitle:

+ *

Description:

+ *

Copyright: Copyright (c) 2003

+ *

Company:

+ * @author not attributable + * @version 1.0 + */ public class ValidationError { private String field; @@ -365,6 +249,94 @@ public class MirBasicPostingSessionHandler implements SessionHandler { } } + /** + * Convenience validation method to test wether a field has been filled in + * + * @param aRequest + * @param aFieldName + * @param anErrorMessageResource + * @param aValidationResults + * @return + */ + + 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) { + aValidationResults.add(new ValidationError(aFieldName, anErrorMessageResource)); + return false; + } + else + return true; + } + + /** + * Convenience validation method to test wether a field is numeric + * @param aRequest + * @param aFieldName + * @param anErrorMessageResource + * @param aValidationResults + * @return + */ + + 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) { + aValidationResults.add(new ValidationError(aFieldName, anErrorMessageResource)); + return false; + } + } + return true; + } + + /** + * Method to generate a one-time password + * + * @return a password, to be used once + */ + 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); + } + + + /** + * + * @param aRequest + * @param aStorage + * @return + * @throws SessionExc + * @throws SessionFailure + */ + + protected static final 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; + } }