]> git.mxchange.org Git - addressbook-war.git/blob
720f64305343c07f5155a2f08447e89ecee0f7db
[addressbook-war.git] /
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.addressbook.beans.user.confirmlink;
18
19 import java.text.MessageFormat;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Objects;
23 import javax.annotation.PostConstruct;
24 import javax.enterprise.context.RequestScoped;
25 import javax.enterprise.event.Event;
26 import javax.enterprise.inject.Any;
27 import javax.faces.view.facelets.FaceletException;
28 import javax.inject.Inject;
29 import javax.inject.Named;
30 import javax.naming.Context;
31 import javax.naming.InitialContext;
32 import javax.naming.NamingException;
33 import org.mxchange.addressbook.beans.BaseAddressbookController;
34 import org.mxchange.addressbook.beans.helper.AddressbookWebRequestHelperController;
35 import org.mxchange.addressbook.beans.user.AddressbookUserWebSessionController;
36 import org.mxchange.jcoreee.events.helper.clear.HelperCleanupEvent;
37 import org.mxchange.jcoreee.events.helper.clear.ObservableHelperCleanupEvent;
38 import org.mxchange.jcoreee.utils.FacesUtils;
39 import org.mxchange.jusercore.events.user.created.CreatedUserEvent;
40 import org.mxchange.jusercore.events.user.created.ObservableCreatedUserEvent;
41 import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
42 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
43 import org.mxchange.jusercore.model.user.User;
44 import org.mxchange.jusercore.model.user.UserSessionBeanRemote;
45 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
46 import org.mxchange.juserlogincore.events.confirmation.ObservableUserConfirmedAccountEvent;
47 import org.mxchange.juserlogincore.events.confirmation.UserConfirmedAccountEvent;
48
49 /**
50  * A web request bean for confirmation link handling
51  * <p>
52  * @author Roland Häder<roland@mxchange.org>
53  */
54 @Named ("userConfirmationLinkController")
55 @RequestScoped
56 public class AddressbookConfirmationLinkWebRequestBean extends BaseAddressbookController implements AddressbookConfirmationLinkWebRequestController {
57
58         /**
59          * Serial number
60          */
61         private static final long serialVersionUID = 57_637_182_796_370L;
62
63         /**
64          * Bean helper instance
65          */
66         @Inject
67         private AddressbookWebRequestHelperController beanHelper;
68
69         /**
70          * Event being fired when a bean helper should be cleaned
71          */
72         @Inject
73         @Any
74         private Event<ObservableHelperCleanupEvent> cleanHelperEvent;
75
76         /**
77          * Confirmation key
78          */
79         private String confirmationKey;
80
81         /**
82          * Remote user bean
83          */
84         private UserSessionBeanRemote userBean;
85
86         /**
87          * Event being fired when a user has confirmed the account
88          */
89         @Inject
90         @Any
91         private Event<ObservableUserConfirmedAccountEvent> userConfirmedEvent;
92
93         /**
94          * User controller
95          */
96         @Inject
97         private AddressbookUserWebSessionController userController;
98
99         /**
100          * Event for when a user instance was created
101          */
102         @Any
103         @Inject
104         private Event<ObservableCreatedUserEvent> userCreatedEvent;
105
106         /**
107          * Default constructor
108          */
109         public AddressbookConfirmationLinkWebRequestBean () {
110                 // Call super constructor
111                 super();
112         }
113
114         @Override
115         public String getConfirmationKey () {
116                 return this.confirmationKey;
117         }
118
119         @Override
120         public void setConfirmationKey (final String confirmationKey) {
121                 this.confirmationKey = confirmationKey;
122         }
123
124         /**
125          * Post-construction method
126          */
127         @PostConstruct
128         public void init () {
129                 // Try it
130                 try {
131                         // Get initial context
132                         Context context = new InitialContext();
133
134                         // Try to lookup
135                         this.userBean = (UserSessionBeanRemote) context.lookup("java:global/addressbook-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote"); //NOI18N
136                 } catch (final NamingException e) {
137                         // Throw again
138                         throw new FaceletException(e);
139                 }
140         }
141
142         @Override
143         public void maybeConfirmUserAccount () {
144                 // Trace message
145                 System.out.println(MessageFormat.format("{0}.maybeConfirmAccount: CALLED!", this.getClass().getSimpleName())); //NOI18N
146
147                 // Is the confirmation key set?
148                 if (this.getConfirmationKey() == null) {
149                         // May be null if not set
150                         return;
151                 } else if (this.getConfirmationKey().isEmpty()) {
152                         // Is empty string
153                         return;
154                 }
155
156                 // Now try to find the user in user list, first get the whole list
157                 List<User> users = this.userController.allUsers();
158
159                 // Debug message
160                 System.out.println(MessageFormat.format("{0}.maybeConfirmAccount: users.size()={1}", this.getClass().getSimpleName(), users.size())); //NOI18N
161
162                 // Get iterator from it
163                 Iterator<User> iterator = users.iterator();
164
165                 // Init instance
166                 User user = null;
167
168                 // Then loop through all
169                 while (iterator.hasNext()) {
170                         // Get next user
171                         User next = iterator.next();
172
173                         // Debug message
174                         System.out.println(MessageFormat.format("{0}.maybeConfirmAccount: this.confirmationKey={1},next.confirmationKey={2}", this.getClass().getSimpleName(), this.getConfirmationKey(), next.getUserConfirmKey())); //NOI18N
175
176                         // Same confirmation key?
177                         if (Objects.equals(this.getConfirmationKey(), next.getUserConfirmKey())) {
178                                 // Debug message
179                                 System.out.println(MessageFormat.format("{0}.maybeConfirmAccount: next={1} - Aborting ...", this.getClass().getSimpleName(), next)); //NOI18N
180
181                                 // Found it, then set it and abort loop
182                                 user = next;
183                                 break;
184                         }
185                 }
186
187                 // Debug message
188                 System.out.println(MessageFormat.format("{0}.maybeConfirmAccount: user={1}", this.getClass().getSimpleName(), user)); //NOI18N
189
190                 // Is the user instance null?
191                 if ((null == user) || (user.getUserAccountStatus() != UserAccountStatus.UNCONFIRMED)) {
192                         // Then clear this bean and the helper
193                         this.cleanHelperEvent.fire(new HelperCleanupEvent());
194                 } else {
195                         // Try to confirm it
196                         this.confirmUserAccount(user);
197                 }
198
199                 // Trace message
200                 System.out.println(MessageFormat.format("{0}.maybeConfirmAccount: EXIT!", this.getClass().getSimpleName())); //NOI18N
201         }
202
203         /**
204          * Tries to confirm the currently set user instance (in helper).
205          * <p>
206          * @param user User instance
207          */
208         private void confirmUserAccount (final User user) {
209                 // Trace message
210                 System.out.println(MessageFormat.format("{0}.confirmUserAccount: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
211
212                 // Should be set
213                 if (null == user) {
214                         // Throw NPE
215                         throw new NullPointerException("user is null"); //NOI18N
216                 } else if (user.getUserId() == null) {
217                         // Abort here
218                         throw new NullPointerException("user.userId is null"); //NOI18N
219                 } else if (user.getUserId() < 1) {
220                         // Invalid number
221                         throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
222                 } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
223                         // Account is already confirmed
224                         throw new FaceletException(new UserStatusConfirmedException(user));
225                 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
226                         // Account is already confirmed
227                         throw new FaceletException(new UserStatusLockedException(user));
228                 } else if (user.getUserConfirmKey() == null) {
229                         // Throw NPE
230                         throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
231                 } else if (user.getUserConfirmKey().isEmpty()) {
232                         // Is empty string
233                         throw new IllegalArgumentException("user.userConfirmKey is empty"); //NOI18N
234                 }
235
236                 // Updated user instance
237                 User updatedUser;
238
239                 try {
240                         // Get base URL
241                         String baseUrl = FacesUtils.generateBaseUrl();
242
243                         // Debug message
244                         System.out.println(MessageFormat.format("{0}.confirmUserAccount: baseUrl={1}", this.getClass().getSimpleName(), baseUrl)); //NOI18N
245
246                         // Confirm account
247                         updatedUser = this.userBean.confirmAccount(user, baseUrl);
248
249                         // Debug message
250                         System.out.println(MessageFormat.format("{0}.confirmUserAccount: updatedUser={1} - Returned from EJB", this.getClass().getSimpleName(), updatedUser)); //NOI18N
251                 } catch (final UserStatusConfirmedException | UserStatusLockedException ex) {
252                         // Something unexpected happened
253                         throw new FaceletException(MessageFormat.format("Cannot confirm user account {0}", user.getUserName()), ex); //NOI18N
254                 }
255
256                 // Fire event that the user has confirmed account
257                 this.userConfirmedEvent.fire(new UserConfirmedAccountEvent(updatedUser));
258
259                 // Debug message
260                 System.out.println(MessageFormat.format("{0}.confirmUserAccount: updatedUser={1}", this.getClass().getSimpleName(), updatedUser)); //NOI18N
261
262                 // Fire event
263                 this.userCreatedEvent.fire(new CreatedUserEvent(updatedUser));
264
265                 // Trace message
266                 System.out.println(MessageFormat.format("{0}.confirmUserAccount: EXIT!", this.getClass().getSimpleName())); //NOI18N
267         }
268
269 }