* Add new MimePart.getInputStream() method.

* Add MimePart constructor that takes a jakarta commons-fileupload FileItem
  as argument.
* Make all fields private, add getters and setters for lastModified and eTag.
* Add some javadoc.
This commit is contained in:
hns 2007-06-04 08:06:00 +00:00
parent 19c2858b32
commit 0068de78ec
3 changed files with 119 additions and 38 deletions

View file

@ -312,10 +312,10 @@ public class GlobalObject extends ImporterTopLevel implements PropertyRecorder {
MimePart mime = new MimePart(filename, body.toByteArray(), contentType); MimePart mime = new MimePart(filename, body.toByteArray(), contentType);
if (lastmod > 0) { if (lastmod > 0) {
mime.lastModified = new Date(lastmod); mime.setLastModified(new Date(lastmod));
} }
mime.eTag = etag; mime.setETag(etag);
return Context.toObject(mime, this); return Context.toObject(mime, this);
} catch (Exception x) { } catch (Exception x) {

View file

@ -16,6 +16,8 @@
package helma.util; package helma.util;
import org.apache.commons.fileupload.FileItem;
import java.io.*; import java.io.*;
import java.util.Date; import java.util.Date;
import java.util.StringTokenizer; import java.util.StringTokenizer;
@ -24,19 +26,19 @@ import java.util.StringTokenizer;
* This represents a MIME part of a HTTP file upload * This represents a MIME part of a HTTP file upload
*/ */
public class MimePart implements Serializable { public class MimePart implements Serializable {
public final String name; private final String name;
public int contentLength; private int contentLength;
public String contentType; private String contentType;
private byte[] content; private byte[] content;
public Date lastModified; private Date lastModified;
public String eTag; private String eTag;
private FileItem fileItem;
/** /**
* Creates a new MimePart object. * Creates a new MimePart object.
* * @param name the file name
* @param name ... * @param content the mime part content
* @param content ... * @param contentType the content type
* @param contentType ...
*/ */
public MimePart(String name, byte[] content, String contentType) { public MimePart(String name, byte[] content, String contentType) {
this.name = normalizeFilename(name); this.name = normalizeFilename(name);
@ -46,50 +48,95 @@ public class MimePart implements Serializable {
} }
/** /**
* * Creates a new MimePart object from a file upload.
* * @param fileItem a commons fileupload file item
* @return ... */
public MimePart(FileItem fileItem) {
name = fileItem.getName();
contentType = fileItem.getContentType();
contentLength = (int) fileItem.getSize();
if (fileItem.isInMemory()) {
content = fileItem.get();
} else {
this.fileItem = fileItem;
}
}
/**
* @return the content type
*/ */
public String getContentType() { public String getContentType() {
return contentType; return contentType;
} }
/** /**
* * Get the number of bytes in the mime part's content
* * @return the content length
* @return ...
*/ */
public int getContentLength() { public int getContentLength() {
return contentLength; return contentLength;
} }
/** /**
* * Get the mime part's name
* * @return the file name
* @return ...
*/ */
public String getName() { public String getName() {
return name; return name;
} }
/** /**
* * Return the content of the mime part as byte array.
* * @return the mime part content as byte array
* @return ...
*/ */
public byte[] getContent() { public byte[] getContent() {
if (content == null && fileItem != null) {
loadContent();
}
return content; return content;
} }
private synchronized void loadContent() {
content = new byte[contentLength];
try {
InputStream in = fileItem.getInputStream();
int read = 0;
while (read < contentLength) {
int r = in.read(content, read, contentLength - read);
if (r == -1)
break;
read += r;
}
in.close();
} catch (IOException iox) {
content = new byte[0];
}
}
/** /**
* Return an InputStream to read the content of the mime part
* @return an InputStream for the mime part content
* @throws IOException an I/O related error occurred
*/
public InputStream getInputStream() throws IOException {
if (fileItem != null) {
return fileItem.getInputStream();
} else {
return new ByteArrayInputStream(getContent());
}
}
/**
* Return the content of the mime part as string, if its content type is
* null, text/* or application/text. Otherwise, return null.
* *
* * @return the content of the mime part as string
* @return ...
*/ */
public String getText() { public String getText() {
if ((contentType == null) || contentType.startsWith("text/") if ((contentType == null) || contentType.startsWith("text/")
|| contentType.startsWith("application/text")) { || contentType.startsWith("application/text")) {
String charset = getSubHeader(contentType, "charset"); String charset = getSubHeader(contentType, "charset");
byte[] content = getContent();
if (charset != null) { if (charset != null) {
try { try {
return new String(content, charset); return new String(content, charset);
@ -104,24 +151,54 @@ public class MimePart implements Serializable {
} }
} }
/** /**
* Get the last modified date
* @return the last modified date
*/
public Date getLastModified() {
return lastModified;
}
/**
* Set the last modified date
* @param lastModified the last modified date
*/
public void setLastModified(Date lastModified) {
this.lastModified = lastModified;
}
/**
* Get the ETag of the mime part
* @return the ETag
*/
public String getETag() {
return eTag;
}
/**
* Set the ETag for the mime part
* @param eTag the ETag
*/
public void setETag(String eTag) {
this.eTag = eTag;
}
/**
* Write the mimepart to a directory, using its name as file name.
* *
* * @param dir the directory to write the file to
* @param dir ... * @return the absolute path name of the file written, or null if an error occurred
*
* @return ...
*/ */
public String writeToFile(String dir) { public String writeToFile(String dir) {
return writeToFile(dir, null); return writeToFile(dir, null);
} }
/** /**
* Write the mimepart to a file.
* *
* * @param dir the directory to write the file to
* @param dir ... * @return the absolute path name of the file written, or null if an error occurred
* @param fname ...
*
* @return ...
*/ */
public String writeToFile(String dir, String fname) { public String writeToFile(String dir, String fname) {
try { try {
@ -150,10 +227,14 @@ public class MimePart implements Serializable {
} }
File file = new File(base, filename); File file = new File(base, filename);
FileOutputStream fout = new FileOutputStream(file);
if (fileItem != null) {
fileItem.write(file);
} else {
FileOutputStream fout = new FileOutputStream(file);
fout.write(getContent()); fout.write(getContent());
fout.close(); fout.close();
}
return filename; return filename;
} catch (Exception x) { } catch (Exception x) {

View file

@ -75,7 +75,7 @@ public class MimePartDataSource implements DataSource {
* @return ... * @return ...
*/ */
public String getContentType() { public String getContentType() {
return part.contentType; return part.getContentType();
} }
/** /**