View Javadoc

1   /*
2    * Copyright 2007 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 1.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl1.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.student.common.ui.client.security;
17  
18  import org.kuali.student.common.ui.client.application.Application;
19  import org.kuali.student.common.ui.client.application.ApplicationContext;
20  import org.kuali.student.common.ui.client.widgets.KSErrorDialog;
21  import org.kuali.student.common.ui.client.widgets.KSLightBox;
22  
23  import com.google.gwt.event.dom.client.ClickEvent;
24  import com.google.gwt.event.dom.client.ClickHandler;
25  import com.google.gwt.http.client.Request;
26  import com.google.gwt.http.client.RequestBuilder;
27  import com.google.gwt.http.client.RequestCallback;
28  import com.google.gwt.http.client.RequestException;
29  import com.google.gwt.http.client.Response;
30  import com.google.gwt.http.client.URL;
31  import com.google.gwt.user.client.ui.Button;
32  import com.google.gwt.user.client.ui.FlexTable;
33  import com.google.gwt.user.client.ui.Label;
34  import com.google.gwt.user.client.ui.PasswordTextBox;
35  import com.google.gwt.user.client.ui.TextBox;
36  import com.google.gwt.user.client.ui.VerticalPanel;
37  
38  /**
39   * This implements the SessionTimeoutHandler. The timeout is handled by displaying a spring
40   * security login panel.  
41   * 
42   * @author Kuali Student Team
43   *
44   */
45  public class SpringSecurityLoginDialogHandler implements SessionTimeoutHandler{
46  	final static ApplicationContext context = Application.getApplicationContext();
47  	static  boolean CASrequiresAPageRefreshViaJavascript = Boolean.TRUE;
48  	public final String TIMEOUT_MSG = "Your session has timed out. Please login again.";
49  	
50  	protected KSLightBox lightbox;
51      protected TextBox username;
52      protected PasswordTextBox password;
53      protected Label errorLabel;
54      
55    private native void reload() /*-{
56      $wnd.location.reload();
57     }-*/;
58  
59  	@Override
60  	public boolean isSessionTimeout(Throwable error) {
61          boolean InvocationException = error.toString().contains("com.google.gwt.user.client.rpc.InvocationException");
62          boolean CAS = error.toString().contains(""); // The return login page will from cas because spring filter it
63          boolean normalLogin =    error.toString().contains("Login"); // the return login will from normal spring Authentication
64  
65          //until I havent had a chance to see what the CAS loging looks like I am making it default that the javascript do
66          // a page refresh rather than showing a login dialogbox. Thus we will be taken to login.jsp screen correlating with
67          // the chosen filter {cas or normal spring form}
68  
69          if(CAS )
70          {
71              CASrequiresAPageRefreshViaJavascript = true;
72          }
73  
74      	return CAS || normalLogin ;
75      }
76      
77      @Override
78  	public void handleSessionTimeout() {
79          if(!CASrequiresAPageRefreshViaJavascript){
80      	if (lightbox == null){
81      		createLoginPanel();
82      	} else {
83      		resetLoginPanel();
84      	}
85      	lightbox.setSize(460, 220);
86          lightbox.show();
87          }else{reload();}
88  	}
89  
90  	private void createLoginPanel(){
91  		lightbox = new KSLightBox();
92          VerticalPanel panel = new VerticalPanel();
93  
94          FlexTable table = new FlexTable();
95  
96          errorLabel = new Label();
97          errorLabel.setText(TIMEOUT_MSG);
98          errorLabel.setStyleName("KSError");
99          
100         username = new TextBox();
101         username.setName("j_username");
102 
103         password = new PasswordTextBox();
104         password.setName("j_password");
105         
106         table.setText(0, 0, "Username");
107         table.setWidget(0, 1, username);
108 
109         table.setText(1,0, "Password");
110         table.setWidget(1,1, password);
111                              
112         table.setWidget(2,0,(new Button("Login", new ClickHandler() {   
113             public void onClick(ClickEvent event) {
114                 
115                 StringBuffer postData = new StringBuffer();
116                 postData.append(URL.encode("j_username")).append("=").append(username.getText());
117                 postData.append("&").append(URL.encode("j_password")).append("=").append(password.getText());
118                 
119                 RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, context.getApplicationContextUrl() + "/j_spring_security_check");
120                 builder.setHeader("Content-type", "application/x-www-form-urlencoded");
121 
122                 try{
123                     builder.sendRequest(postData.toString(), new RequestCallback(){
124 
125                         @Override
126                         public void onError(Request req, Throwable caught) {
127                         	lightbox.hide();
128                         	KSErrorDialog.show(caught);
129                         }
130 
131                         @Override
132                         public void onResponseReceived(Request req, Response res) {
133                             if (res.getStatusCode() == Response.SC_OK && !res.getText().contains("Bad credentials")){
134                             	lightbox.hide();
135                             } else {
136                                 errorLabel.setText("Your login attempt was not successful, try again.");
137                             }
138                         }});
139                 } catch (RequestException e) {
140                 	KSErrorDialog.show(e);
141                 }                
142             }
143         }
144         )));
145                 
146         panel.add(errorLabel);
147         panel.add(table);
148  
149         panel.setStyleName("KSLoginPanel");
150         lightbox.setWidget(panel);
151     }
152 	
153 	private void resetLoginPanel(){
154 		username.setText("");
155 		password.setText("");
156 		errorLabel.setText(TIMEOUT_MSG);		
157 	}
158     
159 }