View Javadoc

1   /*
2    * Copyright 2007 The International Moth Class Association (IMCA)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.imca.services;
17  
18  import java.io.UnsupportedEncodingException;
19  import java.security.NoSuchAlgorithmException;
20  
21  import javax.mail.MessagingException;
22  import javax.mail.internet.AddressException;
23  import javax.mail.internet.InternetAddress;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  import net.sf.imca.model.PersonBO;
29  import net.sf.imca.model.entities.PersonEntity;
30  import net.sf.imca.model.entities.AddressEntity;
31  import net.sf.imca.model.exceptions.DataCheckingException;
32  import net.sf.imca.model.exceptions.LogInException;
33  import net.sf.imca.model.exceptions.RegistrationException;
34  
35  /**
36   * Service for login, and register use cases.
37   *
38   * @author dougculnane
39   */
40  public class LoginRegisterService extends Service {
41  
42      /**
43       * Apache Commons Logger specific to this class.
44       */
45      private Log log = LogFactory.getLog(LoginRegisterService.class);
46  
47      /**
48       * Try to login the using the user supplied parameters.
49       * 
50       * @param email
51       *            Users email address.
52       * @param password
53       *            User supplied password
54       * @return the matching person.
55       * @throws LogInException
56       *             If there is an unsuccessful login.
57       */
58      public PersonBO login(String email, String password) throws LogInException {
59  
60          PersonBO person = null;
61          try {
62              startTransaction();
63              person = new PersonBO(em, email, password);
64              try {
65                  this.endTransaction();
66              } catch (Exception ex) {
67                  log.error(ex);
68                  throw new LogInException(ex.getMessage());
69              }
70              log.info("User " + email + " loged in.");
71          } catch (LogInException lie) {
72              log.warn(lie);
73              try {
74                  this.endTransaction();
75              } catch (Exception ex) {
76                  log.error(ex);
77              }
78              throw lie;
79          }
80          return person;
81      }
82  
83      public PersonBO register(String email, String password,
84              String confirmPassword, String countryCode, 
85              String firstName, String lastName) 
86              throws RegistrationException, DataCheckingException {
87  
88          if (email == null || password == null) {
89              log.warn("Email or Password value is NULL");
90              throw new RegistrationException("Email or Password value is NULL");
91          }
92          if (email.trim().length() == 0) {
93              log.warn("Email value is empty.");
94              throw new RegistrationException("Email value is empty");
95          }
96          if (!(email.indexOf("@") > 0)) {
97              log.warn("Invalid email address: " + email);
98              throw new RegistrationException("Email Address not valid.");
99          }
100         if (password.length() < 8) {
101             log.warn("Password too short.");
102             throw new RegistrationException("Password too short");
103         }
104         if (!confirmPassword.equals(password)) {
105             log.warn("Password and confirm password are not the same.");
106             throw new RegistrationException(
107                     "Password and Confirm Password do not match");
108         }
109 
110         try {
111             new InternetAddress(email);
112         } catch (AddressException e) {
113             log.warn("Invalid email address: " + email, e);
114             throw new RegistrationException("Email Address not valid.");
115         }
116         
117         PersonBO person;
118         try {
119             startTransaction();
120             person = new PersonBO();
121             person.setEntity(new PersonEntity());
122             person.getEntity().setEmail(email);
123             person.setPasswordEncodeAndSet(password);
124             person.getEntity().setFirstName(firstName);
125             person.getEntity().setLastName(lastName);
126             if (person.getEntity().getAddress() == null) {
127                 person.getEntity().setAddress(new AddressEntity());
128             }
129             person.getEntity().getAddress().setCountryCode(countryCode);
130             person.register(em);
131             try {
132                 this.endTransaction();
133             } catch (Exception ex) {
134                 log.error(ex);
135                 throw new RegistrationException(ex.getMessage());
136             }
137         } catch (RegistrationException re) {
138             log.warn(re);
139             try {
140                 this.endTransaction();
141             } catch (Exception ex) {
142                 log.error(ex);
143                 throw new RegistrationException(ex.getMessage());
144             }
145             throw re;
146         } catch (AddressException e) {
147             log.warn("Invalid email address: " + email, e);
148             throw new RegistrationException("Email Address not valid.");
149         } catch (UnsupportedEncodingException e) {
150             log.error(e);
151             throw new RegistrationException("Error encoding password.");
152         } catch (NoSuchAlgorithmException e) {
153             log.error(e);
154             throw new RegistrationException("Error encoding password.");
155         }
156         
157         log.info("Registered new user: " + person.getName() 
158                 + " (" + email+ ")." );
159 
160         return person;
161     }
162 
163     /**
164      * Send a new reset password to the supplied email address.
165      * 
166      * @param email
167      *            email of user requesting password.
168      * @return success
169      * @throws MessagingException
170      *             If there is a problem.
171      */
172     public boolean sendPassword(String email) throws MessagingException, DataCheckingException {
173 
174         InternetAddress intAdr = new InternetAddress(email);
175 
176         try {
177             startTransaction();
178 
179             PersonBO person = new PersonBO(em, email);
180             if (person.getEntity() == null) {
181                 throw new DataCheckingException("Can not find email in database.");
182             }
183             person.sendPassword(em, intAdr);
184             log.info("Password resent and sent to: " + email);
185 
186             try {
187                 this.endTransaction();
188             } catch (Exception ex) {
189                 log.error(ex);
190                 throw new DataCheckingException(ex.getMessage());
191             }
192         } catch (MessagingException me) {
193             log.error(me);
194             try {
195                 this.endTransaction();
196             } catch (Exception ex) {
197                 log.error(ex);
198                 throw new DataCheckingException(ex.getMessage());
199             }
200             throw me;
201         }
202         return true;
203     }
204 
205 }