3 Replies Latest reply on Jan 7, 2015 2:37 AM by ajagnade

    Setting a context variable in RestEasy Implementation

    ajagnade

      Hi All,

       

      I have implemented RestEasy Webservice implementation in switchyard. I have configured ContextMapper class (with mapFrom and mapTo methods) for this rest easy implementation. In response, I have to set http status code. For this, I am trying to set following in impl class.

       

      @Inject

      private Context context;

       

      context.setProperty(Constants.RESPONSE_STATUS, Constants.HTTP_STATUS_ACCEPTED_CODE, Scope.MESSAGE);

       

      where context is switchyard context object .

       

      It is giving me following error :

       

      <errorMessage>SWITCHYARD030422: Illegal call to get the SwitchYard Context; must be called within the execution of an ExchangeHandler chain.</errorMessage>

       

      Please let me how can I set this parameter, so that i can get it MapTo method in ContextMapper Class.

        • 1. Re: Setting a context variable in RestEasy Implementation
          kcbabo

          Context injection is only supported for CDI bean services - you can't inject Context into a context mapper.

          • 2. Re: Setting a context variable in RestEasy Implementation
            ajagnade

            Hi Keith,

             

             

            Thanks for reply, Keith. But I am not trying to do context injection in context mapper. It is already done in Restful Service Implementation from which I am retrieving values that are set in mapFrom method of context mapper. I am just trying to send a parameter from Restful web service implementation class to context mapper through response. So that I can get that parameter in mapTo method and set it as Http Response Status. I was trying to set property in context at restful web service implementation class which resulted in below error.

             

            <errorMessage>SWITCHYARD030422: Illegal call to get the SwitchYard Context; must be called within the execution of an ExchangeHandler chain.</errorMessage>

             

            Somehow I was not willing to send it through return parameter of Restful service implementation class as it is already been used.

             

            Thanks

            Amit

            • 3. Re: Setting a context variable in RestEasy Implementation
              ajagnade

              Hi Keith,

               

              Got Problem solved. I have mention return type of method in web service Implementation class as java.lang.Object. Created javax.ws.rs.core.Response object as per your HTTP response status and set headers and body as per requirement and returning same in return type. Now this sets response with custom header variables and body.

               

              One more thing I would like to mention here is that if I set return type of method as javax.ws.rs.core.Response, it will not show any custom header variable in browser. But when I make it as object, it will show all custom headers I have added.

               

              Thanks for your help.

              Amit.