Example usage for com.vaadin.server VaadinServlet getCurrent

List of usage examples for com.vaadin.server VaadinServlet getCurrent

Introduction

In this page you can find the example usage for com.vaadin.server VaadinServlet getCurrent.

Prototype

public static VaadinServlet getCurrent() 

Source Link

Document

Gets the currently used Vaadin servlet.

Usage

From source file:com.cerebro.cable.xforce.DbHelper.DbHelper.java

public DbHelper() {

    Properties props = new Properties();
    InputStream config = VaadinServlet.getCurrent().getServletContext()
            .getResourceAsStream("/WEB-INF/config.properties");
    if (config != null) {
        try {/*from  w w w. ja v  a2s.c o  m*/
            logger.info("Carico il file .properties");
            props.load(config);
        } catch (Exception ex) {
            logger.error("Errore nel caricamento del file .properties: " + ex.getMessage());
        }
    }
    db_host = props.getProperty(Costants.DB_HOST);
    db_port = props.getProperty(Costants.DB_PORT);
    db_database = props.getProperty(Costants.DB_DATABASE);
    db_username = props.getProperty(Costants.DB_USERNAME);
    db_password = props.getProperty(Costants.DB_PASSWORD);
}

From source file:com.cerebro.gorgone.boot.SecuritySystem.java

public SecuritySystem() {
    Ini ini = new Ini();
    InputStream shiroIni = VaadinServlet.getCurrent().getServletContext()
            .getResourceAsStream("/WEB-INF/shiro.ini");
    ini.load(shiroIni);/*w ww  .  ja v a  2  s .  c o m*/
    Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory(ini);
    org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
    SecurityUtils.setSecurityManager(securityManager);
}

From source file:com.cerebro.gorgone.commons.SendConfEmail.java

public SendConfEmail(String address, String confCode) {

    logger.info("Invio della mail di conferma");
    // Leggo i parametri di invio
    Properties props = new Properties();
    InputStream config = VaadinServlet.getCurrent().getServletContext()
            .getResourceAsStream("/WEB-INF/config.properties");
    if (config != null) {
        try {// w  w w .j a va2 s. com
            logger.info("Carico il file .properties");
            props.load(config);
        } catch (Exception ex) {
            logger.error("Errore nel caricamento del file .properties: " + ex.getMessage());
        }
    }
    smtp_host = props.getProperty(ConfigProperties.SMTP_HOST);
    smtp_port = Integer.getInteger(props.getProperty(ConfigProperties.SMTP_PORT));
    smtp_user = props.getProperty(ConfigProperties.SMTP_USER);
    smtp_pwd = props.getProperty(ConfigProperties.SMTP_PWD);
    smtp_security = Boolean.parseBoolean(ConfigProperties.SMTP_SECURITY);
    // Creo la mail
    HtmlEmail email = new HtmlEmail();
    try {
        email.setHostName(smtp_host);
        email.setSmtpPort(smtp_port);
        email.setSSLOnConnect(smtp_security);
        email.setAuthentication(smtp_user, smtp_pwd);
        email.setFrom("gioco@gioco.com", "Gioco");
        email.setSubject("Conferma il tuo indirizzo email " + confCode);
        email.addTo(address);
        email.setMsg("Messaggio della mail");
        email.send();
        logger.info("Email inviata");
    } catch (EmailException ex) {
        logger.error("Errore nell'invio della mail");
        logger.error(ex.getMessage());
    }
}

From source file:com.cerebro.gorgone.database.DbHelper.java

public DbHelper() {

    Properties props = new Properties();
    InputStream config = VaadinServlet.getCurrent().getServletContext()
            .getResourceAsStream("/WEB-INF/config.properties");
    if (config != null) {
        try {/*from www.j  av  a2s .co  m*/
            logger.info("Carico il file .properties");
            props.load(config);
        } catch (Exception ex) {
            logger.error("Errore nel caricamento del file .properties: " + ex.getMessage());
        }
    }
    db_host = props.getProperty(ConfigProperties.DB_HOST);
    db_port = props.getProperty(ConfigProperties.DB_PORT);
    db_database = props.getProperty(ConfigProperties.DB_DATABASE);
    db_username = props.getProperty(ConfigProperties.DB_USERNAME);
    db_password = props.getProperty(ConfigProperties.DB_PASSWORD);
}

From source file:com.cerebro.gorgone.landingpage.Login.java

