001: package de.schlund.pfixcore.example.bank.ihandler;
002:
003: import de.schlund.pfixcore.auth.Authentication;
004: import de.schlund.pfixcore.example.bank.AuthTokenManager;
005: import de.schlund.pfixcore.example.bank.BankApplication;
006: import de.schlund.pfixcore.example.bank.context.ContextAccount;
007: import de.schlund.pfixcore.example.bank.context.ContextCustomer;
008: import de.schlund.pfixcore.example.bank.iwrapper.Login;
009: import de.schlund.pfixcore.example.bank.model.Account;
010: import de.schlund.pfixcore.example.bank.model.BankDAO;
011: import de.schlund.pfixcore.example.bank.model.Customer;
012: import de.schlund.pfixcore.generator.IHandler;
013: import de.schlund.pfixcore.generator.IWrapper;
014: import de.schlund.pfixcore.workflow.Context;
015: import de.schlund.util.statuscodes.StatusCodeLib;
016:
017: public class LoginHandler implements IHandler {
018:
019: public void handleSubmittedData(Context context, IWrapper wrapper)
020: throws Exception {
021: Login login = (Login) wrapper;
022: BankDAO bankDAO = BankApplication.getInstance().getBankDAO();
023: if (login.getCustomerID() != null) {
024: Customer customer = null;
025: try {
026: long customerId = Long.parseLong(login.getCustomerID());
027: customer = bankDAO.getCustomerById(customerId);
028: String password = login.getPassword();
029: if (password == null
030: || !password.equals(customer.getPassword()))
031: customer = null;
032: } catch (NumberFormatException x) {
033: }
034: if (customer == null) {
035: login
036: .addSCodeCustomerID(StatusCodeLib.PFIXCORE_EXAMPLE_BANK_ILLEGAL_LOGIN);
037: login
038: .addSCodePassword(StatusCodeLib.PFIXCORE_EXAMPLE_BANK_ILLEGAL_LOGIN);
039: } else {
040: ContextCustomer contextCustomer = context
041: .getContextResourceManager().getResource(
042: ContextCustomer.class);
043: contextCustomer.setCustomer(customer);
044: Authentication auth = context.getAuthentication();
045: auth.addRole("UNRESTRICTED");
046: }
047: } else if (login.getAuthToken() != null) {
048: String[] tokens = AuthTokenManager.decodeAuthToken(login
049: .getAuthToken());
050: if (tokens.length == 2) {
051: try {
052: Long cid = Long.parseLong(tokens[0]);
053: Customer customer = bankDAO.getCustomerById(cid);
054: if (customer != null) {
055: Long aid = Long.parseLong(tokens[1]);
056: Account account = customer.getAccountByNo(aid);
057: if (account != null) {
058: ContextCustomer contextCustomer = context
059: .getContextResourceManager()
060: .getResource(ContextCustomer.class);
061: contextCustomer.setCustomer(customer);
062: Authentication auth = context
063: .getAuthentication();
064: auth.addRole("ACCOUNT");
065: ContextAccount contextAccount = context
066: .getContextResourceManager()
067: .getResource(ContextAccount.class);
068: contextAccount.setAccount(account);
069: return;
070: }
071: }
072: } catch (NumberFormatException x) {
073: }
074: }
075: try {
076: Thread.sleep((long) (Math.random() * 1000));
077: } catch (InterruptedException x) {
078: }
079: throw new IllegalArgumentException("Illegal auth token.");
080: }
081: }
082:
083: public boolean isActive(Context context) throws Exception {
084: return true;
085: }
086:
087: public boolean prerequisitesMet(Context context) throws Exception {
088: return true;
089: }
090:
091: public boolean needsData(Context context) throws Exception {
092: return false;
093: }
094:
095: public void retrieveCurrentStatus(Context context, IWrapper wrapper)
096: throws Exception {
097:
098: }
099:
100: }
|