You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Pirola Davide <da...@matrix.it> on 2007/07/20 17:38:25 UTC

Add SOAP HEADER value

Hi,

anyone know how to add some value to Soap Header in cxf?

I have generated a client code with "wsdl2java", but I don't know how to
add this values to the soap header of the message.

I tried with this code:

 

  Map<String, Object> soapHeaders = new HashMap<String, Object>();

    List h1 = new ArrayList();

    h1.add("value1");

    soapHeaders.put("parameter1", h1);

    List h2 = new ArrayList();

    h2.add("parameter2");

    soapHeaders.put("value2", h2);

     ...

     ...

    requestContext.put(MessageContext.HTTP_REQUEST_HEADERS,
soapHeaders);

 

but, this seems that they are ignored by the server part of the web
services.

 

Thanks

Davide


Re: Add SOAP HEADER value

Posted by Daniel Kulp <dk...@apache.org>.

Davide,

In addition to what Ulhas mentioned, you could also have it
done "automatically" at wsdl2java time.   If you run wsdl2java with
the "-exsh true" flag, all the headers would be added as additional
parameters to the methods.

Example:

    @WebMethod
    public void addSyndicationService(
        @WebParam(targetNamespace = "http://www.google.com/api/adsense/v2", name = "synServiceType")
        java.lang.String synServiceType,
        @WebParam(targetNamespace = "http://www.google.com/api/adsense/v2", header = true, name = "debug_zip")
        java.lang.String debugZip,
        @WebParam(targetNamespace = "http://www.google.com/api/adsense/v2", header = true, name = "debug_phone")
        java.lang.String debugPhone,
        @WebParam(targetNamespace = "http://www.google.com/api/adsense/v2", header = true, name = "debug_association_type")
        java.lang.String debugAssociationType,
        @WebParam(targetNamespace = "http://www.google.com/api/adsense/v2", header = true, name = "display_locale")
        java.lang.String displayLocale,
        @WebParam(targetNamespace = "http://www.google.com/api/adsense/v2", header = true, name = "client_id")
        java.lang.String clientId,
        @WebParam(targetNamespace = "http://www.google.com/api/adsense/v2", header = true, name = "developer_email")
        java.lang.String developerEmail,
        @WebParam(targetNamespace = "http://www.google.com/api/adsense/v2", header = true, name = "developer_password")
        java.lang.String developerPassword
    ) throws AdSenseApiException_Exception;


That said, there have been some runtime bugs with that.  You may need 
the latest trunk for that to actually work.

Dan


On Friday 20 July 2007 11:38, Pirola Davide wrote:
> Hi,
>
> anyone know how to add some value to Soap Header in cxf?
>
> I have generated a client code with "wsdl2java", but I don't know how
> to add this values to the soap header of the message.
>
> I tried with this code:
>
>
>
>   Map<String, Object> soapHeaders = new HashMap<String, Object>();
>
>     List h1 = new ArrayList();
>
>     h1.add("value1");
>
>     soapHeaders.put("parameter1", h1);
>
>     List h2 = new ArrayList();
>
>     h2.add("parameter2");
>
>     soapHeaders.put("value2", h2);
>
>      ...
>
>      ...
>
>     requestContext.put(MessageContext.HTTP_REQUEST_HEADERS,
> soapHeaders);
>
>
>
> but, this seems that they are ignored by the server part of the web
> services.
>
>
>
> Thanks
>
> Davide

-- 
J. Daniel Kulp
Principal Engineer
IONA
P: 781-902-8727    C: 508-380-7194
daniel.kulp@iona.com
http://www.dankulp.com/blog

RE: Add SOAP HEADER value

Posted by Pirola Davide <da...@matrix.it>.
Hi,

I have solved my problem.

