|
Loading Images From Non-file resources
As we have seen that loading images from external file resources is fairly easy
with XSL-FO. However, recently, I faced a situation where I had to load and
display images with FOP when images are stored in a database using Binary Large
Objects (BLOB). In this situation, I am not allowed to store the image in any
file system. This is a tricky situation and often daunting with limiting
prospects. However, there is a clever way to still be able to use these sorts of
images. I figured out the following solution architecture for loading and
displaying BLOB images using XSL-FO and FOP produce a FOP document.
Figure-1 The BLOB image loading architecture
In this architecture, I used a Data Access Object (DAO) to get the images as a
BLOB from the database. A Servlet uses this DAO to get the image data in binary
form from the database. The XSL-FO in turn provides the URL of this Servlet as
the image source. By this mechanism, the Servlet will serve the image as a
binary data stream and XSL –FO will interpret the binary stream as image data
and render it as an image.
However straightforward this solution seems, there are few salient points to
take note of.
- How do we tell the Servlet, which image to bring back? This
means we need a mechanism to pass on to the Servlet some sort of
unique identifier for the image. If the image is stored in a
database as a BLOB, we can assign a unique id as primary key and
pass this primary key value to the Servlet and in turn the DAO
to bring back the correct image.
- In order the Servlet to serve the request from XSL-FO, the
Servlet must support GET protocol to obtain the data.
- The Servlet when sending the response back must set the
content type as part of the response. This helps the XSL-FO to
understand the type of image it is dealing with.
Below is an example code for a Servlet to send the image binary stream back to
the caller.
public class MediaServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse
res) throws ServletException, IOException {
OutputStream stream = null;
Attachment attachment = null;
String attUrn =
req.getParameter("attachmentURN");
AttachmentService attachmnetService =
AttachmentServiceFactory.getInstance();
try {
stream = res.getOutputStream();
attachment = attachmentService.getAttachment(attUrn);
if(attchment !=null){
res.setContentType("image/gif");
res.setContentLength(attachment.getLength());
stream.write(attachment.getAttachmentByte());
}
}catch(Exception e){
}finally{
stream.flush();
stream.close();
}
}
}
Listing 1: The MediaServlet program
In this example, we obtain the image as a binary stream using the
AttachmentService Data Access Component. We pass in the attachment URN which is
the primary key for the image in the database, and the service returns an
Attachment object that contains the binary data for the image. We then write the
binary stream back to the caller outputStream.
If we make a call to this service form a XSL-FO stylesheet the MediaServlet will
stream the image binary stream back to the XSL-FO stylesheet. XSL-FO will in
turn interpret the binary stream by looking at its content-type and
content-length and display the image.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|