Author: andrei_exadel
Date: 2008-03-07 09:13:22 -0500 (Fri, 07 Mar 2008)
New Revision: 6623
Removed:
trunk/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/FileUploadException.java
trunk/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/MultipartFilter.java
trunk/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/MultipartRequest.java
Modified:
trunk/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/component/UIFileUpload.java
trunk/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/renderkit/FileUploadRendererBase.java
trunk/ui/fileUpload/src/main/resources/org/richfaces/renderkit/html/js/FileUpload.js
Log:
RF-2396
Modified:
trunk/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/component/UIFileUpload.java
===================================================================
---
trunk/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/component/UIFileUpload.java 2008-03-07
14:11:23 UTC (rev 6622)
+++
trunk/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/component/UIFileUpload.java 2008-03-07
14:13:22 UTC (rev 6623)
@@ -12,14 +12,15 @@
import javax.faces.event.FacesEvent;
import javax.servlet.http.HttpServletRequest;
+import org.ajax4jsf.Filter;
import org.ajax4jsf.context.AjaxContext;
import org.ajax4jsf.context.AjaxContextImpl;
+import org.ajax4jsf.request.MultipartRequest;
import org.richfaces.event.FileUploadListener;
import org.richfaces.event.UploadEvent;
import org.richfaces.org.jboss.seam.ui.renderkit.FileUploadRendererBase;
-import org.richfaces.org.jboss.seam.web.MultipartFilter;
-import org.richfaces.org.jboss.seam.web.MultipartRequest;
+
/**
* JSF component class
*
@@ -51,7 +52,7 @@
private void setupProgressBarValueExpression(FacesContext context, String uid) {
FileUploadRendererBase renderer =
(FileUploadRendererBase)this.getRenderer(context);
UIComponent progressBar = renderer.getProgressBar(context, this);
- String exprStr = "#{" + MultipartFilter.PERCENT_BEAN_NAME +
"['"+uid+"']}";
+ String exprStr = "#{" + Filter.PERCENT_BEAN_NAME +
"['"+uid+"']}";
ValueExpression ex = context.getApplication().getExpressionFactory()
.createValueExpression(context.getELContext(), exprStr,
Integer.class);
@@ -59,60 +60,7 @@
}
-
- // @Override
- // public void processUpdates(FacesContext context) {
- // ValueExpression dataBinding = getValueExpression("data");
- // if (dataBinding != null) {
- // Class clazz = dataBinding.getType(context.getELContext());
- // if (clazz.isAssignableFrom(InputStream.class)) {
- // dataBinding.setValue(context.getELContext(),
- // getLocalInputStream());
- // } else if (clazz.isAssignableFrom(byte[].class)) {
- // byte[] bytes = null;
- // if (getLocalInputStream() != null) {
- // ByteArrayOutputStream bos = new ByteArrayOutputStream();
- // try {
- // byte[] buffer = new byte[512];
- // int read = getLocalInputStream().read(buffer);
- // while (read != -1) {
- // bos.write(buffer, 0, read);
- // read = getLocalInputStream().read(buffer);
- // }
- // bytes = bos.toByteArray();
- // } catch (IOException e) {
- // throw new RuntimeException(e);
- // }
- // }
- // dataBinding.setValue(context.getELContext(), bytes);
- // }
- //
- // if (getLocalContentType() != null) {
- // ValueExpression valueExpression = getValueExpression("contentType");
- // if (valueExpression != null) {
- // valueExpression.setValue(context.getELContext(),
- // getLocalContentType());
- // }
- // }
- //
- // if (getLocalFileName() != null) {
- // ValueExpression valueExpression = getValueExpression("fileName");
- // if (valueExpression != null) {
- // valueExpression.setValue(context.getELContext(),
- // getLocalFileName());
- // }
- // }
- //
- // if (getLocalFileSize() != null) {
- // ValueExpression valueExpression = getValueExpression("fileSize");
- // if (valueExpression != null) {
- // valueExpression.setValue(context.getELContext(),
- // getLocalFileSize());
- // }
- // }
- // }
- // }
-
+
public String getLocalContentType() {
return localContentType;
}
@@ -206,12 +154,12 @@
&& request.getParameterMap().containsKey(
this.getClientId(facesContext))) {
AjaxContext ajaxContext =
AjaxContextImpl.getCurrentInstance(facesContext);
- String uid = request.getParameter("uid");
+ String uid = request.getParameter(Filter.UPLOAD_FILES_ID);
Map<String, Object> sessionMap = externalContext.getSessionMap();
Map<String, MultipartRequest> sessions = (Map<String,
MultipartRequest>) sessionMap
- .get(MultipartFilter.REQUESTS_SESSIONS_BEAN_NAME);
+ .get(Filter.REQUESTS_SESSIONS_BEAN_NAME);
if (sessions != null) {
MultipartRequest multipartRequest = sessions.get(uid);
if (multipartRequest != null) {
Modified:
trunk/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/renderkit/FileUploadRendererBase.java
===================================================================
---
trunk/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/renderkit/FileUploadRendererBase.java 2008-03-07
14:11:23 UTC (rev 6622)
+++
trunk/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/ui/renderkit/FileUploadRendererBase.java 2008-03-07
14:13:22 UTC (rev 6623)
@@ -1,6 +1,7 @@
package org.richfaces.org.jboss.seam.ui.renderkit;
import java.io.IOException;
+import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@@ -13,10 +14,13 @@
import javax.faces.render.RenderKit;
import javax.faces.render.RenderKitFactory;
import javax.servlet.ServletRequest;
+import javax.servlet.http.HttpSession;
+import org.ajax4jsf.Filter;
import org.ajax4jsf.context.AjaxContext;
import org.ajax4jsf.context.AjaxContextImpl;
import org.ajax4jsf.event.AjaxEvent;
+import org.ajax4jsf.exception.FileUploadException;
import org.ajax4jsf.javascript.JSFunction;
import org.ajax4jsf.javascript.JSFunctionDefinition;
import org.ajax4jsf.javascript.JSLiteral;
@@ -24,13 +28,12 @@
import org.ajax4jsf.javascript.ScriptUtils;
import org.ajax4jsf.renderkit.AjaxRendererUtils;
import org.ajax4jsf.renderkit.RendererUtils;
+import org.ajax4jsf.request.MultipartRequest;
import org.ajax4jsf.resource.CountingOutputWriter;
import org.richfaces.component.UIProgressBar;
import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;
import org.richfaces.org.jboss.seam.ui.component.UIFileUpload;
-import org.richfaces.org.jboss.seam.web.FileUploadException;
-import org.richfaces.org.jboss.seam.web.MultipartRequest;
import org.richfaces.renderkit.TemplateEncoderRendererBase;
/**
@@ -41,9 +44,12 @@
*/
public abstract class FileUploadRendererBase extends
TemplateEncoderRendererBase {
-
+
/** Attribute name where collection of files uploaded will be stored */
private static final String _FILES_UPLOADED_ATTRIBUTE_NAME = "uploadData";
+
+ /** Multipart request class name */
+ private static final String _MULTIPART_REQUEST_CLASS_NAME =
MultipartRequest.class.getName();
/*
* (non-Javadoc)
@@ -56,33 +62,41 @@
UIFileUpload fileUpload = (UIFileUpload) component;
ServletRequest request = (ServletRequest) context.getExternalContext()
.getRequest();
-
+
String clientId = component.getClientId(context);
new AjaxEvent(component).queue();
- if (request instanceof MultipartRequest) {
- MultipartRequest multipartRequest = (MultipartRequest) request;
+ Class requestClazz = request.getClass();
+
+ if (_MULTIPART_REQUEST_CLASS_NAME
+ .equals(requestClazz.getName())
+ || "org.jboss.seam.web.MultipartRequest".equals(requestClazz.getName())) {
+
+ HttpSession session = (HttpSession)context.getExternalContext().getSession(false);
+ String uid = request.getParameter(Filter.UPLOAD_FILES_ID);
+ Map<String, MultipartRequest> map = (Map<String,
MultipartRequest>)session.getAttribute(Filter.REQUESTS_SESSIONS_BEAN_NAME);
+
+ MultipartRequest multipartRequest = map.get(uid);
+
clientId = clientId + ":file";
- fileUpload.setLocalInputStream(multipartRequest
- .getFileInputStream(clientId));
- fileUpload.setLocalContentType(multipartRequest
- .getFileContentType(clientId));
- fileUpload.setLocalFileName(multipartRequest.getFileName(clientId));
- fileUpload.setLocalFileSize(multipartRequest.getFileSize(clientId));
- onUploadComplete(context, multipartRequest.getFile(clientId),
- fileUpload);
+ String fileName = multipartRequest.getFileName(clientId);
+ Object file = multipartRequest.getFile(clientId);
+ fileUpload.setLocalFileName(fileName);
+ onUploadComplete(context, file, fileUpload);
}
}
-
+
/**
* Method checks if uploaded files count overflowed
+ *
* @param fileUpload
* @param map
* @return
*/
- private boolean checkFileCount (UIFileUpload fileUpload, Collection filesUploaded) {
+ private boolean checkFileCount(UIFileUpload fileUpload,
+ Collection filesUploaded) {
Integer max = fileUpload.getMaxFilesQuantity();
if (filesUploaded != null && filesUploaded.size() >= max) {
fileUpload.reset();
@@ -92,22 +106,27 @@
}
/**
- * Put uploaded file into data attribute defined
+ * Put uploaded file into data attribute defined
+ *
* @param context
* @param fileUpload
* @param file
*/
@SuppressWarnings("unchecked")
- private void storeData(FacesContext context, UIFileUpload fileUpload, Object file) {
- ValueExpression data = fileUpload.getValueExpression(_FILES_UPLOADED_ATTRIBUTE_NAME);
+ private void storeData(FacesContext context, UIFileUpload fileUpload,
+ Object file) {
+ ValueExpression data = fileUpload
+ .getValueExpression(_FILES_UPLOADED_ATTRIBUTE_NAME);
if (data != null) {
if (data.getValue(context.getELContext()) instanceof Collection) {
- Collection collection = (Collection) data.getValue(context.getELContext());
+ Collection collection = (Collection) data.getValue(context
+ .getELContext());
if (checkFileCount(fileUpload, collection)) {
- UploadItem item = new UploadItem(fileUpload.getLocalFileName(), file);
+ UploadItem item = new UploadItem(fileUpload
+ .getLocalFileName(), file);
new UploadEvent(fileUpload, item).queue();
collection.add(item);
- }else {
+ } else {
throw new FileUploadException("Files count overflow");
}
}
@@ -116,18 +135,20 @@
/**
* Call with method after uploading completed
+ *
* @param context
* @param file
* @param fileUpload
*/
private void onUploadComplete(FacesContext context, Object file,
UIFileUpload fileUpload) {
- storeData(context, fileUpload, file);
+ storeData(context, fileUpload, file);
try {
- AjaxContext ajaxContext = AjaxContextImpl.getCurrentInstance(context);
+ AjaxContext ajaxContext = AjaxContextImpl
+ .getCurrentInstance(context);
ajaxContext.setAjaxRequest(true);
} catch (Exception e) {
- e.getMessage();
+ e.getMessage();
}
}
@@ -140,9 +161,10 @@
protected Class<? extends UIComponent> getComponentClass() {
return UIFileUpload.class;
}
-
+
/**
- * Generates common JS script by action value
+ * Generates common JS script by action value
+ *
* @param context
* @param component
* @param action
@@ -159,7 +181,7 @@
Map options = AjaxRendererUtils.buildEventOptions(context, component);
Map parameters = (Map) options.get("parameters");
parameters.put("action", action);
- parameters.put("uid", new JSReference("uid"));
+ parameters.put(Filter.UPLOAD_FILES_ID, new JSReference("uid"));
parameters.put(clientId, clientId);
if (oncomplete != null) {
options.put("onbeforedomupdate", oncomplete);
@@ -172,18 +194,20 @@
return function.toScript();
}
-
+
/**
* Return accepted types map
+ *
* @param context
* @param component
* @return
*/
public Object getAcceptedTypes(FacesContext context, UIComponent component) {
- String acceptedTypes =
(String)component.getAttributes().get("acceptedTypes");
+ String acceptedTypes = (String) component.getAttributes().get(
+ "acceptedTypes");
if (acceptedTypes != null) {
Map<String, Boolean> accepted = new HashMap<String, Boolean>();
- String [] types = acceptedTypes.split("[,;|]");
+ String[] types = acceptedTypes.split("[,;|]");
if (types != null) {
for (String type : types) {
accepted.put(type.toLowerCase(), true);
@@ -196,6 +220,7 @@
/**
* Generates JS script for stopping uploading process
+ *
* @param context
* @param component
* @return
@@ -206,9 +231,9 @@
return getActionScript(context, component, "stop", null);
}
-
/**
- * Generates JS script for getting file size from server
+ * Generates JS script for getting file size from server
+ *
* @param context
* @param component
* @return
@@ -227,9 +252,10 @@
return getActionScript(context, component, "progress", oncomplete);
}
-
+
/**
* Generates progress label markup
+ *
* @param context
* @param component
* @return
@@ -268,26 +294,31 @@
} catch (Exception e) {
e.getMessage();
}
- return (result != null) ? new JSLiteral(result.toString()) : JSReference.NULL;
+ return (result != null) ? new JSLiteral(result.toString())
+ : JSReference.NULL;
}
/**
* Generate component custom events functions
+ *
* @param context
* @param component
* @param attributeName
* @return
*/
- public String getAsEventHandler(FacesContext context, UIComponent component, String
attributeName) {
- Object eventHandler = RendererUtils.getInstance().getAsEventHandler(context, component,
attributeName, "");
+ public String getAsEventHandler(FacesContext context,
+ UIComponent component, String attributeName) {
+ Object eventHandler = RendererUtils.getInstance().getAsEventHandler(
+ context, component, attributeName, "");
if (eventHandler != null) {
- return eventHandler.toString();
+ return eventHandler.toString();
}
return JSReference.NULL.toScript();
}
/**
- * Gets progress bar Id
+ * Gets progress bar Id
+ *
* @param context
* @param component
* @return
@@ -299,7 +330,8 @@
}
/**
- * Renders progress bar
+ * Renders progress bar
+ *
* @param context
* @param component
* @throws IOException
@@ -312,6 +344,7 @@
/**
* Creates progress bar component
+ *
* @param context
* @param fileUpload
* @return
@@ -329,6 +362,7 @@
/**
* Returns progress bar
+ *
* @param context
* @param component
* @return
Deleted:
trunk/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/FileUploadException.java
===================================================================
---
trunk/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/FileUploadException.java 2008-03-07
14:11:23 UTC (rev 6622)
+++
trunk/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/FileUploadException.java 2008-03-07
14:13:22 UTC (rev 6623)
@@ -1,28 +0,0 @@
-package org.richfaces.org.jboss.seam.web;
-
-/**
- * Thrown when an exception occurs while uploading a file.
- *
- * @author Shane Bryzak
- */
-public class FileUploadException extends RuntimeException
-{
-
- private static final long serialVersionUID = 3142473677581995262L;
-
-
- public FileUploadException()
- {
- this(null, null);
- }
-
- public FileUploadException(String message)
- {
- this(message, null);
- }
-
- public FileUploadException(String message, Throwable cause)
- {
- super(message, cause);
- }
-}
Deleted:
trunk/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/MultipartFilter.java
===================================================================
---
trunk/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/MultipartFilter.java 2008-03-07
14:11:23 UTC (rev 6622)
+++
trunk/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/MultipartFilter.java 2008-03-07
14:13:22 UTC (rev 6623)
@@ -1,197 +0,0 @@
-package org.richfaces.org.jboss.seam.web;
-
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-
-import javax.servlet.Filter;
-import javax.servlet.FilterChain;
-import javax.servlet.FilterConfig;
-import javax.servlet.ServletException;
-import javax.servlet.ServletRequest;
-import javax.servlet.ServletResponse;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpSession;
-
-/**
- * A filter for decoding multipart requests, for
- * use with the file upload control.
- *
- * @author Shane Bryzak
- *
- */
-//@Scope(APPLICATION)
-//(a)Name("org.jboss.seam.web.multipartFilter")
-//@Install(precedence = BUILT_IN)
-//@BypassInterceptors
-//(a)Filter(within={"org.jboss.seam.web.ajax4jsfFilter",
"org.jboss.seam.web.exceptionFilter"})
-public class MultipartFilter implements Filter
-{
- public static final String MULTIPART = "multipart/";
-
- /** Session bean name where multipart requests map will be stored */
- public static final String REQUESTS_SESSIONS_BEAN_NAME =
"_richfaces_upload_sessions";
-
- /** Session bean name where progress bar's percent map will be stored */
- public static final String PERCENT_BEAN_NAME = "_richfaces_upload_percents";
-
- /**
- * Flag indicating whether a temporary file should be used to cache the uploaded file
- */
- private boolean createTempFiles = false;
-
- /**
- * The maximum size of a file upload request. 0 means no limit.
- */
- private int maxRequestSize = 0;
-
- public boolean getCreateTempFiles()
- {
- return createTempFiles;
- }
-
- public void setCreateTempFiles(boolean createTempFiles)
- {
- this.createTempFiles = createTempFiles;
- }
-
- public int getMaxRequestSize()
- {
- return maxRequestSize;
- }
-
- public void setMaxRequestSize(int maxFileSize)
- {
- this.maxRequestSize = maxFileSize;
- }
-
- @SuppressWarnings("unchecked")
-public void doFilter(ServletRequest request, ServletResponse response,
- FilterChain chain) throws IOException, ServletException {
- if (!(response instanceof HttpServletResponse)) {
- chain.doFilter(request, response);
- return;
- }
-
- HttpServletRequest httpRequest = (HttpServletRequest) request;
- String uid = httpRequest.getParameter("uid");
- //String componentId = httpRequest.getParameter("clientId");
- if (isMultipartRequest(httpRequest)) {
- MultipartRequest multipartRequest = new MultipartRequest(
- httpRequest, createTempFiles, maxRequestSize, uid);
-
- 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
- .getAttribute(REQUESTS_SESSIONS_BEAN_NAME);
- percentMap = (Map<String, Object>) session.getAttribute(PERCENT_BEAN_NAME);
- if (sessionsMap == null) {
- sessionsMap = Collections.synchronizedMap(new HashMap<String,
MultipartRequest>());
- session.setAttribute(
- REQUESTS_SESSIONS_BEAN_NAME, sessionsMap);
- }
- if (percentMap == null) {
- percentMap = new HashMap<String, Object>();
- session.setAttribute(PERCENT_BEAN_NAME, percentMap);
- }
- }
-
- percentMap.put(uid, 0); // associate percent value with file entry uid
- sessionsMap.put(uid, multipartRequest);
-
- if (multipartRequest.parseRequest()) {
- chain.doFilter(multipartRequest, response);
- } else {
- 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);
- percentMap.remove(uid);
- }
- }
- } else {
- if ("stop".equals(httpRequest.getParameter("action"))) {
- HttpSession session = httpRequest.getSession();
- Map<String, MultipartRequest> sessions = (Map<String, MultipartRequest>)
session
- .getAttribute(REQUESTS_SESSIONS_BEAN_NAME);
-
- if (sessions != null) {
- MultipartRequest multipartRequest = sessions.get(uid);
- if (multipartRequest != null) {
- multipartRequest.stop();
- HttpServletResponse httpResponse = (HttpServletResponse) response;
- httpResponse.setStatus(HttpServletResponse.SC_NO_CONTENT);
- httpResponse.getOutputStream().close();
- }
- }
- } else {
- chain.doFilter(request, response);
- }
- }
- }
-
- 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()))
- {
- return false;
- }
-
- String contentType = request.getContentType();
- if (contentType == null)
- {
- return false;
- }
-
- if (contentType.toLowerCase().startsWith(MULTIPART))
- {
- return true;
- }
-
- return false;
- }
-
-public void destroy() {
-
-}
-
-public void init(FilterConfig filterConfig) throws ServletException {
- String param = filterConfig.getInitParameter("createTempFiles");
- if (param != null) {
- this.createTempFiles = Boolean.parseBoolean(param);
- }
- param = filterConfig.getInitParameter("maxRequestSize");
- if (param != null) {
- this.maxRequestSize = Integer.parseInt(param);
- }
-
-
-}
-}
Deleted:
trunk/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/MultipartRequest.java
===================================================================
---
trunk/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/MultipartRequest.java 2008-03-07
14:11:23 UTC (rev 6622)
+++
trunk/ui/fileUpload/src/main/java/org/richfaces/org/jboss/seam/web/MultipartRequest.java 2008-03-07
14:13:22 UTC (rev 6623)
@@ -1,653 +0,0 @@
-package org.richfaces.org.jboss.seam.web;
-
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.UnsupportedEncodingException;
-import java.rmi.server.UID;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Enumeration;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletRequestWrapper;
-
-/**
- * Request wrapper for supporting multipart requests, used for file uploading.
- *
- * @author Shane Bryzak
- */
-public class MultipartRequest extends HttpServletRequestWrapper {
- private static final String PARAM_NAME = "name";
- private static final String PARAM_FILENAME = "filename";
- private static final String PARAM_CONTENT_TYPE = "Content-Type";
-
- private static final int BUFFER_SIZE = 2048;
- private static final int CHUNK_SIZE = 512;
-
- private boolean createTempFiles;
-
- private String uid;
-
- private String encoding = null;
-
- private Integer contentLength = 0;
-
- private int read = 0;
-
- private Map<String, Param> parameters = null;
-
- private Map<String, Object> percentMap = null;
-
- private enum ReadState {
- BOUNDARY, HEADERS, DATA
- }
-
- private static final byte CR = 0x0d;
- private static final byte LF = 0x0a;
- private static final byte[] CR_LF = { CR, LF };
-
- private abstract class Param {
- private String name;
-
- public Param(String name) {
- this.name = name;
- }
-
- public String getName() {
- return name;
- }
-
- public abstract void appendData(byte[] data, int start, int length)
- throws IOException;
-
- }
-
- private class ValueParam extends Param {
- private Object value = null;
- private ByteArrayOutputStream buf = new ByteArrayOutputStream();
-
- public ValueParam(String name) {
- super(name);
- }
-
- @Override
- public void appendData(byte[] data, int start, int length)
- throws IOException {
- // read += length;
- buf.write(data, start, length);
- }
-
- public void complete() throws UnsupportedEncodingException {
- String val = encoding == null ? new String(buf.toByteArray())
- : new String(buf.toByteArray(), encoding);
- if (value == null) {
- value = val;
- } else {
- if (!(value instanceof List)) {
- List<String> v = new ArrayList<String>();
- v.add((String) value);
- value = v;
- }
-
- ((List) value).add(val);
- }
- buf.reset();
- }
-
- public Object getValue() {
- return value;
- }
- }
-
- private class FileParam extends Param {
- private String filename;
- private String contentType;
- private int fileSize;
-
- private ByteArrayOutputStream bOut = null;
- private FileOutputStream fOut = null;
- private File tempFile = null;
-
- public FileParam(String name) {
- super(name);
- }
-
- public Object getFile() {
- if (null != tempFile) {
- return tempFile;
- }else if (null != bOut) {
- return bOut.toByteArray();
- }
- return null;
- }
-
- public String getFilename() {
- return filename;
- }
-
- public void setFilename(String filename) {
- this.filename = filename;
- }
-
- public String getContentType() {
- return contentType;
- }
-
- public void setContentType(String contentType) {
- this.contentType = contentType;
- }
-
- public int getFileSize() {
- return fileSize;
- }
-
- public File createTempFile() {
- try {
-
- tempFile = File.createTempFile(new UID().toString().replace(
- ":", "-"), ".upload");
- tempFile.deleteOnExit();
- fOut = new FileOutputStream(tempFile);
- } catch (IOException ex) {
- throw new FileUploadException("Could not create temporary file");
- }
- return tempFile;
- }
-
- @Override
- public void appendData(byte[] data, int start, int length)
- throws IOException {
- // read += length;
- if (fOut != null) {
- fOut.write(data, start, length);
- fOut.flush();
- } else {
- if (bOut == null)
- bOut = new ByteArrayOutputStream();
- bOut.write(data, start, length);
- }
-
- fileSize += length;
- }
-
- public byte[] getData() {
- if (fOut != null) {
- try {
- fOut.close();
- } catch (IOException ex) {
- }
- fOut = null;
- }
-
- if (bOut != null) {
- return bOut.toByteArray();
- } else if (tempFile != null) {
- if (tempFile.exists()) {
- try {
- FileInputStream fIn = new FileInputStream(tempFile);
- ByteArrayOutputStream bOut = new ByteArrayOutputStream();
- byte[] buf = new byte[512];
- int read = fIn.read(buf);
- while (read != -1) {
- bOut.write(buf, 0, read);
- read = fIn.read(buf);
- }
- bOut.flush();
-
- fIn.close();
- tempFile.delete();
- return bOut.toByteArray();
- } catch (IOException ex) { /* too bad? */
- }
- }
- }
-
- return null;
- }
-
- public InputStream getInputStream() {
- if (fOut != null) {
- try {
- fOut.close();
- } catch (IOException ex) {
- }
- fOut = null;
- }
-
- if (bOut != null) {
- return new ByteArrayInputStream(bOut.toByteArray());
- } else if (tempFile != null) {
- try {
- return new FileInputStream(tempFile) {
- @Override
- public void close() throws IOException {
- super.close();
- tempFile.delete();
- }
- };
- } catch (FileNotFoundException ex) {
- }
- }
-
- return null;
- }
- }
-
- private HttpServletRequest request;
-
- private boolean shouldStop = false;
-
- public MultipartRequest(HttpServletRequest request,
- boolean createTempFiles, int maxRequestSize, String uid) {
- super(request);
- this.request = request;
- this.createTempFiles = createTempFiles;
- this.uid = uid;
-
- String contentLength = request.getHeader("Content-Length");
- this.contentLength = Integer.parseInt(contentLength);
- if (contentLength != null && maxRequestSize > 0
- && this.contentLength > maxRequestSize) {
- //TODO : we should make decision if can generate exception in this place
- //throw new FileUploadException(
- // "Multipart request is larger than allowed size");
- }
- }
-
- private String decodeFileName(String name) {
- String fileName = null;
- StringBuffer buffer = new StringBuffer();
- String[] codes = name.split(";");
- if (codes != null) {
- for (String code : codes) {
- if (code.startsWith("&")) {
- String sCode = code.replaceAll("[&#]*", "");
- Integer iCode = Integer.parseInt(sCode);
- buffer.append(Character.toChars(iCode));
- }else {
- buffer.append(code);
- }
- }
- fileName = buffer.toString();
- }
- return fileName;
- }
-
- public boolean parseRequest() {
- byte[] boundaryMarker = getBoundaryMarker(request.getContentType());
- if (boundaryMarker == null) {
- throw new FileUploadException("The request was rejected because "
- + "no multipart boundary was found");
- }
-
- encoding = request.getCharacterEncoding();
-
- parameters = new HashMap<String, Param>();
- File file = null;
- this.percentMap = getProgressData();
-
- try {
- byte[] buffer = new byte[BUFFER_SIZE];
- Map<String, String> headers = new HashMap<String, String>();
-
- ReadState readState = ReadState.BOUNDARY;
-
- InputStream input = request.getInputStream();
- if (!shouldStop) {
-
- int read = input.read(buffer);
- int pos = 0;
-
- Param p = null;
-
- while (read != -1) {
- for (int i = 0; i < read; i++) {
- switch (readState) {
- case BOUNDARY: {
- if (checkSequence(buffer, i, boundaryMarker)
- && checkSequence(buffer, i + 2, CR_LF)) {
- readState = ReadState.HEADERS;
- i += 2;
- pos = i + 1;
-
- }
- break;
- }
- case HEADERS: {
- if (checkSequence(buffer, i, CR_LF)) {
- String param = (encoding == null) ? new String(
- buffer, pos, i - pos - 1) : new String(
- buffer, pos, i - pos - 1, encoding);
- parseParams(param, "; ", headers);
-
- if (checkSequence(buffer, i + CR_LF.length, CR_LF)) {
- readState = ReadState.DATA;
- i += CR_LF.length;
- pos = i + 1;
-
- String paramName = headers.get(PARAM_NAME);
- if (paramName != null) {
- if (headers.containsKey(PARAM_FILENAME)) {
- FileParam fp = new FileParam(paramName);
- if (createTempFiles)
- file = fp.createTempFile();
- fp.setContentType(headers
- .get(PARAM_CONTENT_TYPE));
- fp.setFilename(decodeFileName(headers
- .get(PARAM_FILENAME)));
- p = fp;
- } else {
- if (parameters.containsKey(paramName)) {
- p = parameters.get(paramName);
- } else {
- p = new ValueParam(paramName);
- }
- }
-
- if (!parameters.containsKey(paramName)) {
- parameters.put(paramName, p);
- }
- }
-
- headers.clear();
- } else {
- pos = i + 1;
- }
- }
- break;
- }
- case DATA: {
- // If we've encountered another boundary...
- if (checkSequence(buffer, i - boundaryMarker.length
- - CR_LF.length, CR_LF)
- && checkSequence(buffer, i, boundaryMarker)) {
- // Write any data before the boundary (that hasn't
- // already been written) to the param
- if (pos < i - boundaryMarker.length - CR_LF.length
- - 1) {
- p.appendData(buffer, pos, i - pos
- - boundaryMarker.length - CR_LF.length
- - 1);
- }
-
- if (p instanceof ValueParam)
- ((ValueParam) p).complete();
-
- if (checkSequence(buffer, i + CR_LF.length, CR_LF)) {
- i += CR_LF.length;
- pos = i + 1;
- } else {
- pos = i;
- }
-
- readState = ReadState.HEADERS;
- }
- // Otherwise write whatever data we have to the param
- else if (i > (pos + boundaryMarker.length + CHUNK_SIZE + CR_LF.length)) {
- p.appendData(buffer, pos, CHUNK_SIZE);
- pos += CHUNK_SIZE;
-
- }
- break;
- }
- }
- }
-
- if (!shouldStop) {
- if (pos < read) {
- // move the bytes that weren't read to the start of the
- // buffer
- int bytesNotRead = read - pos;
- System.arraycopy(buffer, pos, buffer, 0, bytesNotRead);
- read = input.read(buffer, bytesNotRead, buffer.length
- - bytesNotRead);
- read += bytesNotRead;
- } else {
- read = input.read(buffer);
- }
- this.read += pos;
- pos = 0;
- fillProgressInfo();
- } else {
- if (file != null) {
- file.delete();
- }
- return false;
- }
- }
-
- return true;
- } else {
- if (file != null) {
- file.delete();
- }
- return false;
- }
- } catch (IOException ex) {
- throw new FileUploadException("IO Error parsing multipart request",
- ex);
- }
- }
-
- @SuppressWarnings("unchecked")
- private Map<String, Object> getProgressData () {
- percentMap = (Map<String,
Object>)getSession().getAttribute(MultipartFilter.PERCENT_BEAN_NAME);
- return percentMap;
- }
-
- private void fillProgressInfo () {
- Double percent = (Double) (100.0 * this.read / this.contentLength);
- percentMap.put(uid, percent);
- //this.percent = percent;
- }
-
- private byte[] getBoundaryMarker(String contentType) {
- Map<String, String> params = parseParams(contentType, ";");
- String boundaryStr = (String) params.get("boundary");
-
- if (boundaryStr == null)
- return null;
-
- try {
- return boundaryStr.getBytes("ISO-8859-1");
- } catch (UnsupportedEncodingException e) {
- return boundaryStr.getBytes();
- }
- }
-
- /**
- * Checks if a specified sequence of bytes ends at a specific position
- * within a byte array.
- *
- * @param data
- * @param pos
- * @param seq
- * @return boolean indicating if the sequence was found at the specified
- * position
- */
- private boolean checkSequence(byte[] data, int pos, byte[] seq) {
- if (pos - seq.length < -1 || pos >= data.length)
- return false;
-
- for (int i = 0; i < seq.length; i++) {
- if (data[(pos - seq.length) + i + 1] != seq[i])
- return false;
- }
-
- return true;
- }
-
- private static final Pattern PARAM_VALUE_PATTERN = Pattern
- .compile("^\\s*([^\\s=]+)\\s*[=:]\\s*(.+)\\s*$");
-
- private static final Pattern FILE_NAME_PATTERN = Pattern
- .compile(".*filename=\"(.*)\"");
-
- private Map<String, String> parseParams(String paramStr, String separator) {
- Map<String, String> paramMap = new HashMap<String, String>();
- parseParams(paramStr, separator, paramMap);
- return paramMap;
- }
-
- private void parseParams(String paramStr, String separator, Map<String, String>
paramMap) {
- String[] parts = paramStr.split(separator);
-
- for (String part : parts) {
- Matcher m = PARAM_VALUE_PATTERN.matcher(part);
- if (m.matches()) {
- String key = m.group(1);
- String value = m.group(2);
-
- // Strip double quotes
- if (value.startsWith("\"") &&
value.endsWith("\""))
- value = value.substring(1, value.length() - 1);
- if (!"filename".equals(key)) {
- paramMap.put(key, value);
- }else {
- paramMap.put(key, parseFileName(paramStr));
- }
- }
- }
- }
-
- private String parseFileName(String parseStr) {
- Matcher m = FILE_NAME_PATTERN.matcher(parseStr);
- if (m.matches()) {
- String name = m.group(1);
- if (name.startsWith("&")) {
- return decodeFileName(name);
- } else{
- return name;
- }
- }
- return null;
- }
-
- private Param getParam(String name) {
- if (parameters == null)
- parseRequest();
- return parameters.get(name);
- }
-
-
- public Integer getSize() {
- return contentLength;
- }
-
- @Override
- public Enumeration getParameterNames() {
- if (parameters == null)
- parseRequest();
-
- return Collections.enumeration(parameters.keySet());
- }
-
- public byte[] getFileBytes(String name) {
- Param p = getParam(name);
- return (p != null && p instanceof FileParam) ? ((FileParam) p)
- .getData() : null;
- }
-
- public InputStream getFileInputStream(String name) {
- Param p = getParam(name);
- return (p != null && p instanceof FileParam) ? ((FileParam) p)
- .getInputStream() : null;
- }
-
- public String getFileContentType(String name) {
- Param p = getParam(name);
- return (p != null && p instanceof FileParam) ? ((FileParam) p)
- .getContentType() : null;
- }
-
- public Object getFile(String name) {
- Param p = getParam(name);
- return (p != null && p instanceof FileParam) ? ((FileParam) p)
- .getFile() : null;
- }
-
-
- public String getFileName(String name) {
- Param p = getParam(name);
- return (p != null && p instanceof FileParam) ? ((FileParam) p)
- .getFilename() : null;
- }
-
- public int getFileSize(String name) {
- Param p = getParam(name);
- return (p != null && p instanceof FileParam) ? ((FileParam) p)
- .getFileSize() : -1;
- }
-
- @Override
- public String getParameter(String name) {
- Param p = getParam(name);
- if (p != null && p instanceof ValueParam) {
- ValueParam vp = (ValueParam) p;
- if (vp.getValue() instanceof String)
- return (String) vp.getValue();
- } else if (p != null && p instanceof FileParam) {
- return "---BINARY DATA---";
- } else {
- return super.getParameter(name);
- }
-
- return null;
- }
-
- @Override
- public String[] getParameterValues(String name) {
- Param p = getParam(name);
- if (p != null && p instanceof ValueParam) {
- ValueParam vp = (ValueParam) p;
- if (vp.getValue() instanceof List) {
- List vals = (List) vp.getValue();
- String[] values = new String[vals.size()];
- vals.toArray(values);
- return values;
- } else {
- return new String[] { (String) vp.getValue() };
- }
- } else {
- return super.getParameterValues(name);
- }
- }
-
- @Override
- public Map getParameterMap() {
- if (parameters == null)
- parseRequest();
-
- Map<String, Object> params = new HashMap<String, Object>(super
- .getParameterMap());
-
- for (String name : parameters.keySet()) {
- Param p = parameters.get(name);
- if (p instanceof ValueParam) {
- ValueParam vp = (ValueParam) p;
- if (vp.getValue() instanceof String) {
- params.put(name, vp.getValue());
- } else if (vp.getValue() instanceof List) {
- params.put(name, getParameterValues(name));
- }
- }
- }
-
- return params;
- }
-
- public void stop() {
- shouldStop = true;
- }
-}
Modified:
trunk/ui/fileUpload/src/main/resources/org/richfaces/renderkit/html/js/FileUpload.js
===================================================================
---
trunk/ui/fileUpload/src/main/resources/org/richfaces/renderkit/html/js/FileUpload.js 2008-03-07
14:11:23 UTC (rev 6622)
+++
trunk/ui/fileUpload/src/main/resources/org/richfaces/renderkit/html/js/FileUpload.js 2008-03-07
14:13:22 UTC (rev 6623)
@@ -839,7 +839,7 @@
parentForm.encoding = "multipart/form-data";
parentForm.enctype = "multipart/form-data";
parentForm.target = this.id;
- parentForm.action = oldAction + (/\?/.test(oldAction) ? '&uid' :
'?uid') + '=' + encodeURI(entry.uid);
+ parentForm.action = oldAction + (/\?/.test(oldAction) ?
'&_richfaces_upload_uid' : '?_richfaces_upload_uid') + '=' +
encodeURI(entry.uid);
try {
var inputs = parentForm.elements;