1

I have an EJB that handles creation of customer user accounts that needs access to a managed bean (account scoped) which manages user verification keys for user accounts (the keys are transient, so they don't need to be handled by database calls). However, I can't figure out a way to send the verification key to the EJB (which generates the verification email that is send to a user).

AccountVerifierBean.java

@ManagedBean(name = "accountVerifierBean", eager = true)
@ApplicationScoped
public class AccountVerifierBean implements Serializable {
    private HashMap<String, String> verificationKeyMapping;

    public AccountVerifierBean() {}

    public boolean verifyKey(String username, String key) {
        return key.equals(verificationKeyMapping.get(username));
    }
    public String generateKey(String username) {
        Date time = new Date();
        String key = username + time.toString();
        key = Encryption.hashSHA(key);
        verificationKeyMapping.put(username, key);
        return key;
    }
}

CustomerService.java

@Named
@Stateless
@LocalBean
public class CustomerService {
    @PersistenceContext(unitName = "MovieProject-ejbPU")
    private EntityManager em;

    private String username;
    private String password;

    //getters and setters

    public void validateEmail() {
        Properties serverConfig = new Properties();
        serverConfig.put("mail.smtp.host", "localhost");
        serverConfig.put("mail.smtp.auth", "true");
        serverConfig.put("mail.smtp.port", "25");

        try {
            Session session = Session.getInstance(serverConfig, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("<ACCOUNT>","<PASSWORD>");
                }
            });
            MimeMessage message = new MimeMessage( session );
            message.setFrom(new InternetAddress("accounts@minimalcomputers.com","VideoPile"));
            message.addRecipient(
                Message.RecipientType.TO, new InternetAddress(username)
            );
            message.setSubject("Welcome to VideoPile!");
            message.setContent("<p>Welcome to VideoPile, Please verify your email.</p><p>" + verifierKey + "</p>", "text/html; charset=utf-8"); //verifierKey is what I'm trying to get from AccountVerifierBean.
            Transport.send( message );
        }
        catch (MessagingException ex){
            Logger.getLogger(CustomerService.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (Exception e) {
            Logger.getLogger(CustomerService.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    public String encrypt(String password) {
        try {
            return new     String(Base64.encode(MessageDigest.getInstance("SHA").digest(password.getBytes())));
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(CustomerService.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
}

I've tried @Inject, @ManagedProperty, using the Application map, and using the ELContext. Nothing seems to work.

EDIT: I think there is something wrong with the bean. Any methods called from bean don't seem to do anything (EL is resolved, no bean method calls though).

I've tested the annotations that it uses (both are javax.faces.bean.*)

Phillip Huff
  • 555
  • 1
  • 6
  • 19

1 Answers1

1

So, the issue was in the AccountVerifierBean.

I added the following lines to faces-config.xml and it is now working.

<managed-bean eager="true">
  <managed-bean-name>accountVerifierBean</managed-bean-name>
  <managed-bean-class>org.Videotheque.beans.AccountVerifierBean</managed-bean-class>
  <managed-bean-scope>application</managed-bean-scope>
</managed-bean>

I'm fairly certain that the problem was because my bean needed to be in the EJB package instead of the WAR so that the EJBs can access it, but because of that, the WAR didn't know that the bean existed.

Phillip Huff
  • 555
  • 1
  • 6
  • 19
  • This configuration looks odd. Are you sure you're using JSF 2? – Luiggi Mendoza Apr 27 '13 at 02:47
  • I am, though to be honest, I have no idea on whether I'm using the proper method. All I know is that this method worked to fix my problem. – Phillip Huff Apr 27 '13 at 04:09
  • @LuiggiMendoza according to http://stackoverflow.com/questions/7583038/what-is-the-use-of-faces-config-xml-in-jsf-2, this is the proper method, just missing a description tag. – Phillip Huff Apr 27 '13 at 04:15
  • So you just dropped the annotations and move the configuration to the *faces-config.xml* file and worked? Really, your problem is too localized. – Luiggi Mendoza Apr 27 '13 at 07:55
  • @LuiggiMendoza I guess. I'm assuming it's because the annotation scanner didn't expect the bean to be in the EJB package, but I could be wrong. It's an odd error, to be sure. – Phillip Huff Apr 27 '13 at 21:05