You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fx-dev@ws.apache.org by Johan Danforth <jo...@bredband.net> on 2005/01/22 23:41:27 UTC
How to set Username and callback handlers dynamically
Lo all,
I managed to dynamically set the username and the other properties
that normally goes into the client WSDD file. Again, many thanks to the nice
people in the WSS4J mailing list.
First I had to modify the client WSDD file from the tutorial and
comment out the properties. If I left them in there, I believe those values
from the wsdd-file will be used. So, the client_deploy.wsdd looks something
like this now:
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
<transport name="http"
pivot="java:org.apache.axis.transport.http.HTTPSender"/>
<globalConfiguration >
<requestFlow >
<handler type="java:org.apache.ws.axis.security.WSDoAllSender" >
<!-- parameters removed -->
</handler>
</requestFlow >
</globalConfiguration >
</deployment>
Then you have to modify the StockServiceClient.java code somewhat to
be able to set the handler properties
package samples.stock.client;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import org.apache.axis.EngineConfiguration;
import org.apache.axis.configuration.FileProvider;
import org.apache.axis.client.Stub;
import java.rmi.Remote;
import org.apache.ws.security.handler.WSHandlerConstants;
import org.apache.ws.security.WSConstants;
import org.apache.ws.security.message.token.UsernameToken;
public class StockServiceClient
{
public StockServiceClient()
{
}
public static void main(String[] args) throws ServiceException,
RemoteException
{
if (args.length == 0)
{
System.out.println("Usage:\njava StockServiceClient
[symbol]");
return;
}
//modify the path to the client_deploy.wsdd
EngineConfiguration config = new
FileProvider("client_deploy.wsdd");
StockQuoteServiceService locator = new
StockQuoteServiceServiceLocator(config);
Remote remote = locator.getPort(StockQuoteService.class);
Stub axisPort = (Stub)remote;
axisPort._setProperty(WSHandlerConstants.ACTION,
WSHandlerConstants.USERNAME_TOKEN);
axisPort._setProperty(UsernameToken.PASSWORD_TYPE,
WSConstants.PASSWORD_DIGEST);
axisPort._setProperty(WSHandlerConstants.USER, "wss4j");
axisPort._setProperty(WSHandlerConstants.PW_CALLBACK_CLASS,
"samples.stock.client.PWCallback");
//possible to pass a callback instance by ref instead of a
class as above
//PWCallback pwCallback = new PWCallback();
//axisPort._setProperty(WSHandlerConstants.PW_CALLBACK_REF,
pwCallback);
//Need to cast
StockQuoteService service =
(StockWss01SoapBindingStub)axisPort;
System.out.println("Calling service...");
float quote = service.getQuote(args[0]);
System.out.println("stock quote service returned " + args[0]
+ ": "
+ quote);
}
}
Next I'll try to call a .NET web service with a username token. And
I'm pretty interested in trying out a binary token. Is there any information
regarding binary tokens somewhere? Thanks again for all the help I've got
through this mailing list - much appreciated!
I've written about what I've done with username tokens and wss4j on
my blog, http://weblogs.asp.net/jdanforth/
/Johan
-----Original Message-----
From: Ashok Shah [mailto:ashoks@sfu.ca]
Sent: den 19 januari 2005 02:56
To: johandanforth@bredband.net
Cc: fx-dev@ws.apache.org
Subject: Re: Problems setting Username and callback handlers dynamically
Johan,
Herez another way to do it
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
System.setProperty("axis.ClientConfigFile",
"conf/ecl_client_deploy.wsdd");
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress( new java.net.URL("service endpoint
goes
here") );
call.setOperationName(new QName("uri:method", "method name goes
here"));
call.setUsername(this.userName);
call.setProperty("action", this.action);
call.setProperty(WSConstants.PASSWORD_TYPE_ATTR,
WSConstants.PASSWORD_DIGEST);
call.setProperty(WSHandlerConstants.PW_CALLBACK_CLASS,
this.passwordCallbackClass);
call.setReturnType( org.apache.axis.encoding.XMLType.XSD_STRING );
String str = (String)call.invoke();
If(str==results){
System.out.println("yaay.. I did it");
}else{
System.out.println("the whole world is gonna burn in hell");
}
You could set as many properties needed in the "call" object. For
e.g. if
you want to add the encryption feature as well. You could say
call.setProperty(WSHandlerConstants.ENCRYPTION_USER, "encryption user
goes
here");
call.setProperty(WSHandlerConstants.ENC_PROP_FILE, "property file
goes here
check wss4j property files for encryption");
and you are all good to go for the encryption as well.
These properties are passed on to the MessageContext object which is
created. The "MessageContext" object is used by WSS4J to extract
those
properties.
Cheers
Ashok.
On Tue, 18 Jan 2005 23:34:31 +0100 johandanforth@bredband.net wrote:
> Hi,
>
> I'm following the tutorial by Rami Jaamour and trying the
last
thing
> he wrote about setting the username and pw callback handler
dynamically in
> run-time, but it doesn't seem to work. Whatever I try, my callback
is
never
> called. This is what the documentation says:
>
> Another way to do this is to have the client application
set the
> username and CallbackHandler implementation programmatically
instead of
> client_deploy.wsdd:
>
> ...
> import org.apache.axis.client.Stub;
> ...
>
> Remote remote = locator.getPort(StockQuoteService.class);
> Stub axisPort = (Stub)remote;
> axisPort._setProperty(UsernameToken.PASSWORD_TYPE,
> WSConstants.PASSWORD_DIGEST);
> axisPort._setProperty(WSHandlerConstants.USER, "wss4j");
> axisPort._setProperty(WSHandlerConstants.PW_CALLBACK_REF,
> pwCallback);
>
> where "pwCallback" is a reference to a PWCallback
> implementation. See
> the Axis handler for WSS4J documentation for more details on this.
>
> I would like to get a complete sample if that's possible. I
tried
> this piece of code, but the callback is never called and I get
errors from
> the web service about the wsse header is missing:
>
> StockQuoteServiceService locator = new
> StockQuoteServiceServiceLocator();
> StockQuoteService service =
locator.getStockWss01();
>
> PWCallback pwCallback = new PWCallback();
> Remote remote =
locator.getPort(StockQuoteService.class);
> Stub axisPort = (Stub)remote;
> axisPort._setProperty(UsernameToken.PASSWORD_TYPE,
> WSConstants.PASSWORD_DIGEST);
> axisPort._setProperty(WSHandlerConstants.USER,
"wss4j");
>
axisPort._setProperty(WSHandlerConstants.PW_CALLBACK_REF,
> pwCallback);
>
>
> System.out.println("Calling service...");
> float quote = service.getQuote(args[0]);
> System.out.println("stock quote service returned "
+
args[0]
> + ": "
> + quote);
>
> I guess I shouldn't call service.getQuote() like that, but
instead
> call the remote method through the Stub, but I'm not sure how to do
that.
> Can anyone help me out before I spend another day in the sample
code? :D
>
> Thanks for any help!
> Johan
>
>
Re: How to set Username and callback handlers dynamically
Posted by Werner Dittmann <We...@t-online.de>.
Johan,
the WSDD setting always overwrite the params set by the program (see
javadoc
for "WSHandlerConstants.java"). We did this to enable the deployer of an
Axis
application to overwrite/modify parameters without the need to change the
source code.
Regards,
Werner
Johan Danforth schrieb:
> Lo all,
>
> I managed to dynamically set the username and the other properties
>that normally goes into the client WSDD file. Again, many thanks to the nice
>people in the WSS4J mailing list.
>
> First I had to modify the client WSDD file from the tutorial and
>comment out the properties. If I left them in there, I believe those values
>from the wsdd-file will be used. So, the client_deploy.wsdd looks something
>like this now:
>
> <deployment xmlns="http://xml.apache.org/axis/wsdd/"
>xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
> <transport name="http"
>pivot="java:org.apache.axis.transport.http.HTTPSender"/>
> <globalConfiguration >
> <requestFlow >
> <handler type="java:org.apache.ws.axis.security.WSDoAllSender" >
> <!-- parameters removed -->
> </handler>
> </requestFlow >
> </globalConfiguration >
> </deployment>
>
>
<snip>