6

I'm learning JAX-RS, and like the idea of returning URLs to other relevant actions in a response. Using Apache TomEE JAX-RS 1.5.1, for some reason the URLs provided by an injected UriInfo instance are always using "localhost" as the host name.

I added a @Context HttpServletRequest, and the getLocalName and getServerName values both matched the public host name. So this information should be available to the CXF-RS runtime bundled with TomEE. It's just not clear why it isn't being used.

Below is the test class, and sample output. How can I get TomEE's embedded CXF-RS to use the correct hostname? Or, if that isn't the right approach, how should I be building URLs that I can return in JAX-RS responses?

@Path("")
public class Test {

    @GET
    @Produces({MediaType.TEXT_PLAIN})
    public String defaultPage(@Context UriInfo uriInfo,
            @Context HttpHeaders hh,
            @Context HttpServletRequest httpServletRequest) {

        StringBuilder response = new StringBuilder();

        response.append("uriInfo.getAbsolutePath(): ");
        response.append(uriInfo.getAbsolutePath());
        response.append("\n");

        response.append("uriInfo.getBaseUri(): ");
        response.append(uriInfo.getBaseUri());
        response.append("\n");

        // snip the repetitive part...

        response.append("httpServletRequest.getServerPort(): ");
        response.append(httpServletRequest.getServerPort());
        response.append("\n\n");

        for (String header : hh.getRequestHeaders().keySet()) {
            response.append(header);
            response.append(":\n");

            for (String value : hh.getRequestHeaders().get(header)) {
                response.append("\t");
                response.append(value);
                response.append("\n");
            }
        }

        return response.toString();
    }

}

And here is the output:

uriInfo.getAbsolutePath(): http://localhost:8081/sample-app-1.0-SNAPSHOT/
uriInfo.getBaseUri(): http://localhost:8081/sample-app-1.0-SNAPSHOT
uriInfo.getRequestUri(): http://localhost:8081/sample-app-1.0-SNAPSHOT/
httpServletRequest.getLocalAddr(): 1.2.3.4
httpServletRequest.getLocalName(): www.example.com
httpServletRequest.getLocalPort(): 8081
httpServletRequest.getServerName(): www.example.com
httpServletRequest.getServerPort(): 8081

Accept:
    text/html
    application/xhtml+xml
    application/xml;q=0.9
    */*;q=0.8
accept-encoding:
    gzip
    deflate
accept-language:
    en-us
cache-control:
    max-age=0
connection:
    keep-alive
Content-Type:
host:
    www.example.com:8081
user-agent:
    Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/536.26.17 (KHTML
    like Gecko) Version/6.0.2 Safari/536.26.17
madth3
  • 7,275
  • 12
  • 50
  • 74
GargantuChet
  • 5,691
  • 1
  • 30
  • 41
  • I'm also hitting this one. So far, I've confirmed that it isn't in the Tomcat layer and yet it's below the level of my webapp. Something in CXF is caching it… – Donal Fellows Feb 21 '13 at 09:43
  • I posted to the TomEE users list, and Romain Manni-Bucau fixed it in the 1.5.2 snapshot. Hopefully the final 1.5.2 release will fix the issue. See [the thread](http://openejb.979440.n4.nabble.com/UriInfo-always-returning-quot-localhost-quot-in-URIs-td4660587.html) for details. – GargantuChet Feb 22 '13 at 04:19
  • I'm not convinced that that is exactly what the issue is; maybe it's just delivering the correct hostname first time round (in which case everything works). It looks to me like something isn't getting flushed (but it's *hard* to hunt; this is an exceptionally complex part of CXF, where things look very different from the normal model view). – Donal Fellows Mar 04 '13 at 21:25
  • 1
    This question appears to be a duplicate of http://stackoverflow.com/questions/13243571/uriinfo-injection-not-getting-updated-for-new-requests – Donal Fellows Mar 06 '13 at 14:34
  • It's different, because the behavior I'd seen is true even on the first request. The issue here isn't that the value is set on the first request. The issue is that it's always wrong. – GargantuChet Mar 06 '13 at 16:15

0 Answers0