2013-08-17 23:46:22
原创作品,允许转载,转载时请务必以超链接形式标明文章
原始出处 、作者信息和本声明。否则将追究法律责任。
http://observer.blog.51cto.com/4267416/1275608
1
2
3
4
|
webservice.wsdl=http://localhost:8080/webServiceCXF/services/Service?wsdl
webservice.url=http://www.observer.com/service
webservice.encoding=utf-8
webservice.fieldname=result
|
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
|
public
class
TestWebServiceClient {
public
static
void
main(String[] args)
throws
FormatException {
ServicePropertiesUtil servicePropertiesUtil =
new
ServicePropertiesUtil(
"webservicetest"
);
//获取读取配置文件的ServicePropertiesUtil类
StringBuffer sb =
new
StringBuffer();
sb.append(
"<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:ser='"
+
servicePropertiesUtil.getWebServiceUrl()+
"'>"
);
sb.append(
"<soapenv:Header/>"
);
sb.append(
"<soapenv:Body>"
);
sb.append(
"<ser:getGradeName>"
);
sb.append(
"<toid>"
); sb.append(
123
); sb.append(
"</toid>"
);
sb.append(
"</ser:getGradeName>"
);
sb.append(
"</soapenv:Body>"
);
sb.append(
"</soapenv:Envelope>"
);
//访问webservcie时需要用到的xml
HttpClient httpClient =
new
HttpClient();
//新建一个HttpClient类
PostMethod postMethod=
new
PostMethod(servicePropertiesUtil.getWebServiceWsdl());
postMethod.setRequestHeader(
"SOAPAction"
,
"http://dao.wfservice.ws.emolay.com"
);
String html =
null
;
try
{
String out = sb.toString();
postMethod.setRequestEntity(
new
StringRequestEntity(out,
"text/xml"
,
servicePropertiesUtil.getWebServiceEncoding()));
//设置需要推送的数据
httpClient.executeMethod(postMethod);
//访问webservcie
html=postMethod.getResponseBodyAsString();
//获取访问webserivce反馈的xml
postMethod.releaseConnection();
}
catch
(HttpException e) {
e.printStackTrace();
throw
new
FormatException(
"连接异常1"
,
"服务器正在维护中,请稍后再试,或者联系服务器维护人员进行处理"
);
}
catch
(IOException e) {
throw
new
FormatException(
"连接异常2"
,
"服务器正在维护中,请稍后再试,或者联系服务器维护人员进行处理"
);
}
Document doc=Jsoup.parse(html);
//使用jsoup解析xml,得到webservice返回的数据
Elements elements=doc.select(servicePropertiesUtil.getWebServiceFieldname());
//根据webservcie的配置,解析xml中的数据
String [] datas =
new
String[elements.size()];
int
i =
0
;
for
(Element ele:elements){
datas[i] = ele.text();
i++;
}
//输出数据
for
(String d : datas){
System.out.println(d);
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
private
static
final
ThreadLocal<HttpSession> threadLocal =
new
ThreadLocal<HttpSession>();
public
static
void
setHttpSession(HttpSession session){
threadLocal.set(session);
}
public
static
void
removeSession(){
threadLocal.remove();
}
/**
* 验证权限,如果不是admin用户,返回状态码"-1"表示没有权限访问该方法
*/
public
String getGradeName(
long
toid) {
HttpSession session = threadLocal.get();
String user = (String)session.getAttribute(
"user"
);
if
(
"admin"
.equals(user)){
System.out.println(
"业务逻辑"
);
}
else
{
return
"-1"
;
}
return
"is succeed"
;
}
|
1
2
3
4
5
6
7
8
9
10
|
<!-- webserivce过滤器 -->
<
filter
>
<
filter-name
>webserviceFilter</
filter-name
>
<
filter-class
>com.observer.cxf.login.filter.LoginFilter</
filter-class
>
</
filter
>
<
filter-mapping
>
<
filter-name
>webserviceFilter</
filter-name
>
<
url-pattern
>/services/*</
url-pattern
>
</
filter-mapping
>
<!-- webserivce过滤器 end -->
|
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
|
public
class
LoginFilter
implements
Filter{
public
void
destroy() {
}
/**
* 过滤器的过滤方法
* 当传过来的html头文件中有islogin并且为true时表示用户需要登录,那么就验证登录
* 当传过来的html头文件中有islogin并且为false时表示用户需要调用webservice,
* 那么首先验证是否已经登录,如果已经登录就继续,如果没有登录就直接返回notlogin提示用户没有登录
*/
public
void
doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2)
throws
IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)arg0;
HttpServletResponse response = (HttpServletResponse)arg1;
HttpSession session = request.getSession();
String islogin = request.getHeader(
"islogin"
);
if
(
"false"
.equals(islogin)){
//判断是要登录还是要访问webservice
String user = (String) session.getAttribute(
"user"
);
if
(user !=
null
){
//判断是否已经登录
GradeServiceImpl.setHttpSession(session);
//访问webservice之前将session放到webservice实现类中
arg2.doFilter(request, response);
GradeServiceImpl.removeSession();
//webservice访问完成后移除里面的session
return
;
}
else
{
PrintWriter out = response.getWriter();
out.println(
"notlogin"
);
out.flush();
out.close();
}
}
else
if
(
"true"
.equals(islogin)){
String username = request.getHeader(
"username"
);
String password = request.getHeader(
"password"
);
//的到头文件中传过来的username与password
String user = LoginManger.login(username, password);
PrintWriter out = response.getWriter();
if
(user!=
null
){
session.setAttribute(
"user"
, user);
out.println(
"islogin"
);
}
else
{
out.println(
"notlogin"
);
}
out.flush();
out.close();
}
}
public
void
init(FilterConfig arg0)
throws
ServletException {
}
}
|
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
|
public
abstract
class
WebServiceClient {
public
static
ServicePropertiesUtil servicePropertiesUtil =
new
ServicePropertiesUtil(
"webservice"
);
private
static
String tmpcookies=
""
;
/**
* 向webservcie发送请求
* @param outXml 请求所需要推送的xml数据
* @return 请求反馈的信息
* @throws FormatException 当webservice服务器关闭或链接不可以等原因造成请求失败时,
* 抛出com.observer.webservcie.error.FormatException
*/
private
String executeMethod(String outXml)
throws
FormatException{
HttpClient httpClient =
new
HttpClient();
PostMethod postMethod=
new
PostMethod(servicePropertiesUtil.getWebServiceWsdl());
postMethod.setRequestHeader(
"Cookie"
, tmpcookies);
postMethod.setRequestHeader(
"islogin"
,
"false"
);
postMethod.setRequestHeader(
"SOAPAction"
,
"http://dao.wfservice.ws.emolay.com"
);
String html =
null
;
try
{
String out = outXml;
postMethod.setRequestEntity(
new
StringRequestEntity(out,
"text/xml"
,
servicePropertiesUtil.getWebServiceEncoding()));
httpClient.executeMethod(postMethod);
html=postMethod.getResponseBodyAsString();
}
catch
(HttpException e) {
e.printStackTrace();
throw
new
FormatException(
"连接异常1"
,
"服务器正在维护中,请稍后再试,或者联系服务器维护人员进行处理"
);
}
catch
(IOException e) {
throw
new
FormatException(
"连接异常2"
,
"服务器正在维护中,请稍后再试,或者联系服务器维护人员进行处理"
);
}
finally
{
postMethod.releaseConnection();
}
return
html;
}
/**
* 向webservcie服务器发送登录请求
* @param username 用户名
* @param password 用户密码
* @return 登录成功后返回服务器反馈的信息:islogin
* @throws FormatException 当webservice服务器关闭或链接不可以等原因造成请求失败或登录失败时,
* 抛出com.observer.webservcie.error.FormatException
*/
public
String login(String username, String password)
throws
FormatException{
HttpClient httpClient =
new
HttpClient();
PostMethod postMethod=
new
PostMethod(servicePropertiesUtil.getWebServiceWsdl());
postMethod.setRequestHeader(
"islogin"
,
"true"
);
postMethod.setRequestHeader(
"username"
,username);
postMethod.setRequestHeader(
"password"
,password);
String html =
null
;
try
{
httpClient.executeMethod(postMethod);
html=postMethod.getResponseBodyAsString();
if
(html!=
null
&&
"notlogin"
.equals(html.trim())){
throw
new
FormatException(
"登录失败"
,
"用户名密码错误"
);
}
Cookie[] cookies = httpClient.getState().getCookies();
StringBuffer sb =
new
StringBuffer();
for
(Cookie c : cookies) {
sb.append(c.toString() +
";"
);
}
synchronized
(
this
){
tmpcookies = sb.toString();
}
}
catch
(HttpException e) {
e.printStackTrace();
throw
new
FormatException(
"连接异常1"
,
"服务器正在维护中,请稍后再试,或者联系服务器维护人员进行处理"
);
}
catch
(IOException e) {
throw
new
FormatException(
"连接异常2"
,
"服务器正在维护中,请稍后再试,或者联系服务器维护人员进行处理"
);
}
finally
{
postMethod.releaseConnection();
}
return
html;
}
/**
* 将服务器反馈回来的xml解析出结果来
* @param xml 服务器返回的xml
* @return 解析出来的结果集
*/
private
String[] parseData(String xml){
Document doc=Jsoup.parse(xml);
Elements elements=doc.select(servicePropertiesUtil.getWebServiceFieldname());
String [] datas =
new
String[elements.size()];
int
i =
0
;
for
(Element ele:elements){
datas[i] = ele.text();
i++;
}
return
datas;
}
/**
* 想webservice方式请求,并且解析webservice反馈的xml得到结果
* @param outXml 需要请求发送的xml
* @return 返回webservice反馈的xml解析之后的结果
* @throws FormatException 当用户没有登录或者
* 服务器已经关闭时抛出com.observer.webservcie.error.FormatException
*/
public
String[] send(String outXml)
throws
FormatException{
String xml = executeMethod(outXml);
if
(
"notlogin"
.equals(xml.trim())){
throw
new
FormatException(
"登陆异常"
,
"对不起您没有登录"
);
}
return
parseData(xml);
}
/**
* 获取webservice中getGradeName方法所需要的xml的抽象方法,该方法让子类重写,
* 让其可以在服务器参数更改是灵活的更改
* @param data 获取webservice中getGradeName所需要的参数
* @return webservice中getGradeName方法所需要的xml
* @throws FormatException 当参数不符合规则时
* 抛出com.observer.webservcie.error.FormatException
*/
public
abstract
String getGradeNameXml(String... data)
throws
FormatException;
/**
* 想webservice请求getGradeName方法的模板方法
* @param data 获取webservice中getGradeName所需要的参数
* @return 返回webservice的getGradeName方法返回的数据
* @throws FormatException 当连接失败或者没有登录或者没有那个权限进行访问该方法时
* 抛出com.observer.webservcie.error.FormatException
*/
public
String[] getGradeName(String... data)
throws
FormatException{
String outXml = getGradeNameXml(data);
String[] datas = send(outXml);
if
(datas.length>
0
&&
"-1"
.equals(datas[
0
])){
throw
new
FormatException(
"操作异常"
,
"对不起你没有权限进行此操作"
);
}
return
datas;
}
}
|
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
|
public
class
WebServiceClientImpl
extends
WebServiceClient {
/**
* 获取webservice中getGradeName方法所需要的xml的实现方法
* @param data 获取webservice中getGradeName所需要的参数
* @return webservice中getGradeName方法所需要的xml
* @throws FormatException 当参数不符合规则时
* 抛出com.observer.webservcie.error.FormatException
*/
public
String getGradeNameXml(String... toid)
throws
FormatException{
int
id;
try
{
id = Integer.valueOf(toid[
0
]);
}
catch
(Exception e) {
throw
new
FormatException(
"参数错误"
,
"输入的参数必须为整数字符串"
);
}
StringBuffer sb =
new
StringBuffer();
sb.append(
"<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:ser='"
+
servicePropertiesUtil.getWebServiceUrl()+
"'>"
);
sb.append(
"<soapenv:Header/>"
);
sb.append(
"<soapenv:Body>"
);
sb.append(
"<ser:getGradeName>"
);
sb.append(
"<toid>"
); sb.append(id); sb.append(
"</toid>"
);
sb.append(
"</ser:getGradeName>"
);
sb.append(
"</soapenv:Body>"
);
sb.append(
"</soapenv:Envelope>"
);
return
sb.toString();
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public
class
TestClient {
public
static
void
main(String[] args) {
WebServiceClient webServiceClient =
new
WebServiceClientImpl();
String[] datas1 =
null
;
String[] datas2 =
null
;
try
{
String login = webServiceClient.login(
"admin"
,
"admin"
);
System.out.println(login);
datas1 = webServiceClient.getGradeName(
"123"
);
datas2 = webServiceClient.getGradeName(
"2222"
);
}
catch
(FormatException e) {
e.printStackTrace();
System.out.println(e.getDetails());
return
;
}
for
(String data : datas1){
System.out.println(data);
}
for
(String data : datas2){
System.out.println(data);
}
}
}
|