Tag: el

Creating a EL function to return browser name

Posted by – January 7, 2009

There are some situations where we need know the browser name to perform some changes on page code, the java code below defines a EL function to help on retrieve the browser name.

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
package br.eti.faces.el;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

public final class Browser {

    public static String name() {
        FacesContext context=FacesContext.getCurrentInstance();
        HttpServletRequest request=
           (HttpServletRequest)context.getExternalContext().
               getRequest();
        String useragent= request.getHeader("user-agent");
        useragent=useragent.toLowerCase();
        if (useragent.indexOf("opera")!=-1)
            return "opera";
        if (useragent.indexOf("netscape")!=-1)
            return "netscape";
        if (useragent.indexOf("msie")!=-1)
            return "ie";
        if (useragent.indexOf("firefox")!=-1)
            return "firefox";
    return "";
    }
}

More

Share

bodyguard – Facelets EL functions for Spring Security

Posted by – November 9, 2008

Yesterday I wrote a small set of EL functions to use with spring security, now I can get easy access to spring security roles and authentication info as well.

Here’s an example of how bodyguard can be used in your facelets code:

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
<html xmlns="http://www.w3.org/1999/xhtml"
   xmlns:bg="http://faces.eti.br/bodyguard">
 <body>
   <!-- Display logged message if the user is authenticated -->
   <t:div rendered="#{bg:isAuthenticated()}">
       <h:outputText rendered="#{bg:remoteUser()}" value="Logged!"/>
   </t:div>

   <!-- Display a message indicating if the user is a user or admin -->
   <t:div rendered="#{bg:isUserInAnyRoles('ROLE_USER,ROLE_ADMIN')}">
       <h:outputText
         value="The user #{bg:remoteUser()} is a user or admin."/>
   </t:div>

   <!-- Display a message indicating if the user isn't a user or admin -->
   <t:div rendered="#{bg:isUserNotInRoles('ROLE_USER,ROLE_ADMIN')}">
       <h:outputText
         value="The user #{bg:remoteUser()} isn't a user or admin."/>
   </t:div>

   <!-- Display a message indicating if the user is a user and admin -->
   <t:div rendered="#{bg:isUserInAllRoles('ROLE_USER,ROLE_ADMIN')}">
       <h:outputText
          value="The user #{bg:remoteUser()} is a user and admin."/>
   </t:div>
 </body>
</html>

You can download the bodyguard by clicking here and sources here.

Share