title: How to find out incoming RESTful request's IP using JAX-RS on Heroku?
body: <p>I'm writing a Java RESTful service hosted on Heroku based on an example -> <a href="https://api.heroku.com/myapps/template-java-jaxrs/clone">https://api.heroku.com/myapps/template-java-jaxrs/clone</a></p>
  
  <p>My sample service is:</p>
  
  <pre><code>package com.example.services;
  
  import com.example.models.Time;
  
  import javax.ws.rs.GET;
  import javax.ws.rs.Path;
  import javax.ws.rs.Produces;
  import javax.ws.rs.core.MediaType;
  
  @Path("/time")
  @Produces(MediaType.APPLICATION_JSON)
  public class TimeService {
  
      @GET
      public Time get() {
          return new Time();
      }
  
  }
  </code></pre>
  
  <p>My main is:</p>
  
  <pre><code>public class Main {
  
      public static final String BASE_URI = getBaseURI();
  
      /**
       * @param args
       */
      public static void main(String[] args) throws Exception{
          final Map&lt;String, String&gt; initParams = new HashMap&lt;String, String&gt;();
          initParams.put("com.sun.jersey.config.property.packages","services.contracts"); 
  
          System.out.println("Starting grizzly...");
          SelectorThread threadSelector = GrizzlyWebContainerFactory.create(BASE_URI, initParams);
          System.out.println(String.format("Jersey started with WADL available at %sapplication.wadl.",BASE_URI, BASE_URI));
      }
  
      private static String getBaseURI() 
      {
          return "http://localhost:"+(System.getenv("PORT")!=null?System.getenv("PORT"):"9998")+"/";      
      }
  
  }
  </code></pre>
  
  <p>My question is how can I find out in my service the IP address and port combination the request is coming from?  I've read stuff on @Context which injects javax.ws.rs.core.HttpHeaders, javax.ws.rs.core.Request, etc.  However, no incoming IP or port info is present.</p>
  
  <p>I know if you implement com.sun.grizzly.tcp.Adapter, you can do something like:</p>
  
  <pre><code>public static void main(String[] args) {
      SelectorThread st = new SelectorThread();
      st.setPort(8282);
      st.setAdapter(new EmbeddedServer());
      try {
          st.initEndpoint();
          st.startEndpoint();
      } catch (Exception e) {
          System.out.println("Exception in SelectorThread: " + e);
      } finally {
          if (st.isRunning()) {
              st.stopEndpoint();
          }
      }
  }
  
  public void service(Request request, Response response)
          throws Exception {
      String requestURI = request.requestURI().toString();
  
      System.out.println("New incoming request with URI: " + requestURI);
      System.out.println("Request Method is: " + request.method());
  
      if (request.method().toString().equalsIgnoreCase("GET")) {
          response.setStatus(HttpURLConnection.HTTP_OK);
          byte[] bytes = "Here is my response text".getBytes();
  
          ByteChunk chunk = new ByteChunk();
          response.setContentLength(bytes.length);
          response.setContentType("text/plain");
          chunk.append(bytes, 0, bytes.length);
          OutputBuffer buffer = response.getOutputBuffer();
          buffer.doWrite(chunk, response);
          response.finish();
      }
  }
  
  public void afterService(Request request, Response response)
          throws Exception {
      request.recycle();
      response.recycle();
  }
  </code></pre>
  
  <p>and access </p>
  
  <pre><code>    request.remoteAddr()
  </code></pre>
  
  <p>But I'd really like to separate my RESTful API in a more structured way like in my first implementation.</p>
  
  <p>Any help would be greatly appreciated.  Thanks!</p>