// Path: flow-bookstore/bookstore-ui/src/main/java/com/github/mcollovati/vertxvaadin/flowdemo/authentication/AccessControl.java
// public interface AccessControl extends Serializable {
// 
//     String ADMIN_ROLE_NAME = "admin";
//     String ADMIN_USERNAME = "admin";
// 
//     public boolean signIn(String username, String password);
// 
//     public boolean isUserSignedIn();
// 
//     public boolean isUserInRole(String role);
// 
//     public String getPrincipalName();
// }
// 
// Path: flow-bookstore/bookstore-ui/src/main/java/com/github/mcollovati/vertxvaadin/flowdemo/authentication/AccessControlFactory.java
// public class AccessControlFactory {
//     private static final AccessControlFactory INSTANCE = new AccessControlFactory();
//     private final AccessControl accessControl = new BasicAccessControl();
// 
//     private AccessControlFactory() {
//     }
// 
//     public static AccessControlFactory getInstance() {
//         return INSTANCE;
//     }
// 
//     public AccessControl createAccessControl() {
//         return accessControl;
//     }
// }
// 
// Path: flow-bookstore/bookstore-ui/src/main/java/com/github/mcollovati/vertxvaadin/flowdemo/authentication/LoginScreen.java
// @Route("Login")
// @PageTitle("Login")
// @StyleSheet("css/shared-styles.css")
// public class LoginScreen extends FlexLayout {
// 
//     private TextField username;
//     private PasswordField password;
//     private Button login;
//     private Button forgotPassword;
//     private AccessControl accessControl;
// 
//     public LoginScreen() {
//         accessControl = AccessControlFactory.getInstance().createAccessControl();
//         buildUI();
//         username.focus();
//     }
// 
//     private void buildUI() {
//         setSizeFull();
//         setClassName("login-screen");
// 
//         // login form, centered in the available part of the screen
//         Component loginForm = buildLoginForm();
// 
//         // layout to center login form when there is sufficient screen space
//         FlexLayout centeringLayout = new FlexLayout();
//         centeringLayout.setSizeFull();
//         centeringLayout.setJustifyContentMode(JustifyContentMode.CENTER);
//         centeringLayout.setAlignItems(Alignment.CENTER);
//         centeringLayout.add(loginForm);
// 
//         // information text about logging in
//         Component loginInformation = buildLoginInformation();
// 
//         add(loginInformation);
//         add(centeringLayout);
//     }
// 
//     private Component buildLoginForm() {
//         FormLayout loginForm = new FormLayout();
// 
//         loginForm.setWidth("310px");
// 
//         loginForm.addFormItem(username = new TextField(), "Username");
//         username.setWidth("15em");
//         username.setValue("admin");
//         loginForm.add(new Html("<br/>"));
//         loginForm.addFormItem(password = new PasswordField(), "Password");
//         password.setWidth("15em");
// 
//         HorizontalLayout buttons = new HorizontalLayout();
//         loginForm.add(new Html("<br/>"));
//         loginForm.add(buttons);
// 
//         buttons.add(login = new Button("Login"));
//         login.addClickListener(event -> login());
//         loginForm.getElement().addEventListener("keypress", event -> login()).setFilter("event.key == 'Enter'");
//         login.getElement().getThemeList().add("success primary");
// 
//         buttons.add(forgotPassword = new Button("Forgot password?"));
//         forgotPassword.addClickListener(event -> showNotification(new Notification("Hint: try anything")));
//         forgotPassword.getElement().getThemeList().add("tertiary");
// 
//         return loginForm;
//     }
// 
//     private Component buildLoginInformation() {
//         VerticalLayout loginInformation = new VerticalLayout();
//         loginInformation.setClassName("login-information");
// 
//         H1 loginInfoHeader = new H1("Login Information");
//         Span loginInfoText = new Span(
//                 "Log in as \"admin\" to have full access. Log in with any other username to have read-only access. For all users, any password is fine.");
//         loginInformation.add(loginInfoHeader);
//         loginInformation.add(loginInfoText);
// 
//         return loginInformation;
//     }
// 
//     private void login() {
//         login.setEnabled(false);
//         try {
//             if (accessControl.signIn(username.getValue(), password.getValue())) {
//                 getUI().get().navigate("");
//             } else {
//                 showNotification(new Notification("Login failed. " +
//                         "Please check your username and password and try again."));
//                 username.focus();
//             }
//         } finally {
//             login.setEnabled(true);
//         }
//     }
// 
//     private void showNotification(Notification notification) {
//         // keep the notification visible a little while after moving the
//         // mouse, or until clicked
//         notification.setDuration(2000);
//         notification.open();
//     }
// }


