Author: konstantin.mishin
Date: 2010-12-10 20:23:52 -0500 (Fri, 10 Dec 2010)
New Revision: 20513
Added:
trunk/ui/input/api/src/main/java/org/richfaces/model/
trunk/ui/input/api/src/main/java/org/richfaces/model/UploadedFile.java
Modified:
trunk/examples/input-demo/src/main/java/org/richfaces/demo/FileUploadBean.java
trunk/examples/input-demo/src/main/webapp/examples/fileupload.xhtml
trunk/ui/input/api/src/main/java/org/richfaces/event/UploadEvent.java
trunk/ui/input/ui/src/main/java/org/richfaces/renderkit/FileUploadRendererBase.java
trunk/ui/input/ui/src/main/java/org/richfaces/request/FileParam.java
trunk/ui/input/ui/src/main/java/org/richfaces/request/MultipartRequest.java
trunk/ui/input/ui/src/main/java/org/richfaces/request/Param.java
trunk/ui/input/ui/src/main/java/org/richfaces/request/ValueParam.java
Log:
RF-9972
Modified: trunk/examples/input-demo/src/main/java/org/richfaces/demo/FileUploadBean.java
===================================================================
---
trunk/examples/input-demo/src/main/java/org/richfaces/demo/FileUploadBean.java 2010-12-10
20:35:57 UTC (rev 20512)
+++
trunk/examples/input-demo/src/main/java/org/richfaces/demo/FileUploadBean.java 2010-12-11
01:23:52 UTC (rev 20513)
@@ -28,7 +28,7 @@
import javax.faces.event.AjaxBehaviorEvent;
import org.richfaces.event.UploadEvent;
-import org.richfaces.model.UploadItem;
+import org.richfaces.model.UploadedFile;
/**
* @author Konstantin Mishin
@@ -41,10 +41,10 @@
private String acceptedTypes;
private boolean enabled = true;
private boolean noDuplicate = false;
- private UploadItem item;
+ private UploadedFile file;
- public UploadItem getItem() {
- return item;
+ public UploadedFile getFile() {
+ return file;
}
// public void paint(OutputStream stream, Object object) throws IOException {
@@ -52,7 +52,7 @@
// }
public void listener(UploadEvent event) throws Exception {
- item = event.getUploadItem();
+ file = event.getUploadedFile();
}
public void setEnabled(boolean enabled) {
Modified: trunk/examples/input-demo/src/main/webapp/examples/fileupload.xhtml
===================================================================
--- trunk/examples/input-demo/src/main/webapp/examples/fileupload.xhtml 2010-12-10
20:35:57 UTC (rev 20512)
+++ trunk/examples/input-demo/src/main/webapp/examples/fileupload.xhtml 2010-12-11
01:23:52 UTC (rev 20513)
@@ -69,7 +69,7 @@
fileUploadListener="#{fileUploadBean.listener}"
noDuplicate="#{fileUploadBean.noDuplicate}"
onfilesubmit="onfilesubmit(event)"
onuploadcomplete="onuploadcomplete(event)"/>
<h:outputText value="File name:" />
- <a4j:outputPanel
ajaxRendered="true">#{fileUploadBean.item.fileName}</a4j:outputPanel>
+ <a4j:outputPanel
ajaxRendered="true">#{fileUploadBean.file.name}</a4j:outputPanel>
<br />
<h:outputText value="Attribute name: "/>
<h:inputText id="name"/>
Modified: trunk/ui/input/api/src/main/java/org/richfaces/event/UploadEvent.java
===================================================================
--- trunk/ui/input/api/src/main/java/org/richfaces/event/UploadEvent.java 2010-12-10
20:35:57 UTC (rev 20512)
+++ trunk/ui/input/api/src/main/java/org/richfaces/event/UploadEvent.java 2010-12-11
01:23:52 UTC (rev 20513)
@@ -1,22 +1,23 @@
-/**
- * License Agreement.
+/*
+ * 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.
*
- * JBoss RichFaces - Ajax4jsf Component Library
+ * 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.
*
- * Copyright (C) 2007 Exadel, Inc.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License version 2.1 as published by the Free Software Foundation.
- *
- * This library is distributed in the hope that it will be useful,
+ * 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
+ * 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 library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * 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.event;
@@ -24,16 +25,20 @@
import javax.faces.event.FacesEvent;
import javax.faces.event.FacesListener;
-import org.richfaces.model.UploadItem;
+import org.richfaces.model.UploadedFile;
+/**
+ * @author Konstantin Mishin
+ *
+ */
public class UploadEvent extends FacesEvent {
private static final long serialVersionUID = -7645197191376210068L;
- private UploadItem uploadItem = null;
+ private UploadedFile uploadedFile = null;
- public UploadEvent(UIComponent component, UploadItem uploadItem) {
+ public UploadEvent(UIComponent component, UploadedFile uploadedFile) {
super(component);
- this.uploadItem = uploadItem;
+ this.uploadedFile = uploadedFile;
}
public boolean isAppropriateListener(FacesListener listener) {
@@ -44,12 +49,7 @@
((FileUploadListener) listener).processUpload(this);
}
- /**
- * Returns UploadItem instance. Returns first element of list of UploadItems in case
of multiple upload.
- *
- * @return the uploadItem
- */
- public UploadItem getUploadItem() {
- return uploadItem;
+ public UploadedFile getUploadedFile() {
+ return uploadedFile;
}
}
Added: trunk/ui/input/api/src/main/java/org/richfaces/model/UploadedFile.java
===================================================================
--- trunk/ui/input/api/src/main/java/org/richfaces/model/UploadedFile.java
(rev 0)
+++ trunk/ui/input/api/src/main/java/org/richfaces/model/UploadedFile.java 2010-12-11
01:23:52 UTC (rev 20513)
@@ -0,0 +1,36 @@
+/*
+ * 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.model;
+
+import java.io.InputStream;
+
+/**
+ * @author Konstantin Mishin
+ *
+ */
+public interface UploadedFile {
+ String getContentType();
+ byte[] getData();
+ InputStream getInputStream();
+ String getName();
+ long getSize();
+}
Modified:
trunk/ui/input/ui/src/main/java/org/richfaces/renderkit/FileUploadRendererBase.java
===================================================================
---
trunk/ui/input/ui/src/main/java/org/richfaces/renderkit/FileUploadRendererBase.java 2010-12-10
20:35:57 UTC (rev 20512)
+++
trunk/ui/input/ui/src/main/java/org/richfaces/renderkit/FileUploadRendererBase.java 2010-12-11
01:23:52 UTC (rev 20513)
@@ -26,7 +26,7 @@
import javax.faces.context.FacesContext;
import org.richfaces.event.UploadEvent;
-import org.richfaces.model.UploadItem;
+import org.richfaces.model.UploadedFile;
import org.richfaces.request.MultipartRequest;
/**
@@ -40,9 +40,9 @@
ExternalContext externalContext = context.getExternalContext();
Object request = externalContext.getRequest();
if (request instanceof MultipartRequest) {
- UploadItem uploadItem = ((MultipartRequest)
request).getUploadItem(component.getClientId(context));
- if (uploadItem != null) {
- component.queueEvent(new UploadEvent(component, uploadItem));
+ UploadedFile file = ((MultipartRequest)
request).getUploadedFile(component.getClientId(context));
+ if (file != null) {
+ component.queueEvent(new UploadEvent(component, file));
}
}
}
Modified: trunk/ui/input/ui/src/main/java/org/richfaces/request/FileParam.java
===================================================================
--- trunk/ui/input/ui/src/main/java/org/richfaces/request/FileParam.java 2010-12-10
20:35:57 UTC (rev 20512)
+++ trunk/ui/input/ui/src/main/java/org/richfaces/request/FileParam.java 2010-12-11
01:23:52 UTC (rev 20513)
@@ -29,123 +29,98 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
import org.richfaces.exception.FileUploadException;
import org.richfaces.log.Logger;
import org.richfaces.log.RichfacesLogger;
+import org.richfaces.model.UploadedFile;
-class FileParam extends Param {
+/**
+ * @author Konstantin Mishin
+ *
+ */
+class FileParam implements Param, UploadedFile {
private static final Logger LOGGER = RichfacesLogger.APPLICATION.getLogger();
- private String filename;
+ private String name;
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();
+ private OutputStream outputStream;
+ private File file;
+ private byte[] data;
+
+ public FileParam(String name, String contentType, boolean createTempFiles, String
tempFilesDirectory) {
+ if (createTempFiles) {
+ try {
+ File dir = null;
+ if (tempFilesDirectory != null) {
+ dir = new File(tempFilesDirectory);
+ }
+ file = File.createTempFile("richfaces_uploaded_file_", null,
dir);
+ file.deleteOnExit();
+ outputStream = new FileOutputStream(file);
+ } catch (IOException ex) {
+ if (outputStream != null) {
+ try {
+ outputStream.close();
+ } catch (IOException e) {
+ LOGGER.error(e.getMessage(), e);
+ }
+ }
+ throw new FileUploadException("Could not create temporary
file");
+ }
+ } else {
+ outputStream = new ByteArrayOutputStream();
}
- return null;
+ this.name = name;
+ this.contentType = contentType;
}
- public String getFilename() {
- return filename;
+ public String getName() {
+ return name;
}
- 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(String tempFilesDirectory) {
- try {
- File dir = null;
- if (tempFilesDirectory != null) {
- dir = new File(tempFilesDirectory);
- }
- tempFile = File.createTempFile("richfaces_uploaded_file_", null,
dir);
- tempFile.deleteOnExit();
- fOut = new FileOutputStream(tempFile);
- } catch (IOException ex) {
- if (fOut != null) {
- try {
- fOut.close();
- } catch (IOException e) {
- LOGGER.error(e.getMessage(), e);
- }
- }
-
- throw new FileUploadException("Could not create temporary file");
+ public long getSize() {
+ long size = -1;
+ if (data != null) {
+ size = data.length;
+ } else {
+ size = file.length();
}
- return tempFile;
+ return size;
}
- public void deleteFile() {
- try {
- if (fOut != null) {
- fOut.close();
- }
- if (tempFile != null) {
- tempFile.delete();
- }
- } catch (Exception e) {
- throw new FileUploadException("Could not delete temporary file");
- }
- }
-
public byte[] getData() {
- if (bOut != null) {
- return bOut.toByteArray();
- } else if (tempFile != null) {
- if (tempFile.exists()) {
- FileInputStream fIn = null;
+ byte[] result = null;
+ if (data != null) {
+ result = data;
+ } else {
+ long fileLength = file.length();
+ if (fileLength > Integer.MAX_VALUE) {
+ throw new IllegalArgumentException("File content is too long to be
allocated as byte[]");
+ } else {
+ FileInputStream inputStream = null;
try {
- long fileLength = tempFile.length();
- if (fileLength > Integer.MAX_VALUE) {
- throw new IllegalArgumentException("File content is too long
to be allocated as byte[]");
- }
-
- fIn = new FileInputStream(tempFile);
-
+ inputStream = new FileInputStream(file);
byte[] fileData = new byte[(int) fileLength];
int totalRead = 0;
- int read = 0;
+ int read;
do {
- read = fIn.read(fileData, totalRead, fileData.length -
totalRead);
- if (read > 0) {
- totalRead += read;
- }
+ read = inputStream.read(fileData, totalRead, fileData.length -
totalRead);
+ totalRead += read;
} while (read > 0);
-
- return fileData;
- } catch (IOException ex) { /* too bad? */
+ result = fileData;
+ } catch (IOException ex) {
LOGGER.error(ex.getMessage(), ex);
} finally {
- if (fIn != null) {
+ if (inputStream != null) {
try {
- fIn.close();
+ inputStream.close();
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
}
@@ -153,54 +128,46 @@
}
}
}
-
- return null;
+ return result;
}
public InputStream getInputStream() {
- if (bOut != null) {
- return new ByteArrayInputStream(bOut.toByteArray());
- } else if (tempFile != null) {
+ InputStream stream = null;
+ if (data != null) {
+ stream = new ByteArrayInputStream(data);
+ } else {
try {
- return new FileInputStream(tempFile) {
- @Override
- public void close() throws IOException {
- super.close();
- tempFile.delete();
- }
- };
+ stream = new FileInputStream(file);
} catch (FileNotFoundException ex) {
LOGGER.error(ex.getMessage(), ex);
}
}
-
- return null;
+ return stream;
}
- @Override
public void complete() throws IOException {
- if (fOut != null) {
+ if (outputStream instanceof ByteArrayOutputStream) {
+ data = ((ByteArrayOutputStream) outputStream).toByteArray();
+ } else {
try {
- fOut.close();
+ outputStream.close();
} catch (IOException ex) {
LOGGER.error(ex.getMessage(), ex);
}
- fOut = null;
}
+ outputStream = null;
}
- public void handle(byte[] bytes, int length) throws IOException {
- // read += length;
- if (fOut != null) {
- fOut.write(bytes, 0, length);
- fOut.flush();
+ public void clear() {
+ if (data != null) {
+ data = null;
} else {
- if (bOut == null) {
- bOut = new ByteArrayOutputStream();
- }
- bOut.write(bytes, 0, length);
+ file.delete();
}
+ }
- fileSize += length;
+ public void handle(byte[] bytes, int length) throws IOException {
+ outputStream.write(bytes, 0, length);
+ outputStream.flush();
}
}
\ No newline at end of file
Modified: trunk/ui/input/ui/src/main/java/org/richfaces/request/MultipartRequest.java
===================================================================
--- trunk/ui/input/ui/src/main/java/org/richfaces/request/MultipartRequest.java 2010-12-10
20:35:57 UTC (rev 20512)
+++ trunk/ui/input/ui/src/main/java/org/richfaces/request/MultipartRequest.java 2010-12-11
01:23:52 UTC (rev 20513)
@@ -46,7 +46,7 @@
import org.richfaces.exception.FileUploadException;
import org.richfaces.log.Logger;
import org.richfaces.log.RichfacesLogger;
-import org.richfaces.model.UploadItem;
+import org.richfaces.model.UploadedFile;
import org.richfaces.request.ByteSequenceMatcher.BytesHandler;
public class MultipartRequest extends HttpServletRequestWrapper {
@@ -196,7 +196,7 @@
while (it.hasNext()) {
Param p = it.next();
if (p instanceof FileParam) {
- ((FileParam) p).deleteFile();
+ ((FileParam) p).clear();
}
}
}
@@ -222,20 +222,14 @@
String paramName = headers.get(PARAM_NAME);
if (paramName != null) {
if (headers.containsKey(PARAM_FILENAME)) {
- FileParam fp = new FileParam(paramName);
+ param = new FileParam(decodeFileName(headers.get(PARAM_FILENAME)),
headers.get(PARAM_CONTENT_TYPE),
+ createTempFiles, tempFilesDirectory);
this.keys.add(paramName);
-
- if (createTempFiles) {
- fp.createTempFile(tempFilesDirectory);
- }
- fp.setContentType(headers.get(PARAM_CONTENT_TYPE));
- fp.setFilename(decodeFileName(headers.get(PARAM_FILENAME)));
- param = fp;
} else {
if (parameters.containsKey(paramName)) {
param = parameters.get(paramName);
} else {
- param = new ValueParam(paramName, encoding);
+ param = new ValueParam(encoding);
}
}
@@ -586,12 +580,10 @@
return params;
}
- public UploadItem getUploadItem(String name) {
+ public UploadedFile getUploadedFile(String name) {
Param param = getParam(name);
- if (param instanceof FileParam) {
- FileParam fileParam = (FileParam) param;
- return new UploadItem(fileParam.getFilename(), fileParam.getFileSize(),
fileParam.getContentType(),
- fileParam.getFile());
+ if (param instanceof UploadedFile) {
+ return (UploadedFile) param;
} else {
return null;
}
Modified: trunk/ui/input/ui/src/main/java/org/richfaces/request/Param.java
===================================================================
--- trunk/ui/input/ui/src/main/java/org/richfaces/request/Param.java 2010-12-10 20:35:57
UTC (rev 20512)
+++ trunk/ui/input/ui/src/main/java/org/richfaces/request/Param.java 2010-12-11 01:23:52
UTC (rev 20513)
@@ -25,16 +25,10 @@
import org.richfaces.request.ByteSequenceMatcher.BytesHandler;
-abstract class Param implements BytesHandler {
- private String name;
-
- public Param(String name) {
- this.name = name;
- }
-
- public String getName() {
- return name;
- }
-
- public abstract void complete() throws IOException;
+/**
+ * @author Konstantin Mishin
+ *
+ */
+interface Param extends BytesHandler {
+ void complete() throws IOException;
}
\ No newline at end of file
Modified: trunk/ui/input/ui/src/main/java/org/richfaces/request/ValueParam.java
===================================================================
--- trunk/ui/input/ui/src/main/java/org/richfaces/request/ValueParam.java 2010-12-10
20:35:57 UTC (rev 20512)
+++ trunk/ui/input/ui/src/main/java/org/richfaces/request/ValueParam.java 2010-12-11
01:23:52 UTC (rev 20513)
@@ -26,14 +26,13 @@
import java.util.ArrayList;
import java.util.List;
-class ValueParam extends Param {
+class ValueParam implements Param {
private Object value = null;
private ByteArrayOutputStream buf = new ByteArrayOutputStream();
private String encoding;
- public ValueParam(String name, String encoding) {
- super(name);
+ public ValueParam(String encoding) {
this.encoding = encoding;
}
@@ -60,7 +59,6 @@
}
public void handle(byte[] bytes, int length) throws IOException {
- // read += length;
buf.write(bytes, 0, length);
}
}
\ No newline at end of file