From: <wea...@us...> - 2003-09-21 22:35:49
|
Update of /cvsroot/struts/struts-polls/src/org/sf/polls In directory sc8-pr-cvs1:/tmp/cvs-serv31936 Modified Files: PollForm.java Log Message: Added support for file uploads and validation agains valid filters Index: PollForm.java =================================================================== RCS file: /cvsroot/struts/struts-polls/src/org/sf/polls/PollForm.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** PollForm.java 20 Sep 2003 20:12:44 -0000 1.2 --- PollForm.java 21 Sep 2003 22:35:45 -0000 1.3 *************** *** 8,11 **** --- 8,13 ---- import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionMapping; + import org.apache.struts.upload.FormFile; + import org.apache.struts.upload.MultipartRequestHandler; import org.apache.struts.validator.ValidatorForm; *************** *** 145,148 **** --- 147,190 ---- private String multimediaResource10; /** + * The optional multimedia upload for option 1. + */ + private FormFile upload1; + /** + * The optional multimedia upload for option 2. + */ + private FormFile upload2; + /** + * The optional multimedia upload for option 3. + */ + private FormFile upload3; + /** + * The optional multimedia upload for option 4. + */ + private FormFile upload4; + /** + * The optional multimedia upload for option 5. + */ + private FormFile upload5; + /** + * The optional multimedia upload for option 6. + */ + private FormFile upload6; + /** + * The optional multimedia upload for option 7. + */ + private FormFile upload7; + /** + * The optional multimedia upload for option 8. + */ + private FormFile upload8; + /** + * The optional multimedia upload for option 9. + */ + private FormFile upload9; + /** + * The optional multimedia upload for option 10. + */ + private FormFile upload10; + /** * Flag specifying if Poll is active. */ *************** *** 668,671 **** --- 710,843 ---- } /** + * Sets the multimedia upload for option 1. + * @param upload The new multimedia upload for option 1 + */ + public void setUpload1(FormFile upload) { + this.upload1 = upload; + } + /** + * Returns the multimedia upload for option 1. + */ + public FormFile getUpload1() { + return upload1; + } + /** + * Sets the multimedia upload for option 2. + * @param upload The new multimedia upload for option 2 + */ + public void setUpload2(FormFile upload) { + this.upload2 = upload; + } + /** + * Returns the multimedia upload for option 2. + */ + public FormFile getUpload2() { + return upload2; + } + /** + * Sets the multimedia upload for option 3. + * @param upload The new multimedia upload for option 3 + */ + public void setUpload3(FormFile upload) { + this.upload3 = upload; + } + /** + * Returns the multimedia upload for option 3. + */ + public FormFile getUpload3() { + return upload3; + } + /** + * Sets the multimedia upload for option 4. + * @param upload The new multimedia upload for option 4 + */ + public void setUpload4(FormFile upload) { + this.upload4 = upload; + } + /** + * Returns the multimedia upload for option 4. + */ + public FormFile getUpload4() { + return upload4; + } + /** + * Sets the multimedia upload for option 5. + * @param upload The new multimedia upload for option 5 + */ + public void setUpload5(FormFile upload) { + this.upload5 = upload; + } + /** + * Returns the multimedia upload for option 5. + */ + public FormFile getUpload5() { + return upload5; + } + /** + * Sets the multimedia upload for option 6. + * @param upload The new multimedia upload for option 6 + */ + public void setUpload6(FormFile upload) { + this.upload6 = upload; + } + /** + * Returns the multimedia upload for option 6. + */ + public FormFile getUpload6() { + return upload6; + } + /** + * Sets the multimedia upload for option 7. + * @param upload The new multimedia upload for option 7 + */ + public void setUpload7(FormFile upload) { + this.upload7 = upload; + } + /** + * Returns the multimedia upload for option 7. + */ + public FormFile getUpload7() { + return upload7; + } + /** + * Sets the multimedia upload for option 8. + * @param upload The new multimedia upload for option 8 + */ + public void setUpload8(FormFile upload) { + this.upload8 = upload; + } + /** + * Returns the multimedia upload for option 8. + */ + public FormFile getUpload8() { + return upload8; + } + /** + * Sets the multimedia upload for option 9. + * @param upload The new multimedia upload for option 9 + */ + public void setUpload9(FormFile upload) { + this.upload9 = upload; + } + /** + * Returns the multimedia upload for option 9. + */ + public FormFile getUpload9() { + return upload9; + } + /** + * Sets the multimedia upload for option 10. + * @param upload The new multimedia upload for option 10 + */ + public void setUpload10(FormFile upload) { + this.upload10 = upload; + } + /** + * Returns the multimedia upload for option 10. + */ + public FormFile getUpload10() { + return upload10; + } + /** * Returns true if option 1 is selected. */ *************** *** 1145,1148 **** --- 1317,1341 ---- /** + * Convinience method to return a filename from a given URL. + * @param url The URL path + * @return String containing the filename extracted, if any + */ + public String extractFilename(String url) { + int lastIndexOfSeparator = url.lastIndexOf( java.io.File.separator ); + if (url.lastIndexOf( "\\" ) > lastIndexOfSeparator){ + lastIndexOfSeparator = url.lastIndexOf( "\\" ); + } + if (url.lastIndexOf( "/" ) > lastIndexOfSeparator){ + lastIndexOfSeparator = url.lastIndexOf( "/" ); + } + int lenght = url.length(); + if (lastIndexOfSeparator >= 0 && lenght > 0){ + String filename = url.substring( lastIndexOfSeparator+1, lenght ); + return filename; + } + return url; + } + + /** * Reset all properties to their default values. * *************** *** 1200,1203 **** --- 1393,1406 ---- this.multimediaResource9 = null; this.multimediaResource10 = null; + this.upload1 = null; + this.upload2 = null; + this.upload3 = null; + this.upload4 = null; + this.upload5 = null; + this.upload6 = null; + this.upload7 = null; + this.upload8 = null; + this.upload9 = null; + this.upload10 = null; this.creationLongDate = null; this.multipleAnswers = getConfig().isDefaultMultipleAnswers()==true?"1":"0"; *************** *** 1242,1245 **** --- 1445,1449 ---- * <li>Name is a required field</li> * <li>If the expiry value is entered, it must be a number</li> + * <li>The uploadable resources must be within the file size limit</li> * </ul> * *************** *** 1272,1275 **** --- 1476,1637 ---- o6 == false && o7 == false && o8 == false && o9 == false && o10 == false){ errors.add(Constants.ERRORS_KEY, new ActionError("polls.error.vote.missing")); + } + } + // Has the maximum length for the uploadable resources been exceeded? + Boolean maxLengthExceeded = (Boolean) + request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED); + if ((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue())){ + errors.add(Constants.ERRORS_KEY, new ActionError("polls.error.filesize.exceeded")); + } + // Ensure uploadable resources have valid extensions + // @todo optimize filter search + if (upload1 != null && !StringUtils.isEmpty( upload1.getFileName() )){ + String[] uploadFilters = getConfig().getValidMultimediaUpload(); + String currentFilter = null; + boolean valid = false; + for (int i = 1; i < uploadFilters.length; i++){ + currentFilter = uploadFilters[i]; + if (currentFilter != null && upload1.getFileName().toLowerCase().endsWith( currentFilter )){ + valid = true; + break; + } + } + if (!valid){ + errors.add(Constants.ERRORS_KEY, new ActionError("polls.error.invalid.file")); + } + } + if (upload2 != null && !StringUtils.isEmpty( upload2.getFileName() )){ + String[] uploadFilters = getConfig().getValidMultimediaUpload(); + String currentFilter = null; + boolean valid = false; + for (int i = 1; i < uploadFilters.length; i++){ + currentFilter = uploadFilters[i]; + if (currentFilter != null && upload2.getFileName().toLowerCase().endsWith( currentFilter )){ + valid = true; + break; + } + } + if (!valid){ + errors.add(Constants.ERRORS_KEY, new ActionError("polls.error.invalid.file")); + } + } + if (upload3 != null && !StringUtils.isEmpty( upload3.getFileName() )){ + String[] uploadFilters = getConfig().getValidMultimediaUpload(); + String currentFilter = null; + boolean valid = false; + for (int i = 1; i < uploadFilters.length; i++){ + currentFilter = uploadFilters[i]; + if (currentFilter != null && upload3.getFileName().toLowerCase().endsWith( currentFilter )){ + valid = true; + break; + } + } + if (!valid){ + errors.add(Constants.ERRORS_KEY, new ActionError("polls.error.invalid.file")); + } + } + if (upload4 != null && !StringUtils.isEmpty( upload4.getFileName() )){ + String[] uploadFilters = getConfig().getValidMultimediaUpload(); + String currentFilter = null; + boolean valid = false; + for (int i = 1; i < uploadFilters.length; i++){ + currentFilter = uploadFilters[i]; + if (currentFilter != null && upload4.getFileName().toLowerCase().endsWith( currentFilter )){ + valid = true; + break; + } + } + if (!valid){ + errors.add(Constants.ERRORS_KEY, new ActionError("polls.error.invalid.file")); + } + } + if (upload5 != null && !StringUtils.isEmpty( upload5.getFileName() )){ + String[] uploadFilters = getConfig().getValidMultimediaUpload(); + String currentFilter = null; + boolean valid = false; + for (int i = 1; i < uploadFilters.length; i++){ + currentFilter = uploadFilters[i]; + if (currentFilter != null && upload5.getFileName().toLowerCase().endsWith( currentFilter )){ + valid = true; + break; + } + } + if (!valid){ + errors.add(Constants.ERRORS_KEY, new ActionError("polls.error.invalid.file")); + } + } + if (upload6 != null && !StringUtils.isEmpty( upload6.getFileName() )){ + String[] uploadFilters = getConfig().getValidMultimediaUpload(); + String currentFilter = null; + boolean valid = false; + for (int i = 1; i < uploadFilters.length; i++){ + currentFilter = uploadFilters[i]; + if (currentFilter != null && upload6.getFileName().toLowerCase().endsWith( currentFilter )){ + valid = true; + break; + } + } + if (!valid){ + errors.add(Constants.ERRORS_KEY, new ActionError("polls.error.invalid.file")); + } + } + if (upload7 != null && !StringUtils.isEmpty( upload7.getFileName() )){ + String[] uploadFilters = getConfig().getValidMultimediaUpload(); + String currentFilter = null; + boolean valid = false; + for (int i = 1; i < uploadFilters.length; i++){ + currentFilter = uploadFilters[i]; + if (currentFilter != null && upload7.getFileName().toLowerCase().endsWith( currentFilter )){ + valid = true; + break; + } + } + if (!valid){ + errors.add(Constants.ERRORS_KEY, new ActionError("polls.error.invalid.file")); + } + } + if (upload8 != null && !StringUtils.isEmpty( upload8.getFileName() )){ + String[] uploadFilters = getConfig().getValidMultimediaUpload(); + String currentFilter = null; + boolean valid = false; + for (int i = 1; i < uploadFilters.length; i++){ + currentFilter = uploadFilters[i]; + if (currentFilter != null && upload8.getFileName().toLowerCase().endsWith( currentFilter )){ + valid = true; + break; + } + } + if (!valid){ + errors.add(Constants.ERRORS_KEY, new ActionError("polls.error.invalid.file")); + } + } + if (upload9 != null && !StringUtils.isEmpty( upload9.getFileName() )){ + String[] uploadFilters = getConfig().getValidMultimediaUpload(); + String currentFilter = null; + boolean valid = false; + for (int i = 1; i < uploadFilters.length; i++){ + currentFilter = uploadFilters[i]; + if (currentFilter != null && upload9.getFileName().toLowerCase().endsWith( currentFilter )){ + valid = true; + break; + } + } + if (!valid){ + errors.add(Constants.ERRORS_KEY, new ActionError("polls.error.invalid.file")); + } + } + if (upload10 != null && !StringUtils.isEmpty( upload10.getFileName() )){ + String[] uploadFilters = getConfig().getValidMultimediaUpload(); + String currentFilter = null; + boolean valid = false; + for (int i = 1; i < uploadFilters.length; i++){ + currentFilter = uploadFilters[i]; + if (currentFilter != null && upload10.getFileName().toLowerCase().endsWith( currentFilter )){ + valid = true; + break; + } + } + if (!valid){ + errors.add(Constants.ERRORS_KEY, new ActionError("polls.error.invalid.file")); } } |