You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by James Carr <ja...@gmail.com> on 2012/10/18 21:06:53 UTC

Better way to set Soap:Header elements?

Hi all,

I generated soap stubs with wsdl2java and I am using JAX-WS. The
service I need to call requires soap:header elements to be set but
these aren't exposed in the generated objects anywhere.

However... they are in the ObjectFactory generated for the elements.
Specifically:

    @XmlElementDecl(namespace =
"http://www.example.com/services/loginservice", name =
"ClientAccessKey")
    public JAXBElement<String> createClientAccessKey(String value) {
        return new JAXBElement<String>(_ClientAccessKey_QNAME,
String.class, null, value);
    }

Currently the only way I could figure out how to do this is by doing this:

	List<Header> headers = new ArrayList<Header>();
		
		JAXBElement<String> userName = f.createUserName("xxxxxxx");
		JAXBElement<String> password = f.createPassword("xxxxxxxxxxxxx");
		JAXBElement<String> accessKey = f.createUserAccessKey("xxxxxx");
		JAXBElement<String> clientKey = f.createClientAccessKey("xxxxxxxxxxxx");
		
		headers.add(new Header(userName.getName(), userName.getValue(), new
JAXBDataBinding(String.class)));
		headers.add(new Header(password.getName(), password.getValue(), new
JAXBDataBinding(String.class)));
		headers.add(new Header(accessKey.getName(), accessKey.getValue(),
new JAXBDataBinding(String.class)));
		headers.add(new Header(clientKey.getName(), clientKey.getValue(),
new JAXBDataBinding(String.class)));
		
		
		proxy.getRequestContext().put(Header.HEADER_LIST, headers);

I don't like it... is there a better way?


Thanks,
James

Re: Better way to set Soap:Header elements?

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


You could likely do something like:


public class StringHeader extends Header {
     private static final JAXBDataBinding DATABINDING 
         = new JAXBDataBinding(JAXBContext.newInstance(String.class));

     public StringHeader(JAXBElement<String> v) {
         super(v.getName(), v.getValue(), DATABINDING);
     }
}


and then do:

headers.add(new StringHeader(f.createUserName("xxxxxxx")));
…..


Dan





On Oct 18, 2012, at 3:06 PM, James Carr <ja...@gmail.com> wrote:

> Hi all,
> 
> I generated soap stubs with wsdl2java and I am using JAX-WS. The
> service I need to call requires soap:header elements to be set but
> these aren't exposed in the generated objects anywhere.
> 
> However... they are in the ObjectFactory generated for the elements.
> Specifically:
> 
>    @XmlElementDecl(namespace =
> "http://www.example.com/services/loginservice", name =
> "ClientAccessKey")
>    public JAXBElement<String> createClientAccessKey(String value) {
>        return new JAXBElement<String>(_ClientAccessKey_QNAME,
> String.class, null, value);
>    }
> 
> Currently the only way I could figure out how to do this is by doing this:
> 
> 	List<Header> headers = new ArrayList<Header>();
> 		
> 		JAXBElement<String> userName = f.createUserName("xxxxxxx");
> 		JAXBElement<String> password = f.createPassword("xxxxxxxxxxxxx");
> 		JAXBElement<String> accessKey = f.createUserAccessKey("xxxxxx");
> 		JAXBElement<String> clientKey = f.createClientAccessKey("xxxxxxxxxxxx");
> 		
> 		headers.add(new Header(userName.getName(), userName.getValue(), new
> JAXBDataBinding(String.class)));
> 		headers.add(new Header(password.getName(), password.getValue(), new
> JAXBDataBinding(String.class)));
> 		headers.add(new Header(accessKey.getName(), accessKey.getValue(),
> new JAXBDataBinding(String.class)));
> 		headers.add(new Header(clientKey.getName(), clientKey.getValue(),
> new JAXBDataBinding(String.class)));
> 		
> 		
> 		proxy.getRequestContext().put(Header.HEADER_LIST, headers);
> 
> I don't like it... is there a better way?
> 
> 
> Thanks,
> James

-- 
Daniel Kulp
dkulp@apache.org - http://dankulp.com/blog
Talend Community Coder - http://coders.talend.com


Re: Better way to set Soap:Header elements?

Posted by Freeman Fang <fr...@gmail.com>.
Also please take a look at CXF faq[1] about this question

[1]http://cxf.apache.org/faq.html#FAQ-HowcanIaddsoapheaderstotherequest%2Fresponse%3F
-------------
Freeman(Yue) Fang

Red Hat, Inc. 
FuseSource is now part of Red Hat
Web: http://fusesource.com | http://www.redhat.com/
Twitter: freemanfang
Blog: http://freemanfang.blogspot.com
http://blog.sina.com.cn/u/1473905042
weibo: http://weibo.com/u/1473905042

On 2012-10-19, at 上午3:15, Mark Streit wrote:

