On Friday 11 June 2010 11:10:43 am Cecchi Sandrone wrote:
> Great, it works Daniel!
> Last question...in order to store the object sent in an object relative to
> the current method invocation and to be accessible from the method itself,
> do I must use a ThreadLocal variable? For example, now that I sent the User
> object, i want to read that object only in the method call relative to the
> request.
Generally, you would store it on the message itself:
message.put(User.class.getName(), user);
That would make it available later. In your impl, you could do one of:
1) JAX-WS standard:
@Resource
WebServiceContext ctx;
....
ctx.getMessgeContext().get(User.class.getName());
2) Through CXF's threadlocal:
PhaseInterceptorChain.getCurrentMessage().get(User.class.getName());
Dan
>
> dkulp wrote:
> > Actually, another option that I think would work is to use the unmarshal
> > method that takes the class as well:
> >
> >
> > JAXBContext.newInstance(User.class).createUnmarshaller()
> >
> > .unmarshal((Node)h.getValue(), User.class);
> >
> > That tells JAXB exactly what you are trying to unmarshal.
> >
> > Dan
> >
> > On Friday 11 June 2010 10:22:25 am Daniel Kulp wrote:
> >> Can you add an @XmlRootElement annotation to User? If you add the name
> >> and namespace attributes to that, I think it would be readable.
> >>
> >> Dan
> >>
> >> On Friday 11 June 2010 10:13:43 am Cecchi Sandrone wrote:
> >> > I successfully added metadata in the soap header using the following
> >> > interceptor:
> >> >
> >> > public class OutputInterceptor extends AbstractSoapInterceptor {
> >> >
> >> > public OutputInterceptor() {
> >> >
> >> > super(Phase.WRITE);
> >> >
> >> > }
> >> >
> >> > public void handleMessage(SoapMessage message) throws Fault {
> >> >
> >> > SoapMessage soapMessage = (SoapMessage) message;
> >> >
> >> > List<Header> list = message.getHeaders();
> >> >
> >> > QName q = new QName("http://common.ws.model.plx.ids.it/",
> >> > "User"); User user = new User();
> >> > user.setName("a.dionisi");
> >> > JAXBDataBinding dataBinding = null;
> >> > try {
> >> >
> >> > dataBinding = new JAXBDataBinding(user.getClass());
> >> >
> >> > } catch (JAXBException e1) {
> >> >
> >> > e1.printStackTrace();
> >> >
> >> > }
> >> >
> >> > SoapHeader header = new SoapHeader(q,user, dataBinding);
> >> > list.add(header);
> >> >
> >> > }
> >> >
> >> > }
> >> >
> >> > I checked the output SOAP message and it's correct.
> >> >
> >> > Now, the problem is that I don't know how to unmarshal the User
> >> > object. I tried with this input interceptor:
> >> >
> >> > public class InputInterceptor extends AbstractSoapInterceptor {
> >> >
> >> > public FaultInterceptor() {
> >> >
> >> > super(Phase.UNMARSHAL);
> >> >
> >> > }
> >> >
> >> > public void handleMessage(SoapMessage message) throws Fault {
> >> >
> >> > Header h = message.getHeaders().get(1);
> >> >
> >> > try {
> >> >
> >> > JAXBElement job =
> >>
> >> (JAXBElement)JAXBContext.newInstance(User.class).createUnmarshaller().un
> >> m
> >>
> >> > ar shal((Node) h.getObject());
> >> >
> >> > User u = (User)job.getValue();
> >> >
> >> > } catch (JAXBException ex) {
> >> >
> >> > //
> >> > ex.printStackTrace();
> >> >
> >> > }
> >> >
> >> > }
> >> >
> >> > }
> >> >
> >> > but I receive the following exception:
> >> >
> >> > javax.xml.bind.UnmarshalException: unexpected element
> >> > (uri:"http://common.ws.model.plx.ids.it/", local:"User"). Expected
> >> > elements are (none)
> >> >
> >> > Any suggestion on how to read an object in the SoapHeader?
> >> > Thanks
> >> >
> >> > dkulp wrote:
> >> > > On Friday 11 June 2010 7:08:18 am Cecchi Sandrone wrote:
> >> > >> Hey Glen, your links are very interesting, especially the last. I'm
> >> > >> following that way now...
> >> > >>
> >> > >> One thing I don't understand; how can I specify that an entire
> >>
> >> service
> >>
> >> > >> takes also some parameters in the header? I would have the
> >>
> >> possibility
> >>
> >> > >> to not specify those parameters in every call. For example (PSEUDO
> >> > >> CODE):
> >> > >>
> >> > >> Service s = new Service();
> >> > >> s.setLocale("en-US");
> >> > >>
> >> > >> s.callMethod1(param1)
> >> > >> s.callMethod2(param2)
> >> > >> s.callMethod3(param3)
> >> > >>
> >> > >> In the way you described, as I understand, every method in the
> >>
> >> remote
> >>
> >> > >> interface must add further parameters. For me, it would desiderable
> >>
> >> to
> >>
> >> > >> not
> >> > >> change the existing interface because I have a great amount of
> >> > >> services.
> >> > >
> >> > > If you look at the FAQ:
> >> > > http://cxf.apache.org/faq.html#FAQ-
> >> > > HowcanIaddsoapheaderstotherequest%252Fresponse%253F
> >> > >
> >> > > there are some options. The Headers set on the request context
> >> > > would be
> >> > > sent for all the calls.
> >> > >
> >> > > Dan
> >> > >
> >> > >> Cecchi Sandrone wrote:
> >> > >> > Thank you I will try one of these!
> >> > >> >
> >> > >> > Glen Mazza wrote:
> >> > >> >> Sure, with JAX-WS Handlers--take a look at the SOAPHandler.java
> >>
> >> in
> >>
> >> > >> Step
> >> > >>
> >> > >> >> #5 here:
> >> http://www.jroller.com/gmazza/entry/jaxws_handler_tutorial
> >>
> >> > >> >> . CXF interceptors are another option, check
> >>
> >> > >> >> ClientInterceptors.java here:
> >> http://www.jroller.com/gmazza/entry/jaxwshandlers_to_cxfinterceptor
> >>
> >> > >> >> s.
> >> > >> >>
> >> > >> >> In either case, you'll most probably want to add these elements
> >>
> >> to
> >>
> >> > >> >> the soap header, not the soap body.
> >> > >> >>
> >> > >> >> Finally, if you're allowed to do WSDL modification, you can add
> >>
> >> the
> >>
> >> > >> >> metadata as implicit SOAP headers which will give you an actual
> >>
> >> > >> >> parameter in your SOAP calls to add this data:
> >> http://www.jroller.com/gmazza/entry/using_implicit_soap_headers_wit
> >>
> >> > >> >> h.
> >> > >> >>
> >> > >> >> HTH,
> >> > >> >> Glen
> >> > >> >>
> >> > >> >> Cecchi Sandrone wrote:
> >> > >> >>> I was wondering if it's possible to add some metadata to a
> >> > >> >>> WebService call. In my case this can be useful to send the
> >>
> >> client
> >>
> >> > >> >>> locale; this
> >> > >>
> >> > >> can
> >> > >>
> >> > >> >>> be very useful to translate error messages or other strings in
> >>
> >> the
> >>
> >> > >> >>> server side. Is it possible to do?
--
Daniel Kulp
[email protected]
http://dankulp.com/blog