是否可以将 Jersey JSP 模板响应重新路由到 InputStream?

发布于 2024-12-06 03:36:59 字数 1239 浏览 1 评论 0原文

总之,

我正在使用 Java/Jersey 1.9 创建一个生成 XML 的 Web 服务。我使用 JSP 模板(显式地通过 Viewable 类)生成 XML。有什么方法可以将 JSP 结果重新路由到本地输入流以进行进一步处理吗?目前,我实际上是从不同的方法调用我自己的 XML Web 服务作为 http 环回 (localhost)。

感谢您的任何见解,

伊恩

@GET @Path("kml")
@Produces("application/vnd.google-earth.kml+xml")
public Viewable getKml(
        @QueryParam("lat") double lat,
        @QueryParam("lon") double lon,
        @QueryParam("alt") double alt) {

    overflights = new SatelliteOverflightModel(
            context, new SatelliteOverflightModel.Params(lat, lon, alt)
            ).getOverflights();

    return new Viewable("kml", this);
}

@GET @Path("kmz")
@Produces("application/vnd.google-earth.kmz")
public InputStream getKmz(@Context UriInfo uriInfo,
        @QueryParam("lat") double lat,
        @QueryParam("lon") double lon,
        @QueryParam("alt") double alt)
        throws IOException {

    Client client = Client.create();
    WebResource webr = 
            client.resource(uriInfo.getBaseUri()+"overflights/kml");
    InputStream result =
            webr.queryParams(uriInfo.getQueryParameters()).get(InputStream.class);

    // Do something with result; e.g., add to ZIP archive and return

    return result;
}

All,

I am using Java/Jersey 1.9 to create a web service that generates XML. I'm generating XML using a JSP template (explicitly via the Viewable class). Is there any way to reroute the JSP results to a local InputStream for further processing? At the moment I'm actually calling my own XML web service as an http loopback (localhost) from a different method.

Thanks for any insights,

Ian

@GET @Path("kml")
@Produces("application/vnd.google-earth.kml+xml")
public Viewable getKml(
        @QueryParam("lat") double lat,
        @QueryParam("lon") double lon,
        @QueryParam("alt") double alt) {

    overflights = new SatelliteOverflightModel(
            context, new SatelliteOverflightModel.Params(lat, lon, alt)
            ).getOverflights();

    return new Viewable("kml", this);
}

@GET @Path("kmz")
@Produces("application/vnd.google-earth.kmz")
public InputStream getKmz(@Context UriInfo uriInfo,
        @QueryParam("lat") double lat,
        @QueryParam("lon") double lon,
        @QueryParam("alt") double alt)
        throws IOException {

    Client client = Client.create();
    WebResource webr = 
            client.resource(uriInfo.getBaseUri()+"overflights/kml");
    InputStream result =
            webr.queryParams(uriInfo.getQueryParameters()).get(InputStream.class);

    // Do something with result; e.g., add to ZIP archive and return

    return result;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多像笑话 2024-12-13 03:36:59

您可以考虑使用 ContainerResponseFilter 而不是资源 - 请参阅例如 Gzip 过滤器 Jersey 提供。不同之处在于您的过滤器将依赖于 Accept 和 Content-Type 标头,而不是 Accept-Encoding 和 Content-Encoding 标头(就像 gzip 过滤器那样)。

如果您坚持使用该资源,您可以在您的资源上注入 Providers 接口,找到正确的 MessageBodyWritter 并调用其写入方法:

@GET @Path("kmz")
@Produces("application/vnd.google-earth.kmz")
public InputStream getKmz(@Context UriInfo uriInfo,
        @QueryParam("lat") double lat,
        @QueryParam("lon") double lon,
        @QueryParam("alt") double alt,
        @Context Providers providers,
        @Context HttpHeaders headers)
        throws IOException {

    Viewable v = getKml(lat, lon, alt);
    MessageBodyWriter<Viewable> w = providers.getMessageBodyWriter(Viewable.class, Viewable.class, new Annotation[0], "application/xml");
    OutputStream os = //create the stream you want to write the viewable to (ByteArrayOutputStream?)
    InputStream result = //create the stream you want to return
    try {
        w.writeTo(v, v.getClass(), v.getClass(), new Annotation[0], headers, os);
        // Do something with result; e.g., add to ZIP archive and return
    } catch (IOException e) {
        // handle
    }
    return result;
}

免责声明:这不是我的想法 - 未经测试:)

You can consider using a ContainerResponseFilter for this instead of a resource - see e.g. the Gzip filter Jersey provides. The difference would be that your filter would depend on Accept and Content-Type headers instead of Accept-Encoding and Content-Encoding headers (like the gzip filter does).

If you insist on using the resource, you can inject Providers interface on your resource, find the right MessageBodyWritter and call the write method on it:

@GET @Path("kmz")
@Produces("application/vnd.google-earth.kmz")
public InputStream getKmz(@Context UriInfo uriInfo,
        @QueryParam("lat") double lat,
        @QueryParam("lon") double lon,
        @QueryParam("alt") double alt,
        @Context Providers providers,
        @Context HttpHeaders headers)
        throws IOException {

    Viewable v = getKml(lat, lon, alt);
    MessageBodyWriter<Viewable> w = providers.getMessageBodyWriter(Viewable.class, Viewable.class, new Annotation[0], "application/xml");
    OutputStream os = //create the stream you want to write the viewable to (ByteArrayOutputStream?)
    InputStream result = //create the stream you want to return
    try {
        w.writeTo(v, v.getClass(), v.getClass(), new Annotation[0], headers, os);
        // Do something with result; e.g., add to ZIP archive and return
    } catch (IOException e) {
        // handle
    }
    return result;
}

DISCLAIMER: This is off the top of my head - untested :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文