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