0

Hey guys I have a little issue here, in my application I manage to get users ip thanks to this implementation how to mark user's current location on Primefaces Gmap? its works fine heres how I implement it:

FacesContext context = FacesContext.getCurrentInstance();
ExternalContext externalContext =  context.getExternalContext();
HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
String ip = request.getRemoteAddr();
System.out.println("ip: " + ip);

The issue is that now I need to do the same but in a rest service, but in a rest service I can't get the FacesContext so I can't get the ExternalContext and so on... do you know hows the way to achieve the same via rest service? I tried this: Accessing FacesContext from Web Service but no luck, this is what I have at the moment, im testing the service with POSTMAN:

@POST
@Path("/test")
public void test() {
        ServletContext servletContext = (ServletContext) context;
        HttpServletRequest request = (HttpServletRequest) servletContext;
        String ip = request.getRemoteAddr();
        System.out.println("ip: " + ip);
    }

This is how I declare context variable:

@Resource
WebServiceContext context;

This second code doesn't pass the HttpServletRequest request = (HttpServletRequest) servletContext; it gives me the error

Caused by: java.lang.ClassCastException: org.jboss.ws.common.injection.ThreadLocalAwareWebServiceContext cannot be cast to javax.servlet.ServletContext
BugsForBreakfast
  • 712
  • 10
  • 30
  • What is the value of`ip` in your second code snippet? Is it what you expect? –  Jul 03 '19 at 15:32
  • @Lutz Horn it doesn't pass the http request cast line mate, gives me this error org.jboss.ws.common.injection.ThreadLocalAwareWebServiceContext cannot be cast to javax.servlet.ServletContext – BugsForBreakfast Jul 03 '19 at 15:39
  • Please [edit] your question include this error. Please also include how `context` is declared in your class. –  Jul 03 '19 at 15:40
  • 3
    You can have the controller method as `public void test(HttpServletRequest request)` and use the `request` within the method. – Madhu Bhat Jul 03 '19 at 15:40
  • 1
    A `WebServiceContext` is not a `HttpServletRequest`. Do as @MadhuBhat suggested. –  Jul 03 '19 at 15:43
  • @MadhuBhat Hmm I could try that, but how do I send the request via POSTMAN to try it? I know how to send jsons, or params in the url but sending request never tried – BugsForBreakfast Jul 03 '19 at 15:43
  • 1
    @BugsForBreakfast you don't "send" the request. JAX-RS sees that your method needs access to the request because it's part of the method parameters, and so it passes it to the method. Note that you won't necessarily get the IP of the user this way. If you want to display the current location on a map, use the JS geolocation service. https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition – JB Nizet Jul 03 '19 at 15:49
  • @BugsForBreakfast you don't have to send anything separately for the `request` on POSTMAN. – Madhu Bhat Jul 03 '19 at 15:50
  • Okay thank you guys, it worked with the answer posted on question you marked me as duplicate @JBNizet , but I also want it to work with receiving that Request object through the method, I need to cast that object to HttpServletRequest right? – BugsForBreakfast Jul 03 '19 at 16:02
  • Read The **two** sentences of the accepted answer. – JB Nizet Jul 03 '19 at 16:03
  • @JBNizet ahhh I just inject HttpServletRequest instead hehe, much simplier than the second answer there but also helpfull, thanks mate. – BugsForBreakfast Jul 03 '19 at 16:07

0 Answers0