6

私は JAX-RS を学んでおり、応答で他の関連するアクションに URL を返すというアイデアが気に入っています。Apache TomEE JAX-RS 1.5.1 を使用すると、何らかの理由で、注入されたUriInfoインスタンスによって提供される URL が常にホスト名として「localhost」を使用します。

を追加し@Context HttpServletRequest、 と のgetLocalNamegetServerNameは両方とも公開ホスト名と一致しました。したがって、この情報は、TomEE にバンドルされている CXF-RS ランタイムで利用できるはずです。なぜ使われていないのかは不明です。

以下は、テストクラスとサンプル出力です。TomEE の組み込み CXF-RS に正しいホスト名を使用させるにはどうすればよいですか? または、それが適切なアプローチでない場合、JAX-RS 応答で返すことができる URL をどのように作成すればよいでしょうか?

@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();
    }

}

出力は次のとおりです。

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
4

0 に答える 0