Wednesday, April 28, 2010

ADFway of rendering a BLOB from a Servlet

Say, you have a BLOB in one of your DB table column.
You want to stream the BLOB onto your ADF page.How to do it?

The ViewOBject contains 4 columns.
PrimaryKey,FileName,MimeType and Blob

Here is the servlet code involving ADFbc.

public ApplicationModule getHPApplicationModuleFromPool(){
try{
//Get the ApplicationModule Client Interface and get the MyVO
String amDef = "mypackage.applicationModule.MyAM";
String amConfig = "MyAMLocal";
ApplicationModule am =
Configuration.createRootApplicationModule(amDef, amConfig);

return am;

}catch(Exception e){
e.printStackTrace();
}

return null;

}


public void blobProcessing(String primaryKey) {
ApplicationModule am = null;
try{
{
//Get the Row for the Passed TopicId
Key key = new Key(new Object[]{primaryKey});
am = getHPApplicationModuleFromPool();
ViewObject vo = am.findViewObject("MyVo");

Row row = vo.getRow(key);
if(topicRow != null){
String fileName = (String)row.getAttribute("FileName");
mimeType = (String)row.getAttribute("MimeType");
//Sets the MimeType
response.setContentType(mimeType != null ? mimeType : "application/octet-stream");
//Sets the content disposition style
response.addHeader("Content-Disposition", "inline; filename="+ fileName);
//Read the row.getBlob() and redirect to an outPutStream
BlobDomain blobContent = (BlobDomain)row.getAttribute("Blob");
//gets the ByteArray from BlobDomain
byte[] bytearray = topicContent.toByteArray();
//gets the Binary InputStream from BlobDomain
InputStream reader = blobContent.getBinaryStream();;
int size = 0;
//Read the InputStream and write to Servlet's output Stream.
while ((size = reader.read(bytearray)) != -1)
response.getOutputStream().write(bytearray,0,size);
//Close the reader stream
reader.close();
}
Configuration.releaseRootApplicationModule(am, false);
}//End Blob processing

}catch(Exception e){
try{
if(am != null)
Configuration.releaseRootApplicationModule(am, false);
e.printStackTrace();
}catch(Exception e1){
e1.printStackTrace();
}
}
}


No comments: