users@jax-ws.java.net

RE: Re: FW: Getting Server Port in a jax-ws web service ?

From: Baker, Brad \(Contr\) <"Baker,>
Date: Wed, 14 Feb 2007 16:23:03 -0500

I have a very similar question, how do I get the IP address of the
Caller?

I am trying:
        MessageContext oMContext = oWSContext.getMessageContext();
        Set<String> oSet = oMContext.keySet();
        for(Iterator<String> it = oSet.iterator(); it.hasNext();) {
            String strKey = it.next();
            if(strKey.indexOf("request")!= -1) {
                Object oRequest = oMContext.get(strKey);
                Class oClass = oRequest.getClass();
                Method[] aMethods = oClass.getMethods();
                int iSZ = aMethods.length;
                for (int i = 0; i < iSZ; ++i) {
                    String strMethName = aMethods[i].getName();
                    
                    if (strMethName.equals("getRemoteAddr")) {
                        try {
                            Object rtv = aMethods[i].invoke(oRequest,
((Object[])null));
                            if (null != rtv) {
                                strCallerIP = rtv.toString();
                            }
                        } catch (Exception e) {
 
System.err.println(ExceptionUtil.getStackTraceString(e));
                        }
                    }
                }
            }
        }

But the value I get back from getRemoteAddr() is not the IP of my box.
This is all local at this stage. No firewall nothing just all local.

-Brad

-----Original Message-----
From: Jitendra.Kotamraju_at_Sun.COM [mailto:Jitendra.Kotamraju_at_Sun.COM]
Sent: Wednesday, February 14, 2007 2:12 PM
To: users_at_jax-ws.dev.java.net
Subject: Re: FW: Getting Server Port in a jax-ws web service ?

Glenn wrote:
> I'm trying to retrieve the server address (host & port), and possibly
> the relative path (relative to server root) from within a jax-ws web
> service.
>
> I have a web service, which includes several images for retrieval
> based on metadata provided by the caller, below is a very simplified
> version of the code. I know I can use InetAddress to get the host
> name, but is there a way to retrieve the port number of the server
> that the web service is running in ?
>
> I'd prefer getting the information at run-time rather than playing
> with properties or application config files, that would have to be
> modified each time the war file is deployed.
>
> Any suggestions would be greatly appreciated.
>
> @WebService()
> public class ImageRetriever {
@Resource
WebServiceContext wsCtxt;
> @WebMethodpublic URL[] getImagery(@WebParam(name="metadata")
> Metadata metadata) {
> URL[] urls = new URL[1];
> String host = null;
> int port = 80;
>
> // TO-DO set host address to web server host
> // TO-DO set port to webserver port
>
> url[0] = new URL("http", host, port, "Imagery/image1.png");
MessageCtxt msgCtxt = wsCtxt.getMessageContext();
HttpServletRequest req =
(HttpServletRequest)msgCtxt.get(MessageContext.SERVLET_REQUEST)
// using req you can get all the information req.getScheme(),
req.getServerName(), req.getServerPort() etc

Jitu

> return urls;
> }
> }
>
> Thanks,
> Glenn
>
------------------------------------------------------------------------
> *Join Excite! - http://www.excite.com*
> The most personalized portal on the Web!

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jax-ws.dev.java.net
For additional commands, e-mail: users-help_at_jax-ws.dev.java.net