JSF Portlet File Upload

[Links Updated]

Recently a project student had a requirement to use file upload functionality in JSF Portlet. Since Mojarra's JSF Implementation does not provide fileupload component, he wanted to use the fileupload component provided by MyFaces Tomahawk component library. He used the example from this blog and portletized it. But it didn't work with either OpenPortal JSF Portlet Bridge or  Apache MyFaces JSR329 Portlet Bridge. I found another blog post that talks about extending OpenPortal JSF Portlet Bridge inorder to provide support for fileupload. It worked, but i wanted a generic solution that works with both OpenPortal JSF Portlet Bridge and Apache MyFaces JSR329 Portlet Bridge or any other JSF Portlet Bridge. So i used the Portlet Filter approach based on that blog. Ofcourse this solution will work only on portals that support JSR286.

Following are the steps to add fileupload functionality in your jsf portlet

1. Add the following JARs to the WEB-INF/lib of the jsf portlet webapp. The version numbers doesn't matter as long as you get the newest.

2. Add the following portlet filter entry in the portlet.xml

<portlet-app ..... version='2.0'>
    <portlet>
        <portlet-name>name_of_the_portlet</portlet-name>
                ........
    </portlet>
    <filter>
        <filter-name>PortletFileUploadFilter</filter-name>
        <filter-class>com.sun.portlet.PortletFileUploadFilter</filter-class>
        <lifecycle>ACTION_PHASE</lifecycle>
    </filter>
    <filter-mapping>
        <filter-name>PortletFileUploadFilter</filter-name>
        <portlet-name>name_of_the_portlet</portlet-name>
    </filter-mapping>
</portlet-app>


3. Define the fileupload component in the JSF page (you will use the name to get FileItem from the request)

<input type="file" name="name_of_the_component" />

4. After you submit the JSF page, you can obtain the org.apache.commons.fileupload.FileItem for the input file component from the PortletRequest as follows. Once you have access to FileItem you can get the name, the I/O stream of the uploaded file.

FacesContext facesContext = FacesContext.getCurrentInstance();
PortletRequest portletRequest = (PortletRequest) facesContext.getExternalContext().getRequest();
FileItem fileItem = (FileItem)portletRequest.getAttribute("name_of_the_component");

See the example jsf fileupload portlet that uses this functionality.

Comments:

Deepak,

What's the news on Portalpack and Netbeans 6.9? At the moment it doesn't work. Also, do you know what's happening with PortalPack development generally? Do you know if Oracle is interested in maintaining any presence in this area?

Posted by Peter West on July 12, 2010 at 01:51 AM IST #

We are aware of the issue and working on it. The portal pack will continue to be developed in open source.

Posted by Deepak on July 14, 2010 at 05:25 AM IST #

We are adding file upload in JSF 2.1

Posted by Ed Burns on July 26, 2010 at 03:35 AM IST #

Great feature! However, the portlet does not seem to work in Liferay 5.2.3 (Tomcat 6 Bundle). The following error is dumped into the server log:
SEVERE: Error listenerStart
18.8.2010 18:45:40 org.apache.catalina.core.StandardContext start
SEVERE: Context [/JSFFileUploadPortlet] startup failed due to previous errors

Posted by haba713 on August 18, 2010 at 05:19 PM IST #

Hey, it works also in Liferay 5.2.3. I couldn't deploy the sample portlet but I wrote a new JSF portlet with the file upload feature and it works smoooooothly. Thanks a lot!

Posted by haba713 on August 18, 2010 at 06:41 PM IST #

You got startup errors in tomcat because the war did not contain JSF jars. I had tested in glassfish which supports JSF. Now i have updated the war with JSF jars so that i will work in Tomcat as well. Thanks.

Posted by Deepak on August 19, 2010 at 09:36 AM IST #

Deepak,

Can you point me to any resources that show how to convert a 6.8 plugin to 6.9? I haven't attempted any plugin work before, so it's a bit of a mystery to me.

Peter

Posted by Peter West on August 31, 2010 at 01:06 AM IST #

Peter,
I am not aware of netbeans plugin development. The Netbeans Portal Pack 3.0.4 that works on Netbeans 6.9 is ready and will be released this week. Sorry for the delay. I hope you will let us know if you find any issues.

Posted by Deepak on August 31, 2010 at 03:12 AM IST #

