Container-Managed Authentication and Authorization

Managing Configuration Information

Whenever your application interfaces with external services, you need to specify configuration parameters: URLs, usernames, passwords, and so on. You should never hardcode these parameters inside your application classes doing so would make it difficult to update passwords, switch to alternative servers, and so on.

In the section on database services, you saw a reasonable approach for managing the database configuration. The configuration information is placed inside server.xml. The servlet container uses this information to construct a data source and bind it to a well-known name. The classes that need to access the database use JNDI look up the data source.

Placing configuration information into server.xml is appropriate for a global resource such as a database. This resource be used by all web applications inside the container. On the other hand, application-specific configuration information should be placed inside web.xml or faces-config.xml. Using the example of an LDAP connection, we explore all three possibilities.

Configuring a Bean

Whenever you define a bean in faces-config.xml, you can provide initialization parameters by using the managed-property element. Here is how we can initialize a bean that connects to an LDAP directory:

  <managed-bean>      <managed-bean-name>userdir</managed-bean-name>      <managed-bean-class>com.corejsf.UserDirectoryBean</managed-bean-class>      <managed-bean-scope>application</managed-bean-scope>      <managed-property>         <property-name>URL</property-name>         <value>ldap://localhost:389</value>      </managed-property>      <managed-property>         <property-name>managerDN</property-name>         <value>cn=Manager,dc=corejsf,dc=com</value>      </managed-property>      <managed-property>         <property-name>managerPassword</property-name>         <value>secret</value>      </managed-property>   </managed-bean>

You see the familiar managed-bean-name and managed-bean-class elements. However, this bean is given application scope. The bean object stays alive for the duration of the entire application, and it can serve multiple sessions. Finally, we used the managed-property settings to initialize the bean. Thus, we achieved our goal of placing these initialization parameters inside a configuration file rather than hardwiring them into the bean code.

Of course, our bean needs setters for these properties:

  public class UserDirectoryBean {      private String url;      private String managerDN;      private String managerPW;      public void setManagerDN(String newValue) { managerDN = newValue; }      public void setManagerPassword(String newValue) { managerPW = newValue; }      public void setURL(String newValue) { url = newValue; }      public DirContext getRootContext() throws NamingException { ... }   }     

When the bean is constructed, the setters are invoked with the values specified in faces-config.xml.

Finally, client code needs to have access to the bean object. For example, suppose the UserBean class wants to connect to the directory:

  UserDirectoryBean userdir = ... // how?   DirContext context = userdir.connect(dn, pw);

To look up a JSF bean, you use its value binding of its name, as in the following statements:

  FacesContext context = FacesContext.getCurrentInstance();   Application app = context.getApplication();   ValueBinding binding = app.createValueBinding("#{userdir}");   UserDirectoryBean dir = (UserDirectoryBean) binding.getValue(context);

In summary, here are the steps for configuring a JSF bean:

1.

Place the configuration parameters inside managed-property elements in the faces-config.xml file.

2.

Provide property setters for these properties in the bean class.

3.

Look up the bean object through its value binding.

This configuration method is straightforward and convenient. However, it is not suitable for configuring objects that should be available to multiple web applications. Moreover, purists might argue that faces-config.xml is intended to describe the logic of a web application, not its interface with external resources, and that web.xml would be more appropriate for the latter. Read on if either of these objections matters to you.

Configuring the External Context

In this section, we assume that your JSF application is launched as a servlet. You can supply parameters in web.xml by providing a set of context-param elements inside the web-app element:

  <web-app>      <context-param>         <param-name>URL</param-name>         <param-value>ldap://localhost:389</param-value>      </context-param>      <context-param>         <param-name>managerDN</param-name>         <param-value>cn=Manager,dc=corejsf,dc=com</param-value>      </context-param>      <context-param>         <param-name>managerPassword</param-name>         <param-value>secret</param-value>      </context-param>      ...   </web-app>

To read a parameter, get the external context object. That object describes the execution environment that launched your JSF application. If you use a servlet container, then the external context is a wrapper around the ServletContext object. The ExternalContext class has a number of convenience methods to access properties of the underlying servlet context. The getInitParameter method retrieves a context parameter value with a given name.

