Documentos de Académico
Documentos de Profesional
Documentos de Cultura
nsftools.com//ApacheAxisClientTips.
1/3
09-02-2011
Using Session-Based Authentication If your web service requires you to use session-based Authentication (which requires you to pass a token as an HTTP Cookie), you can adjust your Axis client code like so: import StockInfoNamespace.*; import org.apache.axis.client.Stub; import org.apache.axis.client.Call; import org.apache.axis.transport.http.HTTPConstants; StockInfoServiceLocator locator = new StockInfoServiceLocator(); StockInfoService service = locator.getStockService(); // to set a cookie: ((Stub) service)._setProperty(Call.SESSION_MAINTAIN_PROPERTY, new Boolean(true)); ((Stub) service)._setProperty(HTTPConstants.HEADER_COOKIE, "AuthToken=abc123"); service.GetStockInfo("FOO"); How you actually get the cookie/token depends on the system you're working with. Please Read The Fabulous Manual for your system to figure that one out. Notice that you also have to set Call.SESSION_MAINTAIN_PROPERTY to a Boolean "true". If that property is not true, the Cookie value will not be sent. Adding SOAP Header Elements Sometimes a web service will also require you to include SOAP Header information, usually for login purposes. In the case of explicit headers (that are defined in the WSDL), the SOAP Header information is supposed to be writable directly from the Axis client code. However, if the headers are implicit (not defined in the WSDL), you will have to generate them manually. There are two kinds of header elements you may have to generate. The first are elements that are simply child nodes of the <Header> element, like so: <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <SOAP-ENV:Header> <ns1:UserName xmlns:ns1="urn:thisNamespace">John Doe</ns1:UserName> </SOAP-ENV:Header> <SOAP-ENV:Body> <ns2:GetStockInfo xmlns:ns2="urn:thisNamespace"> <ns2:symbol>FOO</ns2:symbol> </ns2:GetStockInfo> </SOAP-ENV:Body> </SOAP-ENV:Envelope> This type of envelope can be created with client code similar to this: import StockInfoNamespace.*; import org.apache.axis.client.Stub; StockInfoServiceLocator locator = new StockInfoServiceLocator(); StockInfoService service = locator.getStockService(); // add a <UserName> node to the SOAP Header ((Stub) service).setHeader("urn:thisNamespace", "UserName", "John Doe"); service.GetStockInfo("FOO"); You can also end up with header elements that are nested, such that there are nodes with subnodes within the <Header> element, like so: <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <SOAP-ENV:Header> <ns1:AuthenticationInfo xmlns:ns1="urn:thisNamespace"> <ns1:UserName>John Doe</ns1:UserName> </ns1:AuthenticationInfo> </SOAP-ENV:Header> <SOAP-ENV:Body> <ns2:GetStockInfo xmlns:ns2="urn:thisNamespace"> <ns2:symbol>FOO</ns2:symbol> </ns2:GetStockInfo> </SOAP-ENV:Body>
nsftools.com//ApacheAxisClientTips.
2/3
09-02-2011
</SOAP-ENV:Envelope>
In this case, the <UserName> node is within an <AuthenticationInfo> node within the SOAP Header. This type of envelope can be created with client code similar to this: import import import import StockInfoNamespace.*; org.apache.axis.client.Stub; org.apache.axis.message.SOAPHeaderElement; javax.xml.soap.SOAPElement;
StockInfoServiceLocator locator = new StockInfoServiceLocator(); StockInfoService service = locator.getStockService(); // add an <AuthenticationInfo> node with a <UserName> subnode // to the SOAP Header SOAPHeaderElement header = new SOAPHeaderElement( "urn:thisNamespace", "AuthenticationInfo"); SOAPElement node = header.addChildElement("UserName"); node.addTextNode("John Doe"); ((Stub) service).setHeader(header); service.GetStockInfo("FOO"); Note that we're talking about SOAP Envelope Headers here, not HTTP headers. See the discussion on SOAPAction heders at the top of this page for information on adding custom HTTP Headers. Use a custom client-config.wsdd at runtime The Axis client gets certain settings from a configuration file called client-config.wsdd. Sometimes you want your client code to use custom client-config.wsdd settings, specified at runtime. One way to do this is to edit the __ServiceLocator.java file that was generated by WSDL2Java and add the following method:
protected org.apache.axis.EngineConfiguration getEngineConfiguration() { java.lang.StringBuffer sb = new java.lang.StringBuffer(); sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"); sb.append("<deployment name=\"defaultClientConfig\"\r\n"); sb.append("xmlns=\"http://xml.apache.org/axis/wsdd/\"\r\n"); sb.append("xmlns:java=\"http://xml.apache.org/axis/wsdd/providers/java\">\r\n"); sb.append("<transport name=\"http\" pivot=\"java:org.apache.axis.transport.http.HTTPSender\" />\r sb.append("<transport name=\"local\" pivot=\"java:org.apache.axis.transport.local.LocalSender\" / sb.append("<transport name=\"java\" pivot=\"java:org.apache.axis.transport.java.JavaSender\" />\r sb.append("</deployment>\r\n"); org.apache.axis.configuration.XMLStringProvider config = new org.apache.axis.configuration.XMLStringProvider(sb.toString()); return config; } This code will cause the client to use the specified configuration information above instead of whatever default client-config.wsdd file you have. Note that there are only certain cases where you want to do this, though. Normally you're better off editing the client-config.wsdd file directly. You should also be able to generate the EngineConfiguration object in your client code (as in the method above) and pass it directly to the __ServiceLocator class when you're instantiating it. For example: org.apache.axis.EngineConfiguration myEngine = [see getEngineConfiguration above...] MyServiceLocator locator = new MyServiceLocator(myEngine); Using NTLM Authentication in an Axis web service client I haven't tried doing this, but apparently you can have your Axis client use Windows NTLM authentication by telling the client-config.wsdd file to use "CommonsHTTPSender" instead of just plain "HTTPSender" as the http transport. Do a Google search for "NTLM and CommonsHTTPSender" for more detail. Using WSS4J Authentication in an Axis web service client Using WSS4J to pass WS-Security info is yet another thing I haven't tried, but Johan Danforth has some information on his blog: http://weblogs.asp.net/jdanforth/archive/2005/01/22/358764.aspx http://weblogs.asp.net/jdanforth/archive/2005/01/16/354060.aspx You are on the nsftools.com website. Copyright 2007. last edited March 8, 2007
nsftools.com//ApacheAxisClientTips.
3/3