Hi,
Currently I'm implementing a Client in CXF using JAX-WS Dispatch API. This
client should be as dynamic as possible. But in the dispatch api, while
creating a service and a dispatch, you need to give Qnames for service name
and port name.
The only input that I have is the wsdl file hosted at the remote service.
At the moment, I'm parsing the wsdl using DOM parser to get the necessary
values as servicename, portname and namespace. Is there any other way than
this to receive these values than parsing the wsdl ?
Thanx...!!

Following is the code that I used.

 SpringBusFactory bf = new SpringBusFactory();
            URL busFile = Client.class.getResource("/
client.xml");
            Bus bus = bf.createBus(busFile.toString());
            BusFactory.setDefaultBus(bus);


            URL wsdl = new URL("
http://localhost:9000/SoapContext/GreeterPort?wsdl";);
            HttpURLConnection connection =(HttpURLConnection)
wsdl.openConnection();
            DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.parse(new
InputSource(connection.getInputStream()));

            String service=
document.getElementsByTagName("service").item(0).getAttributes().getNamedItem("name").getNodeValue();
            String
port=document.getElementsByTagName("port").item(0).getAttributes().getNamedItem("name").getNodeValue();
            String
binding=document.getElementsByTagName("port").item(0).getAttributes().getNamedItem("binding").getNodeValue();
            String[] splitTag=binding.split(":");
             String
nameSpace=document.getFirstChild().lookupNamespaceURI(splitTag[0]);


            QName serviceName = new QName(nameSpace, service);
            QName portName = new QName(nameSpace,port);

            Service s =Service.create(wsdl,serviceName);
            Dispatch<SOAPMessage> dispatch = s.createDispatch(portName,
SOAPMessage.class, Service.Mode.MESSAGE);
            InputStream is =
Client.class.getResourceAsStream("/greeterMessage.xml");
            if (is == null) {
                System.err.println("Failed to create input stream from file
");
                System.exit(-1);
            }
            SOAPMessage soapReq1 =
MessageFactory.newInstance().createMessage(null, is);
            dispatch.invoke(soapReq1);
             Thread.sleep(30 * 1000);
            bus.shutdown(true);


-- 
Pasadi Munasinghe
Software Engineer Intern
WSO2, Inc
Mobile: +9471 377 5515

Reply via email to