2011-09-22 10 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

相關問題