added possibility to handle session expiration by your own

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portlets/widgets/session-checker@93510 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Massimiliano Assante 2014-03-25 11:09:12 +00:00
parent ae93c863fe
commit 5768e91010
1 changed files with 29 additions and 13 deletions

View File

@ -18,10 +18,10 @@ import com.google.gwt.user.client.ui.VerticalPanel;
public class CheckSession {
private final int MILLI_SECONDS = 45 * 1000; //45 seconds
//for css and images
private CheckSessionBundle images = GWT.create(CheckSessionBundle.class);
static {
CheckSessionBundle.INSTANCE.css().ensureInjected();
}
@ -37,10 +37,13 @@ public class CheckSession {
private String username;
private String scope;
private Timer t;
//we give the user the possibility to show the dialog or not
private boolean showSessionExpiredDialog = true;
//needed to mask the page when session id up!
private Div maskDiv = new Div();
private static CheckSession singleton;
/**
* use this method if you want to get an event when session expires
@ -71,10 +74,10 @@ public class CheckSession {
* constructor with no events launching when session expires
*/
private CheckSession() {
addMaskDiv2DOM();
maskDiv.setStyleName("div-mask");
//polling timer
t = new Timer() {
@Override
@ -83,7 +86,7 @@ public class CheckSession {
@Override
public void onSuccess(SessionInfoBean result) {
if (result != null) {
username = result.getUsername();
scope = result.getScope();
boolean userValid = ( username != null) ? true : false;
@ -92,7 +95,13 @@ public class CheckSession {
if (! (userValid && scopeValid) ) {
mask(true);
stopPolling();
showLogoutDialog();
if (showSessionExpiredDialog) {
showLogoutDialog();
} else {
if (eventBus == null)
throw new NullPointerException("Hey, it seems you chose to handle yourself session expiration "
+ "but also not to get informed about it (eventbus is null) what's the point then?");
}
if (eventBus != null) {
eventBus.fireEvent(new SessionTimeoutEvent(result));
}
@ -124,22 +133,22 @@ public class CheckSession {
else
maskDiv.getElement().getStyle().setDisplay(Display.NONE);
}
private void showLogoutDialog() {
GCubeDialog dlg = new GCubeDialog();
dlg.setText("Your Session Expired!");
VerticalPanel topPanel = new VerticalPanel();
topPanel.setPixelSize(420, 290);
topPanel.setHorizontalAlignment(HasAlignment.ALIGN_CENTER);
HTML toShow = new HTML("<div style=\"margin-top: 40px;\">"
+ "<img style=\"margin: 0; vertical-align: middle; \" src='" + images.expired().getSafeUri().asString() + "'>"
+ "</div><div style=\"font-size: 18px; height: 20px; padding-top: 20px;\">"
+ "Please <a href=\"javascript: location.reload();\">reload<a/> this page.</div>");
topPanel.add(toShow);
dlg.add(topPanel);
dlg.center();
@ -166,4 +175,11 @@ public class CheckSession {
public void stopPolling() {
t.cancel();
}
public boolean isShowSessionExpiredDialog() {
return showSessionExpiredDialog;
}
public void setShowSessionExpiredDialog(boolean showSessionExpiredDialog) {
this.showSessionExpiredDialog = showSessionExpiredDialog;
}
}