SoapCollThread.java
5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.sitech.ismp.coll.busi;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import bsh.Interpreter;
import com.sitech.ismp.coll.CollBase;
import com.sitech.util.Formater;
public class SoapCollThread implements Runnable {
static Logger logger = Logger.getLogger("BUSI_COLL");
private String unitId = "";
private String keyWord = "";
private String type = "";
private String url = "";
private String[] a_params = null;
private int timeout;
private String expression = "";
private String xmlBody = "";
/** 采集周期 */
private String interval;
public SoapCollThread(HashMap<String, String> params) {
interval = params.get("COLL_INTERVAL");
keyWord = params.get("CONTAINSKEYWORD");
type = params.get("MEANS");
url = params.get("ADDR");
a_params = params.get("PARAMS").split("#");
unitId = params.get("KBPCLASS") + Formater.neatenunitid(url);
String sTimeout = params.get("TIMEOUT");
if (sTimeout == null || sTimeout.trim().equals("")) {
timeout = 60000;
} else {
timeout = Integer.parseInt(sTimeout) * 1000;
}
xmlBody = params.get("XML_BODY");
expression = params.get("EXPRESSION");
}
public void run() {
CollBase collResult = new CollBase();
String info = "TIMEOUT";
String content = "";
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setSoTimeout(timeout);
try {
if (type != null && type.toLowerCase().equals("post")) {
PostMethod method = new PostMethod(url);
method.addRequestHeader("SOAPAction", "");
if (null!=a_params && a_params.length>0) {
for (String str: a_params) {
String[] temp = str.split("=");
if (temp.length==2) {
method.addParameter(temp[0], temp[1]);
}
}
}
if (xmlBody != null && !xmlBody.trim().equals("") && !xmlBody.trim().equals("null")) {
Document document = DocumentHelper.parseText(xmlBody);
System.out.println(document.asXML());
byte[] bs=xmlBody.getBytes("UTF-8");
bs=document.asXML().getBytes("UTF-8");
InputStream is = new ByteArrayInputStream(bs,0,bs.length);
RequestEntity re=new InputStreamRequestEntity(is,bs.length,"text/xml;charset=utf-8");
method.setRequestEntity(re);
}
client.executeMethod(method);
content = method.getResponseBodyAsString();
} else {
GetMethod method = new GetMethod(url);
method.addRequestHeader("SOAPAction", "");
client.executeMethod(method);
content = method.getResponseBodyAsString();
}
if (keyWord != null && !keyWord.equals("")) {
if (content.indexOf("SAXParseException") == -1 && content.indexOf("Error 404") == -1 && content.indexOf("Error 500") == -1) {
// 获取标签体内容
Document document = DocumentHelper.parseText(content);
Element rootElm = document.getRootElement();
String result = getValue(rootElm, keyWord);
// 查询关键字
if (expression != null && !expression.equals("")) {
String EXPRESSION = "result = " + expression;
Interpreter interpreter = new Interpreter();
interpreter.set("RESULT", result);
interpreter.eval(EXPRESSION);
Boolean bool = (Boolean) interpreter.get("result");
System.out.println("bool:" + bool);
if (bool) {
info = "SUCCESS";
} else {
info = "FAIL";
}
} else {
info = "SUCCESS";
}
} else if (content.indexOf("Error 4") != -1) {
info = "FAIL";// info = "找不到对应请求地址!";
} else if (content.indexOf("Error 5") != -1) {
info = "FAIL";// info = "访问服务器失败,请检查请求的URL是否合法!";
} else if (content.indexOf("SAXParseException") != -1) {
info = "FAIL";// info = "报文格式错误,请核对报文格式是否合法!";
}
}
} catch (Exception e) {
logger.error("Exception while collSoap", e);
}
collResult.addKPI(unitId, "CM-01-11-010-01", url, interval);
collResult.addKPI(unitId, "PM-01-11-010-01", info, interval);
collResult.saveKPI2File();
}
@SuppressWarnings("unchecked")
private static String getValue(Element e, String ss) {
String s = "";
String name = e.getName();
String text = e.getText();
if (name.equals(ss)) {
s = e.getStringValue();
} else if (text.trim().indexOf(ss) != -1) {
s = text;
Document document1;
try {
document1 = DocumentHelper.parseText(s);
Element rootElm1 = document1.getRootElement();
s = getValue(rootElm1, ss);
} catch (DocumentException e1) {
e1.printStackTrace();
}
} else {
if (e.elements() != null) {
Iterator i = e.elementIterator();
while (i.hasNext()) {
Element e2 = (Element) i.next();
s = getValue(e2, ss);
if (!s.equals("")) {
break;
}
}
}
}
return s;
}
}