public Login() {
    InputStream iniFile = VaadinServlet.getCurrent().getServletContext()
            .getResourceAsStream("/WEB-INF/shiro.ini");
    if (iniFile == null) {
        logger.error("Il file Shiro.ini non esiste");
        return;//from w w  w.j a v  a2s. c  o m
    } else {
        logger.info("File Shiro.ini presente");
    }
    Ini ini = new Ini();
    ini.load(iniFile);
    Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory(ini);
    org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
    SecurityUtils.setSecurityManager(securityManager);

    email.focus();
    errorMessage.setVisible(false);
    this.addComponents(email, password, errorMessage, rememberMe, loginB);

    loginB.addClickListener((Button.ClickEvent e) -> {
        logger.info("Tentativo di connessione");
        Subject currentUser = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(email.getValue(), password.getValue());
        token.setRememberMe(rememberMe.getValue());
        try {
            currentUser.login(token);
            Session session = currentUser.getSession();
            User user = new User();
            user.loadUser();
            //session.setAttribute("User", user);
            getUI().setContent(new Game());
            VaadinService.reinitializeSession(VaadinService.getCurrentRequest());
        } catch (Exception ex) {
            logger.error(ex.toString());
            email.setValue("");
            password.setValue("");
            errorMessage.setVisible(true);
        }
    });
}

From source file:com.cerebro.provevaadin.CreateShiroIni.java

public CreateShiroIni() {

    File sourceShiroIni = new File(getClass().getResource("shiro.ini").getFile());
    File targetShiroIni = new File(
            VaadinServlet.getCurrent().getServletConfig().getServletContext().getRealPath("WEB-INF")
                    + "/shiro.ini");
    try {//from   ww  w  .  j  a va  2 s  . c om
        Files.copy(sourceShiroIni, targetShiroIni);
    } catch (IOException ex) {
        logger.equals(ex.getMessage());
    }

}

From source file:com.cerebro.provevaadin.database.Configurazione.java

