Author: konstantin.mishin
Date: 2010-11-19 13:02:08 -0500 (Fri, 19 Nov 2010)
New Revision: 20125
Removed:
sandbox/trunk/ui/fileupload/ui/src/main/java/org/richfaces/request/MultipartRequestRegistry.java
Modified:
sandbox/trunk/ui/fileupload/ui/src/main/java/org/richfaces/application/FileUploadPhaselistener.java
sandbox/trunk/ui/fileupload/ui/src/main/java/org/richfaces/request/MultipartRequest.java
Log:
RF-9497
Modified:
sandbox/trunk/ui/fileupload/ui/src/main/java/org/richfaces/application/FileUploadPhaselistener.java
===================================================================
---
sandbox/trunk/ui/fileupload/ui/src/main/java/org/richfaces/application/FileUploadPhaselistener.java 2010-11-19
15:47:43 UTC (rev 20124)
+++
sandbox/trunk/ui/fileupload/ui/src/main/java/org/richfaces/application/FileUploadPhaselistener.java 2010-11-19
18:02:08 UTC (rev 20125)
@@ -52,16 +52,16 @@
private static final long serialVersionUID = 138000954953175986L;
private static final Pattern AMPERSAND = Pattern.compile("&+");
-
+
private static final Logger LOGGER = RichfacesLogger.APPLICATION.getLogger();
-
- private static final String UID = "rf_fu_uid";
+ private static final String UID_KEY = "rf_fu_uid";
+
/** Flag indicating whether a temporary file should be used to cache the uploaded
file */
private boolean createTempFiles = false;
private String tempFilesDirectory;
-
+
/** The maximum size of a file upload request. 0 means no limit. */
private int maxRequestSize = 0;
@@ -73,9 +73,9 @@
} else {
this.createTempFiles = true;
}
-
+
this.tempFilesDirectory =
context.getInitParameter("tempFilesDirectory");
-
+
param = context.getInitParameter("maxRequestSize");
if (param != null) {
this.maxRequestSize = Integer.parseInt(param);
@@ -87,16 +87,16 @@
*
* @see javax.faces.event.PhaseListener#afterPhase(javax.faces.event.PhaseEvent)
*/
- public void afterPhase(PhaseEvent event) {
- if (PhaseId.APPLY_REQUEST_VALUES.equals(event.getPhaseId())) {
- FacesContext facesContext = event.getFacesContext();
- Object request = facesContext.getExternalContext().getRequest();
- if (request instanceof MultipartRequest) {
- printResponse(facesContext, HttpServletResponse.SC_OK,
- "<html id=\"" + UID + ((MultipartRequest)request).getUploadId() +
":done\"/>");
- }
- }
- }
+ public void afterPhase(PhaseEvent event) {
+ if (PhaseId.APPLY_REQUEST_VALUES.equals(event.getPhaseId())) {
+ FacesContext facesContext = event.getFacesContext();
+ Object request = facesContext.getExternalContext().getRequest();
+ if (request instanceof MultipartRequest) {
+ printResponse(facesContext, HttpServletResponse.SC_OK, "<html
id=\"" + UID_KEY
+ + ((MultipartRequest) request).getUploadId() +
":done\"/>");
+ }
+ }
+ }
/*
* (non-Javadoc)
@@ -105,75 +105,58 @@
*/
public void beforePhase(PhaseEvent event) {
if (PhaseId.RESTORE_VIEW.equals(event.getPhaseId())) {
- FacesContext facesContext = event.getFacesContext();
- ExternalContext externalContext = facesContext.getExternalContext();
- HttpServletRequest request = (HttpServletRequest) externalContext
- .getRequest();
- Map<String, String> queryParamMap = parseQueryString(request
- .getQueryString());
- String uid = queryParamMap.get(UID);
- if (uid != null) {
- if (maxRequestSize != 0
- && externalContext.getRequestContentLength() > maxRequestSize) {
- boolean sendError = Boolean.parseBoolean(queryParamMap
- .get(MultipartRequest.SEND_HTTP_ERROR));
- if (sendError) {
- printResponse(
- facesContext,
- HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE,
- null);
- } else {
- printResponse(facesContext, HttpServletResponse.SC_OK,
- "<html id=\"" + UID + uid +
":size_restricted\"/>");
- }
- } else if (!checkFileCount(externalContext,
- queryParamMap.get("id"))) {
- printResponse(facesContext, HttpServletResponse.SC_OK,
- "<html id=\"" + UID + uid +
":forbidden\"/>");
- } else {
- MultipartRequest multipartRequest = new MultipartRequest(
- request, createTempFiles, tempFilesDirectory,
- maxRequestSize, uid);
- try {
- multipartRequest.parseRequest();
- if (!multipartRequest.isDone()) {
- printResponse(facesContext,
- HttpServletResponse.SC_OK,
- "<html id=\"" + UID + uid +
":stopped\"/>");
- } else {
- externalContext.setRequest(multipartRequest);
- }
- } catch (FileUploadException e) {
- printResponse(facesContext,
- HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
- null);
- throw e; //TODO remove it
- } finally {
- multipartRequest.clearRequestData();
- }
- }
- }
- }
+ FacesContext facesContext = event.getFacesContext();
+ ExternalContext externalContext = facesContext.getExternalContext();
+ HttpServletRequest request = (HttpServletRequest)
externalContext.getRequest();
+ Map<String, String> queryParamMap =
parseQueryString(request.getQueryString());
+ String uid = queryParamMap.get(UID_KEY);
+ if (uid != null) {
+ if (maxRequestSize != 0 &&
externalContext.getRequestContentLength() > maxRequestSize) {
+ printResponse(facesContext, HttpServletResponse.SC_OK, "<html
id=\"" + UID_KEY + uid
+ + ":size_restricted\"/>");
+ } else if (!checkFileCount(externalContext,
queryParamMap.get("id"))) {
+ printResponse(facesContext, HttpServletResponse.SC_OK, "<html
id=\"" + UID_KEY + uid
+ + ":forbidden\"/>");
+ } else {
+ MultipartRequest multipartRequest = new MultipartRequest(request,
createTempFiles,
+ tempFilesDirectory, maxRequestSize, uid);
+ try {
+ multipartRequest.parseRequest();
+ if (!multipartRequest.isDone()) {
+ printResponse(facesContext, HttpServletResponse.SC_OK,
"<html id=\"" + UID_KEY + uid
+ + ":stopped\"/>");
+ } else {
+ externalContext.setRequest(multipartRequest);
+ }
+ } catch (FileUploadException e) {
+ printResponse(facesContext,
HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null);
+ throw e; // TODO remove it
+ } finally {
+ multipartRequest.clearRequestData();
+ }
+ }
+ }
+ }
}
private boolean checkFileCount(ExternalContext externalContext, String idParameter)
{
- //TODO implement this method
-// HttpSession session = externalContext.getSession(false);
-//
-// if (session != null) {
-// Map<String, Integer> map = (Map<String, Integer>) session
-// .getAttribute(FileUploadConstants.UPLOADED_COUNTER);
-//
-// if (map != null) {
-// String id = idParameter;
-// if (id != null) {
-// Integer i = map.get(id);
-// if (i != null && i == 0) {
-// return false;
-// }
-// }
-// }
-// }
+ // TODO implement this method
+ // HttpSession session = externalContext.getSession(false);
+ //
+ // if (session != null) {
+ // Map<String, Integer> map = (Map<String, Integer>) session
+ // .getAttribute(FileUploadConstants.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;
}
@@ -196,7 +179,7 @@
parameters.put(name, value);
}
} catch (UnsupportedEncodingException e) {
- //log warning and skip this parameter
+ // log warning and skip this parameter
LOGGER.warn(e.getLocalizedMessage(), e);
}
}
Modified:
sandbox/trunk/ui/fileupload/ui/src/main/java/org/richfaces/request/MultipartRequest.java
===================================================================
---
sandbox/trunk/ui/fileupload/ui/src/main/java/org/richfaces/request/MultipartRequest.java 2010-11-19
15:47:43 UTC (rev 20124)
+++
sandbox/trunk/ui/fileupload/ui/src/main/java/org/richfaces/request/MultipartRequest.java 2010-11-19
18:02:08 UTC (rev 20125)
@@ -51,11 +51,6 @@
public class MultipartRequest extends HttpServletRequestWrapper {
- /**
- * Request parameter that allow to send HTTP error instead of html message
- */
- public static final String SEND_HTTP_ERROR = "_richfaces_send_http_error";
-
public static final String TEXT_HTML = "text/html";
private static final BytesHandler NOOP_HANDLER = new BytesHandler() {
@@ -70,9 +65,6 @@
/** Session bean name where progress bar's percent map will be stored */
private static final String PERCENT_BEAN_NAME =
"_richfaces_upload_percents";
- /** Session bean name where stop keys will be stored */
- private static final String REQUEST_KEYS_BEAN_NAME =
"_richfaces_request_keys";
-
private static final String PARAM_NAME = "name";
private static final String PARAM_FILENAME = "filename";
private static final String PARAM_CONTENT_TYPE = "Content-Type";
@@ -85,8 +77,8 @@
private static final byte CR = 0x0d;
private static final byte LF = 0x0a;
- private static final byte[] CR_LF = {CR, LF};
- private static final byte[] HYPHENS = {0x2d, 0x2d}; // '--'
+ private static final byte[] CR_LF = { CR, LF };
+ private static final byte[] HYPHENS = { 0x2d, 0x2d }; // '--'
private static final Pattern PARAM_VALUE_PATTERN =
Pattern.compile("^\\s*([^\\s=]+)\\s*[=:]\\s*(.+)\\s*$");
@@ -113,12 +105,6 @@
private Map<String, Integer> requestSizeMap = null;
- private Map<String, String> requestKeysMap = null;
-
- private String requestKey = null;
-
- private MultipartRequestRegistry requestRegistry;
-
private List<String> keys = new ArrayList<String>();
private byte[] boundaryMarker;
@@ -188,25 +174,20 @@
private String decodeFileName(String name) {
String fileName = null;
-
try {
- if (getRequest().getParameter(SEND_HTTP_ERROR) != null) {
- fileName = new String(name.getBytes(encoding), "UTF-8");
- } else {
- 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);
- }
+ 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();
}
+ fileName = buffer.toString();
}
} catch (Exception e) {
fileName = name;
@@ -407,29 +388,9 @@
}
}
- public static MultipartRequest lookupRequest(FacesContext context, String uploadId)
{
- Map<String, Object> sessionMap =
context.getExternalContext().getSessionMap();
- @SuppressWarnings("unchecked")
- Map<String, String> requestKeys = (Map<String, String>)
sessionMap.get(REQUEST_KEYS_BEAN_NAME);
- if (requestKeys != null) {
- String requestKey = requestKeys.get(uploadId);
- if (requestKey != null) {
- MultipartRequestRegistry requestRegistry =
MultipartRequestRegistry.getInstance(context);
- if (requestRegistry != null) {
- MultipartRequest request = requestRegistry.getRequest(requestKey);
- if (request != null) {
- return request;
- }
- }
- }
- }
-
- return null;
- }
-
@SuppressWarnings("unchecked")
private void setupProgressData() {
- if (percentMap == null || requestSizeMap == null || requestKeysMap == null) {
+ if (percentMap == null || requestSizeMap == null) {
FacesContext facesContext = FacesContext.getCurrentInstance();
if (facesContext != null) {
ExternalContext externalContext = facesContext.getExternalContext();
@@ -454,25 +415,11 @@
sessionMap.put(REQUEST_SIZE_BEAN_NAME,
requestSizeMap);
}
}
-
- if (requestKeysMap == null) {
- requestKeysMap = (Map<String, String>)
sessionMap.get(REQUEST_KEYS_BEAN_NAME);
- if (requestKeysMap == null) {
- requestKeysMap = new ConcurrentHashMap<String,
String>();
- sessionMap.put(REQUEST_KEYS_BEAN_NAME,
requestKeysMap);
- }
-
- }
}
percentMap.put(uploadId, Double.valueOf(0));
requestSizeMap.put(uploadId, getSize());
-
- requestRegistry =
MultipartRequestRegistry.getInstance(facesContext);
- requestKey = requestRegistry.registerRequest(this);
- requestKeysMap.put(uploadId, requestKey);
-
}
}
}
@@ -645,7 +592,7 @@
vals.toArray(values);
return values;
} else {
- return new String[] {(String) vp.getValue() };
+ return new String[] { (String) vp.getValue() };
}
} else {
return super.getParameterValues(name);
@@ -731,13 +678,5 @@
if (requestSizeMap != null) {
requestSizeMap.remove(uploadId);
}
-
- if (requestKeysMap != null) {
- requestKeysMap.remove(uploadId);
- }
-
- if (requestRegistry != null) {
- requestRegistry.removeRequest(requestKey);
- }
}
}
\ No newline at end of file
Deleted:
sandbox/trunk/ui/fileupload/ui/src/main/java/org/richfaces/request/MultipartRequestRegistry.java
===================================================================
---
sandbox/trunk/ui/fileupload/ui/src/main/java/org/richfaces/request/MultipartRequestRegistry.java 2010-11-19
15:47:43 UTC (rev 20124)
+++
sandbox/trunk/ui/fileupload/ui/src/main/java/org/richfaces/request/MultipartRequestRegistry.java 2010-11-19
18:02:08 UTC (rev 20125)
@@ -1,76 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright ${year}, Red Hat, Inc. and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.richfaces.request;
-
-import java.util.Map;
-import java.util.UUID;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.atomic.AtomicInteger;
-
-import javax.faces.context.FacesContext;
-
-final class MultipartRequestRegistry {
-
- private static final String REGISTRY_ATTRIBUTE_NAME =
MultipartRequestRegistry.class.getName();
-
- private AtomicInteger atomicInteger = new AtomicInteger(0);
-
- private String registryId = UUID.randomUUID().toString();
-
- private Map<String, MultipartRequest> requestsMap = new
ConcurrentHashMap<String, MultipartRequest>();
-
- private MultipartRequestRegistry() {
-
- }
-
- public static MultipartRequestRegistry getInstance(FacesContext context) {
- Map<String, Object> applicationMap =
context.getExternalContext().getApplicationMap();
- // MultipartRequestRegistry requestRegistry = (MultipartRequestRegistry)
applicationMap
- // .get(REGISTRY_ATTRIBUTE_NAME);
- // if (requestRegistry == null) {
- synchronized (applicationMap) {
- MultipartRequestRegistry requestRegistry = (MultipartRequestRegistry)
applicationMap
- .get(REGISTRY_ATTRIBUTE_NAME);
- if (requestRegistry == null) {
- requestRegistry = new MultipartRequestRegistry();
- applicationMap.put(REGISTRY_ATTRIBUTE_NAME, requestRegistry);
- }
- // }
- return requestRegistry;
- }
- }
-
- public String registerRequest(MultipartRequest request) {
- String key = registryId + ":" + atomicInteger.incrementAndGet();
- requestsMap.put(key, request);
-
- return key;
- }
-
- public void removeRequest(String key) {
- requestsMap.remove(key);
- }
-
- public MultipartRequest getRequest(String key) {
- return requestsMap.get(key);
- }
-}