Hi list,
within the letter there is a patch attached which provides the following features:
* It is now possible to EDIT blob columns from within textarea form tag. Here is the simple example:
<db:textAreaForBlobs fieldName="BODY" renderBody="true">
<db:blobContent fieldName="BODY"/>
</db:textAreaForBlobs>Currently the text will be inserted into the database using system default character set, which generally depends on your locale settings.
* As Henner suggested, readDbFieldBlob() and readDiskBlob() methods moved from src/org/dbforms/servlets/FileServlet.java to src/org/dbforms/utils/SqlUtil.java class. As a result, code in taglib.DbBlobContentTag.java looks better.
* BlobInterceptor (src/org/dbforms/event/BlobInterceptor.java) now can optionaly save the mime type and file size of an uploaded file if additional parameters are provided to dbforms-config.xml. It could look like this:
<interceptor className="org.dbforms.event.BlobInterceptor">
<param name="blob-column" value="BODY"/>
<param name="name-column" value="FILE_NAME"/>
<param name="mime-column" value="MIME_TYPE"/>
<param name="size-column" value="BODY_SIZE"/>
</interceptor>* The FileServlet servlet now adds "Cache-control: private" header as according to my tests, without this MSIE gives an error while trying to "Open" the file. "Save" works ok.
Please comment, test and vote.
regards, Dziugas Baltrunas
Index: org/dbforms/config/FieldValue.java
===================================================================
RCS file: /cvsroot/jdbforms/dbforms/src/org/dbforms/config/FieldValue.java,v
retrieving revision 1.14
diff -u -r1.14 FieldValue.java
--- org/dbforms/config/FieldValue.java 13 Apr 2004 13:20:49 -0000 1.14
+++ org/dbforms/config/FieldValue.java 4 Aug 2004 09:23:52 -0000
@@ -418,7 +418,12 @@
break;
case FieldTypes.BLOB :
- res = getFileHolder();
+ // FileHolder object might not be initialized if
textarea is used for blobs
+ if (getFileHolder() == null) {
+ res = value;
+ } else {
+ res = getFileHolder();
+ }
break;
case FieldTypes.DISKBLOB :
Index: org/dbforms/config/JDBCDataHelper.java
===================================================================
RCS file: /cvsroot/jdbforms/dbforms/src/org/dbforms/config/JDBCDataHelper.java,v
retrieving revision 1.6
diff -u -r1.6 JDBCDataHelper.java
--- org/dbforms/config/JDBCDataHelper.java 8 Apr 2004 11:09:55 -0000 1.6
+++ org/dbforms/config/JDBCDataHelper.java 4 Aug 2004 09:23:52 -0000
@@ -83,9 +83,9 @@
if (value == null) {
ps.setNull(col, java.sql.Types.BLOB);
} else {
- FileHolder fileHolder = (FileHolder) value;
-
+ //FileHolder fileHolder = (FileHolder) value;
if (blobStrategy == Table.BLOB_CLASSIC) {
+ FileHolder fileHolder = (FileHolder)
value;
try {
ByteArrayOutputStream byteOut =
new
ByteArrayOutputStream();
@@ -116,10 +116,19 @@
}
// case TABLE.BLOB_INTERCEPTOR: direct
storage, the rest is dont by interceptor
else {
- ps.setBinaryStream(
- col,
-
fileHolder.getInputStreamFromBuffer(),
- fileHolder.getFileLength());
+ if (value instanceof FileHolder) { // if
we have a file upload
+ FileHolder fileHolder = (FileHolder)
value;
+ ps.setBinaryStream(
+ col,
+ fileHolder.getInputStreamFromBuffer(),
+ fileHolder.getFileLength());
+ } else { // if the blob field is updated
from within textarea
+ byte[] data = ((String)
value).getBytes();
+ ps.setBinaryStream(
+ col,
+ new ByteArrayInputStream(data),
+ data.length);
+ }
}
}
Index: org/dbforms/event/BlobInterceptor.java
===================================================================
RCS file: /cvsroot/jdbforms/dbforms/src/org/dbforms/event/BlobInterceptor.java,v
retrieving revision 1.3
diff -u -r1.3 BlobInterceptor.java
--- org/dbforms/event/BlobInterceptor.java 30 Jul 2004 09:22:07 -0000 1.3
+++ org/dbforms/event/BlobInterceptor.java 4 Aug 2004 09:23:52 -0000
@@ -13,8 +13,8 @@
import org.dbforms.util.FileHolder;
/**
- * This Interceptor can be used to automatically store the filenames of BLOBS in
- * some table column specified, so that it is not lost.
+ * This Interceptor can be used to automatically store the filenames, content
+ * types and file sizes of BLOBS in some table column specified, so that it is not
lost.
*
* Currently, the interceptor must be configured manually in dbforms-config.xml
* inside the adequate table definition (see user guide)
@@ -22,11 +22,17 @@
* The interceptor MUST be initialized with the following parameters:
* <ul>
* <li><b>blob-column</b> - the name of the BLOB field
- * <li><b>blob-name</b> - a character field to store the FILENAME of the uploaded file
+ * <li><b>name-column</b> - a character field to store the FILENAME of the uploaded
file
+ * </ul>
+ *
+ *Optionaly may be specified:
+ * <ul>
+ * <li><b>mime-column</b> - a character field to store the content typpe of the
uploaded file
+ * <li><b>size-column</b> - an integer field to store the file size of the uploaded
file
* </ul>
*
* if the table contains multiple BLOBs, then a unique integer n has to be appended
- * to blob-column and blob-name to associate the correct pairs of BLOB and NAME
fields.
+ * to blob-column and blob-name to associate the correct pairs of BLOB, NAME, MIME
and SIZE fields.
*
* Usage Examples:
*
@@ -35,6 +41,8 @@
*<interceptor className="org.dbforms.event.BlobInterceptor"><br>
* <param name="blob-column" value="file"/><br>
* <param name="name-column" value="filename"/><br>
+ * <param name="mime-column" value="mime_type"/><br>
+ * <param name="size-column" value="file_size"/><br>
*</interceptor><br>
*</p>
*
@@ -43,11 +51,17 @@
*<interceptor className="org.dbforms.event.BlobInterceptor"><br>
* <param name="blob-column1" value="file"/><br>
* <param name="name-column1" value="filename"/><br>
+ * <param name="mime-column1" value="mime_type"/><br>
+ * <param name="size-column1" value="file_size"/><br>
* <param name="blob-column2" value="otherfile"/><br>
* <param name="name-column2" value="otherfilename"/><br>
+ * <param name="mime-column2" value="other_mime_type"/><br>
+ * <param name="size-column2" value="other_file_size"/><br>
* <param name="blob-column3" value="foofile"/><br>
* <param name="name-column3" value="foofilename"/><br>
-
+ * <param name="mime-column3" value="foo_mime_type"/><br>
+ * <param name="size-column3" value="foo_file_size"/><br>
+ *
*</interceptor><br>
*</p>
*
@@ -56,9 +70,11 @@
public class BlobInterceptor extends DbEventInterceptorSupport {
- private HashMap blobFieldData;
- private final int BLOB_COL = 0;
- private final int NAME_COL = 1;
+ private HashMap blobFieldData;
+ private final int BLOB_COL = 0;
+ private final int NAME_COL = 1;
+ private final int MIME_COL = 2;
+ private final int SIZE_COL = 3;
/**
* takes params, sorts/groups and stores for later use
@@ -74,7 +90,7 @@
Integer ii = getSuffixAsInteger(key, "blob-column");
String[] s = (String[]) blobFieldData.get(ii);
if(s == null) {
- s = new String[2];
+ s = new String[4];
blobFieldData.put(ii, s);
}
s[BLOB_COL] = value;
@@ -82,11 +98,27 @@
Integer ii = getSuffixAsInteger(key, "name-column");
String[] s = (String[]) blobFieldData.get(ii);
if(s == null) {
- s = new String[2];
+ s = new String[4];
blobFieldData.put(ii, s);
}
s[NAME_COL] = value;
- }
+ } else if(key.startsWith("mime-column")) {
+ Integer ii = getSuffixAsInteger(key, "mime-column");
+ String[] s = (String[]) blobFieldData.get(ii);
+ if(s == null) {
+ s = new String[4];
+ blobFieldData.put(ii, s);
+ }
+ s[MIME_COL] = value;
+ } else if(key.startsWith("size-column")) {
+ Integer ii = getSuffixAsInteger(key, "size-column");
+ String[] s = (String[]) blobFieldData.get(ii);
+ if(s == null) {
+ s = new String[4];
+ blobFieldData.put(ii, s);
+ }
+ s[SIZE_COL] = value;
+ }
}
}
@@ -102,7 +134,7 @@
/**
* goes through params and makes sure that the fileholder's file name
* info associated to the blob field BLOB_COLUMN is stored in the NAME_COLUMN
- * field.
+ * field. The same is with MIME_COLUMN and SIZE_COLUMN fields.
*/
private void assignBlobData(Table table, FieldValues fieldValues) {
for(Iterator iter = blobFieldData.values().iterator(); iter.hasNext();
) {
@@ -112,30 +144,41 @@
Object o = fv.getFieldValueAsObject();
if(o != null && o instanceof FileHolder) {
String fileName = ((FileHolder)
o).getFileName();
- setValue(table, fieldValues, s[NAME_COL],
fileName);
- }
+ String contentType = ((FileHolder)
o).getContentType();
+ int fileLength = ((FileHolder)
o).getFileLength();
+ if (fileName.indexOf('/') >= 0 ||
fileName.indexOf('\\') >= 0) {
+ int pos = (fileName.lastIndexOf('/') >=
fileName.lastIndexOf('\\')) ?
+ fileName.lastIndexOf('/') :
fileName.lastIndexOf('\\');
+ fileName = fileName.substring(pos + 1);
+ }
+ setValue(table, fieldValues, s[NAME_COL],
fileName);
+ if (s[MIME_COL] != null)
+ setValue(table, fieldValues, s[MIME_COL],
contentType);
+ if (s[SIZE_COL] != null)
+ setValue(table, fieldValues, s[SIZE_COL],
String.valueOf(fileLength));
+ }
}
}
}
/**
- * stores filenames of all blobs in the NAME_COLUMNs specified
+ * stores filenames, content types and file sizes of all blobs
+ * in the NAME_COLUMNs, MIME_COLUMNs and SIZE_COLUMNs specified
*/
public int preInsert(HttpServletRequest request, Table table, FieldValues
fieldValues,
DbFormsConfig config, Connection con) throws ValidationException {
assignBlobData(table, fieldValues);
- return GRANT_OPERATION;
+ return GRANT_OPERATION;
}
/**
- * stores filenames of all blobs in the NAME_COLUMNs specified
- */
+ * stores filenames, content types and file sizes of all blobs
+ * in the NAME_COLUMNs, MIME_COLUMNs and SIZE_COLUMNs specified
+ */
public int preUpdate(HttpServletRequest request, Table table, FieldValues
fieldValues,
- DbFormsConfig config, Connection con)
- throws ValidationException
- {
+ DbFormsConfig config, Connection con) throws ValidationException {
assignBlobData(table, fieldValues);
- return GRANT_OPERATION;
+ return GRANT_OPERATION;
}
}
Index: org/dbforms/event/datalist/dao/DataSourceJDBC.java
===================================================================
RCS file:
/cvsroot/jdbforms/dbforms/src/org/dbforms/event/datalist/dao/DataSourceJDBC.java,v
retrieving revision 1.50
diff -u -r1.50 DataSourceJDBC.java
--- org/dbforms/event/datalist/dao/DataSourceJDBC.java 30 Jul 2004 09:30:37 -0000
1.50
+++ org/dbforms/event/datalist/dao/DataSourceJDBC.java 4 Aug 2004 09:23:53 -0000
@@ -430,9 +430,12 @@
Object value = null;
if (fieldType == FieldTypes.BLOB) {
- // in case of a BLOB we supply the FileHolder
object to
- // SqlUtils for further operations
- value = fv.getFileHolder();
+ // in case of a BLOB we supply the FileHolder
object to SqlUtils for further operations
+ if (fv.getFileHolder() == null) { // if the
blob field is updated from within textarea
+ value = fv.getFieldValue();
+ } else { // if we have a file upload
+ value = fv.getFileHolder();
+ }
} else if (fieldType == FieldTypes.DISKBLOB) {
FileHolder fileHolder = fv.getFileHolder();
// encode fileName
Index: org/dbforms/servlets/FileServlet.java
===================================================================
RCS file: /cvsroot/jdbforms/dbforms/src/org/dbforms/servlets/FileServlet.java,v
retrieving revision 1.14
diff -u -r1.14 FileServlet.java
--- org/dbforms/servlets/FileServlet.java 2 Aug 2004 20:28:23 -0000 1.14
+++ org/dbforms/servlets/FileServlet.java 4 Aug 2004 09:23:53 -0000
@@ -29,12 +29,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
-import java.io.FileInputStream;
-import java.io.ObjectInputStream;
-import java.io.FileNotFoundException;
import java.io.IOException;
-import java.io.File;
-import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@@ -44,7 +39,6 @@
import org.dbforms.config.Field;
import org.dbforms.config.FieldTypes;
import org.dbforms.config.Table;
-import org.dbforms.util.FileHolder;
import org.dbforms.util.ParseUtil;
import org.dbforms.util.SqlUtil;
import org.dbforms.util.Util;
@@ -130,6 +124,9 @@
// JPeer 03/2004 - optional parameter
String nameField = request.getParameter("nf");
+
+ InputStream is = null;
+ String fileName = null;
queryBuf.append("SELECT ");
queryBuf.append(field.getName());
@@ -153,23 +150,23 @@
if (rs.next()) {
// use the filesystem;
if (field.getType() == FieldTypes.DISKBLOB) {
- readDiskBlob(rs.getString(1),
field.getDirectory(),
- request, response);
+ fileName = rs.getString(1);
+ is = SqlUtil.readDiskBlob(fileName,
field.getDirectory(), request.getParameter("defaultValue"));
}
// use the rdbms;
else if (field.getType() == FieldTypes.BLOB) {
// if no fileholder is used (new BLOB model)
- String fileName = null;
if (nameField != null) {
fileName = rs.getString(2);
}
- readDbFieldBlob(rs, fileName, request,
response);
+ is = SqlUtil.readDbFieldBlob(rs, fileName);
}
} else {
logCat.info("::doGet - we have got no result using
query "
+ queryBuf);
}
+ if (is != null) writeToClient(request, response, fileName,
is);
SqlUtil.closeConnection(con);
} catch (SQLException sqle) {
logCat.error("::doGet - SQL exception", sqle);
@@ -178,143 +175,6 @@
}
/**
- * Read the database field and write to the client its content
- *
- * @param rs
- * Description of the Parameter
- * @param request
- * Description of the Parameter
- * @param response
- * Description of the Parameter
- * @param fileName
- * is the filename or NULL in the classic (Fileholder-based) BLOB
- * handling
- * @exception IOException
- * Description of the Exception
- * @exception SQLException
- * Description of the Exception
- */
- private void readDbFieldBlob(ResultSet rs, String fileName,
- HttpServletRequest request, HttpServletResponse response)
- throws IOException, SQLException {
- logCat.info("READING BLOB");
-
- try {
- Object o = rs.getObject(1);
- if (o == null) {
- logCat.warn("::readDbFieldBlob - blob null, no
response sent");
- return;
- }
- System.out.println("o instanceof ..." +
o.getClass().getName());
- // if the object the JDBC driver returns to us implements
- // the java.sql.Blob interface, then we use the BLOB object
- // which wraps the binary stream of our FileHolder:
- if (o instanceof java.sql.Blob) {
- Blob blob = rs.getBlob(1);
-
- // classic mode
- if (fileName == null) {
- ObjectInputStream ois = new
ObjectInputStream(blob
- .getBinaryStream());
-
- FileHolder fh = (FileHolder) ois.readObject();
- writeToClient(request, response,
fh.getFileName(), fh
- .getInputStreamFromBuffer());
- }
- // new mode
- else {
- writeToClient(request, response, fileName, blob
- .getBinaryStream());
- }
- }
-
- /*
- * else if(o instanceof java.sql.Clob) { Clob clob =
rs.getClob(1);
- * ObjectInputStream ois = new
- * ObjectInputStream(clob.getAsciiStream()); FileHolder fh =
- * (FileHolder) ois.readObject(); writeToClient(response,
- * fh.getFileName(), fh.getInputStreamFromBuffer()); }
- */
-
- // otherwise we are aquiring the stream directly:
- else {
- if (fileName == null) {
- // old ("classic") mode
- InputStream blobIS = rs.getBinaryStream(1);
- ObjectInputStream ois = new
ObjectInputStream(blobIS);
- FileHolder fh = (FileHolder) ois.readObject();
- writeToClient(request, response,
fh.getFileName(), fh
- .getInputStreamFromBuffer());
- } else {
- // new mode
- InputStream blobIS = rs.getBinaryStream(1);
- writeToClient(request, response, fileName,
blobIS);
- }
- }
- } catch (ClassNotFoundException cnfe) {
- logCat.error("::readDbFieldBlob - class not found", cnfe);
- throw new IOException("error:" + cnfe.toString());
- }
- }
-
- /**
- * Read the blob field from the filesystem and write to the client its
- * content.
- *
- * @param fileName
- * Description of the Parameter
- * @param directory
- * Description of the Parameter
- * @param request
- * Description of the Parameter
- * @param response
- * Description of the Parameter
- * @exception FileNotFoundException
- * Description of the Exception
- * @exception IOException
- * Description of the Exception
- */
- private void readDiskBlob(String fileName, String directory,
- HttpServletRequest request, HttpServletResponse response)
- throws FileNotFoundException, IOException {
- logCat.info(new StringBuffer("READING DISKBLOB\n directory = [")
- .append(directory).append("]\n").append(" fileName =
[")
- .append(fileName).append("]\n").append(" defaultValue
= [")
-
.append(request.getParameter("defaultValue")).append("]\n")
- .toString());
-
- if ((fileName == null) || (fileName.trim().length() == 0)) {
- if ((fileName = request.getParameter("defaultValue")) != null)
{
- logCat
- .info("::readDiskBlob - database data
is null; use the default value ["
- + fileName + "]");
- }
- }
-
- // directory or fileName can be null!
- //if ((directory != null) && (fileName != null))
- if (fileName != null) {
- fileName = fileName.trim();
-
- File file = new File(directory, fileName);
-
- if (file.exists()) {
- logCat.info("::readDiskBlob - file found ["
- + file.getAbsoluteFile() + "]");
-
- FileInputStream fis = new FileInputStream(file);
- writeToClient(request, response, fileName, fis);
- } else {
- logCat.error("::readDiskBlob - file ["
- + (directory + "/" + fileName) + "]
not found");
- }
- } else {
- logCat
- .warn("::readDiskBlob - file name or directory
value is null");
- }
- }
-
- /**
* Write the content of the input file to the client.
*
* @param response
@@ -335,6 +195,7 @@
+ contentType);
if (!Util.isNull(contentType))
response.setContentType(contentType);
+ response.setHeader("Cache-control", "private"); // w/o this MSIE
fails to "Open" the file
response.setHeader("Content-Disposition", "attachment; fileName=\""
+ fileName + "\"");
Index: org/dbforms/taglib/DbBlobContentTag.java
===================================================================
RCS file: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/DbBlobContentTag.java,v
retrieving revision 1.15
diff -u -r1.15 DbBlobContentTag.java
--- org/dbforms/taglib/DbBlobContentTag.java 2 Aug 2004 20:28:23 -0000 1.15
+++ org/dbforms/taglib/DbBlobContentTag.java 4 Aug 2004 09:23:53 -0000
@@ -32,6 +32,7 @@
import java.sql.Blob;
import org.dbforms.config.FieldTypes;
+import org.dbforms.config.Table;
import org.dbforms.util.SqlUtil;
import org.apache.log4j.Category;
@@ -63,7 +64,7 @@
if (getParentForm().getFooterReached()) {
return EVAL_PAGE; // nothing to do when no data
available..
}
-
+
StringBuffer queryBuf = new StringBuffer();
queryBuf.append("SELECT ");
queryBuf.append(getField().getName());
@@ -86,72 +87,29 @@
ResultSet rs = ps.executeQuery();
if (rs.next()) {
+ InputStream is = null;
+ String fileName = null;
if (getField().getType() ==
FieldTypes.DISKBLOB) {
- String fileName = rs.getString(1);
-
- if (fileName != null) {
- fileName = fileName.trim();
- }
-
- logCat.info("READING DISKBLOB
field.getDirectory()="
- +
getField().getDirectory() + " " + "fileName="
- + fileName);
-
- if ((fileName == null)
- ||
(getField().getDirectory() == null)
- || (fileName.length()
== 0)
- ||
(getField().getDirectory().length() == 0)) {
- return EVAL_PAGE;
- }
-
- File file = new
File(getField().getDirectory(),
- fileName);
-
- if (file.exists()) {
- logCat.info("fs- file found "
+ file.getName());
-
- FileInputStream fis = new
FileInputStream(file);
- try {
- BufferedReader br =
new BufferedReader(
- new
InputStreamReader(fis));
- char[] c = new
char[1024];
- int read;
- while ((read =
br.read(c)) != -1) {
-
contentBuf.append(c, 0, read);
- }
- } finally {
- fis.close();
- }
- } else {
- logCat.info("fs- file not
found");
- }
- } else {
- // throw new
IllegalArgumentException("DbBlobContentTag
- // is currently only for
- // DISKBLOBS - feel free to copy code
from
- // FileServlet.java to this place to
- // bring this limitation to an end
:=)");
- try {
- /*
- * TODO: we obviously need a
classic mode blob
- * handling here as well
- */
- Blob blob = rs.getBlob(1);
- InputStream is =
blob.getBinaryStream();
- try {
- BufferedReader br =
new BufferedReader(
- new
InputStreamReader(is));
- char[] c = new
char[1024];
- int read;
- while ((read =
br.read(c)) != -1)
-
contentBuf.append(c, 0, read);
- } finally {
- is.close();
- }
- } catch (NullPointerException e) {
- // the blob field was empty
- }
- }
+ fileName = rs.getString(1);
+ is = SqlUtil.readDiskBlob(fileName,
getField().getDirectory(), null);
+ } else {
+ /* As the classic and new blob
handling modes are distinguished by fileName,
+ * we use a small hack here to
provide empty string or null as the fileName
+ * according to the blob handling
strategy defined in the configuration file.
+ */
+ fileName =
(getField().getTable().getBlobHandlingStrategy() == Table.BLOB_CLASSIC) ? null : "";
+ is = SqlUtil.readDbFieldBlob(rs,
fileName);
+ }
+ if (is != null) {
+ BufferedReader br = new
BufferedReader(
+ new InputStreamReader(is));
+ char[] c = new char[1024];
+ int read;
+ while ((read = br.read(c)) != -1) {
+ contentBuf.append(c, 0, read);
+ }
+ is.close();
+ }
} else {
logCat.info("fs- we have got no result" +
queryBuf);
}
@@ -171,7 +129,7 @@
public void doFinally() {
dbConnectionName = null;
super.doFinally();
- }
+ }
/**
* @see javax.servlet.jsp.tagext.TryCatchFinally#doCatch(java.lang.Throwable)
Index: org/dbforms/util/SqlUtil.java
===================================================================
RCS file: /cvsroot/jdbforms/dbforms/src/org/dbforms/util/SqlUtil.java,v
retrieving revision 1.28
diff -u -r1.28 SqlUtil.java
--- org/dbforms/util/SqlUtil.java 27 Oct 2003 22:08:43 -0000 1.28
+++ org/dbforms/util/SqlUtil.java 4 Aug 2004 09:23:53 -0000
@@ -25,6 +25,16 @@
import java.sql.Connection;
import java.sql.SQLException;
+import java.sql.Blob;
+import java.sql.ResultSet;
+
+import java.io.InputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.ObjectInputStream;
+import java.io.IOException;
+import java.io.FileNotFoundException;
+
import org.apache.log4j.Category;
/**
@@ -87,4 +97,121 @@
public static final void logSqlException(SQLException e) {
SqlUtil.logSqlException(e, null);
}
-}
\ No newline at end of file
+
+ /**
+ * Read the database field and write to the client its content
+ *
+ * @param rs
+ * Description of the Parameter
+ * @param fileName
+ * is the filename or NULL in the classic (Fileholder-based) BLOB
+ * handling
+ * @exception IOException
+ * Description of the Exception
+ * @exception SQLException
+ * Description of the Exception
+ */
+ public static InputStream readDbFieldBlob(ResultSet rs, String fileName)
+ throws IOException, SQLException {
+ logCat.info("READING BLOB");
+ try {
+ Object o = rs.getObject(1);
+ if (o == null) {
+ logCat.warn("::readDbFieldBlob - blob null, no response sent");
+ return null;
+ }
+ System.out.println("o instanceof ..." + o.getClass().getName());
+ // if the object the JDBC driver returns to us implements
+ // the java.sql.Blob interface, then we use the BLOB object
+ // which wraps the binary stream of our FileHolder:
+ if (o instanceof java.sql.Blob) {
+ Blob blob = rs.getBlob(1);
+
+ // classic mode
+ if (fileName == null) {
+ ObjectInputStream ois = new ObjectInputStream(blob
+ .getBinaryStream());
+
+ FileHolder fh = (FileHolder) ois.readObject();
+ fileName = fh.getFileName();
+ return fh.getInputStreamFromBuffer();
+ }
+ // new mode
+ else {
+ return blob.getBinaryStream();
+ }
+ }
+ // otherwise we are aquiring the stream directly:
+ else {
+ if (fileName == null) {
+ // old ("classic") mode
+ InputStream blobIS = rs.getBinaryStream(1);
+ ObjectInputStream ois = new ObjectInputStream(blobIS);
+ FileHolder fh = (FileHolder) ois.readObject();
+ fileName = fh.getFileName();
+ return fh.getInputStreamFromBuffer();
+ } else {
+ // new mode
+ return rs.getBinaryStream(1);
+ }
+ }
+ } catch (ClassNotFoundException cnfe) {
+ logCat.error("::readDbFieldBlob - class not found", cnfe);
+ throw new IOException("error:" + cnfe.toString());
+ }
+ }
+
+ /**
+ * Read the blob field from the filesystem and write to the client its
+ * content.
+ *
+ * @param fileName
+ * Description of the Parameter
+ * @param directory
+ * Description of the Parameter
+ * @param defVal
+ * Default value of the file tag
+ * @exception FileNotFoundException
+ * Description of the Exception
+ * @exception IOException
+ * Description of the Exception
+ */
+ public static FileInputStream readDiskBlob(String fileName, String directory,
String defVal)
+ throws FileNotFoundException, IOException {
+ logCat.info(new StringBuffer("READING DISKBLOB\n directory = [")
+ .append(directory).append("]\n").append(" fileName = [")
+ .append(fileName).append("]\n").append(" defaultValue = [")
+ .append(defVal).append("]\n")
+ .toString());
+
+ if ((fileName == null) || (fileName.trim().length() == 0)) {
+ if ((fileName = defVal) != null) {
+ logCat
+ .info("::readDiskBlob - database data is null; use the default value ["
+ + fileName + "]");
+ }
+ }
+
+ // directory or fileName can be null!
+ //if ((directory != null) && (fileName != null))
+ if (fileName != null) {
+ fileName = fileName.trim();
+
+ File file = new File(directory, fileName);
+
+ if (file.exists()) {
+ logCat.info("::readDiskBlob - file found ["
+ + file.getAbsoluteFile() + "]");
+
+ return new FileInputStream(file);
+ } else {
+ logCat.error("::readDiskBlob - file ["
+ + (directory + "/" + fileName) + "] not found");
+ return null;
+ }
+ } else {
+ logCat.warn("::readDiskBlob - file name or directory value is null");
+ return null;
+ }
+ }
+}