I post the solution that works for me:

 

 

  Document d = DOMUtils.createDocument();

  Element dev_email = d.createElement("developer_email");    

  dev_email.setTextContent("......");

  SoapHeader sh_dev_email = new SoapHeader(new QName("http://www.google.com/api/adsense/v2", "AccountServiceService"), dev_email);

 

  Element dev_pass = d.createElement("developer_password");    

  dev_pass.setTextContent("......");

  SoapHeader sh_dev_pass = new SoapHeader(new QName("http://www.google.com/api/adsense/v2", "AccountServiceService"), dev_pass);

    

  Element client_id = d.createElement("client_id");    

  client_id.setTextContent("......");

  SoapHeader sh_client_id = new SoapHeader(new QName("http://www.google.com/api/adsense/v2", "AccountServiceService"), client_id);

 

    

  List<Header> lshd = new ArrayList<Header>();

  lshd.add(sh_dev_email);

  lshd.add(sh_dev_pass);

  lshd.add(sh_client_id);

  lshd.add(sh_disp_loc);

  requestContext.put(Header.HEADER_LIST, lshd);

 

 

In this way is possible to add Header values to the soap message.

Thanks to everyone that answer me... I appreciate your help!

 

Davide

 

 

 

 

-----Original Message-----
From: Pirola Davide [mailto:davide.pirola@matrix.it] 
Sent: venerdì 20 luglio 2007 18.51
To: cxf-user@incubator.apache.org
Subject: RE: Add SOAP HEADER value

 

Hi Bhole,

Thanks for your response!

 

I must call a web services (Google Adsense):

https://sandbox.google.com/api/adsense/v2/AccountService?wsdl

 

All services, accept request only if in the header contains some value (es: developer_email | developer_password ecc..), otherwise google send back an error.

 

So, in the WSDL they are defined... I simply need to add my values to the soap message.

Google put on its site a sample code for who use AXIS:

http://code.google.com/apis/adsense/developer/samples/java/CreateAccount.java.txt

 

I need to do the same thing, but with CXF :-D

 

Your code is valid also in this case?

 

Thanks

Davide

 

-----Original Message-----

From: Ulhas Bhole(IONA) [mailto:ulhas.bhole@iona.com] 

Sent: venerdì 20 luglio 2007 17.53

To: cxf-user@incubator.apache.org

Subject: Re: Add SOAP HEADER value

 

Hi Davide,

 

Are you trying to add soap headers that are not defined in WSDL? 

(Out-of-band Header) If yes then here is the code snippet that does the 

same.

 

 For adding header :

        InvocationHandler handler  = Proxy.getInvocationHandler(portType);

        BindingProvider  bp = null;

 

        try {

            if (handler instanceof BindingProvider) {

                bp = (BindingProvider)handler;

                Map<String, Object> requestContext = bp.getRequestContext();

               

                OutofBandHeader ob = new OutofBandHeader();

                ob.setName("testOobHeader");

                ob.setValue("testOobHeaderValue");

                ob.setHdrAttribute("testHdrAttribute");

 

                JAXBElement<OutofBandHeader> job = new 

JAXBElement<OutofBandHeader>(

                        new QName(TEST_HDR_NS, TEST_HDR_REQUEST_ELEM), 

OutofBandHeader.class, null, ob);

                Header hdr = new Header(

                        new QName(TEST_HDR_NS, TEST_HDR_REQUEST_ELEM),

                        job,

                        new JAXBDataBinding(ob.getClass()));

 

                List<Header> holder = new ArrayList<Header>();

                holder.add(hdr);

               

                //Add List of headerHolders to requestContext.

                requestContext.put(Header.HEADER_LIST, holder);

            }

        } catch (JAXBException ex) {

            //System.out.println("failed to insert header into request 

context :" + ex);

        }

       

 

 

