Configure HTTP proxy for Axis2 client

Posted on January 8, 2008

0


It’s just a matter of setting some properties before invoking the web service, see example below:

ConfigurationContext ctx = ConfigurationContextFactory
    .createConfigurationContextFromFileSystem("repository",  null);
AnalyticsServiceStub stub = new AnalyticsServiceStub(ctx, url);
ServiceClient sc = stub._getServiceClient();
sc.engageModule("rampart");
ServiceClient client = stub._getServiceClient();
Options options = client.getOptions();
// The username and password are set here.
// WSS4J is responsible of generating correct WS-Security header.
options.setUserName(username);
options.setPassword(password);
HttpTransportProperties.ProxyProperties proxyProperties =
      new HttpTransportProperties.ProxyProperties();
proxyProperties.setProxyName(proxySrv);
proxyProperties.setProxyPort(proxyPort);
options.setProperty(org.apache.axis2.transport.http.HTTPConstants.PROXY,
    proxyProperties);
options.setProperty(HTTPConstants.CHUNKED,false);
client.setOptions(options);

The above code also illustrate how to set WSS username/password in SOAP header. Please be aware that Axis2 doesn’t generate WSS header automatically unless the corresponding WS-Policy is specified at server side. Here is an example of how to specify username/password WSS header in services.xml configuration file:

 <wsp:policy xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"
   xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/
      oasis-200401-wss-wssecurity-utility-1.0.xsd"
      wsu:id="UsernameTokenOverHTTPS">
   <wsp:exactlyone>
      <wsp:all>
         <sp:transportbinding xmlns:sp="http://schemas.xmlsoap.org/
             ws/2005/07/securitypolicy">
            <wsp:policy>
               <sp:transporttoken>
                  <wsp:policy>
                     <sp:httpstoken requireclientcertificate="false">
                     </sp:httpstoken>
                  </wsp:policy>
               </sp:transporttoken>
               <sp:algorithmsuite>
                  <wsp:policy>
                     <sp:basic256></sp:basic256>
                  </wsp:policy>
               </sp:algorithmsuite>
               <sp:layout>
                  <wsp:policy>
                     <sp:lax></sp:lax>
                  </wsp:policy>
               </sp:layout>
               <!-- Timestamps are required in requests -->
               <!--  <sp:IncludeTimestamp />  -->
            </wsp:policy>
         </sp:transportbinding>
         <sp:signedsupportingtokens xmlns:sp="http://schemas.xmlsoap.org/
           ws/2005/07/securitypolicy">
            <wsp:policy>
               <sp:usernametoken sp:includetoken=
                   "http://schemas.xmlsoap.org/ws/
                   2005/07/securitypolicy/IncludeToken/AlwaysToRecipient">
               </sp:usernametoken>
            </wsp:policy>
         </sp:signedsupportingtokens>
         <ramp:rampartconfig xmlns:ramp="http://ws.apache.org/
               rampart/policy">
            <ramp:passwordcallbackclass>
               com.nordea.emarkets.webservice.security.SecurityCheck
            </ramp:passwordcallbackclass>
         </ramp:rampartconfig>
      </wsp:all>
   </wsp:exactlyone>
</wsp:policy>
Posted in: Development