<p>I need to call a web service written in .NET from Java.  The web service implements the WS-Security stack (either WSE 2 or WSE 3, it's not clear from the information I have).  </p>

<p>The information that I received from the service provider included WSDL, a policyCache.config file, some sample C# code, and a sample application that can successfully call the service.</p>

<p>This isn't as useful as it sounds because it's not clear how I'm supposed to use this information to write a Java client.  If the web service request isn't signed according to the policy then it is rejected by the service.  I'm trying to use Apache Axis2 and I can't find any instructions on how I'm supposed to use the policyCahce.config file and the WSDL to generate a client.</p>

<p>There are several examples that I have found on the Web but in all cases the authors of the examples had control of both the service and the client and so were able to make tweaks on both sides in order to get it to work.  I'm not in that position.</p>

<p>Has anyone done this successfully?</p>

[{'answer_id': 16478, 'author': 'ScArcher2', 'author_id': 1310, 'author_profile': 'https://Stackoverflow.com/users/1310', 'pm_score': 0, 'selected': False, 'text': '<p><a href="http://cwiki.apache.org/CXF20DOC/ws-support.html" rel="nofollow noreferrer">CXF</a> - I\'d look into CXF. I\'ve used it to create a web service and client in java using ws-secuirty. I also connected a .net web service to it.</p>\n\n<p>They have pretty good documentation too. I had more luck with it than axis.</p>\n'}, {'answer_id': 16499, 'author': 'ESV', 'author_id': 150, 'author_profile': 'https://Stackoverflow.com/users/150', 'pm_score': 2, 'selected': False, 'text': '<ul>\n<li>Apache Axis can generate proxy code from WSDL <a href="http://ws.apache.org/axis/java/user-guide.html#UsingWSDLWithAxis" rel="nofollow noreferrer">http://ws.apache.org/axis/java/user-guide.html#UsingWSDLWithAxis</a></li>\n<li>NetBeans with the RESTful Web Services plug-in can generate code for you.  Instructions for an example client for the eBay shopping web service are at <a href="http://ebay.custhelp.com/cgi-bin/ebay.cfg/php/enduser/std_adp.php?p_faqid=1230" rel="nofollow noreferrer">http://ebay.custhelp.com/cgi-bin/ebay.cfg/php/enduser/std_adp.php?p_faqid=1230</a>.</li>\n</ul>\n'}, {'answer_id': 16979, 'author': 'ScArcher2', 'author_id': 1310, 'author_profile': 'https://Stackoverflow.com/users/1310', 'pm_score': 2, 'selected': False, 'text': '<p>@Mike</p>\n\n<p>I recently did a test and this is the code I used.\nI\'m not using policy stuff, but I used WS-Security with plain text authentication.\nCXF has really good documentation on how to accomplish this stuff.</p>\n\n<p>I used wsdl2java and then added this code to use the web service with ws-security.</p>\n\n<p>I hope this helps you out.</p>\n\n<pre><code>import java.io.IOException;\nimport java.util.HashMap;\nimport java.util.Map;\n\nimport javax.security.auth.callback.Callback;\nimport javax.security.auth.callback.CallbackHandler;\nimport javax.security.auth.callback.UnsupportedCallbackException;\n\nimport org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;\nimport org.apache.ws.security.WSConstants;\nimport org.apache.ws.security.WSPasswordCallback;\nimport org.apache.ws.security.handler.WSHandlerConstants;\n\npublic class ServiceTest implements CallbackHandler\n{\n\n     public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {\n\n            WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];\n            // set the password for our message.\n            pc.setPassword("buddah");\n        }\n\n    public static void main(String[] args){\n        PatientServiceImplService locator = new PatientServiceImplService();\n        PatientService service = locator.getPatientServiceImplPort();\n\n        org.apache.cxf.endpoint.Client client = org.apache.cxf.frontend.ClientProxy.getClient(service);\n        org.apache.cxf.endpoint.Endpoint cxfEndpoint = client.getEndpoint();\n\n        Map&lt;String, Object&gt; outProps = new HashMap&lt;String, Object&gt;();\n        outProps.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN + " " +  WSHandlerConstants.TIMESTAMP);\n        outProps.put(WSHandlerConstants.USER, "joe");\n        outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);\n\n        // Callback used to retrieve password for given user.\n        outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS, ServiceTest.class.getName());\n\n        WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps);\n        cxfEndpoint.getOutInterceptors().add(wssOut);\n\n\n        try\n        {\n            List list = service.getInpatientCensus();\n            for(Patient p : list){\n                System.out.println(p.getFirstName() + " " + p.getLastName());\n            }\n\n        }\n        catch (Exception e)\n        {\n            // TODO Auto-generated catch block\n            e.printStackTrace();\n        }\n    }\n}\n</code></pre>\n'}, {'answer_id': 83764, 'author': 'Zach', 'author_id': 8720, 'author_profile': 'https://Stackoverflow.com/users/8720', 'pm_score': 3, 'selected': False, 'text': '<p>WS-Security specifications are not typically contained in a WSDL (never in a WSE WSDL). So wsdl2java does not know that WS-Security is even required for this service. The fact that security constraints are not present in a WSE WSDL is a big disappointment to me (WCF will include WS-Trust information in a WSDL).</p>\n\n<p>On the client end, you\'ll need to use <a href="http://ws.apache.org/rampart/" rel="noreferrer">Rampart</a> to add the necessary WS-Security headers to your outgoing client message. Since the WSDL does not report what WS-Security settings are necessary, you\'re best off by asking the service provider what is required. WS-Security requirements may be simple plaintext password, or might be X509 certificates, or might be encrypted message..... Rampart should be able to handle most of these scenarios.</p>\n\n<p>Apache Rampart is "turned on" by engaging the module in your axis2.xml file. You\'ll need to download the Rampart module and put it in a specific place in your axis2 directory, then modify the xml file. You can also engage Rampart programatically (please edit your original question if this is a requirement and I\'ll edit this response).</p>\n\n<p>Depending on how you configure rampart (through other XML files or programatically), it will intercept any outgoing messages and add the necessary WS-Security information to it. I\'ve personally used axis2 with rampart to call a WSE3 service that is secured with UsernameToken in plaintext and it worked great. Similar, but more advanced scenarios should also work.  There are more details on how to set up and get started with Rampart on the site linked above. If you have problems about the specifics of Rampart or how to use Rampart with your particular WSE setup, then edit your question and I\'ll try my best to answer.</p>\n'}, {'answer_id': 711398, 'author': 'Michael Sharek', 'author_id': 1958, 'author_profile': 'https://Stackoverflow.com/users/1958', 'pm_score': 4, 'selected': True, 'text': '<p>This seems to be a popular question so I\'ll provide an overview of what we did in our situation.</p>\n\n<p>It seems that services built in .NET are following an older ws-addressing standard (<a href="http://schemas.xmlsoap.org/ws/2004/03/addressing/" rel="noreferrer">http://schemas.xmlsoap.org/ws/2004/03/addressing/</a>) and axis2 only understands the newer standard (<a href="http://schemas.xmlsoap.org/ws/2004/08/addressing/" rel="noreferrer">http://schemas.xmlsoap.org/ws/2004/08/addressing/</a>).</p>\n\n<p>In addition, the policyCache.config file provided is in a form that the axis2 rampart module can\'t understand.</p>\n\n<p>So the steps we had to do, in a nutshell:</p>\n\n<ul>\n<li>Read the policyCache.config and try to understand it.  Then rewrite it into a policy that rampart could understand.  (Some <a href="http://www.scribd.com/doc/466238/Official-Documentation-ws-apache-org-Axis2-Part-2" rel="noreferrer">updated docs</a> helped.)</li>\n<li>Configure rampart with this policy.</li>\n<li>Take the keys that were provided in the .pfx file and convert them to a java key store.  There is a utility that comes with Jetty that can do that.</li>\n<li>Configure rampart with that key store.</li>\n<li>Write a custom axis2 handler that backward-converts the newer ws-addressing stuff that comes out of axis2 into the older stuff expected by the service.</li>\n<li>Configure axis2 to use the handler on outgoing messages.</li>\n</ul>\n\n<p>In the end it was a lot of configuration and code for something that is supposed to be an open standard supported by the vendors.</p>\n\n<p>Although I\'m not sure what the alternative is...can you wait for the vendors (or in this case, the one vendor) to make sure that everything will inter-op?</p>\n\n<p>As a postscript I\'ll add that I didn\'t end up doing the work, it was someone else on my team, but I think I got the salient details correct.  The other option that I was considering (before my teammate took over) was to call the WSS4J API directly to construct the SOAP envelope as the .NET service expected it.  I think that would have worked too.</p>\n'}]
['https://Stackoverflow.com/questions/16451', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1958/']