For Accessing the headers received from server :

 

        InvocationHandler handler  = Proxy.getInvocationHandler(portType);

        BindingProvider  bp = null;

        if (handler instanceof BindingProvider) {

            bp = (BindingProvider)handler;

            Map<String, Object> responseContext = bp.getResponseContext();

            OutofBandHeader hdrToTest = null;

            List oobHdr = (List) responseContext.get(Header.HEADER_LIST);

            if (oobHdr == null) {

                fail("Should have got List of out-of-band headers ..");

            }

           

            if (oobHdr != null & oobHdr instanceof List) {

                Iterator iter = oobHdr.iterator();

                while (iter.hasNext()) {

                    Object hdr = iter.next();

                    if (hdr instanceof Header) {

                        Header hdr1 = (Header) hdr;

                        if (hdr1.getObject() instanceof Node) {

                            //System.out.println("Node conains : " + 

hdr1.getObject().toString());

                            try {

                                JAXBElement job = (JAXBElement) 

JAXBContext.newInstance(ObjectFactory.class)

                                    .createUnmarshaller()

                                    .unmarshal((Node) hdr1.getObject());

                                hdrToTest = (OutofBandHeader) 

job.getValue();

//                                 System.out.println("oob-hdr contains 

: \nname = "

//                                       + hdrToTest.getName()

//                                       + "  \nvalue = " + 

hdrToTest.getValue()

//                                       + " \natribute = " + 

hdrToTest.getHdrAttribute());

                            } catch (JAXBException ex) {

                                //

                                ex.printStackTrace();

                            }

                        }

                    }

                }

            }

 

 

This code snippet is from the System test and it uses JAXB generated 

class for headers but is not limited to JAXB.

 

Regards,

 

Ulhas Bhole

 

Pirola Davide wrote:

> Hi,

> 

> anyone know how to add some value to Soap Header in cxf?

> 

> I have generated a client code with "wsdl2java", but I don't know how to

> add this values to the soap header of the message.

> 

> I tried with this code:

> 

>  

> 

>   Map<String, Object> soapHeaders = new HashMap<String, Object>();

> 

>     List h1 = new ArrayList();

> 

>     h1.add("value1");

> 

>     soapHeaders.put("parameter1", h1);

> 

>     List h2 = new ArrayList();

> 

>     h2.add("parameter2");

> 

>     soapHeaders.put("value2", h2);

> 

>      ...

> 

>      ...

> 

>     requestContext.put(MessageContext.HTTP_REQUEST_HEADERS,

> soapHeaders);

> 

>  

> 

> but, this seems that they are ignored by the server part of the web

> services.

> 

>  

> 

> Thanks

> 

> Davide

> 

> 

>   

 

----------------------------

IONA Technologies PLC (registered in Ireland)

Registered Number: 171387

Registered Address: The IONA Building, Shelbourne Road, Dublin 4, Ireland


RE: Add SOAP HEADER value

Posted by Pirola Davide <da...@matrix.it>.
Hi Bhole,
Thanks for your response!

I must call a web services (Google Adsense):
https://sandbox.google.com/api/adsense/v2/AccountService?wsdl

All services, accept request only if in the header contains some value (es: developer_email | developer_password ecc..), otherwise google send back an error.

So, in the WSDL they are defined... I simply need to add my values to the soap message.
Google put on its site a sample code for who use AXIS:
http://code.google.com/apis/adsense/developer/samples/java/CreateAccount.java.txt

I need to do the same thing, but with CXF :-D

Your code is valid also in this case?

Thanks
Davide

-----Original Message-----
From: Ulhas Bhole(IONA) [mailto:ulhas.bhole@iona.com] 
Sent: venerdì 20 luglio 2007 17.53
To: cxf-user@incubator.apache.org
Subject: Re: Add SOAP HEADER value

Hi Davide,

