Thank you. I create a sample project and it can publish wsdl with security.
But after client side [mobile users] send string parameters, the security does 
not show any error. Web servers do not require username and password.
What code snippet is missing?
And what the incomming data format should client side sent? Should soap head be 
added?
 

package com.testws.ws;

import java.io.StringReader;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.springframework.stereotype.Component;
import org.xml.sax.InputSource;
import com.testws.util.WSConvert;
import com.testws.util.WStype;
import org.apache.cxf.annotations.Policies;
import org.apache.cxf.annotations.Policy;
/*
 * 
http://localhost:8080/testWS/testWebServiceSecurity/TestWebServiceSecurity?WSDL
 * */

@Component
@WebService(name="testWebServiceSecurity",
        serviceName="testWebServiceSecurity",
        targetNamespace="http://com.testws";            
)
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT,
        use=SOAPBinding.Use.LITERAL,
        parameterStyle=SOAPBinding.ParameterStyle.WRAPPED
)
//@Policies(
//          @Policy(uri="classpath:/AlternativesPolicy.xml", 
//          placement = Policy.Placement.BINDING))

public class TestWebServiceSecurity{

    public TestWebServiceSecurity(){}

    private  static ArrayList<Object[]> parseXml2(String paramString) throws 
Exception{
        StringReader read = new StringReader(paramString);
        InputSource source = new InputSource(read);
        SAXBuilder sb = new SAXBuilder();
        Document doc = sb.build(source);
        Element root = doc.getRootElement();
        Element functionElt=root.getChild("function");
        Element par=root.getChild("params");
        List<Element> listElt=par.getChildren();
        String str[]=new String[listElt.size()+1];
        String rmp [] =new String[listElt.size()];
        String funcitonName=functionElt.getText();
        str[0]=funcitonName;
        String [] rmp1={str[0]};
        for(int i=0;i<listElt.size();i++){
            Element el=listElt.get(i);
            str[i+1]=el.getText();
            rmp[i]=el.getText();
        }
        ArrayList<Object[]>ra=new ArrayList<Object[]>();
        ra.add(rmp1);
        ra.add(rmp);
        return ra;
    }

    @Policies({
                 @Policy(uri = "classpath:/UsernamePasswordPolicy.xml",
                         placement = Policy.Placement.BINDING_OPERATION_INPUT)
    })
//    @Policies(
//              @Policy(uri="classpath:/AlternativesPolicy.xml", 
//              placement = Policy.Placement.BINDING))
    @WebMethod(operationName = "TestService")
    public  String TestService(String paramString) throws Exception{
        Object returnValue = null;
        String functionName="";
        ArrayList<Object []>s = parseXml2(paramString);
        functionName = s.get(0)[0].toString();
        Class<?>[] mapValue = WStype.map.get(functionName);
        Class cl=Class.forName("com.testws.dao.TestWebServiceSecurityDao");
        Method m=cl.getDeclaredMethod(functionName, mapValue);
        m.setAccessible(true);
        Object[] newobject=getObject(s.get(1), mapValue);
        m.invoke(cl.newInstance(), newobject);
        return functionName;
    }
    
    private  Object[] getObject(Object[] ina, Class<?>[] type){
        Object [] o = new Object[ina.length];
        for(int i=0; i<ina.length; i++){
            o[i]=WSConvert.convert(ina[i].toString(), type[i]);

        }
        return o;
    }    
    
}


package com.testws.dao;

import org.springframework.stereotype.Repository;

@Repository
public class TestWebServiceSecurityDao {

    public TestWebServiceSecurityDao(){}

    @SuppressWarnings("unused")
    private String TestOrder(String userName, String type, String startNo, 
String endNo){
        StringBuilder sb = new StringBuilder("<?xml version=\"1.0\" 
encoding=\"utf-8\" ?><Root function=\"TestOrder\" result=\"false\"><status 
str=\"error\" /></Root>");
        System.out.println(sb);
        return sb.toString();
    }
    
}


Testing code:
I use wsdl2java to get java code. And following is my testing code:

package testws.test;

import testws.com.Exception_Exception;
import testws.com.TestWebServiceSecurity;
import testws.com.TestWebServiceSecurity_Service;

public class Test {
    public static void main(String[] args) throws Exception_Exception {
        TestWebServiceSecurity_Service tss = new 
TestWebServiceSecurity_Service();
        TestWebServiceSecurity tws = tss.getTestWebServiceSecurityPort();
        String param = 
"<root><function>TestOrder</function><params><param>1460</param><param>travel</param><param>travel</param><param>2012-11-30
 11:59:57 +0000</param></params></root>";
        System.out.println("18="+tws.testService(param));
    }
}



    

> Date: Tue, 19 Feb 2013 13:41:33 +1100
> Subject: Re: Simple questions about username token example
> From: [email protected]
> To: [email protected]
> 
> It is difficult for me to say without seeing your project and test case.
> 
> If you want to send me a sample project I am happy to have a look at
> it and provide some advice on what you need to do.
> 

                                          

Reply via email to