2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shale.examples.mailreaderjpa;
19  
20  import org.apache.mailreaderjpa.User;
21  import org.apache.shale.view.AbstractViewController;
22  
23  /***
24   * <p>Backing bean for <code>/logon.jsp</code> view.  Upon completion of a
25   * successful logon, a <code>User</code> instance for the logged on user will
26   * be stored in the session-scoped <code>state</code> bean.</p>
27   */
28  public class Logon extends AbstractViewController {
29  
30  
31      // ------------------------------------------------------- Public Properties
32  
33  
34      /***
35       * <p>The business {@link Logic} for this application.  This value
36       * will be injected, based on the managed bean configuration.</p>
37       */
38      private Logic logic = null;
39  
40  
41      /***
42       * <p>Return the business {@link Logic} for this application.</p>
43       */
44      public Logic getLogic() {
45          return this.logic;
46      }
47  
48  
49      /***
50       * <p>Set the business {@link Logic} for this application.</p>
51       *
52       * @param logic The new business logic instance
53       */
54      public void setLogic(Logic logic) {
55          this.logic = logic;
56      }
57  
58  
59      /***
60       * <p>The per-user {@link State} instance we are associated with.  This
61       * value will be injected, based on the managed bean configuration.</p>
62       */
63      private State state = null;
64  
65  
66      /***
67       * <p>Return the per-user {@link State} instance we are associated with.</p>
68       */
69      public State getState() {
70          return this.state;
71      }
72  
73  
74      /***
75       * <p>Set the per-user {@link State} instance we are associated with.</p>
76       *
77       * @param state The new State instance
78       */
79      public void setState(State state) {
80          this.state = state;
81      }
82  
83  
84      // -------------------------------------------------- Input Field Properties
85  
86  
87      private String username;
88  
89      public String getUsername() {
90          return username;
91      }
92  
93      public void setUsername(String username) {
94          this.username = username;
95      }
96  
97      private String password;
98  
99      public String getPassword() {
100         return password;
101     }
102 
103     public void setPassword(String password) {
104         this.password = password;
105     }
106 
107 
108     // -------------------------------------------------------- Lifecycle Events
109 
110 
111     /***
112      * <p>Ensure that the database content has been bootstrapped before
113      * we try to authenticate a user.</p>
114      */
115     public void preprocess() {
116         getLogic().bootstrap();
117     }
118 
119 
120     // ------------------------------------------------------------- View Events
121 
122 
123     /***
124      * <p>Handle a request to log this user on.</p>
125      */
126     public String logon() {
127 
128         try {
129             User user = getLogic().authenticate(getUsername(), getPassword());
130             if (user != null) {
131                 if (user.getPassword().equals(getPassword())) {
132                     getState().setUser(user);
133                     return "success";
134                 }
135             }
136         } catch (Exception e) {
137             log("Exception authenticating user", e);
138             error("Exception authenticating user: " + e);
139             return null;
140         }
141         error("Invalid username and/or password, please try again");
142         return null;
143 
144     }
145 
146 
147 }