Commit 384005c4 authored by Dele Olajide's avatar Dele Olajide

Merge pull request #53 from Redor/master

MUC Service Plugin 0.2.0
parents e0562d9f a98343cc
......@@ -44,6 +44,12 @@
MUC Service Plugin Changelog
</h1>
<p><b>0.2.0</b> -- 07.07.2014</p>
<ul>
<li>Extended the service with /participants endpoint to get all room participants</li>
<li>Extended the muc service to manage chat room roles (owners, admins, members, outcasts)</li>
</ul>
<p><b>0.1.0</b> -- 23.06.2014</p>
<ul>
<li>Initial setup of MUC Service Plugin</li>
......
......@@ -5,7 +5,7 @@
<name>MUC Service</name>
<description>MUC administration over REST Interface</description>
<author>Roman Soldatow</author>
<version>0.1.0</version>
<date>23.06.2014</date>
<version>0.2.0</version>
<date>07.07.2014</date>
<minServerVersion>3.9.1</minServerVersion>
</plugin>
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -35,6 +35,14 @@ public class MUCRoomEntity {
private List<String> broadcastPresenceRoles;
private List<String> owners;
private List<String> admins;
private List<String> members;
private List<String> outcasts;
public MUCRoomEntity() {
}
......@@ -170,12 +178,6 @@ public class MUCRoomEntity {
this.canOccupantsInvite = canOccupantsInvite;
}
@XmlElement(name = "broadcastPresenceRole")
@XmlElementWrapper(name = "broadcastPresenceRoles")
public List<String> getBroadcastPresenceRoles() {
return broadcastPresenceRoles;
}
public void setBroadcastPresenceRoles(List<String> broadcastPresenceRoles) {
this.broadcastPresenceRoles = broadcastPresenceRoles;
}
......@@ -224,4 +226,51 @@ public class MUCRoomEntity {
public void setModerated(boolean moderated) {
this.moderated = moderated;
}
@XmlElement(name = "broadcastPresenceRole")
@XmlElementWrapper(name = "broadcastPresenceRoles")
public List<String> getBroadcastPresenceRoles() {
return broadcastPresenceRoles;
}
@XmlElementWrapper(name = "owners")
@XmlElement(name = "owner")
public List<String> getOwners() {
return owners;
}
public void setOwners(List<String> owners) {
this.owners = owners;
}
@XmlElementWrapper(name = "members")
@XmlElement(name = "member")
public List<String> getMembers() {
return members;
}
public void setMembers(List<String> members) {
this.members = members;
}
@XmlElementWrapper(name = "outcasts")
@XmlElement(name = "outcast")
public List<String> getOutcasts() {
return outcasts;
}
public void setOutcasts(List<String> outcasts) {
this.outcasts = outcasts;
}
@XmlElementWrapper(name = "admins")
@XmlElement(name = "admin")
public List<String> getAdmins() {
return admins;
}
public void setAdmins(List<String> admins) {
this.admins = admins;
}
}
\ No newline at end of file
package org.jivesoftware.openfire.entity;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "participants")
public class ParticipantEntities {
List<ParticipantEntity> participants;
public ParticipantEntities() {
}
public ParticipantEntities(List<ParticipantEntity> participants) {
this.participants = participants;
}
@XmlElement(name = "participant")
public List<ParticipantEntity> getParticipants() {
return participants;
}
public void setParticipants(List<ParticipantEntity> participants) {
this.participants = participants;
}
}
package org.jivesoftware.openfire.entity;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "participant")
public class ParticipantEntity {
private String jid;
private String role;
private String affiliation;
public ParticipantEntity() {
}
@XmlElement
public String getJid() {
return jid;
}
public void setJid(String jid) {
this.jid = jid;
}
@XmlElement
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
@XmlElement
public String getAffiliation() {
return affiliation;
}
public void setAffiliation(String affiliation) {
this.affiliation = affiliation;
}
}
\ No newline at end of file
package org.jivesoftware.openfire.plugin;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.entity.MUCChannelType;
import org.jivesoftware.openfire.entity.MUCRoomEntities;
import org.jivesoftware.openfire.entity.MUCRoomEntity;
import org.jivesoftware.openfire.entity.ParticipantEntities;
import org.jivesoftware.openfire.entity.ParticipantEntity;
import org.jivesoftware.openfire.exception.MUCServiceException;
import org.jivesoftware.openfire.muc.ConflictException;
import org.jivesoftware.openfire.muc.ForbiddenException;
import org.jivesoftware.openfire.muc.MUCRole;
import org.jivesoftware.openfire.muc.MUCRoom;
import org.jivesoftware.openfire.muc.NotAllowedException;
import org.jivesoftware.openfire.utils.MUCRoomUtils;
import org.xmpp.packet.JID;
/**
* The Class MUCRoomController.
......@@ -71,7 +80,7 @@ public class MUCRoomController {
* the service name
* @return the chat room
* @throws MUCServiceException
* the mUC service exception
* the MUC service exception
*/
public MUCRoomEntity getChatRoom(String roomName, String serviceName) throws MUCServiceException {
MUCRoom chatRoom = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName)
......@@ -93,7 +102,7 @@ public class MUCRoomController {
* @param serviceName
* the service name
* @throws MUCServiceException
* the mUC service exception
* the MUC service exception
*/
public void deleteChatRoom(String roomName, String serviceName) throws MUCServiceException {
MUCRoom chatRoom = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName)
......@@ -108,69 +117,95 @@ public class MUCRoomController {
/**
* Creates the chat room.
*
*
* @param serviceName
* the service name
* @param owner
* the owner
* @param mucRoomEntity
* the muc room entity
* the MUC room entity
* @throws MUCServiceException
* the mUC service exception
*/
public void createChatRoom(String serviceName, String owner, MUCRoomEntity mucRoomEntity) throws MUCServiceException {
public void createChatRoom(String serviceName, MUCRoomEntity mucRoomEntity) throws MUCServiceException {
try {
createRoom(mucRoomEntity, serviceName, owner);
createRoom(mucRoomEntity, serviceName);
} catch (NotAllowedException e) {
throw new MUCServiceException("Could not create the channel", mucRoomEntity.getRoomName(),
"NotAllowedException");
} catch (ForbiddenException e) {
throw new MUCServiceException("Could not create the channel", mucRoomEntity.getRoomName(),
"ForbiddenException");
} catch (ConflictException e) {
throw new MUCServiceException("Could not create the channel", mucRoomEntity.getRoomName(),
"ConflictException");
}
}
/**
* Update chat room.
*
*
* @param roomName
* the room name
* @param serviceName
* the service name
* @param owner
* the owner
* @param mucRoomEntity
* the muc room entity
* the MUC room entity
* @throws MUCServiceException
* the mUC service exception
*/
public void updateChatRoom(String roomName, String serviceName, String owner, MUCRoomEntity mucRoomEntity)
public void updateChatRoom(String roomName, String serviceName, MUCRoomEntity mucRoomEntity)
throws MUCServiceException {
try {
// If the roomname is different throw exception
// If the room name is different throw exception
if (!roomName.equals(mucRoomEntity.getRoomName())) {
throw new MUCServiceException("Could not update the channel", roomName,
"The roomname is different to the entity room name");
throw new MUCServiceException(
"Could not update the channel. The room name is different to the entity room name.", roomName,
"IllegalArgumentException");
}
createRoom(mucRoomEntity, serviceName, owner);
// Set modification date
mucRoomEntity.setModificationDate(new Date());
createRoom(mucRoomEntity, serviceName);
} catch (NotAllowedException e) {
throw new MUCServiceException("Could not update the channel", roomName, "NotAllowedException");
} catch (ForbiddenException e) {
throw new MUCServiceException("Could not update the channel", roomName, "ForbiddenException");
} catch (ConflictException e) {
throw new MUCServiceException("Could not update the channel", roomName, "ConflictException");
}
}
/**
* Creates the room.
*
*
* @param mucRoomEntity
* the muc room entity
* the MUC room entity
* @param serviceName
* the service name
* @param owner
* the owner
* @throws NotAllowedException
* the not allowed exception
* @throws ForbiddenException
* the forbidden exception
* @throws ConflictException
* the conflict exception
*/
public void createRoom(MUCRoomEntity mucRoomEntity, String serviceName, String owner) throws NotAllowedException {
private void createRoom(MUCRoomEntity mucRoomEntity, String serviceName) throws NotAllowedException,
ForbiddenException, ConflictException {
// 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);
}
MUCRoom room = XMPPServer.getInstance().getMultiUserChatManager().getMultiUserChatService(serviceName)
.getChatRoom(mucRoomEntity.getRoomName(), XMPPServer.getInstance().createJID(owner, null));
.getChatRoom(mucRoomEntity.getRoomName(), owner);
// Set values
room.setNaturalLanguageName(mucRoomEntity.getNaturalName());
room.setSubject(mucRoomEntity.getSubject());
room.setDescription(mucRoomEntity.getDescription());
......@@ -182,7 +217,6 @@ public class MUCRoomController {
room.setCanOccupantsChangeSubject(mucRoomEntity.isCanOccupantsChangeSubject());
room.setCanOccupantsInvite(mucRoomEntity.isCanOccupantsInvite());
room.setChangeNickname(mucRoomEntity.isCanChangeNickname());
room.setCreationDate(mucRoomEntity.getCreationDate());
room.setModificationDate(mucRoomEntity.getModificationDate());
room.setLogEnabled(mucRoomEntity.isLogEnabled());
room.setLoginRestrictedToNickname(mucRoomEntity.isLoginRestrictedToNickname());
......@@ -191,18 +225,58 @@ public class MUCRoomController {
room.setModerated(mucRoomEntity.isModerated());
room.setRolesToBroadcastPresence(mucRoomEntity.getBroadcastPresenceRoles());
// Set all roles
setRoles(room, mucRoomEntity);
// Set creation date
if (mucRoomEntity.getCreationDate() != null) {
room.setCreationDate(mucRoomEntity.getCreationDate());
} else {
room.setCreationDate(new Date());
}
}
/**
* Convert to muc room entity.
* 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) {
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;
}
/**
* Convert to MUC room entity.
*
* @param room
* the room
* @return the mUC room entity
* @return the MUC room entity
*/
public MUCRoomEntity convertToMUCRoomEntity(MUCRoom room) {
MUCRoomEntity mucRoomEntity = new MUCRoomEntity(room.getNaturalLanguageName(), room.getName(),
room.getDescription());
mucRoomEntity.setCanAnyoneDiscoverJID(room.canAnyoneDiscoverJID());
mucRoomEntity.setCanChangeNickname(room.canChangeNickname());
mucRoomEntity.setCanOccupantsChangeSubject(room.canOccupantsChangeSubject());
......@@ -218,12 +292,70 @@ public class MUCRoomController {
mucRoomEntity.setMembersOnly(room.isMembersOnly());
mucRoomEntity.setModerated(room.isModerated());
mucRoomEntity.setCreationDate(room.getCreationDate());
mucRoomEntity.setModificationDate(room.getModificationDate());
mucRoomEntity.setOwners(MUCRoomUtils.convertJIDsToStringList(room.getOwners()));
mucRoomEntity.setAdmins(MUCRoomUtils.convertJIDsToStringList(room.getAdmins()));
mucRoomEntity.setMembers(MUCRoomUtils.convertJIDsToStringList(room.getMembers()));
mucRoomEntity.setOutcasts(MUCRoomUtils.convertJIDsToStringList(room.getOutcasts()));
mucRoomEntity.setBroadcastPresenceRoles(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());
room.addAdmins(MUCRoomUtils.convertStringsToJIDs(mucRoomEntity.getAdmins()), room.getRole());
for (String memberJid : mucRoomEntity.getMembers()) {
room.addMember(new JID(memberJid), null, room.getRole());
}
for (String outcastJid : mucRoomEntity.getOutcasts()) {
room.addOutcast(new JID(outcastJid), null, room.getRole());
}
}
}
\ No newline at end of file
......@@ -14,14 +14,14 @@ import javax.ws.rs.core.MediaType;
import org.jivesoftware.openfire.entity.MUCChannelType;
import org.jivesoftware.openfire.entity.MUCRoomEntities;
import org.jivesoftware.openfire.entity.MUCRoomEntity;
import org.jivesoftware.openfire.entity.ParticipantEntities;
import org.jivesoftware.openfire.exception.MUCServiceException;
import org.jivesoftware.openfire.plugin.MUCRoomController;
@Path("mucservice")
@Path("mucservice/chatrooms")
public class MUCRoomService {
@GET
@Path("/chatrooms")
@Produces(MediaType.APPLICATION_XML)
public MUCRoomEntities getMUCRooms(@DefaultValue("conference") @QueryParam("servicename") String serviceName,
@DefaultValue(MUCChannelType.PUBLIC) @QueryParam("type") String channelType,
......@@ -30,7 +30,7 @@ public class MUCRoomService {
}
@GET
@Path("/chatrooms/{roomName}")
@Path("/{roomName}")
@Produces(MediaType.APPLICATION_XML)
public MUCRoomEntity getMUCRoom(@PathParam("roomName") String roomName,
@DefaultValue("conference") @QueryParam("servicename") String serviceName) throws MUCServiceException {
......@@ -38,27 +38,31 @@ public class MUCRoomService {
}
@DELETE
@Path("/chatrooms/{roomName}")
@Path("/{roomName}")
public void deleteMUCRoom(@PathParam("roomName") String roomName,
@DefaultValue("conference") @QueryParam("servicename") String serviceName) throws MUCServiceException {
MUCRoomController.getInstance().deleteChatRoom(roomName, serviceName);
}
@POST
@Path("/chatrooms")
public void createMUCRoom(@DefaultValue("conference") @QueryParam("servicename") String serviceName,
@DefaultValue("admin") @QueryParam("owner") String owner, MUCRoomEntity mucRoomEntity)
throws MUCServiceException {
MUCRoomController.getInstance().createChatRoom(serviceName, owner, mucRoomEntity);
MUCRoomEntity mucRoomEntity) throws MUCServiceException {
MUCRoomController.getInstance().createChatRoom(serviceName, mucRoomEntity);
}
@PUT
@Path("/chatrooms/{roomName}")
@Path("/{roomName}")
public void udpateMUCRoom(@PathParam("roomName") String roomName,
@DefaultValue("conference") @QueryParam("servicename") String serviceName,
@DefaultValue("admin") @QueryParam("owner") String owner, MUCRoomEntity mucRoomEntity)
@DefaultValue("conference") @QueryParam("servicename") String serviceName, MUCRoomEntity mucRoomEntity)
throws MUCServiceException {
MUCRoomController.getInstance().updateChatRoom(roomName, serviceName, owner, mucRoomEntity);
MUCRoomController.getInstance().updateChatRoom(roomName, serviceName, mucRoomEntity);
}
@GET
@Path("/{roomName}/participants")
@Produces(MediaType.APPLICATION_XML)
public ParticipantEntities getMUCRoomParticipants(@PathParam("roomName") String roomName,
@DefaultValue("conference") @QueryParam("servicename") String serviceName) throws MUCServiceException {
return MUCRoomController.getInstance().getRoomParticipants(roomName, serviceName);
}
}
package org.jivesoftware.openfire.utils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.xmpp.packet.JID;
/**
* The Class MUCRoomUtils.
*/
public class MUCRoomUtils {
/**
* Instantiates a new MUC room utils.
*/
private MUCRoomUtils() {
throw new AssertionError();
}
/**
* Convert jids to string list.
*
* @param jids
* the jids
* @return the array list< string>
*/
public static ArrayList<String> convertJIDsToStringList(Collection<JID> jids) {
ArrayList<String> result = new ArrayList<String>();
for (JID jid : jids) {
result.add(jid.toBareJID());
}
return result;
}
/**
* Convert strings to jids.
*
* @param jids
* the jids
* @return the list<jid>
*/
public static List<JID> convertStringsToJIDs(List<String> jids) {
List<JID> result = new ArrayList<JID>();
for (String jidString : jids) {
result.add(new JID(jidString));
}
return result;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment