You are viewing a plain text version of this content. The canonical link for it is here.
Posted to rampart-dev@ws.apache.org by Julien Sentier <js...@rocketmail.com> on 2009/11/27 17:12:43 UTC

Building a WS client with servlets

Hi,

I've developed a Web Service with UserName Token authentication (and password digest).

Then I've generated the stubs and I've build a simple client. When I launch my .jar it works fine!

However I must build a website as a WS client deployed on Tomcat. I've used the same code both time:
String target = "http://192.168.0.6:8081/axis2/services/WSInterface";
WSInterfaceStub stub = new WSInterfaceStub(target);
ServiceClient client = stub._getServiceClient();
Options options = client.getOptions();
options.setProperty(RampartMessageData.KEY_RAMPART_POLICY,loadPolicy("policy.xml"));
options.setUserName("alice");
options.setPassword("bob");
client.engageModule("rampart");

Here is loadPolicy():
   private static Policy loadPolicy(String xmlPath) throws Exception {
       StAXOMBuilder builder = new StAXOMBuilder(xmlPath);
       return PolicyEngine.getPolicy(builder.getDocumentElement());
   }

So this code works on my simple client (.jar) but not for the servlet. I can build the .war file and then deploy it on Tomcat but when I go to http://localhost:8080/mysite/Index I got the following error:
java.io.FileNotFoundException: policy.xml (No such file or directory) 

I tried to put policy.xml inside the .war at different location but without success.

I wondering if someone here have already succeeded on creating a secure WS client with Tomcat.

Alternative: do you know another way to specify policy without XML file? I mean, without: options.setProperty(RampartMessageData.KEY_RAMPART_POLICY,loadPolicy("policy.xml"));

Regards,
Julien


      


Re: Building a WS client with servlets

Posted by Thilina Mahesh Buddhika <th...@gmail.com>.
Hi Julien,

There are some alternative approaches you can try.

1. Try placing the the policy.xml inside the root directory of your web-app
at the same level as WEB-INF directory.

2. Otherwise you can include the absolute path of the policy.xml inside the
web.xml as a context parameter.

<context-param>
    <param-name>policy</param-name>
    <param-value>/path/to/the/policy</param-value>
</context-param>

Then inside your servlet you can load this parameter and read the path to
the policy file.

String path = (String) getServletContext().getAttribute("policy");


3. If you do not want to specify the policy file in your client code, use
Axis2 Dynamic Client. More information about using Axis2 Dynamic Client can
be found here [1].

Thanks.
/thilina

[1] -
http://blog.rampartfaq.com/2009/11/how-to-use-axis2-dynamic-client-to.html


Thilina Mahesh Buddhika
http://blog.thilinamb.com


On Fri, Nov 27, 2009 at 9:42 PM, Julien Sentier <js...@rocketmail.com>wrote:

> Hi,
>
> I've developed a Web Service with UserName Token authentication (and
> password digest).
>
> Then I've generated the stubs and I've build a simple client. When I launch
> my .jar it works fine!
>
> However I must build a website as a WS client deployed on Tomcat. I've used
> the same code both time:
> String target = "http://192.168.0.6:8081/axis2/services/WSInterface";
> WSInterfaceStub stub = new WSInterfaceStub(target);
> ServiceClient client = stub._getServiceClient();
> Options options = client.getOptions();
>
> options.setProperty(RampartMessageData.KEY_RAMPART_POLICY,loadPolicy("policy.xml"));
> options.setUserName("alice");
> options.setPassword("bob");
> client.engageModule("rampart");
>
> Here is loadPolicy():
>   private static Policy loadPolicy(String xmlPath) throws Exception {
>       StAXOMBuilder builder = new StAXOMBuilder(xmlPath);
>       return PolicyEngine.getPolicy(builder.getDocumentElement());
>   }
>
> So this code works on my simple client (.jar) but not for the servlet. I
> can build the .war file and then deploy it on Tomcat but when I go to
> http://localhost:8080/mysite/Index I got the following error:
> java.io.FileNotFoundException: policy.xml (No such file or directory)
>
> I tried to put policy.xml inside the .war at different location but without
> success.
>
> I wondering if someone here have already succeeded on creating a secure WS
> client with Tomcat.
>
> Alternative: do you know another way to specify policy without XML file? I
> mean, without:
> options.setProperty(RampartMessageData.KEY_RAMPART_POLICY,loadPolicy("policy.xml"));
>
> Regards,
> Julien
>
>
>
>
>