Thursday, January 9, 2014

Sample on Accessing the Web Service Context

JAX-WS Sample
==============
package temp;

import javax.jws.*;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
import javax.servlet.http.HttpServletRequest;
import javax.annotation.Resource;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;

public class MyWS {

@Resource(name="wsContext")
private WebServiceContext wsContext;

public String hello() {
String reqInfo = getRequestInfo();
return reqInfo;
}

private String getRequestInfo() {
return ((HttpServletRequest)(wsContext.getMessageContext().get(MessageContext.SERVLET_REQUEST))).getRemoteAddr();
}
}
JAX-RPC Sample
=================
package test;

import javax.jws.*;
import javax.xml.rpc.handler.MessageContext;
import javax.xml.rpc.server.ServletEndpointContext;
import javax.xml.rpc.ServiceException;
import weblogic.wsee.message.WlMessageContext;
import weblogic.wsee.connection.Connection;
import weblogic.wsee.connection.transport.Transport;
import weblogic.wsee.connection.transport.servlet.HttpServerTransport;
import javax.xml.rpc.server.ServiceLifecycle;

import weblogic.webservice.context.*;
public class ServiceImpl implements ServiceLifecycle{

private WebServiceContext wsContext;
private ServletEndpointContext wsctx = null;

public void init(Object context) throws ServiceException {
System.out.println("ServletEndpointContext inited...");
wsctx = (ServletEndpointContext)context;
}
public String getRemoteAddress() {
WlMessageContext msgCtx = (WlMessageContext)wsctx.getMessageContext();
Connection conn = msgCtx.getDispatcher().getConnection();
Transport transport = conn.getTransport();

if (transport instanceof HttpServerTransport) {
return ((HttpServerTransport)transport).getRequest().getRemoteAddr();
}
else {
return null;
}
}

public void destroy() {}
public String helloWorld(){
String reqInfo = getRemoteAddress();
return "Hi";
}
}

No comments:

Post a Comment