001package bradleyross.j2ee.beans;
002import java.io.Serializable;
003import java.util.Map;
004import javax.faces.bean.ManagedProperty;
005import javax.faces.bean.ManagedBean;
006import javax.faces.bean.RequestScoped;
007import javax.faces.context.FacesContext;
008import javax.faces.context.ExternalContext;
009import org.apache.logging.log4j.Logger;
010import org.apache.logging.log4j.LogManager;
011// import bradleyross.j2ee.beans.SessionProperties;
012/*
013 * http://stackoverflow.com/questions/550448/get-request-and-session-parameters-and-attributes-from-jsf-pages
014 */
015/**
016 * Sample bean for login information.
017 * 
018 * <p>Uses the annotations {@link ManagedBean}.</p>
019 * 
020 * <p> It appears that I can't use the implicit objects for JSF expression language
021 *     (param, request, response, etc.) in the
022 *     ManagedBean annotations.</p>
023 * 
024 * @author Bradley Ross
025 * 
026 *
027 */
028@SuppressWarnings("serial")
029@ManagedBean
030@RequestScoped
031public class LoginBean implements Serializable {
032        protected Logger logger = null;
033        /**
034         * This constructor reads the HTTP request parameter name and places
035         * it in the user name field of the form.
036         * 
037         * <p>I had previously tried using #{param} in the
038         *    ManagedBean annotation for a property, but it
039         *    didn't seem to work.  Some messages on the web 
040         *    indicated that it worked, but it may be implementation
041         *    dependent.</p>
042         * <p>The constructor will set the userName property to the value of the
043         *    "name" parameter if the parameter is present.</p>
044         * @see FacesContext
045         * @see ExternalContext
046         */
047        public LoginBean() {
048                logger=LogManager.getLogger(this.getClass());
049                String name = 
050                                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("name");
051                if (name == null || name.length() == 0) {
052                        userName = new String();
053                } else {
054                        userName = name;
055                }
056        }
057        /**
058         * User name.
059         * <p>The annotation {@link ManagedProperty}
060         * apparently controls actions by the JSF 
061         * handler with regard to variables.</p>
062         */
063        @ManagedProperty(value="")
064        private String userName;
065        public String getUserName() {
066                return userName;
067        }
068        public void setUserName(String value) {
069                if (value == null) return;
070                userName = value;
071        }
072        /**
073         * User login password.
074         */
075        @ManagedProperty(value="")
076        private String password = new String();
077        public String getPassword() {
078                return password;
079        }
080        public void setPassword(String value) {
081                password = value;
082        }
083        /**
084         * Determines whether the combination of user name and user password represents
085         * a valid user.
086         * 
087         * <p>In this case, the method accepts any login attempt using "password"
088         *    for the password.  In a real application, a more complicated
089         *    algorithm would be used.</p>
090         * @return "login" if properties are valid, otherwise "error"
091         */
092        
093        public String authorized() {
094                if (password.trim().equalsIgnoreCase("password")) {
095                        Map<String,Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
096                        SessionProperties tmp = (SessionProperties) sessionMap.get("sessionProperties");
097                        if (tmp != null ) {
098                                tmp.setUserName(userName);
099                                tmp.setAuthorized(true);
100                        }
101                        logger.warn("LoginBean.authorized: " + userName + " " + password + " login approved");
102                        return "login";
103                } else {
104                        logger.warn("LoginBean.authorized: " + userName + " " + password + " login error");
105                        return "error";
106                }
107        }
108        /**
109         * Diagnostic routine listing session, application, and
110         * request attributes.
111         * 
112         * @return description 
113         * @see FacesContext
114         * @see ExternalContext
115         */
116        public String getList() {
117                FacesContext facesContext = FacesContext.getCurrentInstance();
118                ExternalContext externalContext = facesContext.getExternalContext();
119                Map<String,Object> sessionMap = externalContext.getSessionMap();
120                Map<String,Object> applicationMap = externalContext.getApplicationMap();
121                Map<Object,Object> requestMap = facesContext.getAttributes();
122                StringBuilder builder = new StringBuilder();
123                builder.append("\r\n\r\nApplication Attributes\r\n\r\n");
124                for( Map.Entry<String,Object> entry :  applicationMap.entrySet()) {
125
126                        builder.append("(" + entry.getKey() + " : " + entry.getValue().getClass().getName() + " : " +
127                                        entry.getValue().toString() + ")\r\n ");
128                }
129                builder.append("\r\n\r\nSession Attributes\r\n\r\n");
130                for( Map.Entry<String,Object> entry :  sessionMap.entrySet()) {
131
132                        builder.append("(" + entry.getKey() + " : " + entry.getValue().getClass().getName() + " : " +
133                                        entry.getValue().toString() + ")\r\n ");
134                }
135                builder.append("\r\nRequest Attributes\r\n");
136                if (requestMap != null && !requestMap.isEmpty()) {
137                        for( Map.Entry<Object,Object> entry :  requestMap.entrySet()) {
138                                String string1 = " null value ";
139                                String string2 = " null value ";
140                                String string3 = " null value ";
141                                String string4 = " null value ";
142                                Object key = entry.getKey();
143                                if (key != null) {
144                                        string1 = key.getClass().getName();
145                                        string2 = key.toString();
146                                }
147                                Object value = entry.getValue();
148                                if (value != null) {
149                                        string3 = value.getClass().getName();
150                                        string4 = value.toString();
151                                }
152                                builder.append("(" + string1 + " : " + 
153                                                string2 + " : " + string3 + " : " +
154                                                string4 + ")\r\n ");
155                        }       
156                }else {
157                        builder.append("  No request attributes" );
158                }
159                builder.append("\r\n");
160                return builder.toString();
161        }
162
163        /**
164         * I believe that the ManagedProperty annotation in
165         * this method will cause the sessionProperties
166         * bean to be created at the same time as the
167         * loginBean.
168         * 
169         * <p>The ManagedProperty annotation appears to work when referencing other 
170         *    managed beans but not when referencing implicit objects.</p>
171         */
172    @ManagedProperty("#{sessionProperties}")
173        private SessionProperties sessionBean;
174        public SessionProperties getSessionBean() {
175                return sessionBean;
176        }
177        public void setSessionBean(SessionProperties value) {
178                sessionBean = value;
179        }
180}