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