Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Creating a feed in mem instead of on disk

Help
Brian Pipa
2005-08-12
2013-04-25
  • Brian Pipa
    Brian Pipa
    2005-08-12

    Hi all,

    I am trying to add RSS feeds to a JSP site. I want to create the feed in memory and return the feed via a JSP without ever writing the feed to disk. Looking at the exporters, I didn't see a way to do this - it looks like they only write to disk. It looks like they *almost* have this ability since they can take a Writer (and thus a StringWriter), but there is no way to get the String back. Is there a way to do this in the current API?

    Assuming there was no way to do it in the API, I made some changes to the API so you could get the String representation of a feed without writing it to disk. I'd like to contribute this code back to the project if anyone thinks this may be useful, but I'm not sure how to contribute the code. The changes add 3 new small classes, 1 new interface, and a small change to each of the three current exporters.

    Thanks,
    Brian
    http://pipasoft.com
    http://myvogonpoetry.com

     
    • Brian Pipa
      Brian Pipa
      2005-08-12

      To answer my own post, I got the answer from the mailing list thanks to Doug Matthews - here it is:

      Brian,

      No, you don't need to write the output to disk.

      If you're writing a servlet or jsp page you have access to a "response"
      object of type HttpServletResponse, which can return you a reference to
      a PrintWriter, where you can write your response directly to the output
      stream.

      See this example

                     java.io.PrintWriter writer;
                     try {
                             writer = response.getWriter();

                             // buildChannel is my method to return a channel
                             ChannelIF channel = buildChannel();

                             ChannelExporterIF exporter = new
      RSS_2_0_Exporter(writer,
      response.getCharacterEncoding());
                             exporter.write(channel);

                             writer.flush();
                             response.flushBuffer();

                     } catch (java.io.IOException e) {
                             // TODO log it in with some WARN or ERROR level;

                             e.printStackTrace();
                     }

      You can also get the string representation by just passing a
      StringWriter to the exporter and then calling the writer's toString
      method.