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