Hello, So i am working on weblogic portal 10.3.2 and JSF portlet version 1.2.

When i try to get file upload form upload form, it submit a null fileItem. Can you tell me how to solve this problem ?

Thanks in advance!

Posted by Coy on September 08, 2010 at 01:45 AM IST #

Does weblogic portal 10.3.2 support JSR 286? In order for this to work, the portal should support JSR286 as "filter" feature is being used.

Posted by Deepak on September 08, 2010 at 10:51 AM IST #

Yes, it's support JSR 286. But i don't know how it's doesn't work :( Have you tried on weblogic portal 10.3.2 ?

Posted by Coy on September 09, 2010 at 12:21 AM IST #

No.I have not tried in WebLogic Portal, I have tried in Web Space server, Liferay and OpenPortal Portlet Container. Are you facing the issue when you tried the sample application? Do you see any exceptions in the logs?

Posted by Deepak on September 09, 2010 at 02:14 AM IST #

Thank you very much, the problem is solved :D

Posted by Coy on September 14, 2010 at 01:52 AM IST #

Coy: What was the solution? What I am seeing is that the com.sun.portlet.FileUploadActionRequestWrapper is called and logs the following:

INFO: fieldName: uploadFile

But my action method is never called. Any suggestions greatly appreciated.

Posted by Scott Nelson on October 15, 2010 at 08:24 PM IST #

Coy, how did you solve it?

Posted by Scott Nelson on October 19, 2010 at 10:30 AM IST #

@Scott Nelson: sorry for my late answer. I am too busy. I was overrided some class and replace default oracle jsf ri with sun jsf ri. For more detail, you can send mail to me for detail: mrchu87@gmail.com. I don't want to post the link on here.

Posted by Coy on December 10, 2010 at 07:02 AM IST #

Please post the solution here it wud be better for you to write multiple mails ankur

Posted by ankur on December 10, 2010 at 08:32 AM IST #

Here is the answer:
http://insights2jsf.wordpress.com/2010/04/06/using-file-upload-in-jsf-portlets-with-weblogic-portal/

You can follow it to do. But i was stucking with intellicent method. Not solved yet T T

Posted by Coy on December 13, 2010 at 05:42 AM IST #

Hi Deepakg,
thanks for your posting. It helped a lot. But two questions remain?
1. Why don't you add the source? (I had to recompile it)
2. For what do we need the servlet?

Yours
Arne

Posted by Arne v.Irmer on January 05, 2011 at 09:49 AM IST #

Thanks for pointing it out.
1. You can get the sources from https://jsfportletbridge.dev.java.net/files/documents/5355/154306/portlet-fileupload-filter-src.zip
2. We don't need the servlet.

Posted by Deepak on January 05, 2011 at 03:42 PM IST #

I have attempted the above as shown to the letter, but the portletRequest.getAttribute("myFileInputName") returns null, i.e., when I later call fileItem.getName() or similar, that line is a NullPointerException. What did I miss?

Posted by Wade Love on February 24, 2011 at 01:41 PM IST #

Is your portlet is running in a portal supporting JSR 286.Did you try with the sample application? Does it also return null? If possible can you send your application to dev@jsfportletbridge.java.net

Posted by Deepak on February 25, 2011 at 03:26 AM IST #

My issue, as it turns out, is the same as "Coy" earlier. Sorry for the duplicate, I didn't read the prior posts as thoroughly as I should have. I'll try the solution posted.

Posted by Wade Love on February 28, 2011 at 11:40 AM IST #

Hi Deepak
nice post.
Can you explain how can we achieve the same thing ajaxically. I think that would use Resource Phase. Would appreciate your help.

Posted by Naisha on August 07, 2013 at 10:15 PM IST #

Thanks.
For that you can retrieve FileItem from serveResource method.

Posted by Deepak on August 08, 2013 at 11:29 AM IST #

Thanks for the reply Deepak.
But the Apache Commons API and the PortletFileUploadFilter is designed for action phase , how will we invoke the resource phase in this scenario.

Posted by Naisha on August 08, 2013 at 12:41 PM IST #

In the sample given above it is designed for the action phase. But you can use the example and extend it to Resource phase, by specifying the lifecycle as RESOURCE_PHASE in the filter.

Posted by Deepak on August 08, 2013 at 01:16 PM IST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Deepak Gothe

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today