users@jersey.java.net

returning an output stream

From: alex hendry <al_hendry_at_yahoo.com.au>
Date: Sat, 11 Oct 2008 21:00:13 -0700 (PDT)

Hi all, Thanks to the quick replies to my questions so far, it has been much appreciated as I get my head around jersey. I work for the Australian institute of marine science and I am creating some web services that return weather station data as XML from our weather stations on the Great Barrier Reef. However as the XML files may be hundreds of mb or even gb I want to return the XML as an output stream. How much XML is returned will depend upon what time period is passed in the URL. So far I have done as follows: @Path("{dataPeriod}") @Provider public class XmlExport implements MessageBodyWriter { @GET public Response getXml(@PathParam("dataPeriod")String dataPeriod) { String fileName = ""; if (dataPeriod.equalsIgnoreCase("latest")) { fileName = "latest.xml"; } else if (dataPeriod.equalsIgnoreCase("last2hrs")) { fileName = "last2hrs.xml"; } else if (dataPeriod.equalsIgnoreCase("last48hrs")) { fileName = "last48hrs.xml"; } else if (dataPeriod.equalsIgnoreCase("lastWeek")) { fileName = "lastWeek.xml"; } else if (dataPeriod.equalsIgnoreCase("lastMonth")) { fileName = "lastMonth.xml"; } OutputStream outputStream = new ByteArrayOutputStream(); try { writeTo(null, null, null, null, null, null, outputStream); } catch (IOException e) { e.printStackTrace(); } return Response.ok(outputStream).type("application/xml").header("Content-Disposition", "attachment; filename=" + fileName).build(); } public void writeTo(Object o, Class aClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap multivaluedMap, OutputStream outputStream) throws IOException, WebApplicationException { XMLOutputFactory xof = XMLOutputFactory.newInstance(); XMLStreamWriter xtw = null; try { xtw = xof.createXMLStreamWriter(outputStream); } catch (XMLStreamException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } LastWeekViewDao lastWeekViewDao = new LastWeekViewDao(); Collection<LastWeekViewEntity> lastWeekViewEntities = lastWeekViewDao.selectAll(); SitesEntityManagedBean sitesEntityManagedBean = new SitesEntityManagedBean(); List<SitesEntity> sites = sitesEntityManagedBean.getEntities(); XmlOutputHandler handler; handler = new XmlOutput(); handler.writeHeader(xtw); handler.writeBody(xtw, lastWeekViewEntities, sites); handler.writeFooter(xtw); try { if (xtw != null) { xtw.flush(); } if (xtw != null) { xtw.close(); } if (outputStream != null) { outputStream.flush(); } if (outputStream != null) { outputStream.close(); } } catch (XMLStreamException e) { e.printStackTrace(); } } I have read in the mailing list that you can not return an output stream, only an input stream and I get an error from my code as follows: 12/10/2008 13:06:27 com.sun.jersey.spi.container.ContainerResponse write SEVERE: A message body reader for Java type, class java.io.ByteArrayOutputStream, and MIME media type, application/xml, was not found I can return the outputStream.toString() or create a file etc but I need to minimize my memory over head due to the file sizes. How can I return an output stream? Cheers, Alex Make the switch to the world&#39;s best email. Get Yahoo!7 Mail! http://au.yahoo.com/y7mail