dev@jsr311.java.net

Re: JSR311: Response isn't adequate

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Mon, 10 Mar 2008 11:19:24 -0400

On Mar 10, 2008, at 10:11 AM, Bill Burke wrote:
>
> Marc Hadley wrote:
>> On Mar 7, 2008, at 3:51 PM, Bill Burke wrote:
>>>
>>> Also, can consider a simple Representation interface? I know it
>>> would be easy to implement one with the @Provider model, but I
>>> think its a good idea.
>>>
>> I think the combination of Response and ResponseBuilder already
>> fulfill the role of representation.
>
> @GET
> public Representation get()
> {
>
> Connection conn = ...;
> Blob blob = rs.getBlob(...);
>
> return new Representation() {
> public write(OutputStream stream) {
> // stream the output
> blob.close();
> rs.close();
> conn.close();
> }
> };
>
> }
>
> Instead of requiring the awkward creation and registration of
> MessageBodyWriters, you allow the component to return a
> Representation object, which is really a MessageBodyWriter-like
> object instantiated by the resource.
>

@GET
public InputStream get() {

   Connection conn = ...;
   Blob blob = rs.getBlob(...);

   return new InputStream() {
     InputStream is = blob.getBinaryStream();

     public int read() throws IOException {
       return is.read();
     }

     public void close() throws IOException {
       is.close();
       blob.close();
       rs.close();
     }
   };
}

Wrap the InputStream in a Response to supply the metadata...

Marc.

---
Marc Hadley <marc.hadley at sun.com>
CTO Office, Sun Microsystems.