生活随笔
收集整理的這篇文章主要介紹了
WebServices中使用cxf开发日志拦截器以及自定义拦截器
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
首先下載一個cxf實例,里面包含cxf的jar包。我下的是apache-cxf-2.5.9
1、為什么要設(shè)置攔截器?
為了在webservice請求過程中,能動態(tài)操作請求和響應(yīng)數(shù)據(jù),?CXF設(shè)計了攔截器.
2、攔截器分類
1.?按所處的位置分:服務(wù)器端攔截器,客戶端攔截器
2.?按消息的方向分:入攔截器,出攔截器
3.?按定義者分:系統(tǒng)攔截器,自定義攔截器
3、攔截器API
Interceptor(攔截器接口)
AbstractPhaseInterceptor(自定義攔截器從此繼承)
LoggingInInterceptor(系統(tǒng)日志入攔截器類)
LoggingOutInterceptor(系統(tǒng)日志出攔截器類)
4、編寫實現(xiàn)攔截器
??使用日志攔截器,實現(xiàn)日志記錄
–?LoggingInInterceptor
–?LoggingOutInterceptor
??使用自定義攔截器,實現(xiàn)用戶名與密碼的檢驗
–?服務(wù)器端的in攔截器
–?客戶端的out攔截器
–?benjamin/123456
系統(tǒng)日志攔截器代碼實現(xiàn):
Server:
SEI:
[html]?view plaincopy print?
package?com.wiseweb.ws;????import?javax.jws.WebMethod;??import?javax.jws.WebService;????/**???*?SEI???*?@author?piqiu???*???*/??@WebService??public?interface?HelloWS?{????????@WebMethod??????public?String?sayHello(String?name)?;??}?? SEI的實現(xiàn):
[html]?view plaincopy print?
package?com.wiseweb.ws;????import?javax.jws.WebService;????/**???*?SEI的實現(xiàn)???*?@author?piqiu???*???*/??@WebService??public?class?HelloWSImpl?implements?HelloWS?{????????@Override??????public?String?sayHello(String?name)?{??????????System.out.println("server?sayHello():"?+?name);??????????return?"Hello:?"?+?name;??????}????}?? ServerTest:
[html]?view plaincopy print?
package?com.wiseweb.ws.server;????import?java.util.List;????import?javax.xml.ws.Endpoint;????import?org.apache.cxf.interceptor.Interceptor;??import?org.apache.cxf.interceptor.LoggingInInterceptor;??import?org.apache.cxf.interceptor.LoggingOutInterceptor;??import?org.apache.cxf.jaxws22.EndpointImpl;??import?org.apache.cxf.message.Message;????import?com.wiseweb.ws.HelloWSImpl;????/**???*?發(fā)布webservice???*?@author?piqiu???*???*/??public?class?ServerTest?{????????public?static?void?main(String[]?args)?{??????????String?address?=?"http://10.211.55.3:8888/day01_ws/hellows"?;??????????Endpoint?endpoint?=?Endpoint.publish(address,?new?HelloWSImpl())?;??????????EndpointImpl?endpointImpl?=?(EndpointImpl)?endpoint?;????????????????????List<Interceptor<??extends?Message>>?inInterceptors?=?endpointImpl.getInInterceptors()?;??????????inInterceptors.add(new?LoggingInInterceptor())?;????????????????????List<Interceptor<??extends?Message>>?outInterceptors?=?endpointImpl.getOutInterceptors()?;??????????outInterceptors.add(new?LoggingOutInterceptor())?;????????????????????System.out.println("發(fā)布webservice成功!");??????}??}?? Client: 把下載下來的apache-cxf-2.5.9的bin目錄配置到系統(tǒng)環(huán)境變量的path中去,以便可以在cmd中執(zhí)行bin中的bat文件
在cmd中輸入wsdl2java SEI地址就可以生成客戶端代碼了,同樣也可以使用wsimport命令。
項目截圖:
ClientTest:
[html]?view plaincopy print?
package?com.wiseweb.client;????import?java.util.List;????import?org.apache.cxf.endpoint.Client;??import?org.apache.cxf.frontend.ClientProxy;??import?org.apache.cxf.interceptor.Interceptor;??import?org.apache.cxf.interceptor.LoggingInInterceptor;??import?org.apache.cxf.interceptor.LoggingOutInterceptor;??import?org.apache.cxf.message.Message;????import?com.wiseweb.ws.HelloWS;??import?com.wiseweb.ws.HelloWSImplService;????public?class?ClientTest?{????????public?static?void?main(String[]?args)?{??????????HelloWSImplService?helloWSImplService?=?new?HelloWSImplService()?;??????????HelloWS?helloWS?=?helloWSImplService.getHelloWSImplPort()?;????????????????????Client?client?=?ClientProxy.getClient(helloWS)?;??????????List<Interceptor<??extends?Message>>?outInterceptors?=?client.getOutInterceptors()?;??????????outInterceptors.add(new?LoggingOutInterceptor())?;????????????????????List<Interceptor<??extends?Message>>?inInterceptors?=?client.getInInterceptors()?;??????????inInterceptors.add(new?LoggingInInterceptor())?;????????????????????String?result?=?helloWS.sayHello("benjaminwhx")?;??????????System.out.println(result);????????????????}??}?? 運行結(jié)果Server端和Client端比較: Client:
[html]?view plaincopy print?
信息:?Outbound?Message??---------------------------??ID:?1??Address:?http://10.211.55.3:8888/day01_ws/hellows??Encoding:?UTF-8??Content-Type:?text/xml??Headers:?{Accept=[*/*],?SOAPAction=[""]}??Payload:?<soap:Envelope?xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHello?xmlns:ns2="http://ws.wiseweb.com/"><arg0>benjaminwhx</arg0></ns2:sayHello></soap:Body></soap:Envelope>??--------------------------------------??三月?03,?2015?11:03:17?上午?org.apache.cxf.services.HelloWSImplService.HelloWSImplPort.HelloWS??信息:?Inbound?Message??----------------------------??ID:?1??Response-Code:?200??Encoding:?UTF-8??Content-Type:?text/xml;charset=UTF-8??Headers:?{Content-Length=[224],?content-type=[text/xml;charset=UTF-8],?Server=[Jetty(7.5.4.v20111024)]}??Payload:?<soap:Envelope?xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHelloResponse?xmlns:ns2="http://ws.wiseweb.com/"><return>Hello:?benjaminwhx</return></ns2:sayHelloResponse></soap:Body></soap:Envelope>??--------------------------------------??Hello:?benjaminwhx?? Server:
[html]?view plaincopy print?
發(fā)布webservice成功!??三月?03,?2015?11:03:15?上午?org.apache.cxf.services.HelloWSImplService.HelloWSImplPort.HelloWS??信息:?Inbound?Message??----------------------------??ID:?1??Address:?http://10.211.55.3:8888/day01_ws/hellows?wsdl??Encoding:?UTF-8??Http-Method:?GET??Content-Type:?text/xml??Headers:?{Accept=[*/*],?Cache-Control=[no-cache],?connection=[keep-alive],?content-type=[text/xml],?Host=[10.211.55.3:8888],?Pragma=[no-cache],?User-Agent=[Apache?CXF?2.5.9]}??--------------------------------------??三月?03,?2015?11:03:16?上午?org.apache.cxf.services.HelloWSImplService.HelloWSImplPort.HelloWS??信息:?Inbound?Message??----------------------------??ID:?2??Address:?http://10.211.55.3:8888/day01_ws/hellows??Encoding:?UTF-8??Http-Method:?POST??Content-Type:?text/xml;?charset=UTF-8??Headers:?{Accept=[*/*],?Cache-Control=[no-cache],?connection=[keep-alive],?Content-Length=[197],?content-type=[text/xml;?charset=UTF-8],?Host=[10.211.55.3:8888],?Pragma=[no-cache],?SOAPAction=[""],?User-Agent=[Apache?CXF?2.5.9]}??Payload:?<soap:Envelope?xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHello?xmlns:ns2="http://ws.wiseweb.com/"><arg0>benjaminwhx</arg0></ns2:sayHello></soap:Body></soap:Envelope>??--------------------------------------??server?sayHello():benjaminwhx??三月?03,?2015?11:03:17?上午?org.apache.cxf.services.HelloWSImplService.HelloWSImplPort.HelloWS??信息:?Outbound?Message??---------------------------??ID:?2??Encoding:?UTF-8??Content-Type:?text/xml??Headers:?{}??Payload:?<soap:Envelope?xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHelloResponse?xmlns:ns2="http://ws.wiseweb.com/"><return>Hello:?benjaminwhx</return></ns2:sayHelloResponse></soap:Body></soap:Envelope>??--------------------------------------?? 自定義攔截器代碼實現(xiàn):
Server:
SEI和SEI實現(xiàn)都不做變動,增加一個interceptor:
[html]?view plaincopy print?
package?com.wiseweb.ws.interceptor;????import?javax.xml.namespace.QName;????import?org.apache.cxf.binding.soap.SoapMessage;??import?org.apache.cxf.headers.Header;??import?org.apache.cxf.interceptor.Fault;??import?org.apache.cxf.phase.AbstractPhaseInterceptor;??import?org.apache.cxf.phase.Phase;??import?org.w3c.dom.Element;????public?class?CheckUserInterceptor?extends?AbstractPhaseInterceptor<SoapMessage>{????????public?CheckUserInterceptor()?{??????????super(Phase.PRE_PROTOCOL);??????}????????@Override??????public?void?handleMessage(SoapMessage?message)?throws?Fault?{??????????Header?header?=?message.getHeader(new?QName("wiseweb"))?;??????????if(header?!=?null)?{??????????????Element?element?=?(Element)header.getObject()?;??????????????String?username?=?element.getElementsByTagName("username").item(0).getTextContent()?;??????????????String?password?=?element.getElementsByTagName("password").item(0).getTextContent()?;??????????????if(username.equals("benjamin")?&&?password.equals("123456"))?{??????????????????System.out.println("用戶名與密碼正確,通過驗證!");??????????????????return?;??????????????}else?{??????????????????throw?new?Fault(new?RuntimeException("請輸入正確的用戶名和密碼!"))?;??????????????}??????????}else?{??????????????throw?new?Fault(new?RuntimeException("請輸入用戶名和密碼!"))?;??????????}??????}????}?? ServerTest:
[html]?view plaincopy print?
package?com.wiseweb.ws.server;????import?java.util.List;????import?javax.xml.ws.Endpoint;????import?org.apache.cxf.interceptor.Interceptor;??import?org.apache.cxf.jaxws22.EndpointImpl;??import?org.apache.cxf.message.Message;????import?com.wiseweb.ws.HelloWSImpl;??import?com.wiseweb.ws.interceptor.CheckUserInterceptor;????public?class?ServetTest2?{????????public?static?void?main(String[]?args)?{??????????String?address?=?"http://10.211.55.3:8888/day01_ws/hellows"?;??????????Endpoint?endpoint?=?Endpoint.publish(address,?new?HelloWSImpl())?;??????????EndpointImpl?endpointImpl?=?(EndpointImpl)?endpoint?;????????????????????List<Interceptor<??extends?Message>>?inInterceptors?=?endpointImpl.getInInterceptors()?;??????????inInterceptors.add(new?CheckUserInterceptor())?;????????????????????System.out.println("發(fā)布webservice成功!");??????}??}?? Client:
通過構(gòu)造方法傳入要比較的用戶名和密碼:
[html]?view plaincopy print?
package?com.wiseweb.client.interceptor;????import?java.util.List;????import?javax.xml.namespace.QName;??import?javax.xml.parsers.DocumentBuilder;??import?javax.xml.parsers.DocumentBuilderFactory;??import?javax.xml.parsers.ParserConfigurationException;????import?org.apache.cxf.binding.soap.SoapMessage;??import?org.apache.cxf.headers.Header;??import?org.apache.cxf.interceptor.Fault;??import?org.apache.cxf.phase.AbstractPhaseInterceptor;??import?org.apache.cxf.phase.Phase;??import?org.w3c.dom.Document;??import?org.w3c.dom.Element;????public?class?AddUserInterceptor?extends?AbstractPhaseInterceptor<SoapMessage>{????????private?String?username?;??????private?String?password?;????????????public?AddUserInterceptor(String?username,?String?password)?{??????????super(Phase.PRE_PROTOCOL);??????????this.username?=?username?;??????????this.password?=?password?;??????}????????@Override??????public?void?handleMessage(SoapMessage?message)?throws?Fault?{??????????List<Header>?headers?=?message.getHeaders()?;????????????????????DocumentBuilder?builder?=?null?;??????????try?{??????????????builder?=?DocumentBuilderFactory.newInstance().newDocumentBuilder();??????????}?catch?(ParserConfigurationException?e)?{??????????????e.printStackTrace();??????????}??????????Document?document?=?builder.newDocument()?;??????????Element?root?=?document.createElement("wiseweb")?;??????????Element?username?=?document.createElement("username")?;??????????username.setTextContent(this.username);??????????Element?password?=?document.createElement("password")?;??????????password.setTextContent(this.password);??????????root.appendChild(username)?;??????????root.appendChild(password)?;??????????headers.add(new?Header(new?QName("wiseweb"),?root))?;??????}????}?? ClientTest:
[html]?view plaincopy print?
package?com.wiseweb.client;????import?java.util.List;????import?org.apache.cxf.endpoint.Client;??import?org.apache.cxf.frontend.ClientProxy;??import?org.apache.cxf.interceptor.Interceptor;??import?org.apache.cxf.interceptor.LoggingInInterceptor;??import?org.apache.cxf.interceptor.LoggingOutInterceptor;??import?org.apache.cxf.message.Message;????import?com.wiseweb.client.interceptor.AddUserInterceptor;??import?com.wiseweb.ws.HelloWS;??import?com.wiseweb.ws.HelloWSImplService;????public?class?ClientTest2?{????????public?static?void?main(String[]?args)?{??????????HelloWSImplService?helloWSImplService?=?new?HelloWSImplService()?;??????????HelloWS?helloWS?=?helloWSImplService.getHelloWSImplPort()?;????????????????????Client?client?=?ClientProxy.getClient(helloWS)?;??????????List<Interceptor<??extends?Message>>?outInterceptors?=?client.getOutInterceptors()?;??????????outInterceptors.add(new?AddUserInterceptor("benjamin",?"123456"))?;????????????????????String?result?=?helloWS.sayHello("benjaminwhx")?;??????????System.out.println(result);????????????????}??}?? 運行結(jié)果Server和Client比較: Server:
[html]?view plaincopy print?
發(fā)布webservice成功!??用戶名與密碼正確,通過驗證!??server?sayHello():benjaminwhx??
Client:
[html]?view plaincopy print?
Hello:?benjaminwhx?? 如果輸入的用戶名和密碼不正確,運行結(jié)果為: Server:
[html]?view plaincopy print?
org.apache.cxf.interceptor.Fault:?請輸入正確的用戶名和密碼!??????at?com.wiseweb.ws.interceptor.CheckUserInterceptor.handleMessage(CheckUserInterceptor.java:29)??????at?com.wiseweb.ws.interceptor.CheckUserInterceptor.handleMessage(CheckUserInterceptor.java:1)??????at?org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)??????at?org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:122)??????at?org.apache.cxf.transport.http_jetty.JettyHTTPDestination.serviceRequest(JettyHTTPDestination.java:348)??????at?org.apache.cxf.transport.http_jetty.JettyHTTPDestination.doService(JettyHTTPDestination.java:312)??????at?org.apache.cxf.transport.http_jetty.JettyHTTPHandler.handle(JettyHTTPHandler.java:72)??????at?org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:943)??????at?org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:879)??????at?org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)??????at?org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:250)??????at?org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:110)??????at?org.eclipse.jetty.server.Server.handle(Server.java:349)??????at?org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:441)??????at?org.eclipse.jetty.server.HttpConnection$RequestHandler.content(HttpConnection.java:936)??????at?org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:801)??????at?org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:224)??????at?org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:51)??????at?org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:586)??????at?org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:44)??????at?org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:598)??????at?org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:533)??????at?java.lang.Thread.run(Thread.java:745)??Caused?by:?java.lang.RuntimeException:?請輸入正確的用戶名和密碼!??????...?23?more?? Client:
[html]?view plaincopy print?
Exception?in?thread?"main"?javax.xml.ws.soap.SOAPFaultException:?請輸入正確的用戶名和密碼!??????at?org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:156)??????at?com.sun.proxy.$Proxy25.sayHello(Unknown?Source)??????at?com.wiseweb.client.ClientTest2.main(ClientTest2.java:26)??Caused?by:?org.apache.cxf.binding.soap.SoapFault:?請輸入正確的用戶名和密碼!??????at?org.apache.cxf.binding.soap.interceptor.Soap11FaultInInterceptor.unmarshalFault(Soap11FaultInInterceptor.java:75)??????at?org.apache.cxf.binding.soap.interceptor.Soap11FaultInInterceptor.handleMessage(Soap11FaultInInterceptor.java:46)??????at?org.apache.cxf.binding.soap.interceptor.Soap11FaultInInterceptor.handleMessage(Soap11FaultInInterceptor.java:35)??????at?org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)??????at?org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage(AbstractFaultChainInitiatorObserver.java:114)??????at?org.apache.cxf.binding.soap.interceptor.CheckFaultInterceptor.handleMessage(CheckFaultInterceptor.java:69)??????at?org.apache.cxf.binding.soap.interceptor.CheckFaultInterceptor.handleMessage(CheckFaultInterceptor.java:34)??????at?org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)??????at?org.apache.cxf.endpoint.ClientImpl.onMessage(ClientImpl.java:801)??????at?org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1679)??????at?org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1517)??????at?org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1425)??????at?org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)??????at?org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:650)??????at?org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)??????at?org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)??????at?org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:531)??????at?org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:462)??????at?org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:365)??????at?org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:318)??????at?org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:95)??????at?org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:134)??????...?2?more?? 這樣就可以有效的驗證訪客的身份。
總結(jié)
以上是生活随笔為你收集整理的WebServices中使用cxf开发日志拦截器以及自定义拦截器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。