hi. am using openfire 3.7.1, i will create a new user from outside the openfire… i need how to create a new user via java applications or how to use userservice plugin in java…
coding…
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.util.log.Log;
import org.jivesoftware.admin.AuthCheckFilter;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.user.UserAlreadyExistsException;
import org.jivesoftware.openfire.user.UserNotFoundException;
public class UserServiceServlet extends HttpServlet{
UserServicePlugin plugin;
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
24 plugin = (UserServicePlugin) XMPPServer.getInstance().getPluginManager().getPlugin(“userservice”);
25 // Exclude this servlet from requiring the user to login
//AuthCheckFilter.addExclude(“userservice/userservice”);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
// Printwriter for writing out responses to browser
PrintWriter out = response.getWriter();
out.println(“Resgistration Successfully”);
/*if(!plugin.getAllowedIPs().isEmpty()){
// Get client’s IP address
String ipAddress = request.getHeader(“x-forwarded-for”);
if(ipAddress == null){
ipAddress = request.getHeader(“X_FORWARDED_FOR”);
if(ipAddress == null){
ipAddress = request.getHeader(“X-Forward-For”);
if(ipAddress == null){
ipAddress = request.getRemoteAddr();
}
}
}
System.out.println(“ipAddress in UserServiceServlet.java=”+ipAddress);
if(!plugin.getAllowedIPs().contains(ipAddress)){
System.out.println("User service rejected service to IP address: "+ipAddress);
Log.warn("User service rejected service to IP address: "+ipAddress);
replyError(“RequestNotAuthorised”, response, out);
return;
}
}
String username = request.getParameter(“username”);
String password = request.getParameter(“password”);
String name = request.getParameter(“name”);
String email = request.getParameter(“email”);
String type=“add”;
String secret=“JDzvHGSW”;
//String type = request.getParameter(“type”);
//String secret = request.getParameter(“secret”);
String groupNames = request.getParameter(“groups”);
//No defaults, add, delete, update only
//type = type == null ? “image” : type;
// Check that our plugin is enabled.
if (!plugin.isEnabled()) {
Log.warn("User service plugin is disabled: " + request.getQueryString());
replyError(“UserServiceDisabled”,response, out);
return;
}
// Check this request is authorised
if (secret == null || !secret.equals(plugin.getSecret())){
Log.warn("An unauthorised user service request was received: " + request.getQueryString());
replyError(“RequestNotAuthorised”,response, out);
return;
}
// Check the request type and process accordingly
try
{
if (“add”.equals(type)) {
plugin.createUser(username, password, name, email, groupNames);
replyMessage(“ok”,response, out);
//imageProvider.sendInfo(request, response, presence);
}else if (“delete”.equals(type)) {
plugin.deleteUser(username);
replyMessage(“ok”,response,out);
//xmlProvider.sendInfo(request, response, presence);
}else if (“update”.equals(type)) {
plugin.updateUser(username, password,name,email, groupNames);
replyMessage(“ok”,response,out);
//xmlProvider.sendInfo(request, response, presence);
}else {
Log.warn("The userService servlet received an invalid request of type: " + type);
// TODO Do something
}
}catch(UserAlreadyExistsException e) {
replyError(“UserAlreadyExistsException”,response, out);
}catch(UserNotFoundException e) {
replyError(“UserNotFoundException”,response, out);
}catch(IllegalArgumentException e) {
replyError(“IllegalArgumentException”,response, out);
}catch (Exception e) {
replyError(e.toString(),response, out);
}*/
}
private void replyMessage(String message,HttpServletResponse response, PrintWriter out){
response.setContentType(“text/xml”);
out.println("" + message + “”);
out.flush();
}
private void replyError(String error,HttpServletResponse response, PrintWriter out){
response.setContentType(“text/xml”);
out.println("" + error + “”);
out.flush();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(“Resquest=”+request);
System.out.println(“Response=”+response);
System.out.println(“request.getheader1=”+request.getHeader(“x-forwarded-for”));
System.out.println(“request.getheader2=”+request.getHeader(“X_FORWARDED_FOR”));
System.out.println(“request.getheader3=”+request.getHeader(“X-Forward-Forr”));
System.out.println(“getLocalAddr=”+request.getLocalAddr());
System.out.println(“getLocalName=”+request.getLocalName());
System.out.println(“getLocalPort=”+request.getLocalPort());
System.out.println(“getRemoteAddr=”+request.getRemoteAddr());
System.out.println(“getRemoteHost=”+request.getRemoteHost());
System.out.println(“getRemotePort=”+request.getRemotePort());
System.out.println(“getRemoteUser=”+request.getRemoteUser());
System.out.println(“getMethod=”+request.getMethod());
System.out.println(“getPathInfo=”+request.getPathInfo());
System.out.println(“getRequestURI=”+request.getRequestURI());
System.out.println(“getRequestURL=”+request.getRequestURL());
System.out.println(“getServerName=”+request.getServerName());
System.out.println(“getServerPort=”+request.getServerPort());
System.out.println(“getServletPath=”+request.getServletPath());
System.out.println(“getUserPrincipal=”+request.getUserPrincipal());
doGet(request, response);
}
public void destroy() {
super.destroy();
// Release the excluded URL
AuthCheckFilter.removeExclude(“userService/userservice”);
}
}
UserServiceplugin.java
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.StringTokenizer;
import org.apache.taglibs.standard.tag.common.core.SetSupport;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager;
import org.jivesoftware.openfire.group.Group;
import org.jivesoftware.openfire.group.GroupManager;
import org.jivesoftware.openfire.user.User;
import org.jivesoftware.openfire.user.UserAlreadyExistsException;
import org.jivesoftware.openfire.user.UserManager;
import org.jivesoftware.openfire.user.UserNotFoundException;
import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.PropertyEventDispatcher;
import org.jivesoftware.util.PropertyEventListener;
import org.jivesoftware.util.StringUtils;
import org.xmpp.packet.JID;
public class UserServicePlugin implements Plugin, PropertyEventListener{
UserManager userManager;
XMPPServer server;
String secret;
boolean enabled;
Collection allowedIPs;
public UserServicePlugin()
{
}
public void createUser(String username, String password, String name, String email, String groupNames)
throws UserAlreadyExistsException
{
userManager.createUser(username, password, name, email);
if(groupNames!=null)
{
Collection groups = new ArrayList();
StringTokenizer tkn=new StringTokenizer(groupNames, “,”);
while(tkn.hasMoreTokens())
{
try{
groups.add(GroupManager.getInstance().getGroup(tkn.nextToken()));
}catch (Exception e) {
// TODO: handle exception
}
for(Group group : groups){
group.getMembers().add(server.createJID(username, null));
}
}
}
}
public void deleteUser(String username) throws UserNotFoundException{
User user = getUser(username);
userManager.deleteUser(user);
}
public void updateUser(String username, String password, String name, String email, String groupNames)
throws UserNotFoundException
{
User user = getUser(username);
user.setPassword(password);
user.setName(name);
user.setEmail(email);
if(groupNames!=null){
Collection newGroups = new ArrayList();
StringTokenizer tkn = new StringTokenizer(groupNames, “,”);
while(tkn.hasMoreTokens()){
try{
newGroups.add(GroupManager.getInstance().getGroup(tkn.nextToken()));
}catch (Exception e) {
// TODO: handle exception
}
}
Collection existingGroups = GroupManager.getInstance().getGroups(user);
// Get the list of groups to add to the user
Collection groupsToAdd = new ArrayList(newGroups);
groupsToAdd.removeAll(existingGroups);
// Get the list of groups to remove from the user
Collection groupsToDelete = new ArrayList(existingGroups);
groupsToDelete.removeAll(newGroups);
// Add the user to the new groups
for(Group group : groupsToAdd){
group.getMembers().add(server.createJID(username, null));
}
// Remove the user from the old groups
for(Group group : groupsToDelete){
group.getMembers().remove(server.createJID(username, null));
}
}
}
/**
-
Returns the the requested user or null if there are any
-
problems that don’t throw an error.
-
-
@param username the username of the local user to retrieve.
-
@return the requested user.
-
@throws UserNotFoundException if the requested user
-
does not exist in the local server.
*/
private User getUser(String username) throws UserNotFoundException {
JID targetJID = server.createJID(username, null);
// Check that the sender is not requesting information of a remote server entity
if(targetJID.getNode()!=null){
// Sender is requesting presence information of an anonymous user
throw new UserNotFoundException(“Username is null”);
}
return userManager.getUser(targetJID.getNode());
}
//@Override
public void destroyPlugin() {
// TODO Auto-generated method stub
userManager=null;
// Stop listening to system property events
PropertyEventDispatcher.removeListener(this);
}
//@Override
public void initializePlugin(PluginManager arg0, File arg1) {
// TODO Auto-generated method stub
server=XMPPServer.getInstance();
System.out.println(“server in UserServicePlugin.java=”+server);
userManager=server.getUserManager();
System.out.println(“userManager in UserServicePlugin.java=”+userManager);
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);
// Get the list of IP addresses that can use this service. An empty list means that this filter is disabled.
allowedIPs=StringUtils.stringToCollection(JiveGlobals.getProperty(“plugin.users ervice.allowedIPs”,""));
// Listen to system property events
PropertyEventDispatcher.addListener(this);
}
//@Override
public void propertyDeleted(String property, Map<String, Object> params) {
// TODO Auto-generated method stub
if (property.equals(“plugin.userservice.secret”)) {
this.secret = “”;
}
else if (property.equals(“plugin.userservice.enabled”)) {
this.enabled = false;
}
else if (property.equals(“plugin.userservice.allowedIPs”)) {
this.allowedIPs = Collections.emptyList();
}
}
//@Override
public void propertySet(String property, Map<String, Object> params) {
// TODO Auto-generated method stub
if(property.equals(“plugin.userservice.secret”)){
this.secret = (String) params.get(“value”);
}else if(property.equals(“plugin.userservice.enabled”)){
this.enabled =Boolean.parseBoolean((String) params.get(“value”));
}else if(property.equals(“plugin.userservice.allowedIP”)){
this.allowedIPs =StringUtils.stringToCollection((String) params.get(“value”));
}
}
//@Override
public void xmlPropertyDeleted(String arg0, Map<String, Object> arg1) {
// TODO Auto-generated method stub
// Do nothing
}
//@Override
public void xmlPropertySet(String arg0, Map<String, Object> arg1) {
// TODO Auto-generated method stub
// Do nothing
}
/**
*/
public String getSecret() {
return secret;
}
/**
*/
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 Collection getAllowedIPs() {
return allowedIPs;
}
public void setAllowedIPs(Collection allowedIPs) {
JiveGlobals.setProperty(“plugin.userservice.allowedIPs”, StringUtils.collectionToString(allowedIPs));
this.allowedIPs = allowedIPs;
}
}
i have some following errors are occured after submitting … the error line is 24 in UserServiceServlet.java.
java.lang.NullPointerException
at jabbersmackapi.UserServiceServlet.init(UserServiceServlet.java:24)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1173)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:809)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java: 129)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java: 191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:10 9)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11P rotocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
Jun 14, 2012 4:07:57 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet UserServiceServlet
java.lang.NullPointerException
at jabbersmackapi.UserServiceServlet.init(UserServiceServlet.java:24)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1173)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:809)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java: 129)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java: 191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:10 9)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11P rotocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
the openfire userservice plugin is not connected using java… so pls guys any suggestions … rply me…