📄 userserviceservlet.java
字号:
/**
* $RCSfile$
* $Revision: 1710 $
* $Date: 2005-07-26 11:56:14 -0700 (Tue, 26 Jul 2005) $
*
* Copyright (C) 2004 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.userService;
import org.jivesoftware.wildfire.XMPPServer;
import org.jivesoftware.wildfire.user.UserNotFoundException;
import org.jivesoftware.wildfire.user.UserAlreadyExistsException;
import org.jivesoftware.util.Log;
import org.jivesoftware.wildfire.plugin.UserServicePlugin;
import org.jivesoftware.admin.AuthCheckFilter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Servlet that addition/deletion/modification of the users info in the system.
* Use the <b>type</b>
* parameter to specify the type of action. Possible values are <b>add</b>,<b>delete</b> and
* <b>update</b>. <p>
* <p/>
* The request <b>MUST</b> include the <b>secret</b> parameter. This parameter will be used
* to authenticate the request. If this parameter is missing from the request then
* an error will be logged and no action will occur.
*
* @author Justin Hunt
*/
public class UserServiceServlet extends HttpServlet {
private UserServicePlugin plugin;
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
plugin = (UserServicePlugin) XMPPServer.getInstance().getPluginManager().getPlugin("userservice");
// Exclude this servlet from requiring the user to login
AuthCheckFilter.addExclude("userService/userservice");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String username = request.getParameter("username");
String password = request.getParameter("password");
String name = request.getParameter("name");
String email = request.getParameter("email");
String jid = request.getParameter("jid");
String type = request.getParameter("type");
String secret = request.getParameter("secret");
//No defaults, add, delete, update only
//type = type == null ? "image" : type;
// Printwriter for writing out responses to browser
PrintWriter out = response.getWriter();
// 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);
replyMessage("ok",response, out);
//imageProvider.sendInfo(request, response, presence);
}
else if ("delete".equals(type)) {
plugin.deleteUser(jid);
replyMessage("ok",response,out);
//xmlProvider.sendInfo(request, response, presence);
}
else if ("update".equals(type)) {
plugin.updateUser(jid, password,name,email);
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("<result>" + message + "</result>");
out.flush();
}
private void replyError(String error,HttpServletResponse response, PrintWriter out){
response.setContentType("text/xml");
out.println("<error>" + error + "</error>");
out.flush();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public void destroy() {
super.destroy();
// Release the excluded URL
AuthCheckFilter.removeExclude("userService/userservice");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -