I need to create a map of RequestContext and set it in BindingProvider as a header. The code I have currently is as follows. I need to create a map with the respective setters and pass it as the second argument for the put() method for the header.
RequestContext requestContextBody = new RequestContext();
requestContextBody.setVersion("1.4");
requestContextBody.setLanguage(Language.EN);
requestContextBody.setGroupID("111");
requestContextBody.setRequestReference("UserRef");
requestContextBody.setUserToken(new JAXBElement<String>(new QName("http://purolator.com/pws/datatypes/v1","UserToken"), String.class, "7a0f98ec-6e9f-4eb1-9fcb-40086e2c1040"));
URL wsdlURL = PurolatorClientImpl.class.getClassLoader().getResource("wsdl/EstimatingService.wsdl");
EstimatingService ss = new EstimatingService(wsdlURL, SERVICE_NAME);
EstimatingServiceContract port = ss.getEstimatingServiceEndpoint();
((BindingProvider) port).getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS, requestContextBody); //THIS IS WHERE I NEED TO PASS THE MAP IN PLACE OF THE REQUESTCONTEXTBODY OBJECT
((BindingProvider) port).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "867aa05887b94f48988b6bf46340c5b3");
((BindingProvider) port).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "Ze@5l22N");
StackTrace ::
Exception in thread "main" javax.xml.ws.WebServiceException: java.lang.ClassCastException: com.purolator.pws.datatypes.v1.RequestContext cannot be cast to java.util.Map
at com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.process(HttpTransportPipe.java:207)
at com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.processRequest(HttpTransportPipe.java:122)
at com.sun.xml.internal.ws.transport.DeferredTransportPipe.processRequest(DeferredTransportPipe.java:95)
at com.sun.xml.internal.ws.api.pipe.Fiber.__doRun(Fiber.java:626)
at com.sun.xml.internal.ws.api.pipe.Fiber._doRun(Fiber.java:585)
at com.sun.xml.internal.ws.api.pipe.Fiber.doRun(Fiber.java:570)
at com.sun.xml.internal.ws.api.pipe.Fiber.runSync(Fiber.java:467)
at com.sun.xml.internal.ws.client.Stub.process(Stub.java:308)
at com.sun.xml.internal.ws.client.sei.SEIStub.doProcess(SEIStub.java:163)
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:98)
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:78)
at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:135)
at com.sun.proxy.$Proxy26.getFullEstimate(Unknown Source)
at com.pb.gcs.client.ClientMain.main(ClientMain.java:96)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.ClassCastException: com.purolator.pws.datatypes.v1.RequestContext cannot be cast to java.util.Map
at com.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.process(HttpTransportPipe.java:136)
... 18 more
As described in javadocs MessageContext.HTTP_REQUEST_HEADERS
need a java.util.Map
So you can try this:
Map context = ((BindingProvider) port).getRequestContext();
Map<String, List<String>> requestHeaders = (Map) context.get(MessageContext.HTTP_REQUEST_HEADERS) ;
requestHeaders.put("User-Agent", Collections.singletonList("Mozilla [...]"));
// add all yours setting
context.put(MessageContext.HTTP_REQUEST_HEADERS, headers);