Hi,

Jay SenSharma

Jay SenSharma

If we want to display the HttpHeaders (NOT SOAPHeaders) inside our JAXWS webservice then we need to use   “javax.xml.ws.WebServiceContext” class provided as part of JAXWS APIs. Here is a Sample on How to use this API.

Step 1). Create a “src” directory inside Some Folder like: “C:JavaTestJAXWS_ClientHttpHeaders_ClientIP”

Step 2). Develope the WebService class “HelloWorldImpl.java” inside the “C:JavaTestJAXWS_ClientHttpHeaders_ClientIPsrcexampleswebserviceshello_world” Directory.

package examples.webservices.hello_world;
import javax.jws.*;
import javax.xml.ws.WebServiceContext;
import javax.annotation.Resource;
import javax.xml.ws.handler.MessageContext;
import java.util.*;
import javax.servlet.http.*;
import javax.xml.ws.handler.soap.*;

@WebService
public class HelloWorldImpl
{
@Resource
WebServiceContext wsCtxt;
@WebMethod
public String getClientIP()
{
MessageContext msgCtxt = wsCtxt.getMessageContext();
HttpServletRequest req = (HttpServletRequest)msgCtxt.get(MessageContext.SERVLET_REQUEST);
String clientIP = req.getRemoteAddr();
System.out.println("nnttClient's IP Address is : "+clientIP);
return "nt Your IP Address is : "+clientIP;
}
@WebMethod
public String sayHelloWorld(String message)
{
System.out.println("sayHelloWorld:" + message);
return "Here is the message: '" + message + "'";
}

@WebMethod
public String getHttpHeaders()
{
MessageContext context=wsCtxt.getMessageContext();
System.out.println("nnt Got MessageContext context = "+context);
Map map = (Map)context.get(MessageContext.HTTP_REQUEST_HEADERS);
String str="";

Set entries = map.entrySet();
Iterator iterator = entries.iterator();
while (iterator.hasNext())
{
Map.Entry entry = (Map.Entry)iterator.next();
str=str+"nHeader:" + entry.getKey() + " tValue: " + entry.getValue();
System.out.println("ntHeader:" + entry.getKey() + " : " + entry.getValue());
}

System.out.println("nnttClient's HTTP Headers are : "+str);
return "nt Your HTTP Headers are : "+str;
}
}

Step3). Provide the ANT “build.xml” file inside the “C:JavaTestJAXWS_ClientHttpHeaders_ClientIP”. Step4). Open a Command/Shell window and run the “setDomainEnv.sh/cmd” or “setWLSEnv.sh/cmd”.

Step4). Run the ANT…

Step5). Login to AdminConsole and Click on Test Client Page of deployed WebService…Execute the Method “getHttpHeaders” you will find the following SOAPResoponse:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:getHttpHeadersResponse xmlns:ns2="http://hello_world.webservices.examples/">
<return>Your HTTP Headers are :
Header:Cookie Value: [PreviousWsdl1261036687257=VHJhbnNhY3Rpb25hbEltcGxTZXJ2aWNlfGh0dHA6Ly9sb2NhbGhvc3Q6NzAwMS9UcmFuc2FjdGlvbmFsSW1wbC9UcmFuc2FjdGlvbmFsSW1wbD9XU0RM; PreviousWsdl1261038900211=SGVsbG9Xb3JsZEltcGxTZXJ2aWNlfGh0dHA6Ly9sb2NhbGhvc3Q6NzAwMS9IZWxsb1dvcmxkSW1wbC9IZWxsb1dvcmxkSW1wbFNlcnZpY2U/V1NETA==; ADMINCONSOLESESSION=lNnpLqLNtCsxKGJFQhN517hmQ6c81lDYPF2QryGdcVbPzJfBLqct!-1472209373; JSESSIONID=LyG0LqLL849T3f2k4vLy2XlXKb3yWCDvy1P0XpVQtTvgyyTfLv9v!-1472209373]
Header:Host Value: [localhost:7001]
Header:Content-type Value: 
Header:Content-length Value: [183]
Header:Connection Value: [Keep-Alive]
Header:User-agent Value: [BEA WebLogic Server 10.3.0.0]
Header:Accept Value: 
Header:Soapaction Value: [""]</return>
</ns2:getHttpHeadersResponse>
</S:Body>
</S:Envelope>

.

.

Thanks

Jay SenSharma

If you enjoyed this post, please considerleaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.