import com.vaadin.flow.server.ServiceInitEvent;
import com.vaadin.flow.server.VaadinServiceInitListener;
import com.github.mcollovati.vertxvaadin.flowdemo.authentication.AccessControl;
import com.github.mcollovati.vertxvaadin.flowdemo.authentication.AccessControlFactory;
import com.github.mcollovati.vertxvaadin.flowdemo.authentication.LoginScreen;
package com.github.mcollovati.vertxvaadin.flowdemo;


/**
 * This class is used to listen to BeforeEnter event of all UIs in order to
 * check whether a user is signed in or not before allowing entering any page.
 * It is registered in a file named
 * com.vaadin.flow.server.VaadinServiceInitListener in META-INF/services.
 */
public class BookstoreInitListener implements VaadinServiceInitListener {
    @Override
    public void serviceInit(ServiceInitEvent initEvent) {
        final AccessControl accessControl = AccessControlFactory.getInstance()
                .createAccessControl();

        initEvent.getSource().addUIInitListener(uiInitEvent -> {
            uiInitEvent.getUI().addBeforeEnterListener(enterEvent -> {
                if (!accessControl.isUserSignedIn() && !LoginScreen.class
                        .equals(enterEvent.getNavigationTarget()))
// Path: flow-bookstore/bookstore-ui/src/main/java/com/github/mcollovati/vertxvaadin/flowdemo/authentication/AccessControl.java
// public interface AccessControl extends Serializable {
// 
//     String ADMIN_ROLE_NAME = "admin";
//     String ADMIN_USERNAME = "admin";
// 
//     public boolean signIn(String username, String password);
// 
//     public boolean isUserSignedIn();
// 
//     public boolean isUserInRole(String role);
// 
//     public String getPrincipalName();
// }
// 
// Path: flow-bookstore/bookstore-ui/src/main/java/com/github/mcollovati/vertxvaadin/flowdemo/authentication/AccessControlFactory.java
// public class AccessControlFactory {
//     private static final AccessControlFactory INSTANCE = new AccessControlFactory();
//     private final AccessControl accessControl = new BasicAccessControl();
// 
//     private AccessControlFactory() {
//     }
// 
//     public static AccessControlFactory getInstance() {
//         return INSTANCE;
//     }
// 
//     public AccessControl createAccessControl() {
//         return accessControl;
//     }
// }
// 
// Path: flow-bookstore/bookstore-ui/src/main/java/com/github/mcollovati/vertxvaadin/flowdemo/authentication/LoginScreen.java
// @Route("Login")
// @PageTitle("Login")
// @StyleSheet("css/shared-styles.css")
// public class LoginScreen extends FlexLayout {
// 
//     private TextField username;
//     private PasswordField password;
//     private Button login;
//     private Button forgotPassword;
//     private AccessControl accessControl;
// 
//     public LoginScreen() {
//         accessControl = AccessControlFactory.getInstance().createAccessControl();
//         buildUI();
//         username.focus();
//     }
// 
//     private void buildUI() {
//         setSizeFull();
//         setClassName("login-screen");
// 
//         // login form, centered in the available part of the screen
//         Component loginForm = buildLoginForm();
// 
//         // layout to center login form when there is sufficient screen space
//         FlexLayout centeringLayout = new FlexLayout();
//         centeringLayout.setSizeFull();
//         centeringLayout.setJustifyContentMode(JustifyContentMode.CENTER);
//         centeringLayout.setAlignItems(Alignment.CENTER);
//         centeringLayout.add(loginForm);
// 
//         // information text about logging in
//         Component loginInformation = buildLoginInformation();
// 
//         add(loginInformation);
//         add(centeringLayout);
//     }
// 
//     private Component buildLoginForm() {
//         FormLayout loginForm = new FormLayout();
// 
//         loginForm.setWidth("310px");
// 
//         loginForm.addFormItem(username = new TextField(), "Username");
//         username.setWidth("15em");
//         username.setValue("admin");
//         loginForm.add(new Html("<br/>"));
//         loginForm.addFormItem(password = new PasswordField(), "Password");
//         password.setWidth("15em");
// 
//         HorizontalLayout buttons = new HorizontalLayout();
//         loginForm.add(new Html("<br/>"));
//         loginForm.add(buttons);
// 
//         buttons.add(login = new Button("Login"));
//         login.addClickListener(event -> login());
//         loginForm.getElement().addEventListener("keypress", event -> login()).setFilter("event.key == 'Enter'");
//         login.getElement().getThemeList().add("success primary");
// 
//         buttons.add(forgotPassword = new Button("Forgot password?"));
//         forgotPassword.addClickListener(event -> showNotification(new Notification("Hint: try anything")));
//         forgotPassword.getElement().getThemeList().add("tertiary");
// 
//         return loginForm;
//     }
// 
//     private Component buildLoginInformation() {
//         VerticalLayout loginInformation = new VerticalLayout();
//         loginInformation.setClassName("login-information");
// 
//         H1 loginInfoHeader = new H1("Login Information");
//         Span loginInfoText = new Span(
//                 "Log in as \"admin\" to have full access. Log in with any other username to have read-only access. For all users, any password is fine.");
//         loginInformation.add(loginInfoHeader);
//         loginInformation.add(loginInfoText);
// 
//         return loginInformation;
//     }
// 
//     private void login() {
//         login.setEnabled(false);
//         try {
//             if (accessControl.signIn(username.getValue(), password.getValue())) {
//                 getUI().get().navigate("");
//             } else {
//                 showNotification(new Notification("Login failed. " +
//                         "Please check your username and password and try again."));
//                 username.focus();
//             }
//         } finally {
//             login.setEnabled(true);
//         }
//     }
// 
//     private void showNotification(Notification notification) {
//         // keep the notification visible a little while after moving the
//         // mouse, or until clicked
//         notification.setDuration(2000);
//         notification.open();
//     }
// }


// Path: flow-bookstore/bookstore-ui/src/main/java/com/github/mcollovati/vertxvaadin/flowdemo/BookstoreInitListener.java
import com.vaadin.flow.server.ServiceInitEvent;
import com.vaadin.flow.server.VaadinServiceInitListener;
import com.github.mcollovati.vertxvaadin.flowdemo.authentication.AccessControl;
import com.github.mcollovati.vertxvaadin.flowdemo.authentication.AccessControlFactory;
import com.github.mcollovati.vertxvaadin.flowdemo.authentication.LoginScreen;

package com.github.mcollovati.vertxvaadin.flowdemo;


/**
 * This class is used to listen to BeforeEnter event of all UIs in order to
 * check whether a user is signed in or not before allowing entering any page.
 * It is registered in a file named
 * com.vaadin.flow.server.VaadinServiceInitListener in META-INF/services.
 */
public class BookstoreInitListener implements VaadinServiceInitListener {
    @Override
    public void serviceInit(ServiceInitEvent initEvent) {
        final AccessControl accessControl = AccessControlFactory.getInstance()
                .createAccessControl();

        initEvent.getSource().addUIInitListener(uiInitEvent -> {
            uiInitEvent.getUI().addBeforeEnterListener(enterEvent -> {
                if (!accessControl.isUserSignedIn() && !LoginScreen.class
                        .equals(enterEvent.getNavigationTarget()))
                    enterEvent.rerouteTo(LoginScreen.class);