Using the "exclusive" Window State in uPortal
I have just finished creating a sample application to allow a portlet to transmit binary data to end users. For this particular sample I wanted to take advantage of the uPortal custom Window State called "exclusive". I am happy to report I have this application working.
I started this project with a vanilla uPortal 2.5.3 GA download. I did run into one problem that required obtaining a new version of the pluto jar file. The version I ended up using came from the head of the 2.5 branch, so will be included in a future uPortal 2.5.4 release. (The same jar will also appear in a future uPortal 2.6.0 release as well)
There are 3 things you need to accomplish file downloading in a Portlet with uPortal's exclusive window state.
- Create a
PortletURLwith theexclusiveWindow State - Specify any additional content types your Portlet will use in the portlet.xml file
- In the Portlet code, detect the
exclusiveWindow State, set the appropriate content type and write the binary data to the Portlet OutputStream
To create a PortletURL in your Portlet code for the exclusive Window State follow you can code like this.
PortletURL url = response.createRenderURL(); url.setWindowState(new WindowState("exclusive"));
When you edit your portlet.xml, you must include one additional supports section per content type. The following portlet.xml supports GIF images in addition to the HTML a portlet must normally support for uPortal.
This portlet will show an image when the user clicks on the link ShowImagePortlet Show Image Portlet sample.showimage.ShowImagePortlet 60 text/html VIEW image/gif VIEW en Show Image Portlet Show Image portlet, sample, image
As long as you're editing the portlet.xml file, you might want to add the following section as well. uPortal does not currently require this section, however the JSR 168 portlet specification says it should be included whenever a custom Window State is used.
The portlet controls the entire response exclusive
Finally you're ready to actually respond to the exclusive Window State. Below I show the entire doView method for my sample application. It incorporates very simple HTML output to display a PortletURL to our sample binary resource. If you use the code below don't forget to change the filename and/or copy a valid GIF image to the filename location you specify.
public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException { final WindowState EXCLUSIVE = new WindowState("exclusive"); WindowState state = request.getWindowState(); if (!state.equals(EXCLUSIVE)) { PortletURL url = response.createRenderURL(); url.setWindowState(EXCLUSIVE); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("Hello World!"); out.print("
Show me an Image!"); out.flush(); } else { String filename = "/tmp/mainlogo.gif"; PortletContext context = getPortletContext(); response.setContentType(context.getMimeType(filename)); OutputStream os = response.getPortletOutputStream(); byte b[]=new byte[1024]; InputStream is = new FileInputStream(filename); int numRead=0; while ((numRead=is.read(b)) > 0) { os.write(b, 0, numRead); } os.flush(); } }
I have attached the complete sample application to this blog post. Feel free to download and try it out. Just remember to replace your pluto jar file. Also, I believe with this research we can close out to uPortal JIRA issues UP-1427 and UP-1407.
In a future article I will explain other options for doing file downloading from portlets.
---- Cris J H
| Attachment | Size |
|---|---|
| ShowImagePortletExample.zip | 113.13 KB |
- holdorph's blog
- Login or register to post comments
