How to access HTTP headers from an Axis2 service implementation class
I have seen some users in axis user mailing list ask the question on how to access HTTP headers of the request SOAP message using the service implementation class.
It's easy and straightforward with messageContext class.
Lets see with an example.
1. Create a service implementation class as follows
import javax.servlet.http.HttpServletRequest;
import org.apache.axis2.context.MessageContext;
public class TestService {
public String MyOperation(String s){
MessageContext msgCtx = MessageContext.getCurrentMessageContext();
HttpServletRequest obj =(HttpServletRequest)msgCtx.getProperty("transport.http.servletRequest");
System.out.println("Acceptable Encoding type: "+obj.getHeader("Accept-Encoding"));
System.out.println("Acceptable character set: " +obj.getHeader("Accept-Charset"));
System.out.println("Acceptable Media Type: "+obj.getHeader("Accept"));
return s;
}
}
As you can see in the highlighted statements, first we need to get the current messageContext. Then from the messageContext, we can get the HTTPServletRequest object from which we can get whatever HTTP headers we want.
2. Write service descriptor(services.xml) for the above service class and deploy the service in Axis2 (If you are not familiar with Axis2 deployment, please read Axis2 user's guide )
3. Invoke the service in RESTful manner
http://:/services/TestService/MyOperation?s=hi
You will see the following in Axis2 run time console.
Acceptable Encoding type: gzip,deflate
Acceptable character set: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Acceptable Media Type: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
It's easy and straightforward with messageContext class.
Lets see with an example.
1. Create a service implementation class as follows
import javax.servlet.http.HttpServletRequest;
import org.apache.axis2.context.MessageContext;
public class TestService {
public String MyOperation(String s){
MessageContext msgCtx = MessageContext.getCurrentMessageContext();
HttpServletRequest obj =(HttpServletRequest)msgCtx.getProperty("transport.http.servletRequest");
System.out.println("Acceptable Encoding type: "+obj.getHeader("Accept-Encoding"));
System.out.println("Acceptable character set: " +obj.getHeader("Accept-Charset"));
System.out.println("Acceptable Media Type: "+obj.getHeader("Accept"));
return s;
}
}
As you can see in the highlighted statements, first we need to get the current messageContext. Then from the messageContext, we can get the HTTPServletRequest object from which we can get whatever HTTP headers we want.
2. Write service descriptor(services.xml) for the above service class and deploy the service in Axis2 (If you are not familiar with Axis2 deployment, please read Axis2 user's guide )
3. Invoke the service in RESTful manner
http://
You will see the following in Axis2 run time console.
Acceptable Encoding type: gzip,deflate
Acceptable character set: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Acceptable Media Type: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Comments
call center
is there any way to override the hyttpheader "Content-Type" in the request from Axis2 1.4.1 stub. I want to change it from "text/xml" to "application/x-gzip" as my .NET client expects it to be "application/x-gzip". Any help, plz mail to naveenkvm@gmail.com if possible.
http://community.fox2now.com/venusjj
http://cheappolos.blog.drecom.jp
http://d.hatena.ne.jp/crystal666
http://www2.atword.jp/pumaspeed
http://www.seriousblogging.com/basketspuma
Puma sport
polo shirts
polo shirts
gillette razor blades
original polo clothing