PL-8849 No UI warning because NPE is thrown in web NoUserSessionHandler

This commit is contained in:
Yuriy Artamonov 2017-04-28 15:04:35 +04:00
parent 194943370c
commit 7cc857e524
4 changed files with 90 additions and 59 deletions

View File

@ -27,8 +27,6 @@ import com.haulmont.cuba.security.app.UserSessionService;
import com.haulmont.cuba.security.global.LoginException;
import com.haulmont.cuba.security.global.UserSession;
import com.haulmont.cuba.web.app.UserSettingsTools;
import com.haulmont.cuba.web.auth.CubaAuthProvider;
import com.haulmont.cuba.web.auth.WebAuthConfig;
import com.haulmont.cuba.web.controllers.ControllerUtils;
import com.haulmont.cuba.web.sys.LinkHandler;
import com.haulmont.cuba.web.toolkit.ui.*;
@ -93,10 +91,7 @@ public class AppUI extends UI implements ErrorHandler, CubaHistoryControl.Histor
protected UserSessionSource userSessionSource;
@Inject
protected WebAuthConfig webAuthConfig;
@Inject
protected CubaAuthProvider authProvider;
protected UserSessionService userSessionService;
protected TestIdManager testIdManager = new TestIdManager();
@ -275,11 +270,10 @@ public class AppUI extends UI implements ErrorHandler, CubaHistoryControl.Histor
log.debug("Ping middleware session");
try {
UserSessionService service = AppBeans.get(UserSessionService.NAME);
UserSession session = app.getConnection().getSession();
if (session instanceof ClientUserSession
&& ((ClientUserSession) session).isAuthenticated()) {
service.getUserSession(session.getId());
userSessionService.getUserSession(session.getId());
}
} catch (Exception e) {
app.exceptionHandlers.handle(new com.vaadin.server.ErrorEvent(e));

View File

@ -59,9 +59,9 @@ import com.vaadin.event.ShortcutListener;
import com.vaadin.server.Page;
import com.vaadin.shared.ui.BorderStyle;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.*;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.*;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.Label;
import org.apache.commons.lang.BooleanUtils;
@ -1133,6 +1133,7 @@ public class WebWindowManager extends WindowManager {
VerticalLayout layout = new VerticalLayout();
layout.setStyleName("c-app-message-dialog");
layout.setSpacing(true);
if (messageType.getWidth() != null && messageType.getWidth() == AUTO_SIZE_PX) {
layout.setWidthUndefined();
}
@ -1150,25 +1151,21 @@ public class WebWindowManager extends WindowManager {
}
layout.addComponent(messageLab);
HorizontalLayout buttonsContainer = new HorizontalLayout();
buttonsContainer.setSpacing(true);
DialogAction action = new DialogAction(Type.OK);
Button button = WebComponentsHelper.createButton();
button.setCaption(action.getCaption());
button.setIcon(WebComponentsHelper.getIcon(action.getIcon()));
button.addStyleName(WebButton.ICON_STYLE);
button.addClickListener((Button.ClickListener) event ->
button.addClickListener(event ->
vWindow.close()
);
buttonsContainer.addComponent(button);
button.focus();
layout.addComponent(buttonsContainer);
layout.addComponent(button);
layout.setComponentAlignment(buttonsContainer, com.vaadin.ui.Alignment.BOTTOM_RIGHT);
layout.setComponentAlignment(button, Alignment.BOTTOM_RIGHT);
float width;
DialogParams dialogParams = getDialogParams();
@ -1250,9 +1247,6 @@ public class WebWindowManager extends WindowManager {
}
window.setContent(layout);
HorizontalLayout actionsBar = new HorizontalLayout();
actionsBar.setHeight(-1, Unit.PIXELS);
HorizontalLayout buttonsContainer = new HorizontalLayout();
buttonsContainer.setSpacing(true);
@ -1296,13 +1290,11 @@ public class WebWindowManager extends WindowManager {
((Button) buttonsContainer.getComponent(0)).focus();
}
actionsBar.addComponent(buttonsContainer);
layout.addComponent(messageLab);
layout.addComponent(actionsBar);
layout.addComponent(buttonsContainer);
layout.setExpandRatio(messageLab, 1);
layout.setComponentAlignment(actionsBar, com.vaadin.ui.Alignment.BOTTOM_RIGHT);
layout.setComponentAlignment(buttonsContainer, Alignment.BOTTOM_RIGHT);
ui.addWindow(window);
window.center();

View File

@ -40,7 +40,7 @@ import java.util.*;
*/
public class ExceptionHandlers {
private static Logger log = LoggerFactory.getLogger(ExceptionHandlers.class);
private static final Logger log = LoggerFactory.getLogger(ExceptionHandlers.class);
protected App app;
@ -136,7 +136,7 @@ public class ExceptionHandlers {
Map<String, GenericExceptionHandler> handlerMap = AppBeans.getAll(GenericExceptionHandler.class);
List<GenericExceptionHandler> handlers = new ArrayList<>(handlerMap.values());
Collections.sort(handlers, new OrderComparator());
handlers.sort(new OrderComparator());
for (GenericExceptionHandler handler : handlers) {
addHandler(handler);

View File

@ -16,30 +16,35 @@
*/
package com.haulmont.cuba.web.exception;
import com.google.common.collect.Iterables;
import com.haulmont.cuba.client.ClientConfig;
import com.haulmont.cuba.core.global.AppBeans;
import com.haulmont.cuba.core.global.Configuration;
import com.haulmont.cuba.core.global.Messages;
import com.haulmont.cuba.gui.components.Action;
import com.haulmont.cuba.gui.components.Component;
import com.haulmont.cuba.gui.components.DialogAction;
import com.haulmont.cuba.gui.components.Frame;
import com.haulmont.cuba.gui.components.DialogAction.Type;
import com.haulmont.cuba.gui.theme.ThemeConstantsManager;
import com.haulmont.cuba.security.global.NoUserSessionException;
import com.haulmont.cuba.web.App;
import com.haulmont.cuba.web.AppUI;
import com.haulmont.cuba.web.Connection;
import com.haulmont.cuba.web.WebWindowManager;
import com.haulmont.cuba.web.controllers.ControllerUtils;
import com.haulmont.cuba.web.gui.components.WebButton;
import com.haulmont.cuba.web.gui.components.WebComponentsHelper;
import com.haulmont.cuba.web.toolkit.ui.CubaLabel;
import com.haulmont.cuba.web.toolkit.ui.CubaWindow;
import com.vaadin.server.Page;
import com.vaadin.ui.Window;
import com.vaadin.ui.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import static com.haulmont.cuba.web.gui.components.WebComponentsHelper.setClickShortcut;
/**
* Handles {@link NoUserSessionException}.
*
*/
public class NoUserSessionHandler extends AbstractExceptionHandler {
@ -47,8 +52,6 @@ public class NoUserSessionHandler extends AbstractExceptionHandler {
private Locale locale;
private Window noUserSessionDialog;
public NoUserSessionHandler() {
super(NoUserSessionException.class.getName());
@ -62,40 +65,82 @@ public class NoUserSessionHandler extends AbstractExceptionHandler {
try {
// we may show two or more dialogs if user pressed F5 and we have no valid user session
// just remove previous dialog and show new
if (noUserSessionDialog != null) {
app.getAppUI().removeWindow(noUserSessionDialog);
List<Window> noUserSessionDialogs = app.getAppUI().getWindows().stream()
.filter(w -> w instanceof NoUserSessionExceptionDialog)
.collect(Collectors.toList());
for (Window dialog : noUserSessionDialogs) {
app.getAppUI().removeWindow(dialog);
}
Messages messages = AppBeans.get(Messages.NAME);
WebWindowManager wm = app.getWindowManager();
wm.showOptionDialog(
messages.getMainMessage("dialogs.Information", locale),
messages.getMainMessage("noUserSession.message", locale),
Frame.MessageType.CONFIRMATION,
new Action[]{new LoginAction()}
);
Collection<Window> windows = app.getAppUI().getWindows();
if (!windows.isEmpty()) {
noUserSessionDialog = Iterables.getLast(windows);
}
showNoUserSessionDialog(app);
} catch (Throwable th) {
log.error("Unable to handle NoUserSessionException", throwable);
log.error("Exception in NoUserSessionHandler", th);
}
}
private static class LoginAction extends DialogAction {
protected LoginAction() {
super(DialogAction.Type.OK);
protected void showNoUserSessionDialog(App app) {
Messages messages = AppBeans.get(Messages.NAME);
Window dialog = new NoUserSessionExceptionDialog();
dialog.setStyleName("c-nousersession-dialog");
dialog.setCaption(messages.getMainMessage("dialogs.Information", locale));
dialog.setClosable(false);
dialog.setResizable(false);
dialog.setModal(true);
AppUI ui = app.getAppUI();
if (ui.isTestMode()) {
dialog.setCubaId("optionDialog");
dialog.setId(ui.getTestIdManager().getTestId("optionDialog"));
}
@Override
public void actionPerform(Component component) {
String url = ControllerUtils.getLocationWithoutParams() + "?restartApp";
Label messageLab = new CubaLabel();
messageLab.setWidthUndefined();
messageLab.setValue(messages.getMainMessage("noUserSession.message", locale));
Page.getCurrent().open(url, "_self");
VerticalLayout layout = new VerticalLayout();
layout.setSpacing(true);
layout.setWidthUndefined();
layout.setStyleName("c-nousersession-dialog-layout");
layout.setSpacing(true);
dialog.setContent(layout);
Button reloginBtn = new Button();
if (ui.isTestMode()) {
reloginBtn.setCubaId("reloginBtn");
reloginBtn.setId(ui.getTestIdManager().getTestId("reloginBtn"));
}
reloginBtn.addStyleName(WebButton.ICON_STYLE);
reloginBtn.addStyleName("c-primary-action");
reloginBtn.addClickListener(event -> relogin());
reloginBtn.setCaption(messages.getMainMessage(Type.OK.getMsgKey()));
ThemeConstantsManager thCM = AppBeans.get(ThemeConstantsManager.NAME);
String iconName = thCM.getThemeValue(Type.OK.getIconKey());
reloginBtn.setIcon(WebComponentsHelper.getIcon(iconName));
ClientConfig clientConfig = AppBeans.get(Configuration.class).getConfig(ClientConfig.class);
setClickShortcut(reloginBtn, clientConfig.getCommitShortcut());
reloginBtn.focus();
layout.addComponent(messageLab);
layout.addComponent(reloginBtn);
layout.setComponentAlignment(reloginBtn, Alignment.BOTTOM_RIGHT);
ui.addWindow(dialog);
dialog.center();
}
protected void relogin() {
String url = ControllerUtils.getLocationWithoutParams() + "?restartApp";
Page.getCurrent().open(url, "_self");
}
public static class NoUserSessionExceptionDialog extends CubaWindow {
}
}