As I’‘m new to this community I don’‘t know any other channel so I’'ll post here my code for peer review. First thing I did was to add the group manager to the UserServicePlugin:
/**
- Copyright © 2006 Jive Software. All rights reserved.
-
This software is published under the terms of the GNU Public License (GPL),
-
a copy of which is included in this distribution.
*/
package org.jivesoftware.wildfire.plugin;
import org.jivesoftware.wildfire.container.Plugin;
import org.jivesoftware.wildfire.container.PluginManager;
import org.jivesoftware.wildfire.group.Group;
import org.jivesoftware.wildfire.group.GroupAlreadyExistsException;
import org.jivesoftware.wildfire.group.GroupManager;
import org.jivesoftware.wildfire.group.GroupNotFoundException;
import org.jivesoftware.wildfire.user.UserManager;
import org.jivesoftware.wildfire.user.UserNotFoundException;
import org.jivesoftware.wildfire.user.UserAlreadyExistsException;
import org.jivesoftware.wildfire.user.User;
import org.jivesoftware.wildfire.XMPPServer;
import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.StringUtils;
import org.xmpp.packet.JID;
import java.io.File;
/**
- Plugin that allows the administration of users via HTTP requests.
*/
public class UserServicePlugin implements Plugin {
private UserManager userManager;
private GroupManager groupManager;
private String hostname;
private String secret;
private boolean enabled;
public void initializePlugin(PluginManager manager, File pluginDirectory) {
XMPPServer server = XMPPServer.getInstance();
userManager = server.getUserManager();
groupManager = GroupManager.getInstance();
hostname = server.getServerInfo().getName();
secret = JiveGlobals.getProperty(“plugin.userservice.secret”, “”);
// If no secret key has been assigned to the user service yet, assign a random one.
if (secret.equals("")){
secret = StringUtils.randomString(8);
setSecret(secret);
}
// See if the service is enabled or not.
enabled = JiveGlobals.getBooleanProperty(“plugin.userservice.enabled”, false);
}
public void destroyPlugin() {
userManager = null;
}
public void createUser(String username, String password, String name, String email)
throws UserAlreadyExistsException
{
userManager.createUser(username, password, name, email);
}
public void deleteUser(String jid) throws UserNotFoundException{
User user = getUser(jid);
userManager.deleteUser(user);
}
public void updateUser(String jid, String password, String name, String email)
throws UserNotFoundException
{
User user = getUser(jid);
user.setPassword(password);
user.setName(name);
user.setEmail(email);
}
/**
-
@param jid the bare JID of the entity whose presence is being probed.
-
@return the requested user.
-
@throws UserNotFoundException if the requested user
-
does not exist in the local server.
*/
private User getUser(String jid) throws UserNotFoundException {
JID targetJID = new JID(jid);
// Check that the sender is not requesting information of a remote server entity
if (targetJID.getDomain() == null || XMPPServer.getInstance().isRemote(targetJID)) {
throw new UserNotFoundException(“Domain does not matches local server domain”);
}
if (!hostname.equals(targetJID.getDomain())) {
// Sender is requesting information about component presence
// TODO Implement this
throw new UserNotFoundException(“Presence of components not supported yet!”);
}
if (targetJID.getNode() == null) {
// Sender is requesting presence information of an anonymous user
throw new UserNotFoundException(“Username is null”);
}
return userManager.getUser(targetJID.getNode());
}
/**
- Returns the secret key that only valid requests should know.
*/
public String getSecret() {
return secret;
}
/**
- Sets the secret key that grants permission to use the userservice.
-
@param secret the secret key.
*/
public void setSecret(String secret) {
JiveGlobals.setProperty(“plugin.userservice.secret”, secret);
this.secret = secret;
}
/**
-
Returns true if the user service is enabled. If not enabled, it will not accept
-
requests to create new accounts.
-
@return true if the user service is enabled.
*/
public boolean isEnabled() {
return enabled;
}
/**
-
Enables or disables the user service. If not enabled, it will not accept
-
requests to create new accounts.
-
@param enabled true if the user service should be enabled.
*/
public void setEnabled(boolean enabled) {
this.enabled = enabled;
JiveGlobals.setProperty(“plugin.userservice.enabled”, enabled ? “true” : “false”);
}
public void createGroup(String name) throws GroupAlreadyExistsException{
groupManager.createGroup(name);
}
public void deleteGroup(String name) throws GroupNotFoundException{
Group group = groupManager.getGroup(name);
groupManager.deleteGroup(group);
}
}
Next my first idea was to create a webservice and as I’‘m under a deadline I wanted to use the new JSR 181 annotations to create one (I’'m deploying under JBoss 4.0.4, it has support for the JSR 181 (that is, I hope so, docs say it does) and expose the manager functions through it.
package org.jivesoftware.wildfire.plugin.userService;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import org.jivesoftware.wildfire.XMPPServer;
import org.jivesoftware.wildfire.group.GroupAlreadyExistsException;
import org.jivesoftware.wildfire.group.GroupNotFoundException;
import org.jivesoftware.wildfire.plugin.UserServicePlugin;
import org.jivesoftware.wildfire.user.UserAlreadyExistsException;
import org.jivesoftware.wildfire.user.UserNotFoundException;
@WebService(name = “EndpointInterface”,targetNamespace = “http://org.jboss.ws/samples/jsr181pojo",serviceName="TestService”)
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class UserServiceWS {
private UserServicePlugin plugin;
public UserServiceWS(){
plugin = (UserServicePlugin) XMPPServer.getInstance().getPluginManager().getPlugin(“userservice”);
}
@WebMethod
public void createUser(String username, String password, String name, String email){
try {
plugin.createUser(username, password, name, email);
} catch (UserAlreadyExistsException e) {
e.printStackTrace();
}
}
@WebMethod
public void deleteUser(String jid) {
try {
plugin.deleteUser(jid);
} catch (UserNotFoundException e) {
e.printStackTrace();
}
}
@WebMethod
public void updateUser(String jid, String password, String name, String email){
try {
plugin.updateUser(jid, password, name, email);
} catch (UserNotFoundException e) {
e.printStackTrace();
}
}
@WebMethod
public void createGroup(String name){
try {
plugin.createGroup(name);
} catch (GroupAlreadyExistsException e) {
e.printStackTrace();
}
}
@WebMethod
public void deleteGroup(String name){
try {
plugin.deleteGroup(name);
} catch (GroupNotFoundException e) {
e.printStackTrace();
}
}
}
I’‘ve also changed the web-custom.xml to this (also i switched from the dtd to the j2ee 2.4 schema, perhaps it’‘s a bad mix with the main webapp’'s xml?)
I have built the plugins and on deployment the Warn log said:
2006.05.27 02:33:55 Could not load userservice/*: not a servlet.
I’‘m aware that I’‘ve mixed a bunch of new fraemeworks, perhaps incompatible with WildFire. However this could bring a whole new level of integration of java (actually any WS aware language, .net, php … ) apps with the wildfire server. Can I have some of the developers comment on this solution and what can I do to make it work? I’‘m under a deadline and we must integrate this as a solution for the next week so if someone is willing to help it’‘ll save our butt’‘s . If we integrate this I’'ll try to push the issue to the manager on getting support from jive.
I’‘m re-releasing this code based on gpl under gpl, I didn’‘t put my name anywhere. Also, various approaches could have been made on exception handling on the web service (exceptions yes/no, should I return something or void so that I return immediately). I’‘ve chosen void as it’‘s the simplest and my other app’‘s thread won’‘t get blocked but the user should have various choices/implementations. I don’‘t mind if anyone that want’‘s to help chooses another framework (axis, axis2, xfire… ) or servlet/application server (glassfish… ). I chose the new annotation model as I don’'t have experience with axis based web services. If you guys know to do the service with axis go at it.