2011-09-22 11 views
0

全部,是否有可能将Jersey JSP模板响应重新路由到InputStream?

我使用Java/Jersey 1.9创建生成XML的Web服务。我使用JSP模板生成XML(显式地通过Viewable类)。有什么方法可以将JSP结果重新路由到本地InputStream进行进一步处理?目前我实际上是从另一种方法调用我自己的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; 
} 

回答

0

你可以考虑使用ContainerResponseFilter这个,而不是一个资源 - 例如见泽西岛提供了Gzip filter。区别在于你的过滤器将取决于Accept和Content-Type头而不是Accept-Encoding和Content-Encoding头(就像gzip过滤器一样)。

如果你坚持使用的资源,你可以注入你的资源提供者接口,找到合适的MessageBodyWritter并调用它的write方法:

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

免责声明:这是我的头顶部 - 未经测试:)

+0

谢谢马丁!我仍然在搞清楚泽西岛并且喜欢它的多功能性,但是我花了数小时寻找这个没有潜力的线索。您的两个解决方案都让人大开眼界,我会像您所建议的那样首先尝试过滤器。再次感谢。 – ianmstew

相关问题