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.LoginException;
import com.haulmont.cuba.security.global.UserSession; import com.haulmont.cuba.security.global.UserSession;
import com.haulmont.cuba.web.app.UserSettingsTools; 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.controllers.ControllerUtils;
import com.haulmont.cuba.web.sys.LinkHandler; import com.haulmont.cuba.web.sys.LinkHandler;
import com.haulmont.cuba.web.toolkit.ui.*; import com.haulmont.cuba.web.toolkit.ui.*;
@ -93,10 +91,7 @@ public class AppUI extends UI implements ErrorHandler, CubaHistoryControl.Histor
protected UserSessionSource userSessionSource; protected UserSessionSource userSessionSource;
@Inject @Inject
protected WebAuthConfig webAuthConfig; protected UserSessionService userSessionService;
@Inject
protected CubaAuthProvider authProvider;
protected TestIdManager testIdManager = new TestIdManager(); protected TestIdManager testIdManager = new TestIdManager();
@ -275,11 +270,10 @@ public class AppUI extends UI implements ErrorHandler, CubaHistoryControl.Histor
log.debug("Ping middleware session"); log.debug("Ping middleware session");
try { try {
UserSessionService service = AppBeans.get(UserSessionService.NAME);
UserSession session = app.getConnection().getSession(); UserSession session = app.getConnection().getSession();
if (session instanceof ClientUserSession if (session instanceof ClientUserSession
&& ((ClientUserSession) session).isAuthenticated()) { && ((ClientUserSession) session).isAuthenticated()) {
service.getUserSession(session.getId()); userSessionService.getUserSession(session.getId());
} }
} catch (Exception e) { } catch (Exception e) {
app.exceptionHandlers.handle(new com.vaadin.server.ErrorEvent(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.server.Page;
import com.vaadin.shared.ui.BorderStyle; import com.vaadin.shared.ui.BorderStyle;
import com.vaadin.shared.ui.label.ContentMode; import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.*;
import com.vaadin.ui.Button; import com.vaadin.ui.Button;
import com.vaadin.ui.Component; import com.vaadin.ui.Component;
import com.vaadin.ui.*;
import com.vaadin.ui.CssLayout; import com.vaadin.ui.CssLayout;
import com.vaadin.ui.Label; import com.vaadin.ui.Label;
import org.apache.commons.lang.BooleanUtils; import org.apache.commons.lang.BooleanUtils;
@ -1133,6 +1133,7 @@ public class WebWindowManager extends WindowManager {
VerticalLayout layout = new VerticalLayout(); VerticalLayout layout = new VerticalLayout();
layout.setStyleName("c-app-message-dialog"); layout.setStyleName("c-app-message-dialog");
layout.setSpacing(true);
if (messageType.getWidth() != null && messageType.getWidth() == AUTO_SIZE_PX) { if (messageType.getWidth() != null && messageType.getWidth() == AUTO_SIZE_PX) {
layout.setWidthUndefined(); layout.setWidthUndefined();
} }
@ -1150,25 +1151,21 @@ public class WebWindowManager extends WindowManager {
} }
layout.addComponent(messageLab); layout.addComponent(messageLab);
HorizontalLayout buttonsContainer = new HorizontalLayout();
buttonsContainer.setSpacing(true);
DialogAction action = new DialogAction(Type.OK); DialogAction action = new DialogAction(Type.OK);
Button button = WebComponentsHelper.createButton(); Button button = WebComponentsHelper.createButton();
button.setCaption(action.getCaption()); button.setCaption(action.getCaption());
button.setIcon(WebComponentsHelper.getIcon(action.getIcon())); button.setIcon(WebComponentsHelper.getIcon(action.getIcon()));
button.addStyleName(WebButton.ICON_STYLE); button.addStyleName(WebButton.ICON_STYLE);
button.addClickListener((Button.ClickListener) event -> button.addClickListener(event ->
vWindow.close() vWindow.close()
); );
buttonsContainer.addComponent(button);
button.focus(); 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; float width;
DialogParams dialogParams = getDialogParams(); DialogParams dialogParams = getDialogParams();
@ -1250,9 +1247,6 @@ public class WebWindowManager extends WindowManager {
} }
window.setContent(layout); window.setContent(layout);
HorizontalLayout actionsBar = new HorizontalLayout();
actionsBar.setHeight(-1, Unit.PIXELS);
HorizontalLayout buttonsContainer = new HorizontalLayout(); HorizontalLayout buttonsContainer = new HorizontalLayout();
buttonsContainer.setSpacing(true); buttonsContainer.setSpacing(true);
@ -1296,13 +1290,11 @@ public class WebWindowManager extends WindowManager {
((Button) buttonsContainer.getComponent(0)).focus(); ((Button) buttonsContainer.getComponent(0)).focus();
} }
actionsBar.addComponent(buttonsContainer);
layout.addComponent(messageLab); layout.addComponent(messageLab);
layout.addComponent(actionsBar); layout.addComponent(buttonsContainer);
layout.setExpandRatio(messageLab, 1); layout.setExpandRatio(messageLab, 1);
layout.setComponentAlignment(actionsBar, com.vaadin.ui.Alignment.BOTTOM_RIGHT); layout.setComponentAlignment(buttonsContainer, Alignment.BOTTOM_RIGHT);
ui.addWindow(window); ui.addWindow(window);
window.center(); window.center();

View File

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

View File

@ -16,30 +16,35 @@
*/ */
package com.haulmont.cuba.web.exception; 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.AppBeans;
import com.haulmont.cuba.core.global.Configuration;
import com.haulmont.cuba.core.global.Messages; import com.haulmont.cuba.core.global.Messages;
import com.haulmont.cuba.gui.components.Action; import com.haulmont.cuba.gui.components.DialogAction.Type;
import com.haulmont.cuba.gui.components.Component; import com.haulmont.cuba.gui.theme.ThemeConstantsManager;
import com.haulmont.cuba.gui.components.DialogAction;
import com.haulmont.cuba.gui.components.Frame;
import com.haulmont.cuba.security.global.NoUserSessionException; import com.haulmont.cuba.security.global.NoUserSessionException;
import com.haulmont.cuba.web.App; import com.haulmont.cuba.web.App;
import com.haulmont.cuba.web.AppUI;
import com.haulmont.cuba.web.Connection; import com.haulmont.cuba.web.Connection;
import com.haulmont.cuba.web.WebWindowManager;
import com.haulmont.cuba.web.controllers.ControllerUtils; 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.server.Page;
import com.vaadin.ui.Window; import com.vaadin.ui.*;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.Collection; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.stream.Collectors;
import static com.haulmont.cuba.web.gui.components.WebComponentsHelper.setClickShortcut;
/** /**
* Handles {@link NoUserSessionException}. * Handles {@link NoUserSessionException}.
*
*/ */
public class NoUserSessionHandler extends AbstractExceptionHandler { public class NoUserSessionHandler extends AbstractExceptionHandler {
@ -47,8 +52,6 @@ public class NoUserSessionHandler extends AbstractExceptionHandler {
private Locale locale; private Locale locale;
private Window noUserSessionDialog;
public NoUserSessionHandler() { public NoUserSessionHandler() {
super(NoUserSessionException.class.getName()); super(NoUserSessionException.class.getName());
@ -62,40 +65,82 @@ public class NoUserSessionHandler extends AbstractExceptionHandler {
try { try {
// we may show two or more dialogs if user pressed F5 and we have no valid user session // 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 // just remove previous dialog and show new
if (noUserSessionDialog != null) { List<Window> noUserSessionDialogs = app.getAppUI().getWindows().stream()
app.getAppUI().removeWindow(noUserSessionDialog); .filter(w -> w instanceof NoUserSessionExceptionDialog)
.collect(Collectors.toList());
for (Window dialog : noUserSessionDialogs) {
app.getAppUI().removeWindow(dialog);
} }
Messages messages = AppBeans.get(Messages.NAME); showNoUserSessionDialog(app);
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);
}
} catch (Throwable th) { } catch (Throwable th) {
log.error("Unable to handle NoUserSessionException", throwable); log.error("Unable to handle NoUserSessionException", throwable);
log.error("Exception in NoUserSessionHandler", th); log.error("Exception in NoUserSessionHandler", th);
} }
} }
private static class LoginAction extends DialogAction { protected void showNoUserSessionDialog(App app) {
protected LoginAction() { Messages messages = AppBeans.get(Messages.NAME);
super(DialogAction.Type.OK);
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 Label messageLab = new CubaLabel();
public void actionPerform(Component component) { messageLab.setWidthUndefined();
String url = ControllerUtils.getLocationWithoutParams() + "?restartApp"; 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 {
} }
} }