Hello!

I'm a newbie with WSS4J. I'm going through the first tutorial, and I'm
trying to set the username token at runtime.

I've generated the stubs with WSDL2Java. Attached are the 2 classes I've
made, beside the generated classes and interfaces. I'm trying to connect
to the StockQuoteService web service. Running the example with external
config file works fine. But running the client using the dynamical
setting of the username return the following message:

Exception in thread "main" AxisFault
 faultCode:
{http://schemas.xmlsoap.org/soap/envelope/}Server.generalException
 faultSubcode:
 faultString: WSDoAllReceiver: Request does not contain required
Security header
 faultActor:
 faultNode:
 faultDetail:
        {http://xml.apache.org/axis/}hostname:manu.rogrid.pub.ro

WSDoAllReceiver: Request does not contain required Security header
        at
org.apache.axis.message.SOAPFaultBuilder.createFault(SOAPFaultBuilder.java:221)
        at
org.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder.java:128)
        at
org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087)
        at
org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
        at
org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanEndElement(Unknown
Source)
        at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown
Source)
        at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown
Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown
Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown
Source)
        at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
        at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
        at javax.xml.parsers.SAXParser.parse(SAXParser.java:375)
        at
org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)
        at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)
        at org.apache.axis.Message.getSOAPEnvelope(Message.java:424)
        at
org.apache.axis.handlers.soap.MustUnderstandChecker.invoke(MustUnderstandChecker.java:62)
        at org.apache.axis.client.AxisClient.invoke(AxisClient.java:206)
        at org.apache.axis.client.Call.invokeEngine(Call.java:2765)
        at org.apache.axis.client.Call.invoke(Call.java:2748)
        at org.apache.axis.client.Call.invoke(Call.java:2424)
        at org.apache.axis.client.Call.invoke(Call.java:2347)
        at org.apache.axis.client.Call.invoke(Call.java:1804)
        at
samples.stock.client.StockWss01SoapBindingStub.getQuote(StockWss01SoapBindingStub.java:106)
        at
samples.stock.client.StockServiceClient.main(StockServiceClient.java:61)

Can anyone help me?

Thank you a lot!

Best regards,
Emanuel Haisiuc

/**
 * 
 */
package samples.stock.client;

import java.io.IOException;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;

import org.apache.ws.security.WSPasswordCallback;

/**
 * @author Emanuel Haisiuc
 * 
 */
public class PWCallback implements CallbackHandler {

        /*
         * (non-Javadoc)
         * 
         * @see 
javax.security.auth.callback.CallbackHandler#handle(javax.security.auth.callback.Callback[])
         */
        public void handle(Callback[] arg0) throws IOException,
                        UnsupportedCallbackException {
                for (int i = 0; i < arg0.length; i++) {
                        if (arg0[i] instanceof WSPasswordCallback) {
                                WSPasswordCallback pc = 
(WSPasswordCallback)arg0[i];
                                //set the password given the username
                                if("wss4j".equals(pc.getIdentifer())) {
                                        pc.setPassword("security");
                                }
                        } else {
                                throw new UnsupportedCallbackException(arg0[i],
                                                "Unrecognized Callback");
                        }
                }
        }

}
/**
 * 
 */
package samples.stock.client;

import java.rmi.RemoteException;

import javax.xml.rpc.ServiceException;

import org.apache.axis.EngineConfiguration;
import org.apache.axis.client.Stub;
import org.apache.ws.security.WSConstants;
import org.apache.ws.security.handler.WSHandlerConstants;
import org.apache.ws.security.message.token.UsernameToken;

/**
 * @author Emanuel Haisiuc
 * 
 */
public class StockServiceClient {

        public StockServiceClient() {
                super();
        }

        /**
         * @param args
         */
        public static void main(String[] args) throws ServiceException,
                        RemoteException {
                EngineConfiguration config = null;

                if (args.length == 0) {
                        System.out.println("Usage:\njava StockServiceClient 
[symbol]");
                        return;
                }

                // try {
                // config = new FileProvider("client_deploy.wsdd");
                // } catch (Exception e) {
                // System.err.println("KKT!!!!!!!! Unknown error - ");
                // }
                //
                // StockQuoteServiceService locator = new
                // StockQuoteServiceServiceLocator(
                // config);
                // StockQuoteService service = locator.getStockWss01();

                StockQuoteServiceService locator = new 
StockQuoteServiceServiceLocator();

                Stub axisPort = (Stub) locator.getPort(StockQuoteService.class);
                axisPort._setProperty(UsernameToken.PASSWORD_TYPE,
                                WSConstants.PASSWORD_DIGEST);
                axisPort._setProperty(WSHandlerConstants.USER, "wss4j");
                axisPort._setProperty(WSHandlerConstants.PW_CALLBACK_REF,
                                new PWCallback());

                StockQuoteService service = locator.getStockWss01();
                //StockQuoteService service = (StockQuoteService) 
axisPort._getService();

                float quote = service.getQuote(args[0]);
                System.out.println("stock quote service returned " + args[0] + 
": "
                                + quote);
        }

}

Reply via email to