You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@axis.apache.org by Jen-Ya Ku <jk...@tibco.com> on 2005/09/28 02:18:24 UTC

[Axis2] - how do a get/set headers in my webservice code?

Hi, All:
 
In Axis, we can use following to get headers:  However, for Axis2
service, how to get/set header?
 
Thanks for your info. 
 
Thanks,
Jen-Ya 
------------------------------------------------------------------------
---------------------------------------------------------
 // Axis1x Get current message context
   MessageContext ctx =
org.apache.axis.MessageContext.getCurrentContext();

   // Get SOAP envelope of request
   SOAPEnvelope env = ctx.getRequestMessage().getSOAPEnvelope();

   // Get Header
   SOAPHeaderElement soapHeaderElement =
env.getHeaderByName(headerNamespace, headerName);
------------------------------------------------------------------------
-----------------------------

Re: [Axis2] - how do a get/set headers in my webservice code?

Posted by Ruchith Fernando <ru...@gmail.com>.
Hi Jen-Ya,

I see that you have two options to solve your problem:

First if you are using RawXML*MesageReceivers, then you can use the
message context injection mechanism that is available.

This requires you to declare a method 'init' that accepts one or two
message contexts in your service implementation class, depending on
the MEP (IN only or IN-OUT respectively)

public void init(MessageContext inMsgCtx) {
}

OR

public void init(MessageContext inMsgCtx, MessageContext outMsgCtx) {
}

Using this method you can get hold of SOAPEnvelope using
msgCtx.getEnvelope()  and other information at the service. Also since
you are coming from the Axis1.x world please note that Axis2 has
different message contexts for each of the messages.

Please have a look at org.apache.axis2.swa.EchoSwA service class in
the integration test cases.
(http://svn.apache.org/viewcvs.cgi/webservices/axis2/trunk/java/modules/integration/test/org/apache/axis2/swa/EchoSwA.java?view=markup)

Second option is to write your own message receiver where you will
have access to the request message context.

Thanks,
Ruchith

On 9/28/05, Jen-Ya Ku <jk...@tibco.com> wrote:
>
> Hi, All:
>
> In Axis, we can use following to get headers:  However, for Axis2 service,
> how to get/set header?
>
> Thanks for your info.
>
> Thanks,
> Jen-Ya
> ---------------------------------------------------------------------------------------------------------------------------------
>  // Axis1x Get current message context
>    MessageContext ctx =
> org.apache.axis.MessageContext.getCurrentContext();
>
>    // Get SOAP envelope of request
>    SOAPEnvelope env = ctx.getRequestMessage().getSOAPEnvelope();
>
>    // Get Header
>    SOAPHeaderElement soapHeaderElement =
> env.getHeaderByName(headerNamespace, headerName);
> -----------------------------------------------------------------------------------------------------


--
Ruchith

Re: [Axis2] - how do a get/set headers in my webservice code?

Posted by Eran Chinthaka <ch...@opensource.lk>.
Hi Jen Ya,

Let me know from where you want to get hold of the message context.

1. If you want to do this in the stub level :  I'm sorry you can't do 
it. Actually I don't think its a good idea to get hold of the the msgctx 
in the stub level. But you can use one of the MEP clients to access the 
message contexts.
2. If you want to do this in the handler level : no problem, you will 
get the msgctx as a param in the invoke method of the handle. I hope 
this is not the place you have the problem.
3. If its in the service impl class : I hope Ruchith's answer is great 
in that. But I prefer to write my own message receiver and inject both 
the message contexts in the invokeBusinessLogic method. But see you have 
different ways of doing that.

And in Axis2, we have a clearly defined hierarchy of contexts. See the 
archi guide for more information. So msgctx is not the only context.

HTH. If you still have the problem, feel free to post it here.

-- Chinthaka

Gregor Urbanek wrote:

> Hi Jen-Ya,
>
> looking at the code generated by the wsdl2java tool  reveals, where 
> the soap envelope gets stored
>
>  org.apache.axis2.context.MessageContext _messageContext = 
> getMessageContext();
>  _messageContext.setEnvelope(env);
>
> The _messageContext object, which seems to be connected to the 
> _configurationContext object ( everything in context of the stub 
> object ) has the method
> getEnvelope to get hold of the soap envelope.
>
>  org.apache.axis2.soap.SOAPEnvelope env = _messageContext.getEnvelope();
>
> Once you have the soap envelope, you hold in your hands all the options
> - like get all headers
>    env.getHeader().extractAllHeaderBlocks
> - insert a new header
>    env.getHeader()..addChild( OMNode );
>
> Remember, the soap specific objects like SOAPEnvelope  or SOAPheader 
> are derived from the generic OM objects, with all their flexibility.
>
>
>
>
> Jen-Ya Ku wrote:
>
>> Hi, All:
>>  
>> In Axis, we can use following to get headers:  However, for Axis2 
>> service, how to get/set header?
>>  
>> Thanks for your info.
>>  
>> Thanks,
>> Jen-Ya
>> --------------------------------------------------------------------------------------------------------------------------------- 
>>
>>  // Axis1x Get current message context
>>    MessageContext ctx = 
>> org.apache.axis.MessageContext.getCurrentContext();
>>
>>    // Get SOAP envelope of request
>>    SOAPEnvelope env = ctx.getRequestMessage().getSOAPEnvelope();
>>
>>    // Get Header
>>    SOAPHeaderElement soapHeaderElement = 
>> env.getHeaderByName(headerNamespace, headerName);
>> ----------------------------------------------------------------------------------------------------- 
>>
>
>
>
>


Re: [Axis2] - how do a get/set headers in my webservice code?

Posted by Gregor Urbanek <gr...@menticorp.ch>.
Hi Jen-Ya,

looking at the code generated by the wsdl2java tool  reveals, where the 
soap envelope gets stored

  org.apache.axis2.context.MessageContext _messageContext = 
getMessageContext();
  _messageContext.setEnvelope(env);

The _messageContext object, which seems to be connected to the 
_configurationContext object ( everything in context of the stub object 
) has the method
getEnvelope to get hold of the soap envelope.

  org.apache.axis2.soap.SOAPEnvelope env = _messageContext.getEnvelope();

Once you have the soap envelope, you hold in your hands all the options
- like get all headers
    env.getHeader().extractAllHeaderBlocks
- insert a new header
    env.getHeader()..addChild( OMNode );

Remember, the soap specific objects like SOAPEnvelope  or SOAPheader are 
derived from the generic OM objects, with all their flexibility.

 


Jen-Ya Ku wrote:

> Hi, All:
>  
> In Axis, we can use following to get headers:  However, for Axis2 
> service, how to get/set header?
>  
> Thanks for your info.
>  
> Thanks,
> Jen-Ya
> --------------------------------------------------------------------------------------------------------------------------------- 
>
>  // Axis1x Get current message context
>    MessageContext ctx = 
> org.apache.axis.MessageContext.getCurrentContext();
>
>    // Get SOAP envelope of request
>    SOAPEnvelope env = ctx.getRequestMessage().getSOAPEnvelope();
>
>    // Get Header
>    SOAPHeaderElement soapHeaderElement = 
> env.getHeaderByName(headerNamespace, headerName);
> ----------------------------------------------------------------------------------------------------- 
>