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.application;
17  
18  import org.kuali.student.common.ui.client.security.SessionTimeoutHandler;
19  import org.kuali.student.common.ui.client.security.SpringSecurityLoginDialogHandler;
20  import org.kuali.student.common.ui.client.widgets.KSErrorDialog;
21  
22  import com.google.gwt.core.client.GWT;
23  import com.google.gwt.user.client.rpc.AsyncCallback;
24  
25  /**
26   * This extends the AysncCallback to handle uncaught RPC exceptions and
27   * handle authentication.
28   * 
29   * @author Kuali Student Team
30   *
31   */
32  public abstract class KSAsyncCallback<T> implements AsyncCallback<T>{
33         
34  	private static final SessionTimeoutHandler handler = GWT.create(SpringSecurityLoginDialogHandler.class);
35  	
36  	/**
37  	 * It is recommended that this method not be overrided, as it provides session timeout handling. 
38  	 * Instead the handleFailure method should be overriden to handle rpc call error.
39  	 *  
40  	 */
41  	public void onFailure(Throwable caught) {  
42          if (isSessionTimeout(caught)){
43          	handleTimeout(caught);
44          	handler.handleSessionTimeout();
45          } else {        
46          	handleFailure(caught);
47          }
48      }
49  
50      /**
51       * Override this method to process any exceptions you wish to handle. The default implementation displays 
52       * an error dialog with the message and logs the exception.
53       * 
54       * @param caught
55       */
56      public void handleFailure(Throwable caught){
57      	if (!isSessionTimeout(caught)){
58      		KSErrorDialog.show(caught);
59      	}
60          GWT.log("Exception:", caught);
61      }
62      
63      /**
64       * By default this defers to handleFailure. Override this handle a rpc failure due to timeout differently
65       * from other exceptions.
66       * 
67       * @param caught
68       */
69      public void handleTimeout(Throwable caught){
70      	handleFailure(caught);
71      }
72      
73      private boolean isSessionTimeout(Throwable caught){
74          //TODO: Better detection of session timeout
75      	return caught.toString().contains("Login");
76      }
77  }