Coverage Report - org.kuali.student.common.ui.client.security.SpringSecurityLoginDialogHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
SpringSecurityLoginDialogHandler
0%
0/33
0%
0/2
1.667
SpringSecurityLoginDialogHandler$1
0%
0/11
N/A
1.667
SpringSecurityLoginDialogHandler$1$1
0%
0/8
0%
0/4
1.667
 
 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  0
 public class SpringSecurityLoginDialogHandler implements SessionTimeoutHandler{
 46  0
         final static ApplicationContext context = Application.getApplicationContext();
 47  
         
 48  0
         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  
         
 56  
         @Override
 57  
         public void handleSessionTimeout() {
 58  0
             if (lightbox == null){
 59  0
                     createLoginPanel();
 60  
             } else {
 61  0
                     resetLoginPanel();
 62  
             }
 63  0
             lightbox.setSize(460, 220);
 64  0
         lightbox.show();                
 65  0
         }
 66  
 
 67  
         private void createLoginPanel(){
 68  0
                 lightbox = new KSLightBox();
 69  0
         VerticalPanel panel = new VerticalPanel();
 70  
 
 71  0
         FlexTable table = new FlexTable();
 72  
 
 73  0
         errorLabel = new Label();
 74  0
         errorLabel.setText(TIMEOUT_MSG);
 75  0
         errorLabel.setStyleName("KSError");
 76  
         
 77  0
         username = new TextBox();
 78  0
         username.setName("j_username");
 79  
 
 80  0
         password = new PasswordTextBox();
 81  0
         password.setName("j_password");
 82  
         
 83  0
         table.setText(0, 0, "Username");
 84  0
         table.setWidget(0, 1, username);
 85  
 
 86  0
         table.setText(1,0, "Password");
 87  0
         table.setWidget(1,1, password);
 88  
                              
 89  0
         table.setWidget(2,0,(new Button("Login", new ClickHandler() {   
 90  
             public void onClick(ClickEvent event) {
 91  
                 
 92  0
                 StringBuffer postData = new StringBuffer();
 93  0
                 postData.append(URL.encode("j_username")).append("=").append(username.getText());
 94  0
                 postData.append("&").append(URL.encode("j_password")).append("=").append(password.getText());
 95  
                 
 96  0
                 RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, context.getApplicationContextUrl() + "/j_spring_security_check");
 97  0
                 builder.setHeader("Content-type", "application/x-www-form-urlencoded");
 98  
 
 99  
                 try{
 100  0
                     builder.sendRequest(postData.toString(), new RequestCallback(){
 101  
 
 102  
                         @Override
 103  
                         public void onError(Request req, Throwable caught) {
 104  0
                                 lightbox.hide();
 105  0
                                 KSErrorDialog.show(caught);
 106  0
                         }
 107  
 
 108  
                         @Override
 109  
                         public void onResponseReceived(Request req, Response res) {
 110  0
                             if (res.getStatusCode() == Response.SC_OK && !res.getText().contains("Bad credentials")){
 111  0
                                     lightbox.hide();
 112  
                             } else {
 113  0
                                 errorLabel.setText("Your login attempt was not successful, try again.");
 114  
                             }
 115  0
                         }});
 116  0
                 } catch (RequestException e) {
 117  0
                         KSErrorDialog.show(e);
 118  0
                 }                
 119  0
             }
 120  
         }
 121  
         )));
 122  
                 
 123  0
         panel.add(errorLabel);
 124  0
         panel.add(table);
 125  
  
 126  0
         panel.setStyleName("KSLoginPanel");
 127  0
         lightbox.setWidget(panel);
 128  0
     }
 129  
         
 130  
         private void resetLoginPanel(){
 131  0
                 username.setText("");
 132  0
                 password.setText("");
 133  0
                 errorLabel.setText(TIMEOUT_MSG);                
 134  0
         }
 135  
     
 136  
 }