You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by gcsaba2ms <cs...@morganstanley.com> on 2013/02/22 15:45:23 UTC

How to add custom headers to SOAP response? The code from the FAQ won't work!

Hello,

I have a legacy SOAP application which returns a random number of SOAP
headers. I'm trying to implement this now with JAX-WS and CXF.

I did search the internet and found that the solution for that would be the
following code:

=====

List<Header> headers = new ArrayList<Header>();
Header dummyHeader = new Header(new QName("uri:org.apache.cxf", "dummy"),
"decapitated",
                                new JAXBDataBinding(String.class));
headers.add(dummyHeader);

//server side:
context.getMessageContext().put(Header.HEADER_LIST, headers);

=====

I've tried many variations of that code: without JAXBDataBinding, adding
Element instead of String, using SoapHeader instead of Header... it doesn't
work. Every time I get a SOAP response without a header, like this:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
      ...


If I DON'T include that code above and instead I add headers with
@WebParam(header=true, then it will work, the header will get added. However
I can only add a fixed number of headers that way, which is not what I want.

Any idea why the code above won't work? I don't think I'm doing anything
special... I have a @WebService class, it's exposed through TCP.

I've added an interceptor which extends AbstractOutDatabindingInterceptor,
so it gets called in the final phases. I've checked and at that point the
header list has size = 0




--
View this message in context: http://cxf.547215.n5.nabble.com/How-to-add-custom-headers-to-SOAP-response-The-code-from-the-FAQ-won-t-work-tp5723581.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: How to add custom headers to SOAP response? The code from the FAQ won't work!

Posted by gcsaba2ms <cs...@morganstanley.com>.
I was trying to solve this with a hack but it's not working so far, could
someone help out?

What I have is this:

@WebMethod
public void doStuff(@WebParam(header=true, mode=WebParam.Mode.OUT,
partName="header")Holder<List&lt;SoapHeader>> respHeader) {  ... }

I've added an interceptor which runs during USER_LOGICAL phase. It gets the
MessageInfo and from there finds the MessageInfoPart with the name "header".

I've managed to get this part.

At this point I would like to remove this part from the message, because if
it would continue I would get an exception that List<SoapHeader> is not a
supported type. How to do that?

I tried this but it doesn't seem to work:
msgInfo.removeMessagePart(headerPart.getElementQName);  // headerPart is a
MessagePartInfo

I would also want to actually get the object from the Holder and convert
that into headers. How to do that? I don't know much about CXF interceptors,
how can I add headers to the outgoing message?





--
View this message in context: http://cxf.547215.n5.nabble.com/How-to-add-custom-headers-to-SOAP-response-The-code-from-the-FAQ-won-t-work-tp5723581p5723591.html
Sent from the cxf-user mailing list archive at Nabble.com.

RE: How to add custom headers to SOAP response? The code from the FAQ won't work!

Posted by Andrei Shakirin <as...@talend.com>.
Hi,

Try to use code bellow, it should work:

@WebService(endpointInterface = "demo.hw.server.HelloWorld", serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {
	@Resource
	WebServiceContext context;

	public String sayHi(String text) {
		System.out.println("sayHi called");

		try {
			List<Header> headers = new ArrayList<Header>();
			Header dummyHeader;
			dummyHeader = new Header(new QName("uri:org.apache.cxf", "dummy"),
					"decapitated", new JAXBDataBinding(String.class));
			headers.add(dummyHeader);

			// server side:
			context.getMessageContext().put(Header.HEADER_LIST, headers);

			return "Hello " + text;
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
}

Regards,
Andrei.

> -----Original Message-----
> From: gcsaba2ms [mailto:csaba.galyo@morganstanley.com]
> Sent: Freitag, 22. Februar 2013 15:45
> To: users@cxf.apache.org
> Subject: How to add custom headers to SOAP response? The code from the
> FAQ won't work!
> 
> Hello,
> 
> I have a legacy SOAP application which returns a random number of SOAP
> headers. I'm trying to implement this now with JAX-WS and CXF.
> 
> I did search the internet and found that the solution for that would be the
> following code:
> 
> =====
> 
> List<Header> headers = new ArrayList<Header>(); Header dummyHeader =
> new Header(new QName("uri:org.apache.cxf", "dummy"), "decapitated",
>                                 new JAXBDataBinding(String.class));
> headers.add(dummyHeader);
> 
> //server side:
> context.getMessageContext().put(Header.HEADER_LIST, headers);
> 
> =====
> 
> I've tried many variations of that code: without JAXBDataBinding, adding
> Element instead of String, using SoapHeader instead of Header... it doesn't
> work. Every time I get a SOAP response without a header, like this:
> 
> <soap:Envelope
> xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
>   <soap:Body>
>       ...
> 
> 
> If I DON'T include that code above and instead I add headers with
> @WebParam(header=true, then it will work, the header will get added.
> However I can only add a fixed number of headers that way, which is not
> what I want.
> 
> Any idea why the code above won't work? I don't think I'm doing anything
> special... I have a @WebService class, it's exposed through TCP.
> 
> I've added an interceptor which extends AbstractOutDatabindingInterceptor,
> so it gets called in the final phases. I've checked and at that point the header
> list has size = 0
> 
> 
> 
> 
> --
> View this message in context: http://cxf.547215.n5.nabble.com/How-to-add-
> custom-headers-to-SOAP-response-The-code-from-the-FAQ-won-t-work-
> tp5723581.html
> Sent from the cxf-user mailing list archive at Nabble.com.