WebService's Interceptor

Source: Internet
Author: User

Record WebService add interceptors to achieve the purpose of modifying XML messages

import Org.apache.cxf.endpoint.client;import org.apache.cxf.frontend.ClientProxy;PrivateSoap Createsoap () {Service=NewService (); Soap=Service.getservicesoap (); //Interceptor Retrofit Message HeaderClient =clientproxy.getclient (SOAP); Headerininterceptor Soapheaderininterceptor=NewHeaderininterceptor (); Soapheaderininterceptor.sethostuser ("username","Password");        Client.getoutinterceptors (). Add (Soapheaderininterceptor); returnsoap; } Public classHeaderininterceptor extends Abstractsoapinterceptor { PublicHeaderininterceptor () {super (phase.write); }    PrivateString msg; @Override Public voidhandlemessage (SoapMessage message) throws Fault {Try{List<Header> headers =message.getheaders (); QName QName=NewQName ("Authorization-my-header");//nodes that need to be increasedDocument document =caretedocument (msg); Element documentelement=document.getdocumentelement (); SoapHeader Head=NewSoapHeader (QName, documentelement);        Headers.add (head); } Catch(Exception e) {e.printstacktrace (); }    }     Public voidsethostuser (String username,string pw) {string str="<tem:authorizationsoapheader soapenv:mustunderstand=\ "1\" xmlns:tem=\ "http://tempuri.org/\ ""+"xmlns:soapenv=\ "http://schemas.xmlsoap.org/soap/envelope/\" >\n"+"\t<tem:username>"+ username+"</tem:username>\n"+"\t<tem:password>"+pw+"</tem:password>\n"+"</tem:AuthorizationSoapHeader>";  This. msg =str; }     Public StaticDocument caretedocument (String xml) throws Exception {InputStream strm; STRM=NewBytearrayinputstream (Xml.getbytes ("UTF-8")); Documentbuilderfactory domfactory=documentbuilderfactory. newinstance (); Domfactory.setnamespaceaware (true); Documentbuilder Builder=Domfactory.newdocumentbuilder (); Document Doc=Builder.parse (STRM); returnDoc; }}

PS: In general, use "Wsimport-Clientjar {Xxx.jar}-p {package name}-S {Source package name} {URL}" generated by the client, the creation of the service has no parameter and the method of the argument

No parameter: Generate service for each call to the remote URL

Parameter: Place the WSDL locally and pass the method through the URL to reduce the network overhead per request

Service = new Service (New URL ("file://xxx/xxx.wsdl"));

WebService's Interceptor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.