Are you trying to add soap headers that are not defined in WSDL? 
(Out-of-band Header) If yes then here is the code snippet that does the 
same.

 For adding header :
        InvocationHandler handler  = Proxy.getInvocationHandler(portType);
        BindingProvider  bp = null;

        try {
            if (handler instanceof BindingProvider) {
                bp = (BindingProvider)handler;
                Map<String, Object> requestContext = bp.getRequestContext();
               
                OutofBandHeader ob = new OutofBandHeader();
                ob.setName("testOobHeader");
                ob.setValue("testOobHeaderValue");
                ob.setHdrAttribute("testHdrAttribute");

                JAXBElement<OutofBandHeader> job = new 
JAXBElement<OutofBandHeader>(
                        new QName(TEST_HDR_NS, TEST_HDR_REQUEST_ELEM), 
OutofBandHeader.class, null, ob);
                Header hdr = new Header(
                        new QName(TEST_HDR_NS, TEST_HDR_REQUEST_ELEM),
                        job,
                        new JAXBDataBinding(ob.getClass()));

                List<Header> holder = new ArrayList<Header>();
                holder.add(hdr);
               
                //Add List of headerHolders to requestContext.
                requestContext.put(Header.HEADER_LIST, holder);
            }
        } catch (JAXBException ex) {
            //System.out.println("failed to insert header into request 
context :" + ex);
        }
       


For Accessing the headers received from server :

        InvocationHandler handler  = Proxy.getInvocationHandler(portType);
        BindingProvider  bp = null;
        if (handler instanceof BindingProvider) {
            bp = (BindingProvider)handler;
            Map<String, Object> responseContext = bp.getResponseContext();
            OutofBandHeader hdrToTest = null;
            List oobHdr = (List) responseContext.get(Header.HEADER_LIST);
            if (oobHdr == null) {
                fail("Should have got List of out-of-band headers ..");
            }
           
            if (oobHdr != null & oobHdr instanceof List) {
                Iterator iter = oobHdr.iterator();
                while (iter.hasNext()) {
                    Object hdr = iter.next();
                    if (hdr instanceof Header) {
                        Header hdr1 = (Header) hdr;
                        if (hdr1.getObject() instanceof Node) {
                            //System.out.println("Node conains : " + 
hdr1.getObject().toString());
                            try {
                                JAXBElement job = (JAXBElement) 
JAXBContext.newInstance(ObjectFactory.class)
                                    .createUnmarshaller()
                                    .unmarshal((Node) hdr1.getObject());
                                hdrToTest = (OutofBandHeader) 
job.getValue();
//                                 System.out.println("oob-hdr contains 
: \nname = "
//                                       + hdrToTest.getName()
//                                       + "  \nvalue = " + 
hdrToTest.getValue()
//                                       + " \natribute = " + 
hdrToTest.getHdrAttribute());
                            } catch (JAXBException ex) {
                                //
                                ex.printStackTrace();
                            }
                        }
                    }
                }
            }


This code snippet is from the System test and it uses JAXB generated 
class for headers but is not limited to JAXB.

Regards,

Ulhas Bhole

Pirola Davide wrote:
> Hi,
>
> anyone know how to add some value to Soap Header in cxf?
>
> I have generated a client code with "wsdl2java", but I don't know how to
> add this values to the soap header of the message.
>
> I tried with this code:
>
>  
>
>   Map<String, Object> soapHeaders = new HashMap<String, Object>();
>
>     List h1 = new ArrayList();
>
>     h1.add("value1");
>
>     soapHeaders.put("parameter1", h1);
>
>     List h2 = new ArrayList();
>
>     h2.add("parameter2");
>
>     soapHeaders.put("value2", h2);
>
>      ...
>
>      ...
>
>     requestContext.put(MessageContext.HTTP_REQUEST_HEADERS,
> soapHeaders);
>
>  
>
> but, this seems that they are ignored by the server part of the web
> services.
>
>  
>
> Thanks
>
> Davide
>
>
>   

----------------------------
IONA Technologies PLC (registered in Ireland)
Registered Number: 171387
Registered Address: The IONA Building, Shelbourne Road, Dublin 4, Ireland

Re: Add SOAP HEADER value

Posted by "Ulhas Bhole(IONA)" <ul...@iona.com>.
Hi Davide,

