I am starting a WebService inside a JavaSE application and I want to get the client's caller IP. Is that possible?
I have written a small test case:
import java.net.InetSocketAddress;
import java.util.concurrent.*;
import javax.jws.*;
import javax.xml.ws.Endpoint;
import com.sun.net.httpserver.*;
@WebService
public class TestWs {
@WebMethod public String testMethod(String param) {
String clientHostIp = ""; // how to obtain client's host ?
return "hello "+ clientHostIp;
}
public static void main(String[] args) throws Exception {
ExecutorService executorService = Executors.newFixedThreadPool(2);
HttpServer thttpserver = HttpServer.create(new InetSocketAddress("0.0.0.0", 2000),8);
final HttpServer httpserver = thttpserver;
httpserver.setExecutor(executorService);
httpserver.start();
HttpContext ctx = httpserver.createContext("/TestWs");
final Endpoint wsendpoint = Endpoint.create(new TestWs());
wsendpoint.publish(ctx);
}
}