users@glassfish.java.net

Re: How to get endpoint URL from ServletContext

From: Farrukh Najmi <farrukh_at_wellfleetsoftware.com>
Date: Sat, 13 Mar 2010 21:43:45 -0500

Here is what I have learned so far...

If I want to get the precise protocol and port I have to get it from the
ServletRequest not the ServletContext. reason is, as Jan said this is
request specific an may vary from one request to another.

With spring-ws it is fairly straightforward to get this once a request
is being processed:

             TransportContext tc =
TransportContextHolder.getTransportContext();
             WebServiceConnection wc = tc.getConnection();
             URI uri = wc.getUri();

Viola! the URI has exactly what I need :-)

Thanks very much Jan.

On 03/11/2010 09:33 PM, Jan Luehe wrote:
> Hi Farrukh,
>
> Farrukh Najmi wrote:
>> Hi Jan,
>>
>> Thank you for your kind help.
>>
>> The hostname can be looked up. But How do I get the protocol scheme
>> and port reliably?
>> I am surprised that this seems so hard and that ServletContext does
>> not have the endpoint readily available as a complete URL.
>
> It's impossible for ServletContext to provide this info.
>
> Assume you deploy foo.war to virtual server "bar" at context root "/foo".
>
> Upon deployment, foo.war will be represented by a ServletContext whose
> getContextPath() will return "/foo".
>
> But the "foo" application may be accessed in different ways, depending
> on your domain's configuration. For example, assume virtual server
> "bar" receives requests from a non-secure HTTP listener at port 12345,
> and from a secure HTTP listener at port 44444. Thus, the following
> requests would be routed to the "foo" application:
>
> http://bar:12345/foo
> https://bar:44444/foo
>>
>> Is this not something that is needed very frequently?
>> Is there a better way to do what I am trying to do?
>>
> Perhaps you could configure this info via context attributes?
>
>> Thanks for any further help on this.
>>
>
> You're welcome.
>
> Jan
>
>> On 03/11/2010 07:04 PM, Jan Luehe wrote:
>>>
>>> Try using ServletContext#getContextPath, which returns an
>>> application's prime or preferred context path (in case the
>>> application has been deployed to more than one context root), and
>>> prepend it with the desired scheme, domain (host) name, and port in
>>> order to form a complete URL.
>>>
>>
>> --
>> Regards,
>> Farrukh
>>
>> Web:http://www.wellfleetsoftware.com
>>
>>
>


-- 
Regards,
Farrukh
Web: http://www.wellfleetsoftware.com