abdera-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From James M Snell <jasn...@gmail.com>
Subject StreamWriter and ResponseContext
Date Thu, 15 Nov 2007 16:15:03 GMT
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

Mime
View raw message