KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > web > talk > plugin > DbEndPointRegistration


1 package com.quikj.application.web.talk.plugin;
2
3 import com.quikj.server.framework.*;
4 import com.quikj.server.web.*;
5 import com.quikj.application.web.talk.messaging.*;
6
7 import java.sql.*;
8
9 public class DbEndPointRegistration implements DbOperationInterface
10 {
11     public DbEndPointRegistration(EndPointInterface endpoint, String JavaDoc username,
12             String JavaDoc password, ServiceController parent, AceSQL database)
13     {
14         this.parent = parent;
15         this.database = database;
16         endpointInfo.setEndPoint(endpoint);
17         this.username = username;
18         this.password = password;
19     }
20
21     public boolean registerEndPoint()
22     {
23         try
24         {
25             if ((password == null)
26                     && (FeatureFactory.getInstance().isFeature(username) == true))
27             {
28                 // allow feature login with no password
29

30                 Statement[] statements = UserTable
31                         .getUserElementQueryStatements(
32                                 database.getConnection(), username);
33
34                 operationId = database.executeSQL(statements, (String JavaDoc[]) null,
35                         this);
36             }
37             else
38             {
39                 Statement[] statements = UserTable
40                         .getUserElementQueryStatements(
41                                 database.getConnection(), username, password);
42
43                 operationId = database.executeSQL(statements, (String JavaDoc[]) null,
44                         this);
45             }
46
47             if (operationId == -1)
48             {
49                 lastError = parent.getErrorMessage();
50                 return false;
51             }
52
53             return true;
54         }
55         catch (SQLException ex)
56         {
57             lastError = "Error creating SQL statement : " + ex.getMessage();
58             return false;
59         }
60     }
61
62     public boolean processResponse(AceSQLMessage message) // returns
63
// done
64
// or
65
// not
66
{
67         EndPointInterface endpoint = endpointInfo.getEndPoint();
68
69         if (endpointInfo.getUserData() == null)// first
70
// time
71
// around,
72
// we're
73
// validating
74
// the
75
// user
76
{
77             if (message.getStatus() == AceSQLMessage.SQL_ERROR)
78             {
79                 AceLogger
80                         .Instance()
81                         .log(
82                                 AceLogger.ERROR,
83                                 AceLogger.SYSTEM_LOG,
84                                 parent.getName()
85                                         + "- DbEndPointRegistration.processResponse() -- Database error result authenticating user name "
86                                         + username + ".");
87
88                 // send a error response to the client
89
parent
90                         .sendRegistrationResponse(
91                                 endpoint,
92                                 AceHTTPMessage.INTERNAL_ERROR,
93                                 java.util.ResourceBundle
94                                         .getBundle(
95                                                 "com.quikj.application.web.talk.plugin.language",
96                                                 ServiceController
97                                                         .getLocale(endpoint
98                                                                 .getParam("language")))
99                                         .getString("Authentication_error"),
100                                 null, null);
101
102                 return true;
103             }
104
105             if (message.multipleResultsAvailable() == false)
106             {
107                 AceLogger
108                         .Instance()
109                         .log(
110                                 AceLogger.ERROR,
111                                 AceLogger.SYSTEM_LOG,
112                                 parent.getName()
113                                         + "- DbEndPointRegistration.processResponse() -- Multiple results not returned on user query");
114
115                 // send a error response to the client
116

117                 parent
118                         .sendRegistrationResponse(
119                                 endpoint,
120                                 AceHTTPMessage.INTERNAL_ERROR,
121                                 java.util.ResourceBundle
122                                         .getBundle(
123                                                 "com.quikj.application.web.talk.plugin.language",
124                                                 ServiceController
125                                                         .getLocale(endpoint
126                                                                 .getParam("language")))
127                                         .getString("Database_error"), null,
128                                 null);
129
130                 return true;
131             }
132
133             ResultSet[] result_sets = message.getMultipleResults();
134
135             boolean hasAssociatedGroups = false;
136
137             try
138             {
139                 if (result_sets[0].first() == false)
140                 {
141                     // send error response to the client
142
parent
143                             .sendRegistrationResponse(
144                                     endpoint,
145                                     AceHTTPMessage.FORBIDDEN,
146                                     java.util.ResourceBundle
147                                             .getBundle(
148                                                     "com.quikj.application.web.talk.plugin.language",
149                                                     ServiceController
150                                                             .getLocale(endpoint
151                                                                     .getParam("language")))
152                                             .getString(
153                                                     "User_authentication_failed"),
154                                     null, null);
155
156                     return true;
157                 }
158
159                 UserElement user_data = new UserElement();
160                 user_data.setName(username);
161
162                 endpointInfo.setUserData(user_data);
163                 UserTable.processUserElementQueryResult(user_data, result_sets);
164                 if ((user_data.numBelongsToGroups() > 0)
165                         || (user_data.numOwnsGroups() > 0))
166                 {
167                     hasAssociatedGroups = true;
168                 }
169
170                 // test code start
171
if (false)
172                 {
173                     System.out.println("user ID = " + endpointInfo.getName());
174                     System.out.println("fullname = "
175                             + endpointInfo.getUserData().getFullName());
176                     System.out.println("address = "
177                             + endpointInfo.getUserData().getAddress());
178                     System.out.println("addnl info = "
179                             + endpointInfo.getUserData().getAdditionalInfo());
180                     System.out.println("unavail xferto = "
181                             + endpointInfo.getUserData().getUnavailXferTo());
182                     System.out.println("gatekeeper = "
183                             + endpointInfo.getUserData().getGatekeeper());
184
185                     System.out.println("belongs to "
186                             + endpointInfo.getUserData().numBelongsToGroups()
187                             + " group(s) : ");
188                     String JavaDoc[] groups = endpointInfo.getUserData()
189                             .getBelongsToGroups();
190                     for (int i = 0; i < groups.length; i++)
191                     {
192                         System.out.println(" " + groups[i]);
193                     }
194
195                     System.out.println("owns "
196                             + endpointInfo.getUserData().numOwnsGroups()
197                             + " group(s) : ");
198                     groups = endpointInfo.getUserData().getOwnsGroups();
199                     for (int i = 0; i < groups.length; i++)
200                     {
201                         System.out.println(" " + groups[i]);
202                     }
203                 }
204                 //test code end
205
}
206             catch (Exception JavaDoc ex)
207             {
208                 AceLogger
209                         .Instance()
210                         .log(
211                                 AceLogger.ERROR,
212                                 AceLogger.SYSTEM_LOG,
213                                 parent.getName()
214                                         + "- DbEndPointRegistration.processResponse() -- Exception processing result : "
215                                         + ex.getMessage());
216
217                 // send a error response to the client
218

219                 parent
220                         .sendRegistrationResponse(
221                                 endpoint,
222                                 AceHTTPMessage.INTERNAL_ERROR,
223                                 java.util.ResourceBundle
224                                         .getBundle(
225                                                 "com.quikj.application.web.talk.plugin.language",
226                                                 ServiceController
227                                                         .getLocale(endpoint
228                                                                 .getParam("language")))
229                                         .getString("Database_error"), null,
230                                 null);
231
232                 return true;
233             }
234
235             if (hasAssociatedGroups == false)
236             {
237                 finished(endpoint);
238                 return true;
239             }
240
241             // get group info
242
Statement[] statements = null;
243
244             try
245             {
246                 statements = GroupTable.getGroupInfoByUserQueryStatements(
247                         database.getConnection(), username);
248             }
249             catch (SQLException ex)
250             {
251                 lastError = "Error creating SQL statement : " + ex.getMessage();
252
253                 AceLogger
254                         .Instance()
255                         .log(
256                                 AceLogger.ERROR,
257                                 AceLogger.SYSTEM_LOG,
258                                 "DbEndPointRegistration.processResponse() -- Failure getting group info for user "
259                                         + username + ", error : " + lastError);
260
261                 parent.sendRegistrationResponse(endpoint,
262                         AceHTTPMessage.INTERNAL_ERROR,
263                         "Database error (group data)", null, null);
264
265                 return true;
266             }
267
268             operationId = database
269                     .executeSQL(statements, (String JavaDoc[]) null, this);
270
271             if (operationId == -1)
272             {
273                 lastError = parent.getErrorMessage();
274
275                 AceLogger
276                         .Instance()
277                         .log(
278                                 AceLogger.ERROR,
279                                 AceLogger.SYSTEM_LOG,
280                                 "DbEndPointRegistration.processResponse() -- Failure getting group info for user "
281                                         + username + ", error : " + lastError);
282
283                 parent.sendRegistrationResponse(endpoint,
284                         AceHTTPMessage.INTERNAL_ERROR,
285                         "Database error (group data)", null, null);
286
287                 return true;
288             }
289
290             return false;
291         }
292
293         // second time around, processing group info result
294

295         if (message.getStatus() == AceSQLMessage.SQL_ERROR)
296         {
297             AceLogger
298                     .Instance()
299                     .log(
300                             AceLogger.ERROR,
301                             AceLogger.SYSTEM_LOG,
302                             parent.getName()
303                                     + "- DbEndPointRegistration.processResponse() -- Database error result getting group info for user "
304                                     + username + ".");
305
306             // send a error response to the client
307
parent.sendRegistrationResponse(endpoint,
308                     AceHTTPMessage.INTERNAL_ERROR,
309                     java.util.ResourceBundle.getBundle(
310                             "com.quikj.application.web.talk.plugin.language",
311                             ServiceController.getLocale(endpoint
312                                     .getParam("language"))).getString(
313                             "Database_error_(group_data)"), null, null);
314
315             return true;
316         }
317
318         if (message.multipleResultsAvailable() == false)
319         {
320             AceLogger
321                     .Instance()
322                     .log(
323                             AceLogger.ERROR,
324                             AceLogger.SYSTEM_LOG,
325                             parent.getName()
326                                     + "- DbEndPointRegistration.processResponse() -- Database result error getting group info for user "
327                                     + username + ".");
328
329             // send a error response to the client
330
parent.sendRegistrationResponse(endpoint,
331                     AceHTTPMessage.INTERNAL_ERROR,
332                     java.util.ResourceBundle.getBundle(
333                             "com.quikj.application.web.talk.plugin.language",
334                             ServiceController.getLocale(endpoint
335                                     .getParam("language"))).getString(
336                             "Database_error_(group_data)"), null, null);
337
338             return true;
339         }
340
341         try
342         {
343             groupInfos = GroupTable.processGroupInfoByUserQueryResult(username,
344                     message.getMultipleResults());
345
346             // test code start
347
if (false)
348             {
349                 for (int i = 0; i < groupInfos.length; i++)
350                 {
351                     GroupInfo group = groupInfos[i];
352
353                     System.out.println("GROUP NAME = " + group.getName());
354                     System.out.println(" owner = " + group.getOwner());
355                     System.out.println(" owner login notif = "
356                             + group.getGroupData()
357                                     .getOwnerLoginNotificationControl());
358                     System.out.println(" owner busy notif = "
359                             + group.getGroupData()
360                                     .getOwnerBusyNotificationControl());
361                     System.out.println(" member login notif = "
362                             + group.getGroupData()
363                                     .getMemberLoginNotificationControl());
364                     System.out.println(" member busy notif = "
365                             + group.getGroupData()
366                                     .getMemberBusyNotificationControl());
367                     System.out.print(" active user count = ");
368                     GroupInfo active_grp = GroupList.Instance().findGroup(
369                             group.getName());
370                     if (active_grp != null)
371                     {
372                         System.out.println(active_grp.getActiveUserCount() + 1);
373                     }
374                     else
375                     {
376                         System.out.println(group.getActiveUserCount() + 1);
377                     }
378
379                     String JavaDoc[] members = group.getMembers();
380                     for (int j = 0; j < members.length; j++)
381                     {
382                         System.out.println(" member = " + members[j]);
383                     }
384                 }
385             }
386         }
387         catch (Exception JavaDoc ex)
388         {
389             AceLogger
390                     .Instance()
391                     .log(
392                             AceLogger.ERROR,
393                             AceLogger.SYSTEM_LOG,
394                             parent.getName()
395                                     + "- DbEndPointRegistration.processResponse() -- Exception processing group info result : "
396                                     + ex.getMessage());
397
398             // send a error response to the client
399

400             parent.sendRegistrationResponse(endpoint,
401                     AceHTTPMessage.INTERNAL_ERROR,
402                     java.util.ResourceBundle.getBundle(
403                             "com.quikj.application.web.talk.plugin.language",
404                             ServiceController.getLocale(endpoint
405                                     .getParam("language"))).getString(
406                             "Database_error_(group_data)"), null, null);
407
408             return true;
409         }
410
411         finished(endpoint);
412         return true;
413     }
414
415     public void cancel()
416     {
417         //System.out.println("DbEndPointRegistration.cancel() called");
418
database.cancelSQL(operationId, parent);
419     }
420
421     public String JavaDoc getLastError()
422     {
423         return lastError;
424     }
425
426     public EndPointInterface getEndPoint()
427     {
428         return endpointInfo.getEndPoint();
429     }
430
431     public void finished(EndPointInterface endpoint)
432     {
433         if ((endpoint instanceof FeatureInterface) == false) // not
434
// a
435
// feature
436
{
437             boolean proceed = true;
438
439             // if a gatekeeper is specified
440
if (endpointInfo.getUserData().getGatekeeper().length() > 0)
441             {
442                 // find the gatekeeper endpoint from the registered endpoint
443
// list
444
EndPointInterface gk = EndPointList.Instance()
445                         .findRegisteredEndPoint(
446                                 endpointInfo.getUserData().getGatekeeper());
447                 if (gk != null) // present
448
{
449                     if ((gk instanceof GatekeeperInterface) == true)
450                     {
451                         proceed = ((GatekeeperInterface) gk).allow(endpoint,
452                                 endpointInfo);
453                     }
454                     else
455                     {
456                         // do not allow the end-point to proceed
457
proceed = false;
458                     }
459                 }
460                 else
461                 // gatekeeper not present
462
{
463                     // do not allow the end-point to proceed
464
proceed = false;
465                 }
466             }
467
468             if (proceed == true)
469             {
470                 // check if we have the license
471
if (AceLicenseManager.getInstance().consumeUnits(
472                         "max-registered-users", 1) == false)
473                 {
474                     proceed = false;
475                 }
476             }
477
478             if (proceed == false)
479             {
480                 // print error message
481
AceLogger
482                         .Instance()
483                         .log(
484                                 AceLogger.ERROR,
485                                 AceLogger.SYSTEM_LOG,
486                                 parent.getName()
487                                         + "- DbEndPointRegistration.finished() -- Failed to acquire license for the new user: "
488                                         + AceLicenseManager.getInstance()
489                                                 .getErrorMessage());
490
491                 parent
492                         .sendRegistrationResponse(
493                                 endpoint,
494                                 AceHTTPMessage.FORBIDDEN,
495                                 java.util.ResourceBundle
496                                         .getBundle(
497                                                 "com.quikj.application.web.talk.plugin.language",
498                                                 ServiceController
499                                                         .getLocale(endpoint
500                                                                 .getParam("language")))
501                                         .getString(
502                                                 "We_are_unabled_to_process_the_login_request_because_of_license_limitations"),
503                                 null, null);
504
505                 return;
506             }
507         }
508         // Check if the user is already registered, in that case, drop the
509
// previous session
510
// and remove the previous session from the registered user list
511
EndPointInterface old_ep = EndPointList.Instance()
512                 .findRegisteredEndPoint(endpointInfo.getName());
513         if (old_ep != null)
514         {
515             // send a messsage to the end-point to instruct it to drop-out
516
if (old_ep.sendEvent(new DropEndpointEvent()) == false)
517             {
518                 // print an error message
519
AceLogger
520                         .Instance()
521                         .log(
522                                 AceLogger.ERROR,
523                                 AceLogger.SYSTEM_LOG,
524                                 "DbEndPointRegistration.finished() (TALK) -- Could not send drop endpoint event to the endpoint "
525                                         + old_ep);
526             }
527
528             // unregister the end-point
529
ServiceController.Instance().unregisterUser(endpointInfo.getName());
530         }
531
532         if (EndPointList.Instance().addRegisteredEndPoint(endpointInfo) == false)
533         {
534             // print error message
535
AceLogger
536                     .Instance()
537                     .log(
538                             AceLogger.ERROR,
539                             AceLogger.SYSTEM_LOG,
540                             parent.getName()
541                                     + "- DbEndPointRegistration.finished() -- Could not add the endpoint "
542                                     + endpointInfo.getName()
543                                     + " to the registered endpoint list");
544
545             parent.sendRegistrationResponse(endpoint,
546                     AceHTTPMessage.INTERNAL_ERROR,
547                     java.util.ResourceBundle.getBundle(
548                             "com.quikj.application.web.talk.plugin.language",
549                             ServiceController.getLocale(endpoint
550                                     .getParam("language"))).getString(
551                             "Failed_to_add_to_the_registered_user_list"), null,
552                     null);
553
554             AceLicenseManager.getInstance().returnUnits("max-registered-users",
555                     1);
556             return;
557         }
558
559         if (groupInfos != null)
560         {
561             for (int i = 0; i < groupInfos.length; i++)
562             {
563                 GroupList.Instance().addGroup(groupInfos[i]);
564             }
565         }
566
567         RegistrationResponseMessage response = new RegistrationResponseMessage();
568
569         response.setLoginDate(new java.util.Date JavaDoc());
570         TextElement text = new TextElement();
571         text.setMessage(java.util.ResourceBundle.getBundle(
572                 "com.quikj.application.web.talk.plugin.language",
573                 ServiceController.getLocale(endpoint.getParam("language")))
574                 .getString("Hello_")
575                 + ' '
576                 + endpointInfo.getName()
577                 + java.util.ResourceBundle.getBundle(
578                         "com.quikj.application.web.talk.plugin.language",
579                         ServiceController.getLocale(endpoint
580                                 .getParam("language"))).getString(
581                         ",_welcome_to_the_Talk_Instant_Messaging_Server"));
582
583         MediaElements elements = new MediaElements();
584         elements.addMediaElement(text);
585         response.setMediaElements(elements);
586
587         CallPartyElement call_party = new CallPartyElement();
588         call_party.setName(endpointInfo.getName());
589         call_party.setFullName(endpointInfo.getUserData().getFullName());
590         call_party.setEmail(endpointInfo.getUserData().getAddress());
591         call_party.setComment(endpointInfo.getUserData().getAdditionalInfo());
592         call_party.setLanguage(endpoint.getParam("language"));
593         response.setCallPartyInfo(call_party);
594
595         // set Group(s) members status info in message
596
String JavaDoc[] group_members = EndPointList.Instance().getActiveMembers(
597                 endpointInfo.getName());
598         if (group_members != null)
599         {
600             com.quikj.application.web.talk.messaging.GroupElement group = new com.quikj.application.web.talk.messaging.GroupElement();
601             for (int i = 0; i < group_members.length; i++)
602             {
603                 EndPointInfo info = EndPointList.Instance()
604                         .findRegisteredEndPointInfo(group_members[i]);
605                 if (info == null)
606                 {
607                     AceLogger
608                             .Instance()
609                             .log(
610                                     AceLogger.ERROR,
611                                     AceLogger.SYSTEM_LOG,
612                                     parent.getName()
613                                             + "- DbEndPointRegistration.finished() -- Could not find EndPointInfo for group member "
614                                             + group_members[i]);
615
616                     continue;
617                 }
618
619                 if (info.isDnd() == false)
620                 {
621                     GroupMemberElement element = new GroupMemberElement();
622                     element.setOperation(GroupMemberElement.OPERATION_ADD_LIST);
623                     element.setUser(group_members[i]);
624                     element.setFullName(info.getUserData().getFullName());
625                     element.setCallCount(info.getCallCount());
626                     group.addElement(element);
627                 }
628             }
629
630             response.setGroup(group);
631         }
632
633         // add group info
634
com.quikj.application.web.talk.messaging.GroupList groups = new com.quikj.application.web.talk.messaging.GroupList();
635
636         // next, add canned elements specific to the group(s) this guy
637
// owns/belongs
638
if (groupInfos != null)
639         {
640             for (int i = 0; i < groupInfos.length; i++)
641             {
642                 GroupInfo group = groupInfos[i];
643                 groups.addElement(group.getName());
644             }
645         }
646
647         if (groups.numElements() > 0)
648         {
649             response.setGroupList(groups);
650         }
651
652         if (parent.sendRegistrationResponse(endpoint, AceHTTPMessage.OK, "OK",
653                 response, null) == false)
654         {
655             // remove from the list
656
EndPointList.Instance().removeRegisteredEndPoint(
657                     endpointInfo.getName());
658             AceLicenseManager.getInstance().returnUnits("max-registered-users",
659                     1);
660             return;
661         }
662
663         // create a login CDR
664
RegisteredUserLoginCDR cdr = new RegisteredUserLoginCDR(endpointInfo
665                 .getEndPoint().getIdentifier(), endpointInfo.getName());
666
667         // send the CDR to the CDR processing thread
668
ServiceController.Instance().sendCDR(cdr);
669
670         parent.groupNotifyOfAvailabilityChange(endpointInfo.getName(), true);
671     }
672
673     private EndPointInfo endpointInfo = new EndPointInfo();
674
675     private GroupInfo[] groupInfos = null;
676
677     private String JavaDoc username;
678
679     private String JavaDoc password;
680
681     private int operationId;
682
683     private String JavaDoc lastError = "";
684
685     private ServiceController parent;
686
687     private AceSQL database;
688
689 }
690
691
Popular Tags