Author: nbelaevski
Date: 2008-09-25 09:54:14 -0400 (Thu, 25 Sep 2008)
New Revision: 10558
Modified:
trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/BaseFilter.java
Log:
https://jira.jboss.org/jira/browse/RF-2914
Modified: trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/BaseFilter.java
===================================================================
--- trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/BaseFilter.java 2008-09-25
12:06:18 UTC (rev 10557)
+++ trunk/framework/impl/src/main/java/org/ajax4jsf/webapp/BaseFilter.java 2008-09-25
13:54:14 UTC (rev 10558)
@@ -24,11 +24,13 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
+import java.util.regex.Pattern;
import javax.faces.application.ViewHandler;
import javax.servlet.Filter;
@@ -38,7 +40,6 @@
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
-import javax.servlet.ServletResponseWrapper;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
@@ -201,6 +202,41 @@
}
}
+ private static final Pattern AMPERSAND = Pattern.compile("&+");
+
+ private Map<String, String> parseQueryString(String queryString) {
+ if (queryString != null) {
+ Map<String, String> parameters = new HashMap<String, String>();
+
+ String[] nvPairs = AMPERSAND.split(queryString);
+ for (String nvPair : nvPairs) {
+ if (nvPair.length() == 0) {
+ continue;
+ }
+
+ int eqIdx = nvPair.indexOf('=');
+ if (eqIdx >= 0) {
+ try {
+ String name = URLDecoder.decode(nvPair.substring(0, eqIdx), "UTF-8");
+ if (!parameters.containsKey(name)) {
+ String value = URLDecoder.decode(nvPair.substring(eqIdx + 1), "UTF-8");
+
+ parameters.put(name, value);
+ }
+ } catch (UnsupportedEncodingException e) {
+ //log warning and skip this parameter
+ log.warn(e.getLocalizedMessage(), e);
+ }
+ }
+ }
+
+ return parameters;
+ } else {
+
+ return Collections.EMPTY_MAP;
+ }
+ }
+
private boolean isMultipartRequest(HttpServletRequest request) {
if (!"post".equals(request.getMethod().toLowerCase())) {
return false;
@@ -225,19 +261,23 @@
return false;
}
- private boolean checkFileCount(HttpServletRequest request) {
- HttpSession session = request.getSession();
- Map<String, Integer> map = (Map<String, Integer>) session
- .getAttribute(UPLOADED_COUNTER);
- if (map != null) {
- String id = request.getParameter("id");
- if (id != null) {
- Integer i = map.get(id);
- if (i != null && i == 0) {
- return false;
+ private boolean checkFileCount(HttpServletRequest request, String idParameter) {
+ HttpSession session = request.getSession(false);
+
+ if (session != null) {
+ Map<String, Integer> map = (Map<String, Integer>)
session.getAttribute(UPLOADED_COUNTER);
+
+ if (map != null) {
+ String id = idParameter;
+ if (id != null) {
+ Integer i = map.get(id);
+ if (i != null && i == 0) {
+ return false;
+ }
}
}
}
+
return true;
}
@@ -292,94 +332,97 @@
* @throws ServletException
*/
protected void processUploadsAndHandleRequest(HttpServletRequest request,
HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
- HttpServletRequest httpRequest = (HttpServletRequest) request;
- String uid = httpRequest.getParameter(UPLOAD_FILES_ID);
+ HttpServletRequest httpRequest = (HttpServletRequest) request;
+
+ Map<String, String> queryParamMap =
parseQueryString(httpRequest.getQueryString());
+ String uid = queryParamMap.get(UPLOAD_FILES_ID);
- if (uid != null) {
-
- 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)) {
-
- boolean sendError = Boolean.parseBoolean(request.getParameter(SEND_HTTP_ERROR));
- if (sendError) {
- response.sendError(HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE);
- System.err.println("ERROR " +
HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE + "request entity is larger than the
server is willing or able to process.");
- return;
+ if (uid != null) {
+
+ 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)) {
+
+ boolean sendError = Boolean.parseBoolean(queryParamMap.get(SEND_HTTP_ERROR));
+ if (sendError) {
+ response.sendError(HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE);
+ System.err.println("ERROR " +
HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE + "request entity is larger than the
server is willing or able to process.");
+ return;
+ } else {
+ printResponse(response, "<html
id=\"_richfaces_file_upload_size_restricted\"></html>");
+ }
+
+ } else if (!checkFileCount(httpRequest, queryParamMap.get("id"))) {
+ printResponse(response, "<html
id=\"_richfaces_file_upload_forbidden\"></html>");
+ } else {
+
+ 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);
+ }
+ }
+
+ /* associate percent value with file entry uid */
+ percentMap.put(uid, 0);
+ sessionsMap.put(uid, multipartRequest);
+
+ if (multipartRequest.parseRequest()) {
+ handleRequest(multipartRequest, multipartRequest.isFormUpload() ? response :
+ new HttpServletResponseWrapper(response){
+ @Override
+ public void setContentType(String type) {
+ super.setContentType(BaseXMLFilter.TEXT_HTML + ";charset=UTF-8");
+ }
+ }, chain);
+ } else {
+ printResponse(response, "<html
id=\"_richfaces_file_upload_stopped\"></html>");
+ }
+
+ }
+
+ } finally {
+
+ if (sessionsMap != null) {
+ sessionsMap.remove(uid);
+ percentMap.remove(uid);
+ }
+
+ }
} else {
- printResponse(response, "<html
id=\"_richfaces_file_upload_size_restricted\"></html>");
- }
-
- } else if (!checkFileCount(httpRequest)) {
- printResponse(response, "<html
id=\"_richfaces_file_upload_forbidden\"></html>");
- } else {
-
- 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);
- }
- }
-
- /* associate percent value with file entry uid */
- percentMap.put(uid, 0);
- sessionsMap.put(uid, multipartRequest);
-
- if (multipartRequest.parseRequest()) {
- handleRequest(multipartRequest, multipartRequest.isFormUpload() ? response :
- new HttpServletResponseWrapper(response){
- @Override
- public void setContentType(String type) {
- super.setContentType(BaseXMLFilter.TEXT_HTML + ";charset=UTF-8");
- }
- }, chain);
- } else {
- printResponse(response, "<html
id=\"_richfaces_file_upload_stopped\"></html>");
- }
-
- }
-
- } finally {
-
- if (sessionsMap != null) {
- sessionsMap.remove(uid);
- percentMap.remove(uid);
- }
-
- }
- } else {
-
- if
("richfaces_file_upload_action_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();
+ if
("richfaces_file_upload_action_stop".equals(queryParamMap.get("action")))
{
+ HttpSession session = httpRequest.getSession(false);
+ if (session != null) {
+ 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();
+ }
+ handleRequest(request, response, chain);
+ }
+ }
+ } else {
+ handleRequest(request, response, chain);
+ }
}
+ } else {
handleRequest(request, response, chain);
- }
-
- } else {
- handleRequest(request, response, chain);
}
- }
- } else {
- handleRequest(request, response, chain);
}
- }
/**
* @param httpServletRequest