Commit 8bc8c539 authored by daryl herzmann's avatar daryl herzmann

Merge pull request #189 from Redor/openfire

Update to REST API plugin version 1.0.2
parents 4b5079da 6a72aa1b
...@@ -44,6 +44,12 @@ ...@@ -44,6 +44,12 @@
REST API Plugin Changelog REST API Plugin Changelog
</h1> </h1>
<p><b>1.0.2</b> -- March 3rd, 2015</p>
<ul>
<li>User will be kicked by a lockout (ban)</li>
<li>Added: new endpoints for groups (Get overview over all or specific group and to create, update or delete a group)</li>
</ul>
<p><b>1.0.1</b> -- February 20th, 2015</p> <p><b>1.0.1</b> -- February 20th, 2015</p>
<ul> <ul>
<li>Added possibility to rename a user (Thanks to JustMarried plugin)</li> <li>Added possibility to rename a user (Thanks to JustMarried plugin)</li>
......
...@@ -5,8 +5,8 @@ ...@@ -5,8 +5,8 @@
<name>REST API</name> <name>REST API</name>
<description>Allows administration over a RESTful API.</description> <description>Allows administration over a RESTful API.</description>
<author>Roman Soldatow</author> <author>Roman Soldatow</author>
<version>1.0.1</version> <version>1.0.2</version>
<date>02/20/2015</date> <date>03/03/2015</date>
<minServerVersion>3.9.0</minServerVersion> <minServerVersion>3.9.0</minServerVersion>
<adminconsole> <adminconsole>
......
This diff is collapsed.
package org.jivesoftware.openfire.plugin.rest.controller;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.ws.rs.core.Response;
import org.jivesoftware.openfire.group.Group;
import org.jivesoftware.openfire.group.GroupAlreadyExistsException;
import org.jivesoftware.openfire.group.GroupManager;
import org.jivesoftware.openfire.group.GroupNotFoundException;
import org.jivesoftware.openfire.plugin.rest.entity.GroupEntity;
import org.jivesoftware.openfire.plugin.rest.exceptions.ExceptionType;
import org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException;
/**
* The Class GroupController.
*/
public class GroupController {
/** The Constant INSTANCE. */
public static final GroupController INSTANCE = new GroupController();
/**
* Gets the single instance of GroupController.
*
* @return single instance of GroupController
*/
public static GroupController getInstance() {
return INSTANCE;
}
/**
* Gets the groups.
*
* @return the groups
* @throws ServiceException
* the service exception
*/
public List<GroupEntity> getGroups() throws ServiceException {
Collection<Group> groups = GroupManager.getInstance().getGroups();
List<GroupEntity> groupEntities = new ArrayList<GroupEntity>();
for (Group group : groups) {
GroupEntity groupEntity = new GroupEntity(group.getName(), group.getDescription());
groupEntities.add(groupEntity);
}
return groupEntities;
}
/**
* Gets the group.
*
* @param groupName
* the group name
* @return the group
* @throws ServiceException
* the service exception
*/
public GroupEntity getGroup(String groupName) throws ServiceException {
Group group;
try {
group = GroupManager.getInstance().getGroup(groupName);
} catch (GroupNotFoundException e) {
throw new ServiceException("Could not find group", groupName, ExceptionType.GROUP_NOT_FOUND,
Response.Status.NOT_FOUND, e);
}
GroupEntity groupEntity = new GroupEntity(group.getName(), group.getDescription());
return groupEntity;
}
/**
* Creates the group.
*
* @param groupEntity
* the group entity
* @return the group
* @throws ServiceException
* the service exception
*/
public Group createGroup(GroupEntity groupEntity) throws ServiceException {
Group group;
if (groupEntity != null && !groupEntity.getName().isEmpty()) {
try {
group = GroupManager.getInstance().createGroup(groupEntity.getName());
group.setDescription(groupEntity.getDescription());
group.getProperties().put("sharedRoster.showInRoster", "onlyGroup");
group.getProperties().put("sharedRoster.displayName", groupEntity.getName());
group.getProperties().put("sharedRoster.groupList", "");
} catch (GroupAlreadyExistsException e) {
throw new ServiceException("Could not create a group", groupEntity.getName(),
ExceptionType.GROUP_ALREADY_EXISTS, Response.Status.CONFLICT, e);
}
} else {
throw new ServiceException("Could not create new group", "groups",
ExceptionType.ILLEGAL_ARGUMENT_EXCEPTION, Response.Status.BAD_REQUEST);
}
return group;
}
/**
* Update group.
*
* @param groupName the group name
* @param groupEntity the group entity
* @return the group
* @throws ServiceException the service exception
*/
public Group updateGroup(String groupName, GroupEntity groupEntity) throws ServiceException {
Group group;
if (groupEntity != null && !groupEntity.getName().isEmpty()) {
if (groupName.equals(groupEntity.getName())) {
try {
group = GroupManager.getInstance().getGroup(groupName);
group.setDescription(groupEntity.getDescription());
} catch (GroupNotFoundException e) {
throw new ServiceException("Could not find group", groupName, ExceptionType.GROUP_NOT_FOUND,
Response.Status.NOT_FOUND, e);
}
} else {
throw new ServiceException(
"Could not update the group. The group name is different to the payload group name.", groupName + " != " + groupEntity.getName(),
ExceptionType.ILLEGAL_ARGUMENT_EXCEPTION, Response.Status.BAD_REQUEST);
}
} else {
throw new ServiceException("Could not update new group", "groups",
ExceptionType.ILLEGAL_ARGUMENT_EXCEPTION, Response.Status.BAD_REQUEST);
}
return group;
}
/**
* Delete group.
*
* @param groupName
* the group name
* @throws ServiceException
* the service exception
*/
public void deleteGroup(String groupName) throws ServiceException {
try {
Group group = GroupManager.getInstance().getGroup(groupName);
GroupManager.getInstance().deleteGroup(group);
} catch (GroupNotFoundException e) {
throw new ServiceException("Could not find group", groupName, ExceptionType.GROUP_NOT_FOUND,
Response.Status.NOT_FOUND, e);
}
}
}
\ No newline at end of file
...@@ -6,14 +6,15 @@ import java.util.List; ...@@ -6,14 +6,15 @@ import java.util.List;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import org.jivesoftware.openfire.SessionManager;
import org.jivesoftware.openfire.SharedGroupException; import org.jivesoftware.openfire.SharedGroupException;
import org.jivesoftware.openfire.XMPPServer; import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.group.Group; import org.jivesoftware.openfire.group.Group;
import org.jivesoftware.openfire.group.GroupAlreadyExistsException;
import org.jivesoftware.openfire.group.GroupManager; import org.jivesoftware.openfire.group.GroupManager;
import org.jivesoftware.openfire.group.GroupNotFoundException; import org.jivesoftware.openfire.group.GroupNotFoundException;
import org.jivesoftware.openfire.lockout.LockOutManager; import org.jivesoftware.openfire.lockout.LockOutManager;
import org.jivesoftware.openfire.plugin.rest.dao.PropertyDAO; import org.jivesoftware.openfire.plugin.rest.dao.PropertyDAO;
import org.jivesoftware.openfire.plugin.rest.entity.GroupEntity;
import org.jivesoftware.openfire.plugin.rest.entity.RosterEntities; import org.jivesoftware.openfire.plugin.rest.entity.RosterEntities;
import org.jivesoftware.openfire.plugin.rest.entity.RosterItemEntity; import org.jivesoftware.openfire.plugin.rest.entity.RosterItemEntity;
import org.jivesoftware.openfire.plugin.rest.entity.UserEntities; import org.jivesoftware.openfire.plugin.rest.entity.UserEntities;
...@@ -26,11 +27,13 @@ import org.jivesoftware.openfire.plugin.rest.utils.UserUtils; ...@@ -26,11 +27,13 @@ import org.jivesoftware.openfire.plugin.rest.utils.UserUtils;
import org.jivesoftware.openfire.roster.Roster; import org.jivesoftware.openfire.roster.Roster;
import org.jivesoftware.openfire.roster.RosterItem; import org.jivesoftware.openfire.roster.RosterItem;
import org.jivesoftware.openfire.roster.RosterManager; import org.jivesoftware.openfire.roster.RosterManager;
import org.jivesoftware.openfire.session.ClientSession;
import org.jivesoftware.openfire.user.User; import org.jivesoftware.openfire.user.User;
import org.jivesoftware.openfire.user.UserAlreadyExistsException; import org.jivesoftware.openfire.user.UserAlreadyExistsException;
import org.jivesoftware.openfire.user.UserManager; import org.jivesoftware.openfire.user.UserManager;
import org.jivesoftware.openfire.user.UserNotFoundException; import org.jivesoftware.openfire.user.UserNotFoundException;
import org.xmpp.packet.JID; import org.xmpp.packet.JID;
import org.xmpp.packet.StreamError;
/** /**
* The Class UserServiceController. * The Class UserServiceController.
...@@ -47,6 +50,9 @@ public class UserServiceController { ...@@ -47,6 +50,9 @@ public class UserServiceController {
/** The server. */ /** The server. */
private XMPPServer server; private XMPPServer server;
/** The lock out manager. */
private LockOutManager lockOutManager;
/** /**
* Gets the single instance of UserServiceController. * Gets the single instance of UserServiceController.
...@@ -64,6 +70,7 @@ public class UserServiceController { ...@@ -64,6 +70,7 @@ public class UserServiceController {
server = XMPPServer.getInstance(); server = XMPPServer.getInstance();
userManager = server.getUserManager(); userManager = server.getUserManager();
rosterManager = server.getRosterManager(); rosterManager = server.getRosterManager();
lockOutManager = server.getLockOutManager();
} }
/** /**
...@@ -88,6 +95,9 @@ public class UserServiceController { ...@@ -88,6 +95,9 @@ public class UserServiceController {
ExceptionType.USER_ALREADY_EXISTS_EXCEPTION, Response.Status.CONFLICT); ExceptionType.USER_ALREADY_EXISTS_EXCEPTION, Response.Status.CONFLICT);
} }
addProperties(userEntity.getUsername(), userEntity.getProperties()); addProperties(userEntity.getUsername(), userEntity.getProperties());
} else {
throw new ServiceException("Could not create new user",
"users", ExceptionType.ILLEGAL_ARGUMENT_EXCEPTION, Response.Status.BAD_REQUEST);
} }
} }
...@@ -186,7 +196,7 @@ public class UserServiceController { ...@@ -186,7 +196,7 @@ public class UserServiceController {
*/ */
public void enableUser(String username) throws ServiceException { public void enableUser(String username) throws ServiceException {
getAndCheckUser(username); getAndCheckUser(username);
LockOutManager.getInstance().enableAccount(username); lockOutManager.enableAccount(username);
} }
/** /**
...@@ -199,7 +209,15 @@ public class UserServiceController { ...@@ -199,7 +209,15 @@ public class UserServiceController {
*/ */
public void disableUser(String username) throws ServiceException { public void disableUser(String username) throws ServiceException {
getAndCheckUser(username); getAndCheckUser(username);
LockOutManager.getInstance().disableAccount(username, null, null); lockOutManager.disableAccount(username, null, null);
if (lockOutManager.isAccountDisabled(username)) {
final StreamError error = new StreamError(StreamError.Condition.not_authorized);
for (ClientSession sess : SessionManager.getInstance().getSessions(username)) {
sess.deliverRawText(error.toXML());
sess.close();
}
}
} }
/** /**
...@@ -367,7 +385,7 @@ public class UserServiceController { ...@@ -367,7 +385,7 @@ public class UserServiceController {
group = GroupManager.getInstance().getGroup(groupName); group = GroupManager.getInstance().getGroup(groupName);
} catch (GroupNotFoundException e) { } catch (GroupNotFoundException e) {
// Create this group // Create this group
group = createGroup(groupName); group = GroupController.getInstance().createGroup(new GroupEntity(groupName, ""));
} }
groups.add(group); groups.add(group);
} }
...@@ -444,29 +462,6 @@ public class UserServiceController { ...@@ -444,29 +462,6 @@ public class UserServiceController {
} }
} }
/**
* Creates the group.
*
* @param groupName
* the group name
* @return the group
* @throws ServiceException
* the service exception
*/
private Group createGroup(String groupName) throws ServiceException {
Group group = null;
try {
group = GroupManager.getInstance().createGroup(groupName);
group.getProperties().put("sharedRoster.showInRoster", "onlyGroup");
group.getProperties().put("sharedRoster.displayName", groupName);
group.getProperties().put("sharedRoster.groupList", "");
} catch (GroupAlreadyExistsException e) {
throw new ServiceException("Could not create group", groupName, ExceptionType.GROUP_ALREADY_EXISTS,
Response.Status.BAD_REQUEST, e);
}
return group;
}
/** /**
* Gets the and check user. * Gets the and check user.
* *
......
package org.jivesoftware.openfire.plugin.rest.entity;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
* The Class GroupEntities.
*/
@XmlRootElement(name = "groups")
public class GroupEntities {
/** The groups. */
List<GroupEntity> groups;
/**
* Instantiates a new group entities.
*/
public GroupEntities() {
}
/**
* Instantiates a new group entities.
*
* @param groups the groups
*/
public GroupEntities(List<GroupEntity> groups) {
this.groups = groups;
}
/**
* Gets the groups.
*
* @return the groups
*/
@XmlElement(name = "group")
public List<GroupEntity> getGroups() {
return groups;
}
/**
* Sets the groups.
*
* @param groups the new groups
*/
public void setGroups(List<GroupEntity> groups) {
this.groups = groups;
}
}
package org.jivesoftware.openfire.plugin.rest.entity;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* The Class GroupEntity.
*/
@XmlRootElement(name = "group")
@XmlType(propOrder = { "name", "description" })
public class GroupEntity {
/** The name. */
private String name;
/** The description. */
private String description;
/**
* Instantiates a new group entity.
*/
public GroupEntity() {
}
/**
* Instantiates a new group entity.
*
* @param name
* the name
* @param description
* the description
*/
public GroupEntity(String name, String description) {
this.name = name;
this.description = description;
}
/**
* Gets the name.
*
* @return the name
*/
@XmlElement
public String getName() {
return name;
}
/**
* Sets the name.
*
* @param name
* the new name
*/
public void setName(String name) {
this.name = name;
}
/**
* Gets the description.
*
* @return the description
*/
@XmlElement
public String getDescription() {
return description;
}
/**
* Sets the description.
*
* @param description
* the new description
*/
public void setDescription(String description) {
this.description = description;
}
}
package org.jivesoftware.openfire.plugin.rest.service;
import javax.annotation.PostConstruct;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.jivesoftware.openfire.plugin.rest.controller.GroupController;
import org.jivesoftware.openfire.plugin.rest.entity.GroupEntities;
import org.jivesoftware.openfire.plugin.rest.entity.GroupEntity;
import org.jivesoftware.openfire.plugin.rest.exceptions.ServiceException;
@Path("restapi/v1/groups")
public class GroupService {
private GroupController groupController;
@PostConstruct
public void init() {
groupController = GroupController.getInstance();
}
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public GroupEntities getGroups() throws ServiceException {
return new GroupEntities(groupController.getGroups());
}
@POST
public Response createGroup(GroupEntity groupEntity) throws ServiceException {
groupController.createGroup(groupEntity);
return Response.status(Response.Status.CREATED).build();
}
@GET
@Path("/{groupName}")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public GroupEntity getGroup(@PathParam("groupName") String groupName) throws ServiceException {
return groupController.getGroup(groupName);
}
@PUT
@Path("/{groupName}")
public Response updateGroup(@PathParam("groupName") String groupName, GroupEntity groupEntity) throws ServiceException {
groupController.updateGroup(groupName, groupEntity);
return Response.status(Response.Status.OK).build();
}
@DELETE
@Path("/{groupName}")
public Response deleteUserFromGroups(@PathParam("groupName") String groupName) throws ServiceException {
groupController.deleteGroup(groupName);
return Response.status(Response.Status.OK).build();
}
}
...@@ -71,6 +71,8 @@ public class JerseyWrapper extends ServletContainer { ...@@ -71,6 +71,8 @@ public class JerseyWrapper extends ServletContainer {
prc.getClasses().add(UserGroupService.class); prc.getClasses().add(UserGroupService.class);
prc.getClasses().add(UserLockoutService.class); prc.getClasses().add(UserLockoutService.class);
prc.getClasses().add(GroupService.class);
prc.getClasses().add(RESTExceptionMapper.class); prc.getClasses().add(RESTExceptionMapper.class);
} }
......
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