Author: jleroux
Date: Wed Dec 14 09:18:04 2011
New Revision: 1214124

URL: http://svn.apache.org/viewvc?rev=1214124&view=rev
Log:
A patch from Wai https://issues.apache.org/jira/browse/OFBIZ-4620  "fail 
uploading audio/video files to database"

When uploading binary files (eg. image/audio/video) to the database, ofbiz 
throws a type casting exception because it is trying to type cast 
HeapByteBuffer to byte[].

I think the problem stems from configuring ofbiz to use java.nio.ByteBuffer and 
its subclass java.nio.HeapByteBuffer to contain uploaded binary data but no 
accommodation is made in the code to work with this type of object.

To test this, you need to set 
content.properties/content.upload.always.local.file=false
an use partymgr/control/viewprofile?partyId=admin and upload an image file.

java.nio.HeapByteBuffer comes from java's rt.jar (make sure your classpath 
includes this)

Modified:
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/JdbcValueHandler.java

Modified: 
ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/JdbcValueHandler.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/JdbcValueHandler.java?rev=1214124&r1=1214123&r2=1214124&view=diff
==============================================================================
--- 
ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/JdbcValueHandler.java 
(original)
+++ 
ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/JdbcValueHandler.java 
Wed Dec 14 09:18:04 2011
@@ -25,6 +25,7 @@ import java.io.InputStream;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.io.Reader;
+import java.nio.ByteBuffer;
 import java.sql.Blob;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
@@ -324,16 +325,20 @@ public abstract class JdbcValueHandler<T
         }
         @Override
         protected void castAndSetValue(PreparedStatement ps, int 
parameterIndex, Object obj) throws SQLException {
-            try {
-                // FIXME: This is here for backwards compatibility. Client code
-                // that uses a Blob java-type for a byte array should use a
-                // byte[] java-type instead.
-                byte[] bytes = (byte[]) obj;
-                Debug.logWarning("Blob java-type used for byte array. Use 
byte[] java-type instead.", module);
-                ps.setBytes(parameterIndex, bytes);
-                return;
-            } catch (ClassCastException e) {}
-            ps.setBlob(parameterIndex, (Blob) obj);
+            // FIXME: This is here for backwards compatibility. Client code
+            // that uses a Blob java-type for a byte array should use a
+            // byte[] java-type instead.
+            if (obj instanceof Blob) {
+                ps.setBlob(parameterIndex, (Blob)obj);
+            } else if (obj instanceof byte[]) {
+                ps.setBytes(parameterIndex, (byte[]) obj);
+            } else if (obj instanceof ByteBuffer) {
+                ps.setBytes(parameterIndex, ((ByteBuffer)obj).array());
+            } else {
+                Debug.logError("JdbcValueHandler.castAndSetValue(): Unexpected 
type found. type=" + obj.getClass().getName(), module);
+                throw new IllegalArgumentException(obj.getClass().getName());
+            }
+            return;
         }
         @Override
         public Object getValue(ResultSet rs, int columnIndex) throws 
SQLException {


Reply via email to