com.cerebro.provevaadin.Start.java Source code

Java tutorial

Introduction

Here is the source code for com.cerebro.provevaadin.Start.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cerebro.provevaadin;

import com.vaadin.server.VaadinService;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import java.io.InputStream;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.Ini;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author matteo
 */
class Start extends HorizontalLayout {

    Logger logger = LoggerFactory.getLogger(Start.class);

    private TextField username = new TextField("Username/Email");
    private PasswordField password = new PasswordField("Password");
    private CheckBox rememberMe = new CheckBox("Ricordati di me");
    private Button login = new Button("Login");
    private Label errorMessage = new Label("Username o password non validi");

    private TextField usernameSignIn = new TextField("Email");
    private PasswordField passwordSignIn = new PasswordField("Password");
    private PasswordField passwordConf = new PasswordField("Conferma Password");
    private TextField nome = new TextField("Nome");
    private TextField cognome = new TextField("Cognome");
    private OptionGroup sesso = new OptionGroup("Sesso");
    private TextField eta = new TextField("Et");
    private Button signIn = new Button("Iscriviti");

    public Start() {

        InputStream iniFile = VaadinServlet.getCurrent().getServletContext()
                .getResourceAsStream("/WEB-INF/shiro.ini");
        if (iniFile == null) {
            logger.error("Il file Shiro.ini non esiste");
            return;
        } else {
            logger.info("File presente");
        }
        Ini ini = new Ini();
        ini.load(iniFile);
        Factory<SecurityManager> factory = new IniSecurityManagerFactory(ini);
        SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);

        HorizontalLayout root = new HorizontalLayout();
        root.setWidth("800px");
        root.setHeight("600px");
        this.addComponent(root);
        this.setComponentAlignment(root, Alignment.MIDDLE_CENTER);

        FormLayout loginDiv = new FormLayout();
        root.addComponent(loginDiv);
        username.focus();
        errorMessage.setVisible(false);
        loginDiv.addComponents(username, password, rememberMe, login, errorMessage);

        login.addClickListener((Button.ClickEvent e) -> {
            logger.info("Pulsante: " + e.toString());
            Subject currentUser = SecurityUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(username.getValue(), password.getValue());
            token.setRememberMe(rememberMe.getValue());
            try {
                currentUser.login(token);
                if (currentUser.hasRole("firsttime")) {
                    logger.info("Configurazione parametri del primo avvio");
                    getUI().setContent(new FirstTime());
                } else {
                    logger.info("Classe di gioco principale");
                    getUI().setContent(new Game());
                    VaadinService.reinitializeSession(VaadinService.getCurrentRequest());
                }
            } catch (Exception ex) {
                logger.error("Errore nel login: " + ex.getMessage());
                username.setValue("");
                password.setValue("");
                errorMessage.setVisible(true);
            }
        });

        FormLayout signInForm = new FormLayout();
        root.addComponent(signInForm);
        usernameSignIn.focus();
        sesso.addItems("Maschio", "Femmina");
        signInForm.addComponents(usernameSignIn, passwordSignIn, passwordConf, nome, cognome, sesso, eta, signIn);
        signIn.addClickListener((Button.ClickEvent event) -> {
            logger.info("Iscrizione al sito");
            User utente = new User();
            utente.setEmail(usernameSignIn.getValue());
            utente.setPassword(passwordSignIn.getValue());
            utente.setNomeUtente(nome.getValue());
            utente.setCognomeUtente(cognome.getValue());
            utente.setSessoUtente(sesso.getValue().toString());
            utente.setDataNascitaUtente(eta.getValue());
            SignIn signInWindow = new SignIn(utente);
            signInWindow.center();
            UI.getCurrent().addWindow(signInWindow);
        });

    }

}