> You might want to look at Glen Mazza's blog - he has number of extremely
> helpful tutorials including some on SOAP headers  (using Handlers).  Your
> generated objects don't require knowledge of the SOAP Header elements.
> 
> http://www.jroller.com/gmazza/entry/blog_article_index
> 
> http://www.jroller.com/gmazza/entry/jaxws_handler_tutorial  - Adding JAX-WS
> handlers to web services and SOAP
> clients<http://www.jroller.com/gmazza/entry/jaxws_handler_tutorial>
> *(June 2012) *
> 
> 
> Hope this helps.
> 
> On Thu, Oct 18, 2012 at 3:06 PM, James Carr <ja...@gmail.com> wrote:
> 
>> Hi all,
>> 
>> I generated soap stubs with wsdl2java and I am using JAX-WS. The
>> service I need to call requires soap:header elements to be set but
>> these aren't exposed in the generated objects anywhere.
>> 
>> However... they are in the ObjectFactory generated for the elements.
>> Specifically:
>> 
>>    @XmlElementDecl(namespace =
>> "http://www.example.com/services/loginservice", name =
>> "ClientAccessKey")
>>    public JAXBElement<String> createClientAccessKey(String value) {
>>        return new JAXBElement<String>(_ClientAccessKey_QNAME,
>> String.class, null, value);
>>    }
>> 
>> Currently the only way I could figure out how to do this is by doing this:
>> 
>>        List<Header> headers = new ArrayList<Header>();
>> 
>>                JAXBElement<String> userName = f.createUserName("xxxxxxx");
>>                JAXBElement<String> password =
>> f.createPassword("xxxxxxxxxxxxx");
>>                JAXBElement<String> accessKey =
>> f.createUserAccessKey("xxxxxx");
>>                JAXBElement<String> clientKey =
>> f.createClientAccessKey("xxxxxxxxxxxx");
>> 
>>                headers.add(new Header(userName.getName(),
>> userName.getValue(), new
>> JAXBDataBinding(String.class)));
>>                headers.add(new Header(password.getName(),
>> password.getValue(), new
>> JAXBDataBinding(String.class)));
>>                headers.add(new Header(accessKey.getName(),
>> accessKey.getValue(),
>> new JAXBDataBinding(String.class)));
>>                headers.add(new Header(clientKey.getName(),
>> clientKey.getValue(),
>> new JAXBDataBinding(String.class)));
>> 
>> 
>>                proxy.getRequestContext().put(Header.HEADER_LIST, headers);
>> 
>> I don't like it... is there a better way?
>> 
>> 
>> Thanks,
>> James
>> 
> 
> 
> 
> -- 
> *Mark
> *


Re: Better way to set Soap:Header elements?

Posted by Mark Streit <mc...@gmail.com>.
You might want to look at Glen Mazza's blog - he has number of extremely
helpful tutorials including some on SOAP headers  (using Handlers).  Your
generated objects don't require knowledge of the SOAP Header elements.

http://www.jroller.com/gmazza/entry/blog_article_index

http://www.jroller.com/gmazza/entry/jaxws_handler_tutorial  - Adding JAX-WS
handlers to web services and SOAP
clients<http://www.jroller.com/gmazza/entry/jaxws_handler_tutorial>
 *(June 2012) *


Hope this helps.

On Thu, Oct 18, 2012 at 3:06 PM, James Carr <ja...@gmail.com> wrote:

> Hi all,
>
> I generated soap stubs with wsdl2java and I am using JAX-WS. The
> service I need to call requires soap:header elements to be set but
> these aren't exposed in the generated objects anywhere.
>
> However... they are in the ObjectFactory generated for the elements.
> Specifically:
>
>     @XmlElementDecl(namespace =
> "http://www.example.com/services/loginservice", name =
> "ClientAccessKey")
>     public JAXBElement<String> createClientAccessKey(String value) {
>         return new JAXBElement<String>(_ClientAccessKey_QNAME,
> String.class, null, value);
>     }
>
> Currently the only way I could figure out how to do this is by doing this:
>
>         List<Header> headers = new ArrayList<Header>();
>
>                 JAXBElement<String> userName = f.createUserName("xxxxxxx");
>                 JAXBElement<String> password =
> f.createPassword("xxxxxxxxxxxxx");
>                 JAXBElement<String> accessKey =
> f.createUserAccessKey("xxxxxx");
>                 JAXBElement<String> clientKey =
> f.createClientAccessKey("xxxxxxxxxxxx");
>
>                 headers.add(new Header(userName.getName(),
> userName.getValue(), new
> JAXBDataBinding(String.class)));
>                 headers.add(new Header(password.getName(),
> password.getValue(), new
> JAXBDataBinding(String.class)));
>                 headers.add(new Header(accessKey.getName(),
> accessKey.getValue(),
> new JAXBDataBinding(String.class)));
>                 headers.add(new Header(clientKey.getName(),
> clientKey.getValue(),
> new JAXBDataBinding(String.class)));
>
>
>                 proxy.getRequestContext().put(Header.HEADER_LIST, headers);
>
> I don't like it... is there a better way?
>
>
> Thanks,
> James
>



-- 
*Mark
*