public Configurazione() {

    System.out.println("Componente per la personalizzazione");

    this.setMargin(true);

    TextField db_host = new TextField("Database Host Name");
    db_host.isRequired();//from   w ww  .  j a va  2s .c  o  m
    TextField db_port = new TextField("Database Port");
    db_port.isRequired();
    TextField db_user = new TextField("Username");
    db_user.isRequired();
    PasswordField db_pwd = new PasswordField("Password");
    db_pwd.isRequired();
    PasswordField pwd_conf = new PasswordField("Conferma Password");
    pwd_conf.setImmediate(true);

    Properties props = new Properties();
    InputStream config = VaadinServlet.getCurrent().getServletContext()
            .getResourceAsStream("/WEB-INF/config.properties");
    if (config != null) {
        System.out.println("Carico file di configurazione");
        try {
            props.load(config);
        } catch (IOException ex) {
            Logger.getLogger(MySQLServer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    db_host.setValue(props.getProperty("db_host"));
    db_user.setValue(props.getProperty("db_user"));

    Button salva = new Button("Salva");
    salva.addClickListener((Button.ClickEvent event) -> {
        System.out.println("Salvo i parametri");
        if (db_host.isValid() && db_port.isValid() && db_user.isValid() && db_pwd.isValid()
                && pwd_conf.getValue().equals(db_pwd.getValue())) {
            String dbHost = "jdbc:mysql://" + db_host.getValue() + ":" + db_port.getValue()
                    + "/grizzly?useSSL=false&serverTimezone=UTC";
            props.setProperty("db_host", dbHost);
            props.setProperty("db_user", db_user.getValue());
            props.setProperty("db_pwd", db_pwd.getValue());
            String webInfPath = VaadinServlet.getCurrent().getServletConfig().getServletContext()
                    .getRealPath("WEB-INF");
            File f = new File(webInfPath + "/config.properties");
            try {
                OutputStream o = new FileOutputStream(f);
                try {
                    props.store(o, "Prova");
                } catch (IOException ex) {
                    Logger.getLogger(Configurazione.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Configurazione.class.getName()).log(Level.SEVERE, null, ex);
            }
            Notification.show("Parametri salvati");
        } else {
            Notification.show("Ricontrolla i parametri");
        }
    });

    Button test = new Button("Test per i parametri salvati");
    test.addClickListener((Button.ClickEvent event) -> {
        System.out.println("Testo i parametri");
        MySQLServer mysql = new MySQLServer();
        JDBCConnectionPool conn = mysql.getJDBCConnPool();
        if (!mysql.testJDBCConnPool(conn)) {
            Notification.show("Errore nella connessione al database");
        } else {
            Notification.show("Connessione avvenuta con successo");
        }
    });

    this.addComponents(db_host, db_port, db_user, db_pwd, pwd_conf, salva, test);

}

From source file:com.cerebro.provevaadin.database.MySQLServer.java

public MySQLServer() {

    //        String config_path = VaadinServlet.getCurrent().getServletContext().getResourceAsStream(db_pwd)
    //                "/WEB-INF/config.properties";
    Properties props = new Properties();
    InputStream config = VaadinServlet.getCurrent().getServletContext()
            .getResourceAsStream("/WEB-INF/config.properties");

    //        try {
    //            config = new FileInputStream(config_path);
    //            
    //        } catch (FileNotFoundException ex) {
    //            Logger.getLogger(MySQLServer.class.getName()).log(Level.SEVERE, null, ex);
    //        }/*from  ww  w  .j  av  a  2  s  . c  o  m*/

    if (config != null) {
        try {
            props.load(config);
        } catch (IOException ex) {
            Logger.getLogger(MySQLServer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    System.out.println(props.getProperty("db_pwd"));
    db_host = props.getProperty("db_host");
    db_user = props.getProperty("db_user");
    db_pwd = props.getProperty("db_pwd");

}

From source file:com.cerebro.provevaadin.smtp.ConfigurazioneSMTP.java

public ConfigurazioneSMTP() {

    this.setMargin(true);

    TextField smtpHost = new TextField("SMTP Host Server");
    smtpHost.setRequired(true);/*from  www.java2 s.c  o m*/
    TextField smtpPort = new TextField("SMTP Port");
    smtpPort.setRequired(true);
    TextField smtpUser = new TextField("SMTP Username");
    smtpUser.setRequired(true);
    TextField smtpPwd = new TextField("SMTP Password");
    smtpPwd.setRequired(true);
    TextField pwdConf = new TextField("Conferma la Password");
    pwdConf.setRequired(true);
    CheckBox security = new CheckBox("Sicurezza del server");

    Properties props = new Properties();
    InputStream config = VaadinServlet.getCurrent().getServletContext()
            .getResourceAsStream("/WEB-INF/config.properties");
    if (config != null) {
        System.out.println("Carico file di configurazione");
        try {
            props.load(config);
        } catch (IOException ex) {
            Logger.getLogger(ConfigurazioneSMTP.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    smtpHost.setValue(props.getProperty("smtp_host"));
    smtpUser.setValue(props.getProperty("smtp_user"));
    security.setValue(Boolean.parseBoolean(props.getProperty("smtp_sec")));

    Button salva = new Button("Salva i parametri");
    salva.addClickListener((Button.ClickEvent event) -> {
        System.out.println("Salvo i parametri SMTP");
        if (smtpHost.isValid() && smtpPort.isValid() && smtpUser.isValid() && smtpPwd.isValid()
                && smtpPwd.getValue().equals(pwdConf.getValue())) {
            props.setProperty("smtp_host", smtpHost.getValue());
            props.setProperty("smtp_port", smtpPort.getValue());
            props.setProperty("smtp_user", smtpUser.getValue());
            props.setProperty("smtp_pwd", smtpPwd.getValue());
            props.setProperty("smtp_sec", security.getValue().toString());
            String webInfPath = VaadinServlet.getCurrent().getServletConfig().getServletContext()
                    .getRealPath("WEB-INF");
            File f = new File(webInfPath + "/config.properties");
            try {
                OutputStream o = new FileOutputStream(f);
                try {
                    props.store(o, "Prova");
                } catch (IOException ex) {
                    Logger.getLogger(ConfigurazioneSMTP.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (FileNotFoundException ex) {
                Logger.getLogger(ConfigurazioneSMTP.class.getName()).log(Level.SEVERE, null, ex);
            }
            Notification.show("Parametri salvati");
        } else {
            Notification.show("Ricontrolla i parametri");
        }

    });

    TextField emailTest = new TextField("Destinatario Mail di Prova");
    emailTest.setImmediate(true);
    emailTest.addValidator(new EmailValidator("Mail non valida"));

    Button test = new Button("Invia una mail di prova");
    test.addClickListener((Button.ClickEvent event) -> {
        System.out.println("Invio della mail di prova");
        if (emailTest.isValid()) {
            try {
                System.out.println("Invio mail di prova a " + emailTest.getValue());
                HtmlEmail email = new HtmlEmail();
                email.setHostName(props.getProperty("smtp_host"));
                email.setSmtpPort(Integer.parseInt(props.getProperty("smtp_port")));
                email.setSSLOnConnect(Boolean.parseBoolean(props.getProperty("smtp_sec")));
                email.setAuthentication(props.getProperty("smtp_user"), props.getProperty("smtp_pwd"));
                email.setFrom("prova@prova.it");
                email.setSubject("Mail di prova");
                email.addTo(emailTest.getValue());
                email.setHtmlMsg("This is the message");
                email.send();
            } catch (EmailException ex) {
                Logger.getLogger(ConfigurazioneSMTP.class.getName()).log(Level.SEVERE, null, ex);
            }

        } else {
            Notification.show("Controlla l'indirizzo mail del destinatario");
        }
    });

    this.addComponents(smtpHost, smtpPort, smtpUser, smtpPwd, pwdConf, security, salva, emailTest, test);

}

From source file:com.cerebro.provevaadin.smtp.ConfigurazioneSMTPSpring.java

public ConfigurazioneSMTPSpring() {

    TextField smtpHost = new TextField("SMTP Host Server");
    smtpHost.setRequired(true);/*from   w  ww.jav  a 2  s.  c o m*/
    TextField smtpPort = new TextField("SMTP Port");
    smtpPort.setRequired(true);
    TextField smtpUser = new TextField("SMTP Username");
    smtpUser.setRequired(true);
    PasswordField smtpPwd = new PasswordField("SMTP Password");
    smtpPwd.setRequired(true);
    PasswordField pwdConf = new PasswordField("Conferma la Password");
    pwdConf.setRequired(true);
    CheckBox security = new CheckBox("Sicurezza del server");

    Properties props = new Properties();
    InputStream config = VaadinServlet.getCurrent().getServletContext()
            .getResourceAsStream("/WEB-INF/config.properties");
    if (config != null) {
        System.out.println("Carico file di configurazione");
        try {
            props.load(config);
        } catch (IOException ex) {
            Logger.getLogger(ConfigurazioneSMTP.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    smtpHost.setValue(props.getProperty("mail.smtp.host"));
    smtpUser.setValue(props.getProperty("smtp_user"));
    security.setValue(Boolean.parseBoolean(props.getProperty("smtp_sec")));

    Button salva = new Button("Salva i parametri");
    salva.addClickListener((Button.ClickEvent event) -> {
        System.out.println("Salvo i parametri SMTP");
        if (smtpHost.isValid() && smtpPort.isValid() && smtpUser.isValid() && smtpPwd.isValid()
                && smtpPwd.getValue().equals(pwdConf.getValue())) {
            System.out.println(smtpHost.getValue() + smtpPort.getValue() + smtpUser.getValue()
                    + smtpPwd.getValue() + security.getValue().toString());
            props.setProperty("mail.smtp.host", smtpHost.getValue());
            props.setProperty("mail.smtp.port", smtpPort.getValue());
            props.setProperty("smtp_user", smtpUser.getValue());
            props.setProperty("smtp_pwd", smtpPwd.getValue());
            props.setProperty("mail.smtp.ssl.enable", security.getValue().toString());
            String webInfPath = VaadinServlet.getCurrent().getServletConfig().getServletContext()
                    .getRealPath("WEB-INF");
            File f = new File(webInfPath + "/config.properties");
            try {
                OutputStream o = new FileOutputStream(f);
                try {
                    props.store(o, "Prova");
                } catch (IOException ex) {
                    Logger.getLogger(ConfigurazioneSMTP.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (FileNotFoundException ex) {
                Logger.getLogger(ConfigurazioneSMTP.class.getName()).log(Level.SEVERE, null, ex);
            }
            Notification.show("Parametri salvati");
        } else {
            Notification.show("Ricontrolla i parametri");
        }

    });

    TextField emailTest = new TextField("Destinatario Mail di Prova");
    emailTest.setImmediate(true);
    emailTest.addValidator(new EmailValidator("Mail non valida"));

    Button test = new Button("Invia una mail di prova");
    test.addClickListener((Button.ClickEvent event) -> {
        System.out.println("Invio della mail di prova");
        if (emailTest.isValid() && !emailTest.isEmpty()) {
            System.out.println("Invio mail di prova a " + emailTest.getValue());
            JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
            mailSender.setJavaMailProperties(props);
            mailSender.setUsername(props.getProperty("smtp_user"));
            mailSender.setPassword(props.getProperty("smtp_pwd"));
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message);
            try {
                helper.setFrom("dottmatteocasagrande@gmail.com");
                helper.setSubject("Subject");
                helper.setText("It works!");
                helper.addTo(emailTest.getValue());
                mailSender.send(message);
            } catch (MessagingException ex) {
                Logger.getLogger(ConfigurazioneSMTPSpring.class.getName()).log(Level.SEVERE, null, ex);
            }

        } else {
            Notification.show("Controlla l'indirizzo mail del destinatario");
        }
    });

    this.addComponents(smtpHost, smtpPort, smtpUser, smtpPwd, pwdConf, security, salva, emailTest, test);

}