-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
MUCRoomController.java
707 lines (632 loc) · 29.6 KB
/
MUCRoomController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
package org.jivesoftware.openfire.plugin.rest.controller;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Iterator;
import java.util.Set;
import java.util.HashSet;
import java.util.stream.Collectors;
import javax.ws.rs.core.Response;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.cluster.ClusterManager;
import org.jivesoftware.openfire.container.PluginManager;
import org.jivesoftware.openfire.group.ConcurrentGroupList;
import org.jivesoftware.openfire.group.Group;
import org.jivesoftware.openfire.muc.*;
import org.jivesoftware.openfire.muc.cluster.RoomUpdatedEvent;
import org.jivesoftware.openfire.muc.cluster.RoomRemovedEvent;
import org.jivesoftware.openfire.muc.spi.LocalMUCRoom;
import org.jivesoftware.openfire.plugin.rest.RESTServicePlugin;
import org.jivesoftware.openfire.plugin.rest.entity.*;
import org.jivesoftware.openfire.plugin.rest.exceptions.ExceptionType;
import org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException;
import org.jivesoftware.openfire.plugin.rest.utils.MUCRoomUtils;
import org.jivesoftware.openfire.plugin.rest.utils.UserUtils;
import org.jivesoftware.util.AlreadyExistsException;
import org.jivesoftware.util.cache.CacheFactory;
import org.xmpp.packet.JID;
import org.xmpp.packet.Presence;
import org.xmpp.packet.Message;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class MUCRoomController.
*/
public class MUCRoomController {
private static Logger LOG = LoggerFactory.getLogger(MUCRoomController.class);
/** The Constant INSTANCE. */
public static final MUCRoomController INSTANCE = new MUCRoomController();
/**
* Gets the single instance of MUCRoomController.
*
* @return single instance of MUCRoomController
*/
public static MUCRoomController getInstance() {
return INSTANCE;
}
private static final PluginManager pluginManager = XMPPServer.getInstance().getPluginManager();
private static final RESTServicePlugin plugin = (RESTServicePlugin) pluginManager.getPlugin("restapi");
public static void log(String logMessage) {
if (plugin.isServiceLoggingEnabled())
LOG.info(logMessage);
}
/**
* Gets the chat rooms.
*
* @param serviceName
* the service name
* @param channelType
* the channel type
* @param roomSearch
* the room search
* @return the chat rooms
*/
public MUCRoomEntities getChatRooms(String serviceName, String channelType, String roomSearch, boolean expand) {
log("Get the chat rooms");
List<MUCRoom> rooms = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName)
.getChatRooms();
List<MUCRoomEntity> mucRoomEntities = new ArrayList<MUCRoomEntity>();
for (MUCRoom chatRoom : rooms) {
if (roomSearch != null) {
if (!chatRoom.getName().contains(roomSearch)) {
continue;
}
}
if (channelType.equals(MUCChannelType.ALL)) {
mucRoomEntities.add(convertToMUCRoomEntity(chatRoom, expand));
} else if (channelType.equals(MUCChannelType.PUBLIC) && chatRoom.isPublicRoom()) {
mucRoomEntities.add(convertToMUCRoomEntity(chatRoom, expand));
}
}
return new MUCRoomEntities(mucRoomEntities);
}
/**
* Gets the chat room.
*
* @param roomName
* the room name
* @param serviceName
* the service name
* @return the chat room
* @throws ServiceException
* the service exception
*/
public MUCRoomEntity getChatRoom(String roomName, String serviceName, boolean expand) throws ServiceException {
log("Get the chat room: " + roomName);
MUCRoom chatRoom = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName)
.getChatRoom(roomName);
if (chatRoom == null) {
throw new ServiceException("Could not find the chat room", roomName, ExceptionType.ROOM_NOT_FOUND, Response.Status.NOT_FOUND);
}
MUCRoomEntity mucRoomEntity = convertToMUCRoomEntity(chatRoom, expand);
return mucRoomEntity;
}
/**
* Delete chat room.
*
* @param roomName
* the room name
* @param serviceName
* the service name
* @throws ServiceException
* the service exception
*/
public void deleteChatRoom(String roomName, String serviceName) throws ServiceException {
log("Delete the chat room: " + roomName);
MUCRoom chatRoom = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName)
.getChatRoom(roomName.toLowerCase());
if (chatRoom != null) {
chatRoom.destroyRoom(null, null);
if (ClusterManager.isClusteringStarted()) {
CacheFactory.doClusterTask(new RoomRemovedEvent((LocalMUCRoom) chatRoom));
}
} else {
throw new ServiceException("Could not remove the channel", roomName, ExceptionType.ROOM_NOT_FOUND, Response.Status.NOT_FOUND);
}
}
/**
* Creates the chat room.
*
* @param serviceName
* the service name
* @param mucRoomEntity
* the MUC room entity
* @throws ServiceException
* the service exception
*/
public void createChatRoom(String serviceName, MUCRoomEntity mucRoomEntity) throws ServiceException {
log("Create a chat room: " + mucRoomEntity.getRoomName());
try {
createRoom(mucRoomEntity, serviceName);
} catch (NotAllowedException | ForbiddenException e) {
throw new ServiceException("Could not create the channel", mucRoomEntity.getRoomName(),
ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e);
} catch (ConflictException e) {
throw new ServiceException("Could not create the channel", mucRoomEntity.getRoomName(),
ExceptionType.NOT_ALLOWED, Response.Status.CONFLICT, e);
} catch (AlreadyExistsException e) {
throw new ServiceException("Could not create the channel", mucRoomEntity.getRoomName(),
ExceptionType.ALREADY_EXISTS, Response.Status.CONFLICT, e);
}
}
/**
* Update chat room.
*
* @param roomName
* the room name
* @param serviceName
* the service name
* @param mucRoomEntity
* the MUC room entity
* @throws ServiceException
* the service exception
*/
public void updateChatRoom(String roomName, String serviceName, MUCRoomEntity mucRoomEntity)
throws ServiceException {
log("Update a chat room: " + mucRoomEntity.getRoomName());
try {
// If the room name is different throw exception
if (!roomName.equals(mucRoomEntity.getRoomName())) {
throw new ServiceException(
"Could not update the channel. The room name is different to the entity room name.", roomName,
ExceptionType.ILLEGAL_ARGUMENT_EXCEPTION, Response.Status.BAD_REQUEST);
}
createRoom(mucRoomEntity, serviceName);
} catch (NotAllowedException | ForbiddenException e) {
throw new ServiceException("Could not update the channel", roomName, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e);
} catch (ConflictException e) {
throw new ServiceException("Could not update the channel", roomName, ExceptionType.NOT_ALLOWED, Response.Status.CONFLICT, e);
} catch (AlreadyExistsException e) {
throw new ServiceException("Could not update the channel", mucRoomEntity.getRoomName(),
ExceptionType.ALREADY_EXISTS, Response.Status.CONFLICT, e);
}
}
/**
* Creates the room.
*
* @param mucRoomEntity
* the MUC room entity
* @param serviceName
* the service name
* @throws NotAllowedException
* the not allowed exception
* @throws ForbiddenException
* the forbidden exception
* @throws ConflictException
* the conflict exception
* @throws AlreadyExistsException
*/
private void createRoom(MUCRoomEntity mucRoomEntity, String serviceName) throws NotAllowedException,
ForbiddenException, ConflictException, AlreadyExistsException {
log("Create a chat room: " + mucRoomEntity.getRoomName());
// Set owner
JID owner = XMPPServer.getInstance().createJID("admin", null);
if (mucRoomEntity.getOwners() != null && mucRoomEntity.getOwners().size() > 0) {
owner = new JID(mucRoomEntity.getOwners().get(0));
} else {
List<String> owners = new ArrayList<String>();
owners.add(owner.toBareJID());
mucRoomEntity.setOwners(owners);
}
// Check if chat service is available, if not create a new one
boolean serviceRegistered = XMPPServer.getInstance().getMultiUserChatManager().isServiceRegistered(serviceName);
if(!serviceRegistered) {
XMPPServer.getInstance().getMultiUserChatManager().createMultiUserChatService(serviceName, serviceName, false);
}
MUCRoom room = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName)
.getChatRoom(mucRoomEntity.getRoomName().toLowerCase(), owner);
// Set values
room.setNaturalLanguageName(mucRoomEntity.getNaturalName());
room.setSubject(mucRoomEntity.getSubject());
room.setDescription(mucRoomEntity.getDescription());
room.setPassword(mucRoomEntity.getPassword());
room.setPersistent(mucRoomEntity.isPersistent());
room.setPublicRoom(mucRoomEntity.isPublicRoom());
room.setRegistrationEnabled(mucRoomEntity.isRegistrationEnabled());
room.setCanAnyoneDiscoverJID(mucRoomEntity.isCanAnyoneDiscoverJID());
room.setCanOccupantsChangeSubject(mucRoomEntity.isCanOccupantsChangeSubject());
room.setCanOccupantsInvite(mucRoomEntity.isCanOccupantsInvite());
room.setChangeNickname(mucRoomEntity.isCanChangeNickname());
room.setModificationDate(mucRoomEntity.getModificationDate());
room.setLogEnabled(mucRoomEntity.isLogEnabled());
room.setLoginRestrictedToNickname(mucRoomEntity.isLoginRestrictedToNickname());
room.setMaxUsers(mucRoomEntity.getMaxUsers());
room.setMembersOnly(mucRoomEntity.isMembersOnly());
room.setModerated(mucRoomEntity.isModerated());
// Set broadcast presence roles
if (mucRoomEntity.getBroadcastPresenceRoles() != null) {
room.setRolesToBroadcastPresence(MUCRoomUtils.convertStringsToRoles(mucRoomEntity.getBroadcastPresenceRoles()));
} else {
room.setRolesToBroadcastPresence(new ArrayList<>());
}
// Set all roles
if (!equalToAffiliations(room, mucRoomEntity)) {
setRoles(room, mucRoomEntity);
}
// Set creation date
if (mucRoomEntity.getCreationDate() != null) {
room.setCreationDate(mucRoomEntity.getCreationDate());
} else {
room.setCreationDate(new Date());
}
// Set modification date
if (mucRoomEntity.getModificationDate() != null) {
room.setModificationDate(mucRoomEntity.getModificationDate());
} else {
room.setModificationDate(new Date());
}
// Unlock the room, because the default configuration lock the room.
room.unlock(room.getRole());
// Fire RoomUpdateEvent if cluster is started
if (ClusterManager.isClusteringStarted()) {
CacheFactory.doClusterTask(new RoomUpdatedEvent((LocalMUCRoom) room));
}
// Save the room to the DB if the room should be persistant
if (room.isPersistent()) {
room.saveToDB();
}
}
private boolean equalToAffiliations(MUCRoom room, MUCRoomEntity mucRoomEntity) {
if (mucRoomEntity == null || room == null) {
return false;
}
Set<String> admins = mucRoomEntity.getAdmins() != null ? new HashSet<>(mucRoomEntity.getAdmins()) : new HashSet<>();
Set<String> owners = mucRoomEntity.getOwners() != null ? new HashSet<>(mucRoomEntity.getOwners()) : new HashSet<>();
Set<String> members = mucRoomEntity.getMembers() != null ? new HashSet<>(mucRoomEntity.getMembers()) : new HashSet<>();
Set<String> outcasts = mucRoomEntity.getOutcasts() != null ? new HashSet<>(mucRoomEntity.getOutcasts()) : new HashSet<>();
Set<String> roomAdmins = room.getAdmins() != null ? new HashSet<>(MUCRoomUtils.convertJIDsToStringList(room.getAdmins())) : new HashSet<>();
Set<String> roomOwners = room.getOwners() != null ? new HashSet<>(MUCRoomUtils.convertJIDsToStringList(room.getOwners())) : new HashSet<>();
Set<String> roomMembers = room.getMembers() != null ? new HashSet<>(MUCRoomUtils.convertJIDsToStringList(room.getMembers())) : new HashSet<>();
Set<String> roomOutcasts = room.getOutcasts() != null ? new HashSet<>(MUCRoomUtils.convertJIDsToStringList(room.getOutcasts())) : new HashSet<>();
return admins.equals(roomAdmins)
&& owners.equals(roomOwners)
&& members.equals(roomMembers)
&& outcasts.equals(roomOutcasts);
}
/**
* Gets the room participants.
*
* @param roomName
* the room name
* @param serviceName
* the service name
* @return the room participants
*/
public ParticipantEntities getRoomParticipants(String roomName, String serviceName) {
log("Get room participants for room: " + roomName);
ParticipantEntities participantEntities = new ParticipantEntities();
List<ParticipantEntity> participants = new ArrayList<ParticipantEntity>();
Collection<MUCRole> serverParticipants = XMPPServer.getInstance().getMultiUserChatManager()
.getMultiUserChatService(serviceName).getChatRoom(roomName).getParticipants();
for (MUCRole role : serverParticipants) {
ParticipantEntity participantEntity = new ParticipantEntity();
participantEntity.setJid(role.getRoleAddress().toFullJID());
participantEntity.setRole(role.getRole().name());
participantEntity.setAffiliation(role.getAffiliation().name());
participants.add(participantEntity);
}
participantEntities.setParticipants(participants);
return participantEntities;
}
/**
* Gets the room occupants.
*
* @param roomName
* the room name
* @param serviceName
* the service name
* @return the room occupants
*/
public OccupantEntities getRoomOccupants(String roomName, String serviceName) {
log("Get room occupants for room: " + roomName);
OccupantEntities occupantEntities = new OccupantEntities();
List<OccupantEntity> occupants = new ArrayList<OccupantEntity>();
Collection<MUCRole> serverOccupants = XMPPServer.getInstance().getMultiUserChatManager()
.getMultiUserChatService(serviceName).getChatRoom(roomName).getOccupants();
for (MUCRole role : serverOccupants) {
OccupantEntity occupantEntity = new OccupantEntity();
occupantEntity.setJid(role.getRoleAddress().toFullJID());
occupantEntity.setUserAddress(role.getUserAddress().toFullJID());
occupantEntity.setRole(role.getRole().name());
occupantEntity.setAffiliation(role.getAffiliation().name());
occupants.add(occupantEntity);
}
occupantEntities.setOccupants(occupants);
return occupantEntities;
}
/**
* Gets the room chat history.
*
* @param roomName
* the room name
* @param serviceName
* the service name
* @return the room chat history
*/
public MUCRoomMessageEntities getRoomHistory(String roomName, String serviceName) throws ServiceException {
log("Get room history for room: " + roomName);
MUCRoomMessageEntities mucRoomMessageEntities = new MUCRoomMessageEntities();
List<MUCRoomMessageEntity> listMessages = new ArrayList<>();
MultiUserChatService mucS =XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName);
MUCRoom chatRoom = mucS.getChatRoom(roomName);
if (chatRoom == null) {
throw new ServiceException("Could not find the chat room", roomName, ExceptionType.ROOM_NOT_FOUND, Response.Status.NOT_FOUND);
}
MUCRoomHistory mucRH = chatRoom.getRoomHistory();
Iterator<Message> messageHistory = mucRH.getMessageHistory();
while (messageHistory.hasNext()) {
Message message = messageHistory.next();
MUCRoomMessageEntity mucMsgEntity = new MUCRoomMessageEntity();
if (message.getTo()!=null && message.getTo().toString().length()!=0)
mucMsgEntity.setTo(message.getTo().toString());
if (message.getFrom()!=null && message.getFrom().toString().length()!=0)
mucMsgEntity.setFrom(message.getFrom().toFullJID());
if (message.getType()!=null && message.getType().toString().length()!=0)
mucMsgEntity.setType(message.getType().name());
if (message.getBody()!=null && message.getBody().length()!=0)
mucMsgEntity.setBody(message.getBody());
Element delay = message.getChildElement("delay","urn:xmpp:delay");
if (delay!=null) {
mucMsgEntity.setDelayStamp(delay.attributeValue("stamp"));
String delayFrom = delay.attributeValue("from");
if (delayFrom!=null)
mucMsgEntity.setDelayFrom(delayFrom);
}
listMessages.add(mucMsgEntity);
}
mucRoomMessageEntities.setMessages(listMessages);
return mucRoomMessageEntities;
}
/**
* Invites the user to the MUC room.
*
* @param serviceName
* the service name
* @param roomName
* the room name
* @param jid
* the jid to invite
* @throws ServiceException
* the service exception
*/
public void inviteUser(String serviceName, String roomName, String jid, MUCInvitationEntity mucInvitationEntity)
throws ServiceException {
MUCRoom room = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName)
.getChatRoom(roomName.toLowerCase());
try {
room.sendInvitation(UserUtils.checkAndGetJID(jid), mucInvitationEntity.getReason(), room.getRole(), null);
} catch (ForbiddenException | CannotBeInvitedException e) {
throw new ServiceException("Could not invite user", jid, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e);
}
}
/**
* Convert to MUC room entity.
*
* @param room
* the room
* @return the MUC room entity
*/
public MUCRoomEntity convertToMUCRoomEntity(MUCRoom room, boolean expand) {
MUCRoomEntity mucRoomEntity = new MUCRoomEntity(room.getNaturalLanguageName(), room.getName(),
room.getDescription());
mucRoomEntity.setSubject(room.getSubject());
mucRoomEntity.setCanAnyoneDiscoverJID(room.canAnyoneDiscoverJID());
mucRoomEntity.setCanChangeNickname(room.canChangeNickname());
mucRoomEntity.setCanOccupantsChangeSubject(room.canOccupantsChangeSubject());
mucRoomEntity.setCanOccupantsInvite(room.canOccupantsInvite());
mucRoomEntity.setPublicRoom(room.isPublicRoom());
mucRoomEntity.setPassword(room.getPassword());
mucRoomEntity.setPersistent(room.isPersistent());
mucRoomEntity.setRegistrationEnabled(room.isRegistrationEnabled());
mucRoomEntity.setLogEnabled(room.isLogEnabled());
mucRoomEntity.setLoginRestrictedToNickname(room.isLoginRestrictedToNickname());
mucRoomEntity.setMaxUsers(room.getMaxUsers());
mucRoomEntity.setMembersOnly(room.isMembersOnly());
mucRoomEntity.setModerated(room.isModerated());
ConcurrentGroupList<JID> owners = new ConcurrentGroupList<JID>(room.getOwners());
ConcurrentGroupList<JID> admins = new ConcurrentGroupList<JID>(room.getAdmins());
ConcurrentGroupList<JID> members = new ConcurrentGroupList<JID>(room.getMembers());
ConcurrentGroupList<JID> outcasts = new ConcurrentGroupList<JID>(room.getOutcasts());
if (expand) {
for(Group ownerGroup : owners.getGroups()) {
owners.addAllAbsent(ownerGroup.getAll());
}
for(Group adminGroup : admins.getGroups()) {
admins.addAllAbsent(adminGroup.getAll());
}
for(Group memberGroup : members.getGroups()) {
members.addAllAbsent(memberGroup.getAll());
}
for(Group outcastGroup : outcasts.getGroups()) {
outcasts.addAllAbsent(outcastGroup.getAll());
}
}
mucRoomEntity.setOwners(MUCRoomUtils.convertJIDsToStringList(owners));
mucRoomEntity.setAdmins(MUCRoomUtils.convertJIDsToStringList(admins));
mucRoomEntity.setMembers(MUCRoomUtils.convertJIDsToStringList(members));
mucRoomEntity.setOutcasts(MUCRoomUtils.convertJIDsToStringList(outcasts));
mucRoomEntity.setOwnerGroups(MUCRoomUtils.convertGroupsToStringList(owners.getGroups()));
mucRoomEntity.setAdminGroups(MUCRoomUtils.convertGroupsToStringList(admins.getGroups()));
mucRoomEntity.setMemberGroups(MUCRoomUtils.convertGroupsToStringList(members.getGroups()));
mucRoomEntity.setOutcastGroups(MUCRoomUtils.convertGroupsToStringList(outcasts.getGroups()));
mucRoomEntity.setBroadcastPresenceRoles(MUCRoomUtils.convertRolesToStringList(room.getRolesToBroadcastPresence()));
mucRoomEntity.setCreationDate(room.getCreationDate());
mucRoomEntity.setModificationDate(room.getModificationDate());
return mucRoomEntity;
}
/**
* Reset roles.
*
* @param room
* the room
* @param mucRoomEntity
* the muc room entity
* @throws ForbiddenException
* the forbidden exception
* @throws NotAllowedException
* the not allowed exception
* @throws ConflictException
* the conflict exception
*/
private void setRoles(MUCRoom room, MUCRoomEntity mucRoomEntity) throws ForbiddenException, NotAllowedException,
ConflictException {
List<JID> roles = new ArrayList<JID>();
Collection<JID> owners = new ArrayList<JID>();
Collection<JID> existingOwners = new ArrayList<JID>();
List<JID> mucRoomEntityOwners = MUCRoomUtils.convertStringsToJIDs(mucRoomEntity.getOwners());
owners.addAll(room.getOwners());
// Find same owners
for (JID jid : owners) {
if (mucRoomEntityOwners.contains(jid)) {
existingOwners.add(jid);
}
}
// Don't delete the same owners
owners.removeAll(existingOwners);
room.addOwners(MUCRoomUtils.convertStringsToJIDs(mucRoomEntity.getOwners()), room.getRole());
// Collect all roles to reset
roles.addAll(owners);
roles.addAll(room.getAdmins());
roles.addAll(room.getMembers());
roles.addAll(room.getOutcasts());
for (JID jid : roles) {
room.addNone(jid, room.getRole());
}
room.addOwners(MUCRoomUtils.convertStringsToJIDs(mucRoomEntity.getOwners()), room.getRole());
if (mucRoomEntity.getAdmins() != null) {
room.addAdmins(MUCRoomUtils.convertStringsToJIDs(mucRoomEntity.getAdmins()), room.getRole());
}
if (mucRoomEntity.getMembers() != null) {
for (String memberJid : mucRoomEntity.getMembers()) {
room.addMember(new JID(memberJid), null, room.getRole());
}
}
if (mucRoomEntity.getOutcasts() != null) {
for (String outcastJid : mucRoomEntity.getOutcasts()) {
room.addOutcast(new JID(outcastJid), null, room.getRole());
}
}
}
/**
* Adds the admin.
*
* @param serviceName
* the service name
* @param roomName
* the room name
* @param jid
* the jid
* @throws ServiceException
* the service exception
*/
public void addAdmin(String serviceName, String roomName, String jid) throws ServiceException {
MUCRoom room = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName)
.getChatRoom(roomName.toLowerCase());
try {
room.addAdmin(UserUtils.checkAndGetJID(jid), room.getRole());
} catch (ForbiddenException e) {
throw new ServiceException("Could not add admin", jid, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e);
} catch (ConflictException e) {
throw new ServiceException("Could not add admin", jid, ExceptionType.NOT_ALLOWED, Response.Status.CONFLICT, e);
}
}
/**
* Adds the owner.
*
* @param serviceName
* the service name
* @param roomName
* the room name
* @param jid
* the jid
* @throws ServiceException
* the service exception
*/
public void addOwner(String serviceName, String roomName, String jid) throws ServiceException {
MUCRoom room = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName)
.getChatRoom(roomName.toLowerCase());
try {
room.addOwner(UserUtils.checkAndGetJID(jid), room.getRole());
} catch (ForbiddenException e) {
throw new ServiceException("Could not add owner", jid, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e);
}
}
/**
* Adds the member.
*
* @param serviceName
* the service name
* @param roomName
* the room name
* @param jid
* the jid
* @throws ServiceException
* the service exception
*/
public void addMember(String serviceName, String roomName, String jid) throws ServiceException {
MUCRoom room = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName)
.getChatRoom(roomName.toLowerCase());
try {
room.addMember(UserUtils.checkAndGetJID(jid), null, room.getRole());
} catch (ForbiddenException | ConflictException e) {
throw new ServiceException("Could not add member", jid, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e);
}
}
/**
* Adds the outcast.
*
* @param serviceName
* the service name
* @param roomName
* the room name
* @param jid
* the jid
* @throws ServiceException
* the service exception
*/
public void addOutcast(String serviceName, String roomName, String jid) throws ServiceException {
MUCRoom room = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName)
.getChatRoom(roomName.toLowerCase());
try {
room.addOutcast(UserUtils.checkAndGetJID(jid), null, room.getRole());
} catch (NotAllowedException | ForbiddenException e) {
throw new ServiceException("Could not add outcast", jid, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e);
} catch (ConflictException e) {
throw new ServiceException("Could not add outcast", jid, ExceptionType.NOT_ALLOWED, Response.Status.CONFLICT, e);
}
}
/**
* Delete affiliation.
*
* @param serviceName
* the service name
* @param roomName
* the room name
* @param jid
* the jid
* @throws ServiceException
* the service exception
*/
public void deleteAffiliation(String serviceName, String roomName, String jid) throws ServiceException {
MUCRoom room = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName)
.getChatRoom(roomName.toLowerCase());
try {
JID userJid = UserUtils.checkAndGetJID(jid);
// Send a presence to other room members
List<Presence> addNonePresence = room.addNone(userJid, room.getRole());
for (Presence presence : addNonePresence) {
MUCRoomUtils.send(room, presence, room.getRole());
}
} catch (ForbiddenException e) {
throw new ServiceException("Could not delete affiliation", jid, ExceptionType.NOT_ALLOWED, Response.Status.FORBIDDEN, e);
} catch (ConflictException e) {
throw new ServiceException("Could not delete affiliation", jid, ExceptionType.NOT_ALLOWED, Response.Status.CONFLICT, e);
} catch (IllegalAccessException | InvocationTargetException e) {
// Completely unknown implementation of MUCRoom::send
throw new ServiceException("Could not delete affiliation", jid, ExceptionType.ILLEGAL_ARGUMENT_EXCEPTION, Response.Status.INTERNAL_SERVER_ERROR, e);
}
}
}