Re: Is it possible to get the list of operations and its parameters (name and type)?

4,026 views
Skip to first unread message

Thomas Bayer

unread,
Jul 5, 2012, 1:29:26 PM7/5/12
to soa-...@googlegroups.com
Hi,
see the listing below.

--
Thomas


package sample.wsdl;

import java.util.List;

import com.predic8.wsdl.*;

public class ListWSDLOperations {

  public static void main(String[] args) {
    WSDLParser parser = new WSDLParser();

    Definitions defs = parser
        .parse("TicketRS_WS.wsdl");

    for (PortType pt : defs.getPortTypes()) {
      System.out.println(pt.getName());
      for (Operation op : pt.getOperations()) {
        System.out.println(" -" + op.getName());
        for(Part  part :  op.getInput().getMessage().getParts()) {
        System.out.println(part.getName() + " " + part.getElement());
        }
        for(Part  part :  op.getOutput().getMessage().getParts()) {
        System.out.println(part.getName() + " " + part.getElement());
        }
      }
    }
  }
}

Thomas Bayer

unread,
Jul 5, 2012, 2:53:52 PM7/5/12
to soa-...@googlegroups.com

Hi,

you can get the following output:


TicketRS_WS

 -getTicketReservation

Request Template

<ns1:getTicketReservationElement xmlns:ns1='http://Euro2012/types/'>

  <ns1:PESEL>?XXX?</ns1:PESEL>

  <ns1:Data>?XXX?</ns1:Data>

</ns1:getTicketReservationElement>


Response Template

<ns1:getTicketReservationResponseElement xmlns:ns1='http://Euro2012/types/'>

  <ns1:result>?XXX?</ns1:result>

</ns1:getTicketReservationResponseElement>


From the following Java program:


package sample.wsdl;


import groovy.xml.MarkupBuilder;


import java.io.StringWriter;

import java.util.List;


import com.predic8.schema.Element;

import com.predic8.schema.creator.SchemaCreator;

import com.predic8.schema.creator.SchemaCreatorContext;

import com.predic8.wsdl.*;

import com.predic8.wstool.creator.RequestCreatorContext;

import com.predic8.wstool.creator.RequestCreatorContextTest;

import com.predic8.wstool.creator.RequestTemplateCreator;

import com.predic8.wstool.creator.RequestTemplateCreatorContext;

import com.predic8.wstool.creator.SOARequestCreator;


public class ListWSDLOperations {


  public static void main(String[] args) {

    WSDLParser parser = new WSDLParser();


    Definitions defs = parser

        .parse("resources/TicketRS_WS.wsdl");


    for (PortType pt : defs.getPortTypes()) {

      System.out.println(pt.getName());

      for (Operation op : pt.getOperations()) {

        System.out.println(" -" + op.getName());

        

       

            System.out.println("Request Template");

 

            printTemplate(defs.getElement(op.getInput().getMessage().getParts().get(0).getElement()));

           

            System.out.println();

           

            System.out.println("Response Template");

           

            printTemplate(defs.getElement(op.getOutput().getMessage().getParts().get(0).getElement()));        

      }

    }

  }


private static void printTemplate(Element e) {

StringWriter writer = new StringWriter();

RequestTemplateCreator creator = new RequestTemplateCreator();

creator.setBuilder(new MarkupBuilder(writer));  

e.create(creator, new RequestTemplateCreatorContext());

System.out.println(writer);

}

}


To get a list of Parameters like that:

TicketRS_WS

 -getTicketReservation

Request Parameters