Caution

Do not confuse context-param with init-param. The latter tag is used for parameters that a servlet can process at startup. It is unfortunate that the method for reading a context parameter is called getInitParameter.


Here is the code for getting an LDAP context from configuration parameters in web.xml:

  public DirContext getRootContext() throws NamingException {      ExternalContext external         = FacesContext.getCurrentInstance().getExternalContext();      String managerDN = external.getInitParameter("managerDN");      String managerPW = external.getInitParameter("managerPassword");      String url = external.getInitParameter("URL");      Hashtable env = new Hashtable();      env.put(Context.SECURITY_PRINCIPAL, managerDN);      env.put(Context.SECURITY_CREDENTIALS, managerPW);      DirContext initial = new InitialDirContext(env);      Object obj = initial.lookup(url);      if (!(obj instanceof DirContext))         throw new NamingException("No directory context");      return (DirContext) obj;   }

Follow these steps for accessing resources through the external context:

1.

Place the configuration parameters inside context-param elements in the web.xml file.

2.

Use the ExternalContext to look up the parameter values.

3.

Turn the parameters into objects for your application.

As you can see, this configuration method works at a lower level than the configuration of a JSF bean. The web.xml file contains an unstructured list of parameters. It is up to you to construct objects that make use of these parameters.

javax.faces.context.FacesContext

  • ExternalContext getExternalContext()

    Gets the external context, a wrapper such as a servlet or portlet context around the execution environment of this JSF application.


javax.faces.context.ExternalContext

  • String getInitParameter(String name)

    Gets the initialization parameter with the given name.


Configuring a Container-Managed Resource

We now discuss how to specify container-wide resources. The information in this section is specific to GlassFish and Tomcat. Other containers will have similar mechanisms, but the details will differ.

Earlier in this chapter, we showed you how to configure a JDBC data source so that you can locate it through resource injection or with a JNDI lookup. This is an attractive method for specifying systemwide resources. Fortunately, Glass-Fish and Tomcat let you fit your own resources into the same mechanism.

In GlassFish, you use the administration interface to add a custom JNDI resource (see Figure 10-12). In our sample application, the custom resource is an LDAP directory. Supply the name ldap/mydir and type javax.naming.directory.DirContext. Unfortunately, there is no standard factory for LDAP directory contexts, and we will implement a custom factory of type com.corejsf.DirContextFactory. Finally, specify the following connection parameters as additional properties:

  URL=ldap://localhost:389   java.naming.security.principal=cn=Manager,dc=corejsf,dc=com   java.naming.security.credentials=secret

Figure 10-12. Configuring an LDAP resource in GlassFish


Note

We use the standard JNDI environment names for the principal and credentials. The Context interface constants that we used previously are merely shortcuts for the environment names. For example, Context.SECURITY_PRINCIPAL is the string "java.naming.security.principal".


You also need to supply a file WEB-INF/sun-web.xml, as shown in Listing 10-8. This file maps the application server's resource name to the JNDI name. We use the same name for both.

Listing 10-8. ldap3/web/WEB-INF/sun-web.xml (GlassFish only)

  1. <!DOCTYPE sun-web-app PUBLIC   2.    "-//Sun Microsystems, Inc.//DTD Application Server 9.0 Servlet 2.5//EN"   3.    "http://www.sun.com/software/appserver/dtds/sun-web-app_2_5-0.dtd">   4. <sun-web-app>   5.    <resource-ref>   6.       <res-ref-name>ldap/mydir</res-ref-name>   7.       <jndi-name>ldap/mydir</jndi-name>   8.    </resource-ref>   9. </sun-web-app>     

Now we need to implement the factory class. All factories need to implement the ObjectFactory interface type and implement the getObjectInstance method.

  public class DirContextFactory implements ObjectFactory {      public Object getObjectInstance(Object obj,         Name n, Context nameCtx, Hashtable environment)         throws NamingException {         ...      }   }

This method, defined in glorious generality, can be used to produce any object from arbitrary configuration information. There is quite a bit of variability in how the parameters are used, but fortunately we need only to understand what parameters the application server supplies when requesting a resource.

The configuration parameters are placed into a Reference object, a kind of hash table on a megadose of steroids. Our factory places the parameters into a plain hash table and then gets the directory context (see Listing 10-9 for the complete source code).

Note

The class com.sun.jndi.ldap.LdapCtxFactory (which is used in Sun's JNDI tutorial) also implements the ObjectFactory interface. Unfortunately, you cannot use that class as a factory for LDAP connections in an application server. The getObjectInstance method of com.sun.jndi.ldap.LdapCtx-Factory expects an Object parameter that is either a URL string, an array of URL strings, or a Reference object containing a value with key "URL". The other environment settings must be provided in the Hashtable parameter. That is not what the application server supplies.


Listing 10-9. ldap3/setup/java/com/corejsf/DirContextFactory.java

  1. package com.corejsf;   2.   3. import java.util.Enumeration;   4. import java.util.Hashtable;   5.   6. import javax.naming.Context;   7. import javax.naming.Name;   8. import javax.naming.NamingException;   9. import javax.naming.RefAddr;  10. import javax.naming.Reference;  11. import javax.naming.directory.DirContext;  12. import javax.naming.directory.InitialDirContext;  13. import javax.naming.spi.ObjectFactory;  14.  15. public class DirContextFactory implements ObjectFactory {  16.    public Object getObjectInstance(Object obj,  17.       Name n, Context nameCtx, Hashtable environment)  18.       throws NamingException {  19.  20.       Hashtable<String, String> env = new Hashtable<String, String>();  21.       String url = null;  22.       Reference ref = (Reference) obj;  23.       Enumeration addrs = ref.getAll();  24.       while (addrs.hasMoreElements()) {  25.           RefAddr addr = (RefAddr) addrs.nextElement();  26.           String name = addr.getType();  27.           String value = (String) addr.getContent();  28.           if (name.equals("URL")) url = value;  29.           else env.put(name, value);  30.       }  31.       DirContext initial = new InitialDirContext(env);  32.       if (url == null) return initial;  33.       else return initial.lookup(url);  34.    }  35. }     

Note

Compile this file and place it inside a JAR file.

cd corejsf-examples/ch10/ldap3/setup/java javac com/corejsf/DirContextFactory.java jar cvf dirctxfactory.jar com/corejsf/*.class

Then move the JAR file into the glassfish/domains/domain1/lib/ext directory.


With Tomcat, the steps are similar. You specify a Resource and its ResourceParams in server.xml.

  <Resource name="ldap/mydir" auth="Container"      type="javax.naming.directory.DirContext"/>   <ResourceParams name="ldap/mydir">      <parameter>         <name>factory</name>         <value>com.corejsf.DirContextFactory</value>      </parameter>      <parameter>         <name>URL</name>         <value>ldap://localhost:389</value>      </parameter>      <parameter>         <name>java.naming.security.principal</name>         <value>cn=Manager,dc=corejsf,dc=com</value>      </parameter>      <parameter>         <name>java.naming.security.credentials</name>         <value>secret</value>      </parameter>   </ResourceParams>

Place the JAR file with the DirContextFactory into the common/lib directory of Tomcat and restart the server. You do not need a sun-web.xml file.

Creating an LDAP Application

We will now put together a complete application that stores user information in an LDAP directory.

The application simulates a news web site that gives users free access to news as long as they provide some information about themselves. We do not actually provide any news. We simply provide a screen to log in (Figure 10-13) and a separate screen to register for the service (Figure 10-14). Upon successful login, users can read news and update their personal information (Figure 10-15).

Figure 10-13. Logging in to the news service


Figure 10-14. Registering for the news service


Figure 10-15. Main screen of the news service


The update screen is similar to the registration screen, and we do not show it. Figure 10-16 shows the directory structure, and Figure 10-17 shows the page flow between the news service pages.

Figure 10-16. The directory structure of the LDAP example


Figure 10-17. Page flow of the news service


We provide three versions of this application, with configuration information in faces-config.xml, web.xml, and the application server, respectively.

All three versions have identical web pages (see Listing 10-10 through Listing 10-13). (We omit the listings of repetitive pages.) The primary difference between the versions is the implementation of the getRootContext method in the UserBean class (Listing 10-14).

The first application has a UserDirectoryBean class (Listing 10-15) that is configured in faces-config.xml (Listing 10-16). The second application makes an ad hoc lookup of servlet initialization parameters. The third version uses resource injection, using the class of Listing 10-9. See the preceding sections for details. Finally, for completeness, Listing 10-17 contains the code for the Name class that is used in the UserBean class.

Listing 10-10. ldap/web/index.jsp

  1. <html>   2.    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>   3.    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>   4.    <f:view>   5.       <head>   6.          <title><h:outputText value="#{msgs.title}"/></title>   7.       </head>   8.       <body>   9.          <h:form>  10.             <h1><h:outputText value="#{msgs.enterNameAndPassword}"/></h1>  11.             <h:panelGrid columns="2">  12.                <h:outputText value="#{msgs.loginID}"/>  13.                <h:inputText value="#{user.id}"/>   14.  15.                <h:outputText value="#{msgs.password}"/>  16.                <h:inputSecret value="#{user.password}"/>  17.             </h:panelGrid>  18.             <h:commandButton value="#{msgs.login}" action="#{user.login}"/>  19.             <br/>  20.             <h:outputText value="#{msgs.signupNow}"/>  21.             <h:commandButton value="#{msgs.signup}" action="signup"/>  22.          </h:form>  23.       </body>  24.    </f:view>  25. </html>     

Listing 10-11. ldap/web/signup.jsp

  1. <html>   2.    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>   3.    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>   4.    <f:view>   5.       <head>   6.          <title><h:outputText value="#{msgs.title}"/></title>   7.       </head>   8.       <body>   9.          <h:form>  10.             <h1><h:outputText value="#{msgs.newUserSignup}"/></h1>  11.             <p><h:outputText value="#{msgs.newUserSignup_detail}"/></p>  12.             <h:panelGrid columns="2">  13.                <h:outputText value="#{msgs.firstName}"/>  14.                <h:inputText value="#{user.name.first}"/>  15.  16.                <h:outputText value="#{msgs.middleInitial}"/>  17.                <h:inputText value="#{user.name.middle}"/>  18.  19.                <h:outputText value="#{msgs.lastName}"/>  20.                <h:inputText value="#{user.name.last}"/>  21.  22.                <h:outputText value="#{msgs.email}"/>  23.                <h:inputText value="#{user.email}"/>  24.  25.                <h:outputText value="#{msgs.loginID}"/>  26.                <h:inputText value="#{user.id}"/>  27.  28.                <h:outputText value="#{msgs.password}"/>  29.                <h:inputSecret value="#{user.password}"/>  30.             </h:panelGrid>  31.             <h:commandButton value="#{msgs.submit}" action="#{user.signup}"/>  32.             <h:commandButton value="#{msgs.cancel}" action="signup_cancel"/>  33.          </h:form>  34.       </body>  35.    </f:view>  36. </html>     

Listing 10-12. ldap/web/welcome.jsp

  1. <html>   2.    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>   3.    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>   4.    <f:view>   5.       <head>   6.          <title><h:outputText value="#{msgs.title}"/></title>   7.       </head>   8.       <body>   9.          <h:form>  10.             <h1><h:outputText value="#{msgs.success}"/></h1>  11.             <p>  12.                <h:outputText value="#{msgs.welcome}"/>  13.                <h:outputText value="#{user.name}"/>!  14.             </p>  15.             <p>  16.                <h:outputText value="#{msgs.success_detail}"/>  17.             </p>  18.             <h:commandButton value="#{msgs.update}" action="update"/>  19.             <h:commandButton value="#{msgs.logout}" action="#{user.logout}"/>  20.          </h:form>  21.       </body>  22.    </f:view>  23. </html>     

Listing 10-13. ldap/web/loginError.jsp

  1. <html>   2.    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>   3.    <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>   4.    <f:view>   5.       <head>   6.          <title><h:outputText value="#{msgs.title}"/></title>   7.       </head>   8.       <body>   9.          <h:form>  10.             <h1><h:outputText value="#{msgs.loginError}"/></h1>  11.             <p>  12.                <h:outputText value="#{msgs.loginError_detail}"/>  13.             </p>  14.             <p>  15.                <h:commandButton value="#{msgs.tryAgain}" action="login"/>  16.                <h:commandButton value="#{msgs.signup}" action="signup"/>  17.             </p>  18.          </h:form>  19.       </body>  20.    </f:view>  21. </html>     

Listing 10-14. ldap/src/java/com/corejsf/UserBean.java

  1. package com.corejsf;   2.   3. import java.util.logging.Level;   4. import java.util.logging.Logger;   5.   6. import javax.el.ELContext;   7. import javax.el.ExpressionFactory;   8. import javax.el.ValueExpression;   9. import javax.faces.application.Application;  10. import javax.faces.context.FacesContext;  11. import javax.naming.NameNotFoundException;  12. import javax.naming.NamingException;  13. import javax.naming.directory.Attributes;  14. import javax.naming.directory.BasicAttributes;  15. import javax.naming.directory.DirContext;  16.  17. public class UserBean {  18.    private Name name;  19.    private String id;  20.    private String email;  21.    private String password;  22.    private Logger logger = Logger.getLogger("com.corejava");  23.  24.    public UserBean() { name = new Name(); }   25.  26.    public DirContext getRootContext() throws NamingException {  27.       FacesContext context = FacesContext.getCurrentInstance();  28.       ELContext elContext = context.getELContext();  29.       Application app = context.getApplication();  30.       ExpressionFactory factory = app.getExpressionFactory();  31.       ValueExpression ex = factory.createValueExpression(elContext,   32.             "#{userdir}", UserDirectoryBean.class);  33.       UserDirectoryBean dir =  34.          (UserDirectoryBean) ex.getValue(elContext);  35.       return dir.getRootContext();  36.    }  37.  38.    public Name getName() { return name; }  39.    public void setName(Name newValue) { name = newValue; }  40.  41.    public String getEmail() { return email; }  42.    public void setEmail(String newValue) { email = newValue; }  43.  44.    public String getId() { return id; }  45.    public void setId(String newValue) { id = newValue; }  46.  47.    public String getPassword() { return password; }  48.    public void setPassword(String newValue) { password = newValue; }  49.  50.    public String login() {  51.       try {  52.          DirContext context = getRootContext();  53.          try {  54.             String dn = "u,ou=people,dc=corejsf,dc=com";  55.             Attributes userAttributes = context.getAttributes(dn);   56.             String cn = (String) userAttributes.get("cn").get();  57.             name.parse(cn);  58.             email = (String) userAttributes.get("mail").get();  59.             byte[] pw = (byte[])   60.                userAttributes.get("userPassword").get();  61.                if (password.equals(new String(pw)))  62.                   return "login_success";  63.                else  64.                   return "login_failure";  65.             } finally {  66.                context.close();  67.             }  68.          }  69.          catch (NamingException ex) {  70.             logger.log(Level.SEVERE, "loginAction", ex);  71.             return "login_error";  72.          }  73.    }  74.  75.    public String signup() {  76.       try {  77.          DirContext context = getRootContext();  78.          try {  79.             String dn = "u,ou=people,dc=corejsf,dc=com";  80.  81.             try {  82.                context.lookup(dn);   83.                return "signup_failure";  84.             }  85.             catch (NameNotFoundException ex) {}  86.  87.             Attributes attrs = new BasicAttributes();  88.             attrs.put("objectClass", "inetOrgPerson");  89.             attrs.put("uid", id);  90.             attrs.put("sn", name.getLast());  91.             attrs.put("cn", name.toString());  92.             attrs.put("mail", email);  93.             attrs.put("userPassword", password.getBytes());  94.             context.createSubcontext(dn, attrs);  95.          } finally {  96.             context.close();  97.          }  98.       }  99.       catch (NamingException ex) { 100.          logger.log(Level.SEVERE, "loginAction", ex); 101.          return "signup_error"; 102.       } 103. 104.       return "signup_success"; 105.    } 106. 107.    public String update() { 108.       try { 109.          DirContext context = getRootContext(); 110.          try { 111.             String dn = "u,ou=people,dc=corejsf,dc=com"; 112.             Attributes attrs = new BasicAttributes(); 113.             attrs.put("sn", name.getLast()); 114.             attrs.put("cn", name.toString()); 115.             attrs.put("mail", email); 116.             attrs.put("userPassword", password.getBytes()); 117.             context.modifyAttributes(dn,  118.                DirContext.REPLACE_ATTRIBUTE, attrs); 119.          } finally { 120.             context.close(); 121.          } 122.       } 123.       catch (NamingException ex) { 124.          logger.log(Level.SEVERE, "updateAction", ex); 125.          return "internal_error"; 126.       } 127. 128.       return "update_success"; 129.     } 130. 131.     public String logout() { 132.        password = ""; 133.        return "logout_success"; 134.     } 135. }     

Listing 10-15. ldap/src/java/com/corejsf/UserDirectoryBean.java

  1. package com.corejsf;   2.   3. import java.util.Hashtable;   4. import javax.naming.Context;   5. import javax.naming.NamingException;   6. import javax.naming.directory.DirContext;   7. import javax.naming.directory.InitialDirContext;   8.   9. public class UserDirectoryBean {  10.    private String url;  11.    private String managerDN;  12.    private String managerPW;  13.  14.    public void setManagerDN(String newValue) { managerDN = newValue; }  15.    public void setManagerPassword(String newValue) {   16.       managerPW = newValue; }  17.    public void setURL(String newValue) { url = newValue; }  18.  19.    public DirContext getRootContext() throws NamingException {  20.       Hashtable<String, String> env = new Hashtable<String, String>();  21.       env.put(Context.SECURITY_PRINCIPAL, managerDN);  22.       env.put(Context.SECURITY_CREDENTIALS, managerPW);  23.       DirContext initial = new InitialDirContext(env);  24.  25.       Object obj = initial.lookup(url);  26.       if (!(obj instanceof DirContext))  27.          throw new NamingException("No directory context");  28.       return (DirContext) obj;  29.    }  30. }     

Listing 10-16. ldap/web/WEB-INF/faces-config.xml

  1. <?xml version="1.0"?>   2. <faces-config xmlns="http://java.sun.com/xml/ns/javaee"   3.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   4.    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    5.       http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"   6.    version="1.2">   7.    <navigation-rule>   8.       <from-view-id>/index.jsp</from-view-id>   9.       <navigation-case>  10.          <from-outcome>login_success</from-outcome>  11.          <to-view-id>/welcome.jsp</to-view-id>  12.       </navigation-case>  13.       <navigation-case>  14.          <from-outcome>login_error</from-outcome>  15.          <to-view-id>/loginError.jsp</to-view-id>  16.       </navigation-case>  17.       <navigation-case>  18.          <from-outcome>login_failure</from-outcome>  19.          <to-view-id>/loginError.jsp</to-view-id>  20.       </navigation-case>  21.    </navigation-rule>  22.    <navigation-rule>  23.       <from-view-id>/signup.jsp</from-view-id>  24.       <navigation-case>  25.          <from-outcome>signup_success</from-outcome>  26.          <to-view-id>/welcome.jsp</to-view-id>  27.       </navigation-case>  28.       <navigation-case>  29.          <from-outcome>signup_failure</from-outcome>  30.          <to-view-id>/signupError.jsp</to-view-id>  31.       </navigation-case>  32.       <navigation-case>  33.          <from-outcome>signup_error</from-outcome>  34.          <to-view-id>/signupError.jsp</to-view-id>  35.       </navigation-case>  36.       <navigation-case>  37.          <from-outcome>signup_cancel</from-outcome>  38.          <to-view-id>/index.jsp</to-view-id>  39.       </navigation-case>  40.    </navigation-rule>  41.    <navigation-rule>  42.       <from-view-id>/welcome.jsp</from-view-id>  43.       <navigation-case>  44.          <from-outcome>update</from-outcome>  45.          <to-view-id>/update.jsp</to-view-id>  46.       </navigation-case>  47.       <navigation-case>  48.          <from-outcome>logout_success</from-outcome>  49.          <to-view-id>/index.jsp</to-view-id>  50.       </navigation-case>  51.    </navigation-rule>  52.    <navigation-rule>  53.       <from-view-id>/update.jsp</from-view-id>  54.       <navigation-case>  55.          <from-outcome>update_success</from-outcome>  56.          <to-view-id>/welcome.jsp</to-view-id>  57.       </navigation-case>  58.       <navigation-case>  59.          <from-outcome>update_cancel</from-outcome>  60.          <to-view-id>/welcome.jsp</to-view-id>  61.       </navigation-case>  62.    </navigation-rule>  63.    <navigation-rule>  64.       <navigation-case>  65.          <from-outcome>login</from-outcome>  66.          <to-view-id>/index.jsp</to-view-id>  67.       </navigation-case>  68.       <navigation-case>  69.          <from-outcome>signup</from-outcome>  70.          <to-view-id>/signup.jsp</to-view-id>  71.       </navigation-case>  72.       <navigation-case>  73.          <from-outcome>internal_error</from-outcome>  74.          <to-view-id>/internalError.jsp</to-view-id>  75.       </navigation-case>  76.    </navigation-rule>  77.  78.    <managed-bean>   79.       <managed-bean-name>user</managed-bean-name>  80.       <managed-bean-class>com.corejsf.UserBean</managed-bean-class>   81.       <managed-bean-scope>session</managed-bean-scope>   82.    </managed-bean>  83.  84.    <managed-bean>  85.       <managed-bean-name>userdir</managed-bean-name>  86.       <managed-bean-class>com.corejsf.UserDirectoryBean</managed-bean-class>  87.       <managed-bean-scope>application</managed-bean-scope>  88.       <managed-property>  89.          <property-name>URL</property-name>  90.          <value>ldap://localhost:389</value>  91.       </managed-property>  92.       <managed-property>  93.          <property-name>managerDN</property-name>  94.          <value>cn=Manager,dc=corejsf,dc=com</value>  95.       </managed-property>  96.       <managed-property>  97.          <property-name>managerPassword</property-name>  98.          <value>secret</value>  99.       </managed-property> 100.    </managed-bean> 101. 102.    <application> 103.       <resource-bundle> 104.          <base-name>com.corejsf.messages</base-name> 105.          <var>msgs</var> 106.       </resource-bundle> 107.    </application> 108. </faces-config>     

Listing 10-17. ldap/src/java/com/corejsf/Name.java

  1. package com.corejsf;   2.   3. public class Name {   4.    private String first;   5.    private String middle;   6.    private String last;   7.   8.    public Name() { first = ""; middle = ""; last = ""; }   9.  10.    public String getFirst() { return first; }  11.    public void setFirst(String newValue) { first = newValue; }  12.    public String getMiddle() { return middle; }  13.    public void setMiddle(String newValue) { middle = newValue; }  14.    public String getLast() { return last; }  15.    public void setLast(String newValue) { last = newValue; }  16.  17.    public void parse(String fullName) {  18.       int firstSpace = fullName.indexOf(' ');  19.       int lastSpace = fullName.lastIndexOf(' ');  20.       if (firstSpace == -1) {  21.          first = "";   22.          middle = "";  23.          last = fullName;  24.       }  25.       else {  26.          first = fullName.substring(0, firstSpace);  27.          if (firstSpace < lastSpace)   28.             middle = fullName.substring(firstSpace + 1, lastSpace);  29.          else  30.             middle = "";  31.          last = fullName.substring(lastSpace + 1, fullName.length());  32.       }  33.    }  34.  35.    public String toString() {   36.       StringBuilder builder = new StringBuilder();  37.       builder.append(first);  38.       builder.append(' ');  39.       if (middle.length() > 0) {  40.          builder.append(middle.charAt(0));  41.          builder.append(". ");  42.       }  43.       builder.append(last);  44.       return builder.toString();  45.     }  46. }     



Core JavaServerT Faces
Core JavaServer(TM) Faces (2nd Edition)
ISBN: 0131738860
EAN: 2147483647
Year: 2004
Pages: 84

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net