2011-09-22 2 views
0

모두Jersey JSP 템플릿 응답을 InputStream으로 리 라우팅 할 수 있습니까?

XML을 생성하는 웹 서비스를 만들기 위해 Java/Jersey 1.9를 사용하고 있습니다. 저는 JSP 템플릿을 사용하여 XML을 생성합니다 (명시 적으로 Viewable 클래스를 통해). 추가 처리를 위해 JSP 결과를 로컬 InputStream으로 경로 재 지정하는 방법이 있습니까? 지금은 실제로 다른 방법에서 http 루프백 (localhost)으로 자체 XML 웹 서비스를 호출하고 있습니다. 어떤 통찰력

감사합니다,

이안

@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-Encoding 및 Content-Encoding 헤더 대신 Accept 및 Content-Type 헤더에 의존한다는 것입니다 (gzip 필터와 동일).

당신이 자원을 사용하여 주장하는 경우, 당신은 바로 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; 
} 

면책 조항 :이 내 머리 위로 떨어져이다 - 테스트 안 함 :

+0

고마워요! 나는 아직도 저지를 알아 내고 다재 다능 한 것을 좋아하지만, 나는 이것에 대해 단서없는 시간을 보냈다. 두 솔루션 모두 눈을 뜨고 있습니다. 필자가 제안한 것처럼 먼저 필터를 사용해 보겠습니다. 다시 한번 감사드립니다. – ianmstew

관련 문제