PESEL {http://www.w3.org/2001/XMLSchema}string

Data {http://www.w3.org/2001/XMLSchema}string


Response Parameters

result {http://www.w3.org/2001/XMLSchema}string


You can use the following program:


/* Copyright 2012 predic8 GmbH, www.predic8.com


   Licensed under the Apache License, Version 2.0 (the "License");

   you may not use this file except in compliance with the License.

   You may obtain a copy of the License at


   http://www.apache.org/licenses/LICENSE-2.0


   Unless required by applicable law or agreed to in writing, software

   distributed under the License is distributed on an "AS IS" BASIS,

   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

   See the License for the specific language governing permissions and

   limitations under the License. */


package sample.wsdl;


import groovy.xml.MarkupBuilder;


import java.io.StringWriter;

import java.util.List;


import com.predic8.schema.ComplexType;

import com.predic8.schema.Element;

import com.predic8.schema.Sequence;

import com.predic8.schema.creator.SchemaCreator;

import com.predic8.schema.creator.SchemaCreatorContext;

import com.predic8.wsdl.*;

import com.predic8.wstool.creator.RequestCreatorContext;

import com.predic8.wstool.creator.RequestCreatorContextTest;

import com.predic8.wstool.creator.RequestTemplateCreator;

import com.predic8.wstool.creator.RequestTemplateCreatorContext;

import com.predic8.wstool.creator.SOARequestCreator;


public class ListWSDLOperations {


public static void main(String[] args) {

WSDLParser parser = new WSDLParser();


Definitions defs = parser.parse("resources/TicketRS_WS.wsdl");


for (PortType pt : defs.getPortTypes()) {

System.out.println(pt.getName());

for (Operation op : pt.getOperations()) {

System.out.println(" -" + op.getName());


System.out.println("Request Parameters");


listParameters(defs.getElement(op.getInput().getMessage()

.getParts().get(0).getElement()));


System.out.println();


System.out.println("Response Parameters");


listParameters(defs.getElement(op.getOutput().getMessage()

.getParts().get(0).getElement()));


}

}

}


private static void listParameters(Element element) {

ComplexType ct = (ComplexType) element.getEmbeddedType();

for (Element e : ct.getSequence().getElements()) {

System.out.println(e.getName() + " " + e.getType());

}

}


}


--
Thomas

Thomas Bayer

unread,
Jul 6, 2012, 3:24:27 AM7/6/12
to soa-...@googlegroups.com
Hi,
the schema has now a different structure. You have to test how the schema is build. In this WSDL the wrapper element does not have an embedded complexType, instead it references a type with the type attribute. You can change the listParameters method as follows to get the complexType.

   

private static void listParameters(Element element) {
        ComplexType ct = (ComplexType) element.getEmbeddedType();
        
        if(ct == null) {
            ct = (ComplexType) element.getSchema().getType(element.getType());
        }
        
        for (Element e : ct.getSequence().getElements()) {
            System.out.println(e.getName() + " " + e.getType());
        }
}

But this will not work for every WSDL. If the ct has a choice or all subelement instead of a sequence you have to test for those. If you want a sample how to do that for all schema options have a look at the SchemaCreator class in the SOA Model source code. You can subclass the AbstractSchemaCreator that has already a lot of logic to traverse a schema and build on that your logic.

--
Thomas

oma...@gmail.com

unread,
Sep 27, 2013, 7:04:42 AM9/27/13
to soa-...@googlegroups.com
Hi Thomas,
My name is Omar, I am doing a project in Web Service Discovery area, I am using your predic8 library to parse WSDl Files online and conduct the discovery mechanism afterwords.
However, I am facing a problem in retrieving the list of parameters from the service operation, I referred to the post you made one guy who was having similar problem on google+ Here, and followed the steps you mentioned in that post, but I am still getting error :NullPointerException when it come to parameters list. Woul you please me solve that headache and I really appreciate your help.
Following is the Code I have:

package parser;

import com.predic8.schema.ComplexType;
import com.predic8.schema.Element;
import com.predic8.wsdl.Definitions;
import com.predic8.wsdl.Operation;
import com.predic8.wsdl.Port;
import com.predic8.wsdl.PortType;
import com.predic8.wsdl.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class MSoa2 {
   
    private static String servicename,address,tns,documentation;
    private Definitions defs;
   
    public MSoa2(){
       
    }
    public MSoa2(String svc){
       
        parseWSDL(svc);
    }
    /**
     * @return the servicename
     */
    public String getServicename() {
        return servicename;
    }

    /**
     * @param aServicename the servicename to set
     */
    public void setServicename(String aServicename) {
        servicename = aServicename;
    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @param aAddress the address to set
     */
    public void setAddress(String aAddress) {
        address = aAddress;
    }

    /**
     * @return the tns
     */
    public String getTns() {
        return tns;
    }

    /**
     * @param aTns the tns to set
     */
    public void setTns(String aTns) {
        tns = aTns;
    }

    /**
     * @return the documentation
     */
    public String getDocumentation() {
        return documentation;
    }

    /**
     * @param aDocumentation the documentation to set
     */
    public void setDocumentation(String aDocumentation) {
        documentation = aDocumentation;
    }
   
    private void parseWSDL(String srvc){
       
        com.predic8.wsdl.WSDLParser parser = new com.predic8.wsdl.WSDLParser();
        defs = parser.parse(srvc);
       
        setTns(defs.getTargetNamespace());
        if(defs.getDocumentation() != null){
            setDocumentation(defs.getDocumentation().toString());
        }
       
       
        for (Service service : defs.getServices()) {
               
                for (Port port : service.getPorts()) {
                       
                        setAddress(port.getAddress().getLocation());
                        setServicename(service.getName());
                }
        }
    }
   
    public Map<String,ArrayList> getOperations(){
       
        //srvc="wsdl_repo/BLZService.wsdl";
       
        Map<String,ArrayList> listOps = new HashMap<>();
        ArrayList params;
        for (PortType pt : defs.getPortTypes()) {           
            for (Operation op : pt.getOperations()) {               
                params =  listParameters(defs.getElement(op.getName()));               
                //params = listParameters(defs.getElement(op.getInput().getMessage().getParts().get(0).getElement()));
                listOps.put(op.getName(), params);             
            }
        }
        return listOps;
       
    }
  
    /*private ArrayList<String> listParameters(Element element){
        ArrayList<String> listElement = new ArrayList<>();
       
        ComplexType ct = (ComplexType)element.getEmbeddedType();
       
        if(ct == null){
            ct = element.getSchema().getComplexType(element.getType().getLocalPart());
        }
       
        for(Element e : ct.getSequence().getElements()){

            listElement.add(e.getName()+" "+e.getType().getLocalPart());
        }
        return listElement;
       
    }*/
   
   
    private ArrayList<String> listParameters(Element element) {
        ArrayList<String> listElement = new ArrayList<>();

        ComplexType ct = (ComplexType) element.getEmbeddedType();

        for (Element e : ct.getSequence().getElements()) {
            listElement.add(e.getName()+" "+e.getType().getLocalPart());
            //System.out.println(e.getName() + " " + e.getType());
        }             
        return listElement;
    }
}


Attached is the wsdl file I get error while trying to  get operation parameters and their types ( e.g.: name:string).
I have tried the other function which I commented as well in case the embedded type is missing but I got same error.
Hopefully you can help in this.
Sincere Regards,
Omar.
33_Argentina_TiempoService.wsdl

Kaveh Keshavarzi

unread,
Sep 27, 2013, 8:08:57 AM9/27/13
to soa-...@googlegroups.com, oma...@gmail.com
Hi Omar,

in class Part, the methods you are using return already the right element or type objects. So you use:

params = listParameters(op.getInput().getMessage().getParts().get(0).getElement());

instead of: 

params = listParameters(defs.getElement(op.getInput().getMessage().getParts().get(0).getElement()));

Let me know if the problem still exists.

Cheers, Kaveh

oma...@gmail.com

unread,
Sep 30, 2013, 5:00:28 AM9/30/13
to soa-...@googlegroups.com, oma...@gmail.com

Hi Kaveh,
Thank you indeed for your reply, I tried the way you told me, it is returning element of type object like this:
element[name=getTime,type=null,ref=null,embeddedType=complexType[qname=null,model=sequence[name= null, particles=[element[name=strNombre,type={http://www.w3.org/2001/XMLSchema}string,ref=null,embeddedType=null]]]]]

However, what I want is to get only parameter name and type like:
(strNombre,string), I dont want the whole element as above.
Hope you can help me and I am grateful to you.

Sincere Regards,
Omar.

Kaveh Keshavarzi

unread,
Sep 30, 2013, 5:12:29 AM9/30/13
to soa-...@googlegroups.com, oma...@gmail.com
Hi Omar,

no problem! Consider you get element 'e'
Then:

e.getName() == 'strNombre'
e.getType().getLocalPart() == 'string'

Hope this can help you ;-)

Cheers, Kaveh

oma...@gmail.com

unread,
Sep 30, 2013, 5:46:28 AM9/30/13
to soa-...@googlegroups.com, oma...@gmail.com
Hi Kaveh,
Thanks again for the reply, please bear with me, i am a newbie in dealing with WSDL complexities :D, with your help I will improve for sure.
I tried your suggestions in this way:

/function call
params = listParameters(op.getInput().getMessage().getParts().get(0).getElement());

// function
listParameters(...)

private ArrayList<String> listParameters(Element element) {
       
        ArrayList<String> listElement = new ArrayList<>();
        ComplexType ct = (ComplexType) element.getEmbeddedType();

        if(ct == null){
            ct = element.getSchema().getComplexType(element.getType().getLocalPart());
        }
       
        for (Element e : ct.getSequence().getElements()) {
            listElement.add(e.getName()+" "+e.getType().getLocalPart());
           
        }
              
        return listElement;
    }


I am getting exception when I try to execute it from the main, here is the exception:

Exception in thread "main" java.lang.NullPointerException
    at parser.MSoa2.listParameters(MSoa2.java:162)
    at parser.MSoa2.getOperations(MSoa2.java:126)

What am I doing wrong here?? and what is the best solution in your pov.

Thankks for helping me.
Regards.

Kaveh Keshavarzi

unread,
Sep 30, 2013, 6:02:13 AM9/30/13
to soa-...@googlegroups.com, oma...@gmail.com
Dear Omar,

I think this problem does not refer to WSDL complexities but Java!
In your code I can't see the line numbers! The exception is in the line 162. If you call any method on an element, which is null, you get this exception.
Please notice, that an element do not have a name, type or an embeddedType necessarily. So you have to avoid the NPE any time you call one of these on your element.

oma...@gmail.com

unread,
Sep 30, 2013, 7:24:17 AM9/30/13
to soa-...@googlegroups.com, oma...@gmail.com


Dear Kaveh,
I am really tring to solve this headache but that NulException is killing me...
Now I removed everything and this is what I have:

public Map<String,ArrayList> getOperations(){

      
        Map<String,ArrayList> listOps = new HashMap<>();
        ArrayList params;
        for (PortType pt : defs.getPortTypes()) {
           
            for (Operation op : pt.getOperations()) {
              
                params =  listParameters(op);
                listOps.put(op.getName(), params);
            }
        }
        return listOps;
    } 
   
    private ArrayList<String> listParameters(Operation op) {

        ArrayList<String> listElement = new ArrayList<>();
        Element e = op.getInput().getMessage().getParts().get(0).getElement();
        listElement.add(e.toString())
       
        //I need to get the paraameter name "strNombre" and its Type "string" from the element junk returned. How???
       
        return listElement;
    }

the above function listParameters returns the element like this:

element[name=getTime,type=null,ref=null,embeddedType=complexType[qname=null,model=sequence[name= null, particles=[element[name=strNombre,type={http://www.w3.org/2001/XMLSchema}string,ref=null,embeddedType=null]]]]]

If you can give a working code for the listParameters function to return only ("strNombre" : "string") would be great. Sorry for bothering you my friend.

Regards.

oma...@gmail.com

unread,
Oct 1, 2013, 4:47:50 AM10/1/13
to soa-...@googlegroups.com
Hi Thomas,
I have this code and I need to get the parameter name and its type ("strNombre" : "string") :

Thomas Bayer

unread,
Oct 1, 2013, 4:58:55 AM10/1/13
to soa-...@googlegroups.com
Hi,
have a look at the previous posts in this thread. There you should find everything to solve that problem. Take the following code as a starting point:

private static void listParameters(Element element) {
        ComplexType ct = (ComplexType) element.getEmbeddedType();
        
        if(ct == null) {
            ct = (ComplexType) element.getSchema().getType(element.getType());
        }
        
        for (Element e : ct.getSequence().getElements()) {
            System.out.println(e.getName() + " " + e.getType());
        }
}

--
Thomas

Am 10/1/13 10:47 AM, schrieb oma...@gmail.com:
--
You received this message because you are subscribed to the Google Groups "Membrane SOA Model" group.
To unsubscribe from this group and stop receiving emails from it, send an email to soa-model+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/soa-model/85cb2431-b79a-4c18-8fb3-0f5d8cdf56bf%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

oma...@gmail.com

unread,
Oct 1, 2013, 5:19:45 AM10/1/13
to soa-...@googlegroups.com
Hi Thomas,
Thanks for the reply.
I have tried your previous posts and the code you gave, but without success, following is the WSDL file I am trying to extract the arguments from, please try with it and update if it works and how.
I really need your help and appreciate your cooperation.
Thanks alot.
33_Argentina_TiempoService.wsdl

Thomas Bayer

unread,
Oct 1, 2013, 7:50:58 AM10/1/13
to soa-...@googlegroups.com
Hi,
we can give you the support provided here. But you have to code it
yourself or we can provide you paid development or support.

--
Thomas


Am 10/1/13 11:19 AM, schrieb oma...@gmail.com:
> --
> You received this message because you are subscribed to the Google
> Groups "Membrane SOA Model" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to soa-model+...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/soa-model/fc1230c8-9fe1-4025-9f7b-272fb7ee8075%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages