背景
公司需求为导出在线用户信息,其中第一步便是取到在线用户,本文选择去ejabberd网页取爬取在线用户页面,获得返回报文后进行截取获得用户账号。后续方便进行取缓存数据获取必要的数据为导出excel做准备。
主要代码
连接ejabberd,获取返回报文
/**
* 请求ejabbered集群管理页面的post请求方法
*
* @param url
* @param userNameAndPwd
* @return
*/
public static String sendPost(String urlAddress, String userNameAndPwd) {
String reponseStr = "";
BufferedReader in = null;
String line = "";
try {
URL url = new URL(urlAddress);
URLConnection connection = url.openConnection();
logger.debug("与ejabberd建立连接的地址为:"+urlAddress);
connection.setDoOutput(true);
connection.setRequestProperty("Pragma:", "no-cache");
connection.setRequestProperty("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
connection.setRequestProperty("Cache-Control", "no-cache");
connection.setRequestProperty("Content-Type", "text/xml");
connection.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
connection.setRequestProperty("Authorization", "Basic " + userNameAndPwd);
connection.connect();
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line = in.readLine()) != null) {
reponseStr += line;
}
logger.info("请求回执:" + reponseStr);
} catch (Exception e) {
logger.error(e);
}
return reponseStr;
}
返回报文样例
ejabberd Web Admin
ejabberd (c) 2002-2017
ProcessOne, leader in messaging and push solutions
报文为页面html,分为导航栏、内容等部分,我们关注内容部分即可;下面a标签内为我们想要的在线用户账号(多个用户则为多个a标签)
截取返回报文,获得在线用户账号Jid
package com.onewaveinc.utils;
//省略了import部分
...
/**
* 此类原为导出excel表格,其中获取在线用户账号为其中一步;
* 从ejabberd返回报文进行解析截取用户账号
* @author wxin
*
*/
public class ExportXMPPUserInfo {
private UserManager userManager;
private UserChannelLoginManager userChannelLoginManager;
private String path;
// private final static String CONNECTED_USERS ="connected_users";
/**
* 加载ejabbered集群的管理页面账号信息至内存
*/
private Map ejabberedUserMap=new HashMap();
/**
* 加载ejabbered集群的url
*/
private Map ejabberedUrlMap=new HashMap();
private String xmppManageListStr;
private ChannelManager channelManager;
private MemcachedFactory memcachedFactory;
private static Logger logger = Logger.getInstance(ExportXMPPUserInfo.class);
private String zipPath;
/**
* 定时导出XMPP每个机房(一个集群)的在线用户的信息
* 导出信息:用户账号,mac地址,登陆的IP,登陆域名,机顶盒的型号,版本,和以及登陆所在节点的ip,
* 显示 登陆的时间,登陆的时长(现在的时间减去登陆的时间)。
*/
public void getEjabberdJid() {
try {
List channelList = new ArrayList();
try {
channelList = channelManager.findChannelIdList();
} catch (Exception e) {
e.printStackTrace();
}
String serverName = "";
String serverDomain = "";
String urlAddress = "";
String userNameAndPwd = "";
String respStr = "";
String usersUrlAddress = "";
String usersRespStr = "";
for (Channel channel : channelList) {
serverName = channel.getName();
serverDomain = channel.getHost();
logger.info("此次处理的集群名称为:" + serverName);
//获取所有在线用户账号
urlAddress = ejabberedUrlMap.get(serverName)+"server/"+ serverDomain +"/online-users";
userNameAndPwd = ejabberedUserMap.get(serverName);
respStr = HttpUtil.sendPost(urlAddress, userNameAndPwd);
List jidList = new ArrayList();
jidList = getJidList(respStr);
}
} catch (Exception e) {
result = "failed";
e.printStackTrace();
}
return result;
}
/**
* 截取用户jidList
* @param respStr
* @return List jidList
*/
public List getJidList(String respStr) {
List jidList = new ArrayList();
int indexOne = respStr.indexOf("Online Users
")+"Online Users
".length();
int indexTwo = respStr.indexOf("