Author: andrei_exadel
Date: 2008-02-21 11:49:10 -0500 (Thu, 21 Feb 2008)
New Revision: 6264
Modified:
trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/MultipartFilter.java
trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/MultipartRequest.java
trunk/sandbox/ui/fileUpload/src/main/resources/org/richfaces/renderkit/html/js/FileUpload.js
Log:
file size restriction functionality
Modified:
trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/MultipartFilter.java
===================================================================
---
trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/MultipartFilter.java 2008-02-21
16:45:41 UTC (rev 6263)
+++
trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/MultipartFilter.java 2008-02-21
16:49:10 UTC (rev 6264)
@@ -83,6 +83,7 @@
Map<String, MultipartRequest> sessionsMap = null;
Map<String, Object> percentMap = null;
try {
+ if (!isFileSizeRestricted(request, maxRequestSize)) {
HttpSession session = httpRequest.getSession();
synchronized (session) {
sessionsMap = (Map<String, MultipartRequest>) session
@@ -105,18 +106,15 @@
if (multipartRequest.parseRequest()) {
chain.doFilter(multipartRequest, response);
} else {
- HttpServletResponse httpResponse = (HttpServletResponse) response;
- httpResponse.setStatus(HttpServletResponse.SC_OK);
- httpResponse.setContentType("text/html");
-
- PrintWriter writer = httpResponse.getWriter();
- writer
- .write("<html
id=\"_richfaces_file_upload_stopped\"></html>");
- writer.close();
+ printResponse(response, "<html
id=\"_richfaces_file_upload_stopped\"></html>");
}
+ }else {
+ printResponse(response, "<html
id=\"_richfaces_file_upload_size_restricted\"></html>");
+ }
} finally {
if (sessionsMap != null) {
sessionsMap.remove(uid);
+ sessionsMap.remove(componentId);
}
}
} else {
@@ -141,6 +139,22 @@
}
}
+ private boolean isFileSizeRestricted (ServletRequest request, int maxSize) {
+ if (request.getContentLength() > maxSize) {
+ return true;
+ }
+ return false;
+ }
+
+ private void printResponse (ServletResponse response, String message) throws
IOException{
+ HttpServletResponse httpResponse = (HttpServletResponse) response;
+ httpResponse.setStatus(HttpServletResponse.SC_OK);
+ httpResponse.setContentType("text/html");
+ PrintWriter writer = httpResponse.getWriter();
+ writer.write(message);
+ writer.close();
+ }
+
private boolean isMultipartRequest(HttpServletRequest request)
{
if (!"post".equals(request.getMethod().toLowerCase()))
@@ -171,6 +185,11 @@
if (param != null) {
this.createTempFiles = Boolean.parseBoolean(param);
}
+ param = filterConfig.getInitParameter("maxRequestSize");
+ if (param != null) {
+ this.maxRequestSize = Integer.parseInt(param);
+ }
+
}
}
Modified:
trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/MultipartRequest.java
===================================================================
---
trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/MultipartRequest.java 2008-02-21
16:45:41 UTC (rev 6263)
+++
trunk/sandbox/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/MultipartRequest.java 2008-02-21
16:49:10 UTC (rev 6264)
@@ -256,8 +256,9 @@
this.contentLength = Integer.parseInt(contentLength);
if (contentLength != null && maxRequestSize > 0
&& this.contentLength > maxRequestSize) {
- throw new FileUploadException(
- "Multipart request is larger than allowed size");
+ //TODO : we should make decision if can generate exception in this place
+ //throw new FileUploadException(
+ // "Multipart request is larger than allowed size");
}
}
Modified:
trunk/sandbox/ui/fileUpload/src/main/resources/org/richfaces/renderkit/html/js/FileUpload.js
===================================================================
---
trunk/sandbox/ui/fileUpload/src/main/resources/org/richfaces/renderkit/html/js/FileUpload.js 2008-02-21
16:45:41 UTC (rev 6263)
+++
trunk/sandbox/ui/fileUpload/src/main/resources/org/richfaces/renderkit/html/js/FileUpload.js 2008-02-21
16:49:10 UTC (rev 6264)
@@ -89,6 +89,7 @@
FileUploadEntry.UPLOAD_SUCCESS = "success";
FileUploadEntry.UPLOAD_TRANSFER_ERROR = "transfer_error";
FileUploadEntry.UPLOAD_SERVER_ERROR = "server_error";
+FileUploadEntry.UPLOAD_SIZE_ERROR = "size_error";
FileUploadEntry.LABELS = {};
FileUploadEntry.LABELS[FileUploadEntry.INITIALIZED] = '';
@@ -98,6 +99,7 @@
FileUploadEntry.LABELS[FileUploadEntry.UPLOAD_SUCCESS] = 'done';
FileUploadEntry.LABELS[FileUploadEntry.UPLOAD_TRANSFER_ERROR] =
'transfer_error';
FileUploadEntry.LABELS[FileUploadEntry.UPLOAD_SERVER_ERROR] = 'server_error';
+FileUploadEntry.LABELS[FileUploadEntry.UPLOAD_SIZE_ERROR] = 'File size
restricted';
FileUploadEntry.getComponent = function(elt) {
while (elt) {
@@ -315,8 +317,11 @@
onload: function() {
var iframeDocument = this.iframe.contentWindow.document;
var elt = iframeDocument.getElementById('_richfaces_file_upload_stopped');
+ var restr =
iframeDocument.getElementById('_richfaces_file_upload_size_restricted');
if (elt) {
this.callback(FileUploadEntry.UPLOAD_CANCELED);
+ } if (restr) {
+ this.callback(FileUploadEntry.UPLOAD_SIZE_ERROR);
} else {
//TODO handle server internal errors, etc.
this.callback(FileUploadEntry.UPLOAD_SUCCESS);
@@ -403,6 +408,7 @@
getFileSize: function (data) {
if (data) {
+ this.progressBar.enable();
var progressData = new ProgressData(data);
this.progressData = progressData;
if (this.activeEntry) {
@@ -417,13 +423,13 @@
prepareProgressBar: function () {
this.progressBar.setValue(0);
Element.show(this._progressBar);
- this.progressBar.enable();
+ //this.progressBar.enable();
},
finishProgressBar: function () {
this.progressBar.disable();
this.progressBar.setValue(100);
- Element.hide(this._progressBar);
+ //Element.hide(this._progressBar);
},
setupAutoUpload: function() {
@@ -462,7 +468,7 @@
var l = this.entries.length;
for (var i = 0; i < l; i++) {
var entry = this.entries[i];
- if (entry.state != FileUploadEntry.UPLOAD_SUCCESS) {
+ if (entry.state == FileUploadEntry.READY || entry.state ==
FileUploadEntry.INITIALIZED) {
return entry;
}
@@ -644,7 +650,7 @@
notifyStateChange: function(entry, oldState) {
var newState = entry.state;
- if (newState == FileUploadEntry.UPLOAD_SUCCESS) {
+ if (newState == FileUploadEntry.UPLOAD_SUCCESS || newState ==
FileUploadEntry.UPLOAD_SIZE_ERROR) {
//todo clear completed
//if (this.activeEntry) {
@@ -663,6 +669,7 @@
//but this.runUpload can be false if upload
//has been requested to stop by user
this.setupAutoUpload();
+ Element.hide(this._progressBar);
}
this._updateEntriesState();
Show replies by date