You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@abdera.apache.org by James M Snell <ja...@gmail.com> on 2007/11/15 17:15:03 UTC

StreamWriter and ResponseContext

All,

General FYI...

The new StreamWriter interface provides significantly better performance
 when writing out documents than the object model.  To give you an idea
of the improvement, in my testing I was able to create 100 feeds with
5000 entries each in around 500ms.

When implementing an Atompub server, a significant performance
improvement can be realized by implementing a custom ResponseContext
implementation that takes pojo's and writes them out using StreamWriter
instead of building up an object model and using the
BaseResponseContext.  Below is a simple example of a custom
ResponseContext using StreamWriter:

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

import org.apache.abdera.Abdera;
import org.apache.abdera.protocol.server.impl.AbstractResponseContext;

public class MyObjectResponseContext
  extends AbstractResponseContext {

  private final Abdera abdera;
  private final Foo foo;

  public MyObjectResponseContext(Abdera abdera, Foo foo, int status) {
    this.abdera = abdera;
    this.foo = foo;
    setStatus(status);
  }

  public boolean hasEntity() {
    return foo != null;
  }

  public void writeTo(
    OutputStream out)
      throws IOException {
    writeTo(new OutputStreamWriter(out,"UTF-8"));
  }

  public void writeTo(
    Writer javaWriter)
      throws IOException {
    if (hasEntity()) {
    abdera.newStreamWriter()
      .setWriter(javaWriter)
      .startElement("foo")
        .startElement("a")
          .writeElementText(foo.a)
        .endElement()
        .startElement("b")
          .writeElementText(String.valueOf(foo.b))
        .endElement()
      .endElement()
      .flush();
    }
  }

  public void writeTo(
    OutputStream out,
    org.apache.abdera.writer.Writer writer)
      throws IOException {
    throw new UnsupportedOperationException();
  }

  public void writeTo(
    Writer javaWriter,
    org.apache.abdera.writer.Writer abderaWriter)
      throws IOException {
    throw new UnsupportedOperationException();
  }

}

- James