Are you trying to add soap headers that are not defined in WSDL? 
(Out-of-band Header) If yes then here is the code snippet that does the 
same.

 For adding header :
        InvocationHandler handler  = Proxy.getInvocationHandler(portType);
        BindingProvider  bp = null;

        try {
            if (handler instanceof BindingProvider) {
                bp = (BindingProvider)handler;
                Map<String, Object> requestContext = bp.getRequestContext();
               
                OutofBandHeader ob = new OutofBandHeader();
                ob.setName("testOobHeader");
                ob.setValue("testOobHeaderValue");
                ob.setHdrAttribute("testHdrAttribute");

                JAXBElement<OutofBandHeader> job = new 
JAXBElement<OutofBandHeader>(
                        new QName(TEST_HDR_NS, TEST_HDR_REQUEST_ELEM), 
OutofBandHeader.class, null, ob);
                Header hdr = new Header(
                        new QName(TEST_HDR_NS, TEST_HDR_REQUEST_ELEM),
                        job,
                        new JAXBDataBinding(ob.getClass()));

                List<Header> holder = new ArrayList<Header>();
                holder.add(hdr);
               
                //Add List of headerHolders to requestContext.
                requestContext.put(Header.HEADER_LIST, holder);
            }
        } catch (JAXBException ex) {
            //System.out.println("failed to insert header into request 
context :" + ex);
        }
       


For Accessing the headers received from server :

        InvocationHandler handler  = Proxy.getInvocationHandler(portType);
        BindingProvider  bp = null;
        if (handler instanceof BindingProvider) {
            bp = (BindingProvider)handler;
            Map<String, Object> responseContext = bp.getResponseContext();
            OutofBandHeader hdrToTest = null;
            List oobHdr = (List) responseContext.get(Header.HEADER_LIST);
            if (oobHdr == null) {
                fail("Should have got List of out-of-band headers ..");
            }
           
            if (oobHdr != null & oobHdr instanceof List) {
                Iterator iter = oobHdr.iterator();
                while (iter.hasNext()) {
                    Object hdr = iter.next();
                    if (hdr instanceof Header) {
                        Header hdr1 = (Header) hdr;
                        if (hdr1.getObject() instanceof Node) {
                            //System.out.println("Node conains : " + 
hdr1.getObject().toString());
                            try {
                                JAXBElement job = (JAXBElement) 
JAXBContext.newInstance(ObjectFactory.class)
                                    .createUnmarshaller()
                                    .unmarshal((Node) hdr1.getObject());
                                hdrToTest = (OutofBandHeader) 
job.getValue();
//                                 System.out.println("oob-hdr contains 
: \nname = "
//                                       + hdrToTest.getName()
//                                       + "  \nvalue = " + 
hdrToTest.getValue()
//                                       + " \natribute = " + 
hdrToTest.getHdrAttribute());
                            } catch (JAXBException ex) {
                                //
                                ex.printStackTrace();
                            }
                        }
                    }
                }
            }


This code snippet is from the System test and it uses JAXB generated 
class for headers but is not limited to JAXB.

Regards,

Ulhas Bhole

Pirola Davide wrote:
> Hi,
>
> anyone know how to add some value to Soap Header in cxf?
>
> I have generated a client code with "wsdl2java", but I don't know how to
> add this values to the soap header of the message.
>
> I tried with this code:
>
>  
>
>   Map<String, Object> soapHeaders = new HashMap<String, Object>();
>
>     List h1 = new ArrayList();
>
>     h1.add("value1");
>
>     soapHeaders.put("parameter1", h1);
>
>     List h2 = new ArrayList();
>
>     h2.add("parameter2");
>
>     soapHeaders.put("value2", h2);
>
>      ...
>
>      ...
>
>     requestContext.put(MessageContext.HTTP_REQUEST_HEADERS,
> soapHeaders);
>
>  
>
> but, this seems that they are ignored by the server part of the web
> services.
>
>  
>
> Thanks
>
> Davide
>
>
>   

----------------------------
IONA Technologies PLC (registered in Ireland)
Registered Number: 171387
Registered Address: The IONA Building, Shelbourne Road, Dublin 4, Ireland