15

I know this might seem a stupid question but I am just not able to find any information regarding this question. I have a java web service (generated using NetBeans) and inside the web service class I would like to know the URL at which the web service was deployed. For example, if I am deploying the web service on my local glassFish server, the web service is available at "http://localhost:8080/MyService/" where "MyService" is the name of my service. The reason I need to know this URL is because my web service generates some files that I need to make available at this URL. For example, the web service call returns a URL "http://localhost:8080/MyService/report.html" I have found some links about "WebServiceContext" but I am not able to get the URL at which my web service is running.

Edited

To clarify: Inside MyWebService.java class I want to find out the URL at which my web service was deployed (in this case, my web service is running at "http://localhost:8080/MyService/", but once it is deployed on a production server, this URL will change)

Johnny
  • 2,503
  • 6
  • 21
  • 22

5 Answers5

8

Easier in my opinion, for example:

@GET
public URI redirectSendMail(@Context UriInfo ui) {
  return ui.getBaseUri();
}

If we want to send a String back to the client, indicating the exact path of some resource, we might want something like this.

@GET
public String getResourcePath(@Context UriInfo ui) {
  return ui.getAbsolutePath();
}
dda
  • 6,030
  • 2
  • 25
  • 34
Rozgeboy
  • 91
  • 1
  • 3
5

If you are asking how to find the hostname (e.g. 'localhost' or 'www.example.com') that your servlet container is listening to you have a few options:

  • Add a configuration point that is set at deployment time (e.g. config file, system property)
  • Look into if your servlet container exposes any 'virtual host' configuration via JMX or read its config files (e.g. tomcat hosts)
  • Find the IP Address of the server and do a DNS lookup to get its configured hostname
  • Inspect the 'Host' header of the incoming HttpServletRequest

    String hostname = request.getRequestHeader("Host");

Uriah Carpenter
  • 6,656
  • 32
  • 28
  • I currently have it set up to read the URL from a configuration file. I am trying to remove this configuration setting since I thought it should be easy enough to find out the host URL. – Johnny Feb 04 '11 at 17:23
  • If you truly want zero server-side configuration you will need to use the 'Host' header that's in the HTTP request. – Uriah Carpenter Feb 05 '11 at 16:53
  • Uriah, I was not able to get that URL from the "Host" header. If you can show code please let me know. thanks. – Johnny Mar 14 '11 at 23:58
  • If wiki is right... getRequestHeader("Host") may also contain Port number https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Request_fields – JGFMK Sep 27 '17 at 18:18
3

Add the below property in your webservice class.

@Resource    
private WebServiceContext wsCtxt;

Now below code snippet will give you the values you are looking for:

    MessageContext msgCtxt = wsCtxt.getMessageContext();
    HttpServletRequest request =
        (HttpServletRequest)msgCtxt.get(MessageContext.SERVLET_REQUEST);

    String hostName = request .getServerName();
    int port = request .getServerPort();

to check from where the webservice was invoked, use the below code.

    String clientIP = request .getRemoteAddr();

Related imports are below:

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;

import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;

import weblogic.jws.Context;

import weblogic.wsee.jws.JwsContext;
glts
  • 21,808
  • 12
  • 73
  • 94
  • It seems that @Resource will return null under certain circumstances... See here: https://stackoverflow.com/questions/15346630/jax-ws-webservicecontext-remains-null-annotation-injection-fails – JGFMK Sep 27 '17 at 18:00
2

Hopefully I am able to help you, I have just recently started working with webservices (Jersey REST) and I have found that the url to your endpoint is : 'http://localhost:8080/MyService/XXX/YYY'
where XXX = the URL pattern in the servlet mapping in your web.xml file (eg. file below)

<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>   

and the YYY is the path defined by your webservice's @Path() parameter so in the case of this example it would be something like:

'http://localhost:8080/MyService/rest/myPathDefinition'

Another thing to note is that you can in fact change the web context root in eclipse, though it will default to the name of your Java project. Please let me know if you need further clarification or if this did not help / someone can expand on it.

Mike
  • 8,137
  • 6
  • 28
  • 46
  • 2
    I don't think I made myself clear enough, so I will try to reformulate the question. I know the URL of my web service because I am deploying it. The question is, how do I programmaticly get that URL. So inside MyWebservice.java class, I want to know the URL at which my web service has been deployed (in this case it's "http://localhost:8080/MyService", but this will change once the web service is deployed on a production server). – Johnny Feb 03 '11 at 23:44
0

It could be found on your wsdl file as:

therefore: http://localhost:8080/TestSOAPWebservice/services/TestClassImpl?wsdl would be the url to your wsdl file.