Wednesday, February 11, 2015

JSF Tip: Utilizing Expression Language to Create Cross Platform File Links

Does your organization contain a multi-platform environment?  If so, then you may have the requirement to add links to file shares within your Java EE application, and those links may require use of a different protocol depending upon platform of the user's machine.  For instance, in an organization that contains both Windows and OS X, a network file share may require use of NFS (file://) prefix from a Windows machine, and use of SMB (smb://) prefix from an OS X machine. How can you easily distinguish which link to provide to your users within a web application?

There are many solutions to this problem.  Some solutions involve use of a servlet to determine the user's operating system, or some other solution that requires use of server-side code.  In this tip, I'll show you how to determine the user's operating system via the request object from Expression Language.

To determine whether a user requires the use of NFS or SMB for accessing a file share, simply use expression language to obtain the user-agent from the request object as such:

#{header['user-agent'].contains('Windows')?'file://':'smb://'}rest/of/url/here

In the EL above, if the user-agent contains "Windows", then the
file://
prefix is used, otherwise
smb://
will be utilized for the link. This solution can be implemented directly within the views, relieving the requirement to recompile code.

No comments:

Post a Comment

Please leave a comment...