Revision: 468
http://svn.sourceforge.net/stripes/?rev=468&view=rev
Author: bengunter
Date: 2006-11-15 21:05:29 -0800 (Wed, 15 Nov 2006)
Log Message:
-----------
Resolved STS-304: add FileBean.getReader()
Modified Paths:
--------------
trunk/stripes/src/net/sourceforge/stripes/action/FileBean.java
trunk/stripes/src/net/sourceforge/stripes/controller/multipart/CommonsMultipartWrapper.java
trunk/stripes/src/net/sourceforge/stripes/controller/multipart/CosMultipartWrapper.java
trunk/tests/src/net/sourceforge/stripes/action/FileBeanTests.java
Modified: trunk/stripes/src/net/sourceforge/stripes/action/FileBean.java
===================================================================
--- trunk/stripes/src/net/sourceforge/stripes/action/FileBean.java
2006-11-10 13:59:07 UTC (rev 467)
+++ trunk/stripes/src/net/sourceforge/stripes/action/FileBean.java
2006-11-16 05:05:29 UTC (rev 468)
@@ -48,6 +48,7 @@
private String contentType;
private String fileName;
private File file;
+ private String charset;
private boolean saved;
@@ -63,6 +64,21 @@
this.contentType = contentType;
this.fileName = originalName;
}
+
+ /**
+ * Constructs a FileBean pointing to an on-disk representation of the file
uploaded by the user.
+ *
+ * @param file the File object on the server which holds the uploaded
contents of the file
+ * @param contentType the content type of the file declared by the browser
during uplaod
+ * @param originalName the name of the file as declared by the user's
browser
+ * @param charset the charset specified by the servlet request
+ */
+ public FileBean(File file, String contentType, String originalName, String
charset) {
+ this.file = file;
+ this.contentType = contentType;
+ this.fileName = originalName;
+ this.charset = charset;
+ }
/**
* Returns the name of the file that the user selected and uplaoded (this
is not necessarily
@@ -92,8 +108,37 @@
public InputStream getInputStream() throws IOException {
return new FileInputStream(this.file);
}
+
+ /**
+ * Gets a reader to read characters from the uploaded file. If the servlet
request specifies a
+ * charset, then that charset is used. Otherwise, the reader uses the
default charset.
+ *
+ * @return a new reader
+ * @throws UnsupportedEncodingException
+ * @throws IOException
+ */
+ public Reader getReader() throws UnsupportedEncodingException, IOException
{
+ if (charset == null) {
+ return new InputStreamReader(getInputStream());
+ }
+ else {
+ return getReader(charset);
+ }
+ }
/**
+ * Gets a reader to read characters from the uploaded file using the given
charset.
+ *
+ * @param charset the charset the reader should use
+ * @return a new reader
+ * @throws UnsupportedEncodingException
+ * @throws IOException
+ */
+ public Reader getReader(String charset) throws
UnsupportedEncodingException, IOException {
+ return new InputStreamReader(getInputStream(), charset);
+ }
+
+ /**
* Saves the uploaded file to the location on disk represented by File.
First attemps a
* simple rename of the underlying file that was created during upload as
this is the
* most efficient route. If the rename fails an attempt is made to copy
the file bit
Modified:
trunk/stripes/src/net/sourceforge/stripes/controller/multipart/CommonsMultipartWrapper.java
===================================================================
---
trunk/stripes/src/net/sourceforge/stripes/controller/multipart/CommonsMultipartWrapper.java
2006-11-10 13:59:07 UTC (rev 467)
+++
trunk/stripes/src/net/sourceforge/stripes/controller/multipart/CommonsMultipartWrapper.java
2006-11-16 05:05:29 UTC (rev 468)
@@ -46,6 +46,7 @@
public class CommonsMultipartWrapper implements MultipartWrapper {
private Map<String,FileItem> files = new HashMap<String,FileItem>();
private Map<String,String[]> parameters = new HashMap<String, String[]>();
+ private String charset;
/**
* Pseudo-constructor that allows the class to perform any initialization
necessary.
@@ -63,6 +64,7 @@
public void build(HttpServletRequest request, File tempDir, long
maxPostSize)
throws IOException, FileUploadLimitExceededException {
try {
+ this.charset = request.getCharacterEncoding();
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setRepository(tempDir);
ServletFileUpload upload = new ServletFileUpload(factory);
@@ -151,7 +153,7 @@
// Use an anonymous inner subclass of FileBean that overrides all
the
// methods that rely on having a File present, to use the FileItem
// created by commons upload instead.
- return new FileBean(null, item.getContentType(), item.getName()) {
+ return new FileBean(null, item.getContentType(), item.getName(),
this.charset) {
@Override public long getSize() { return item.getSize(); }
@Override public InputStream getInputStream() throws
IOException {
Modified:
trunk/stripes/src/net/sourceforge/stripes/controller/multipart/CosMultipartWrapper.java
===================================================================
---
trunk/stripes/src/net/sourceforge/stripes/controller/multipart/CosMultipartWrapper.java
2006-11-10 13:59:07 UTC (rev 467)
+++
trunk/stripes/src/net/sourceforge/stripes/controller/multipart/CosMultipartWrapper.java
2006-11-16 05:05:29 UTC (rev 468)
@@ -43,6 +43,7 @@
Pattern.compile("Posted content length of (\\d*) exceeds limit of
(\\d*)");
private MultipartRequest multipart;
+ private String charset;
/**
* Pseudo-constructor that allows the class to perform any initialization
necessary.
*
@@ -60,10 +61,11 @@
throws IOException, FileUploadLimitExceededException {
try {
+ this.charset = request.getCharacterEncoding();
this.multipart = new MultipartRequest(request,
tempDir.getAbsolutePath(),
(int) maxPostSize,
-
request.getCharacterEncoding());
+ this.charset);
}
catch (IOException ioe) {
Matcher matcher = EXCEPTION_PATTERN.matcher(ioe.getMessage());
@@ -131,7 +133,8 @@
if (file != null) {
return new FileBean(file,
this.multipart.getContentType(name),
- this.multipart.getOriginalFileName(name));
+ this.multipart.getOriginalFileName(name),
+ this.charset);
}
else {
return null;
Modified: trunk/tests/src/net/sourceforge/stripes/action/FileBeanTests.java
===================================================================
--- trunk/tests/src/net/sourceforge/stripes/action/FileBeanTests.java
2006-11-10 13:59:07 UTC (rev 467)
+++ trunk/tests/src/net/sourceforge/stripes/action/FileBeanTests.java
2006-11-16 05:05:29 UTC (rev 468)
@@ -6,6 +6,7 @@
import org.testng.Assert;
import java.io.*;
+import java.nio.charset.Charset;
/**
* Basic set of tests for the FileBean object.
@@ -55,6 +56,53 @@
assertContents(this.to);
}
+ @Test(groups = "fast")
+ public void testReader() throws Exception {
+ FileBean bean = new FileBean(from, "text/plain", "somefile.txt");
+
+ Writer writer = new FileWriter(this.to);
+ Reader reader = bean.getReader();
+ char[] buf = new char[1024];
+ for (int count; (count = reader.read(buf)) > 0;)
+ writer.write(buf, 0, count);
+
+ Assert.assertTrue(this.to.exists());
+ Assert.assertFalse(this.from.exists());
+ assertContents(this.to);
+ }
+
+ @Test(groups = "fast")
+ public void testReaderWithCharset1() throws Exception {
+ String charset = Charset.defaultCharset().name();
+ FileBean bean = new FileBean(from, "text/plain", "somefile.txt",
charset);
+
+ Writer writer = new FileWriter(this.to);
+ Reader reader = bean.getReader();
+ char[] buf = new char[1024];
+ for (int count; (count = reader.read(buf)) > 0;)
+ writer.write(buf, 0, count);
+
+ Assert.assertTrue(this.to.exists());
+ Assert.assertFalse(this.from.exists());
+ assertContents(this.to);
+ }
+
+ @Test(groups = "fast")
+ public void testReaderWithCharset2() throws Exception {
+ String charset = Charset.defaultCharset().name();
+ FileBean bean = new FileBean(from, "text/plain", "somefile.txt");
+
+ Writer writer = new FileWriter(this.to);
+ Reader reader = bean.getReader(charset);
+ char[] buf = new char[1024];
+ for (int count; (count = reader.read(buf)) > 0;)
+ writer.write(buf, 0, count);
+
+ Assert.assertTrue(this.to.exists());
+ Assert.assertFalse(this.from.exists());
+ assertContents(this.to);
+ }
+
@Test(groups="fast")
public void testSaveByCopy() throws Exception {
FileBean bean = new FileBean(from, "text/plain", "somefile.txt");
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development