1

I need to get the full url with the host name or IP address from a restful request made to my rest service. Is there any way this can be done?

Similar to InetAddress ip = ip.getHostName(); in java.

Ashir Khan
  • 13
  • 1
  • 3
  • [Refer this link](http://stackoverflow.com/questions/1043346/access-request-object-from-rest) on how to access request object in rest. – Toni Jan 28 '16 at 07:36

5 Answers5

1

When you say InetAddress ip = ip.getHostName(); you will get the hostname of the machine where your rest service is running, to get the the name of the machine from where request is originated you will need to use something similar to request.getRemoteHost(); this value could be depend on the network environment of the machine from where request was originated.

to get full url please refer below post. What's the best way to get the current URL in Spring MVC?

Community
  • 1
  • 1
user3247727
  • 174
  • 1
  • 7
0

With Jersey you can do the following

String clientHost = req.getRemoteHost(); // req is HttpServletRequest
String clientAddr = req.getRemoteAddr();
Dragan
  • 455
  • 4
  • 12
  • 2
    Thanks that worked also found the below code which works: @GET @Path("/test") public Response test(@Context HttpContext context) { String url = context.getRequest().getAbsolutePath().getPath(); String query = context.getRequest().getRequestUri().toASCIIString(); String reqString = url + " + " + query; return Response.status(Status.OK).entity(reqString).build(); } – Ashir Khan Mar 10 '15 at 07:18
  • This works but its not generic the result would differ if used on local machine for example http://localhost:8080/test wont return "localhost" instead it will return something like this "0:0:0:0:0:0:0:1" which is the IPv6 representation of local loopback address ::1 or ::1/128 – Tanuj Oct 26 '20 at 11:05
0

If you use UriInfo, it will get you the server-side URI, not the client-side one.

If you want the client-side URI, you will have to use HttpServletRequest:

@POST
@Path("/test")
public Response uriTest(@Context HttpServletRequest request) { 
    try {
        URI baseUri = new URL(request.getRequestURL().toString()).toURI();
        System.out.println(baseUri.getHost());
        System.out.println(baseUri.getPort());
        return Response.ok(null).build();
    } catch (MalformedURLException | URISyntaxException e) {
        throw new IllegalStateException("Error building the base URI.", e);
    }
}
Haroldo_OK
  • 6,612
  • 3
  • 43
  • 80
-1

Using CDI and @Context, similar to the comment by Ashir Khan above, the requestContext or parts of the request context can be made available at run time. Many types can be bound by the @Context annotation and injected by the container, although it would be better to:

  1. Reduce the visibility of the injected data as much as possible ie bind to the method rather than the class and
  2. Reduce the amount of information injected as possible ie bind the UriInfo if all you want is the baseUri rather than the full request context.

Sounds like a lot, but is as simple as (a modification on) Ashir's code:

@GET
@Path("/test") 
// Look Ma, no hands!
public Response test(@Context javax.ws.rs.core.UriInfo uriInfo) { 
  return Response.status(Status.OK).entity(uriInfo.getBaseUri()).build();
}

See also this response

David O'Meara
  • 2,983
  • 25
  • 38
-1

You can use the UriInfo class and bind it using the @Context annotation, then at every request you can access the URI information like Hostname, Port, Request Path etc.

Here is an example -

@POST
@Path("/test")
public Response uriTest(@Context UriInfo uriInfo){ //helps you get all URI related information.
    System.out.println(uriInfo.getBaseUri().getHost());
    System.out.println(uriInfo.getBaseUri().getPort());
    return Response.ok(null).build();
}

You can read more about URIInfo here

Tanuj
  • 2,032
  • 10
  • 18
  • This should be the correct answer since it's based on the jax-rs standard @Ashir Khan – tisaksen Jan 18 '21 at 13:50
  • 1
    This is absolutly not the correct answer, since it returns server information not the client information that we want in this question. Which is why the other answer is accepted. – JPS Dec 01 '21 at 14:16