4 Replies Latest reply on Jan 17, 2008 3:19 AM by Mattias Öhrn

    Adding application specific properties to the MessageContext

    Tracey Somo Newbie

      I'm trying to add application specific properties to the MessageContext to retrieve either in my service via WebServiceContext or in a SoapMessageHandler via messageContext.

      No matter how I try to retrieve them, the properties are always null:

      Here's my test code:

      Service service = getWebService( ); //works fine
       HaloImpl endPoint = service.getPort( HaloImpl.class );
      
       //setup custom properties in RequestContext
       BindingProvider bp = (BindingProvider) endPoint;
       bp.getRequestContext( );
       Map<String,Object> context = bp.getRequestContext( );
       context.put( "prop1", "value1");
       context.put( "prop2", "value2");
      
       return (HaloImpl) bp;


      Then I call the endpoint service, no problem.

      In the handler:


      public class HaloHandler implements SOAPHandler<SOAPMessageContext>
      {
      
       //@Override
       public boolean handleMessage( SOAPMessageContext msgContext )
       {
      
       try
       {
       String prop1 = (String) msgContext.get( "prop1" );
       String prop2 = (String) msgContext.get( "prop2" );
      
       } catch( Exception e )
       {
       throw new RuntimeException( e );
       }
      
       return true;
       }


      The handler is configured on the server side via the @HandlerChain annotation, and it is being executed. However, prop1 and prop2 are always null, when they should have "value1" and "value2" respectively.

      I've tried adding the WebServiceContext in the web service impl class like so:

      @Resource
       WebServiceContext ctx;
      
       public HaloImpl( )
       {
      
       }


      and then accessing the properties in a method when a service method is called:

      private void validateProperties( )
       {
       MessageContext msg = ctx.getMessageContext( );
       if (msg != null){
      
       String prop1 = (String) msg.get( "prop1" );
       String prop2 = (String) msg.get( "prop2" );
       }
       }



      but again, the properties are null.

      Am I missing a setting? I've seen several examples in different implementations showing this is the way to access custom properties, but I can't seem to get this to work.

      I am using Jboss 4.2.2 with the default jax-ws stack it came with in there ( v2.0.1.SP2 ).

      Anybody have any ideas? Thanks guys :)



        • 1. Re: Adding application specific properties to the MessageCon
          Tracey Somo Newbie

          I updated to v2.0.2.GA, but still the same issue - the custom properties are not retained when I set them in the client and try to get them in the handler or the service.

          • 2. Re: Adding application specific properties to the MessageCon
            Mattias Öhrn Newbie

            Hi,
            I'm not sure if I understand you correctly, but you cannot transfer properties from client to server this way. The properties you add to the client proxy request context can be read by client side handlers but they are not transferred to the server.

            Regards,
            Mattias

            • 3. Re: Adding application specific properties to the MessageCon
              Tracey Somo Newbie

              I am trying to populate the properties in the client and read them in the server, either in the handler or in the service. I don't know what you mean by "transfer properties".

              The docs say you can get the RequestContext from the BindingProvider, add properties all on the client, and then that is accessible via the MessageContext on the server.

              But so far, no success :(

              • 4. Re: Adding application specific properties to the MessageCon
                Mattias Öhrn Newbie

                 

                The docs say you can get the RequestContext from the BindingProvider, add properties all on the client, and then that is accessible via the MessageContext on the server.

                In what document have you read that? The properties you set on a BindingProvider request context is used to initialize the client side message context which is available to all client side handlers and the client side JAX-WS runtime.

                If you want the properties set on BindingProvider request context to be transferred to the server you can implement a custom handler on the client side that puts it in the SOAP header and a server side handler that retrieves the properties from the header and puts it in the message context on the server side. The following is quoted from the JAX-WS 2.1 spec:
                Handlers may manipulate the values and scope of properties within the message context as desired. E.g.,
                a handler in a client-side SOAP binding might introduce a header into a SOAP request message to carry
                metadata from a property that originated in a BindingProvider request context; a handler in a server-side
                SOAP binding might add application scoped properties to the message context from the contents of a header
                in a request SOAP message that is then made available in the context available (via injection) to a service
                endpoint implementation.