You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Mike Watson <mi...@gmail.com> on 2008/06/20 04:53:05 UTC

Rest plugin and binary data

Hi Folks,

I'm trying to figure out how to return binary data from the REST plugin.

I'd like to be able to return images that are generated on the fly by
a REST request but looking at ContentTypeHandlerManager, it assumes
that we'll only ever want to return a string and so passes a
StringWriter as the output for implementers of
ContentTypeHandler.fromObject.

Am I missing something really obvious or do I need to 'tweak' this
behaviour and decide what type of writer to provide?

Thanks in advance.

Mike

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Rest plugin and binary data

Posted by Mike Watson <mi...@gmail.com>.
Hi Jeromy,

Thanks for the response, that's really helpful.

Could you possibly provide a little bit more detail though as I
haven't been able to get this working yet.

This might seem like a stupid question, but in your code snippet below
where you have: // create the stream
Where does the output go? Should I use ModelDriven and assign it to the model?

Also how do I get a handle on the StreamResult object so I can specify
attributes programmatically? All the samples I have seen do so
declaratively.

Thanks again for the tips!

Mike


2008/6/20 Jeromy Evans <je...@blueskyminds.com.au>:
> Mike Watson wrote:
>>
>> Hi Folks,
>>
>> I'm trying to figure out how to return binary data from the REST plugin.
>>
>> I'd like to be able to return images that are generated on the fly by
>> a REST request but looking at ContentTypeHandlerManager, it assumes
>> that we'll only ever want to return a string and so passes a
>> StringWriter as the output for implementers of
>> ContentTypeHandler.fromObject.
>>
>>
>
> At first, don't create a custom ContentTypeHander.
> Just have your Controller declare a StreamResult. The default
> ContentTypeHandler will drop through for result processing by XWork (as per
> a ServletRedirectResult).
>
> eg. GET /namespace/image/21
>
> @Result("stream", type = StreamResult.class, value="image")
> class ImageController {
>  public InputStream getImage() {}
>
>  public String show() {
>    // create the stream
>    return "stream";    }
> }
>
> See StreamResult for how to set the Content-type and other properties.
>
> One that works, you could (potentially) register a ContentTypeHandler for
> each image content type you want to return (eg. register the jpg and png
> extensions).  The ContentTypeHandler should do nothing except return a
> result name for XWork that is mapped to a StreamResult.  The image retrieval
> would have to be performed in the Controller rather than the Handler though.
>  There is probably a bit of work to do in the plugin to handle binary
> results better though.
>
> Hope that gets you started.
> Jeromy Evans
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Rest plugin and binary data

Posted by Jeromy Evans <je...@blueskyminds.com.au>.
Mike Watson wrote:
> Hi Jeromy,
>
> Please ignore my other email, I was being a dumb-ass.
>
> Thanks heaps for the tips, I managed to get this working today.
>
> Mike
>   

Good to hear.  Sorry I hadn't got back to you.

regards,
 Jeromy Evans

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Rest plugin and binary data

Posted by Mike Watson <mi...@gmail.com>.
Hi Jeromy,

Please ignore my other email, I was being a dumb-ass.

Thanks heaps for the tips, I managed to get this working today.

Mike

2008/6/20 Jeromy Evans <je...@blueskyminds.com.au>:
> Mike Watson wrote:
>>
>> Hi Folks,
>>
>> I'm trying to figure out how to return binary data from the REST plugin.
>>
>> I'd like to be able to return images that are generated on the fly by
>> a REST request but looking at ContentTypeHandlerManager, it assumes
>> that we'll only ever want to return a string and so passes a
>> StringWriter as the output for implementers of
>> ContentTypeHandler.fromObject.
>>
>>
>
> At first, don't create a custom ContentTypeHander.
> Just have your Controller declare a StreamResult. The default
> ContentTypeHandler will drop through for result processing by XWork (as per
> a ServletRedirectResult).
>
> eg. GET /namespace/image/21
>
> @Result("stream", type = StreamResult.class, value="image")
> class ImageController {
>  public InputStream getImage() {}
>
>  public String show() {
>    // create the stream
>    return "stream";    }
> }
>
> See StreamResult for how to set the Content-type and other properties.
>
> One that works, you could (potentially) register a ContentTypeHandler for
> each image content type you want to return (eg. register the jpg and png
> extensions).  The ContentTypeHandler should do nothing except return a
> result name for XWork that is mapped to a StreamResult.  The image retrieval
> would have to be performed in the Controller rather than the Handler though.
>  There is probably a bit of work to do in the plugin to handle binary
> results better though.
>
> Hope that gets you started.
> Jeromy Evans
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Rest plugin and binary data

Posted by Jeromy Evans <je...@blueskyminds.com.au>.
Mike Watson wrote:
> Hi Folks,
>
> I'm trying to figure out how to return binary data from the REST plugin.
>
> I'd like to be able to return images that are generated on the fly by
> a REST request but looking at ContentTypeHandlerManager, it assumes
> that we'll only ever want to return a string and so passes a
> StringWriter as the output for implementers of
> ContentTypeHandler.fromObject.
>
>   

At first, don't create a custom ContentTypeHander.
Just have your Controller declare a StreamResult. The default 
ContentTypeHandler will drop through for result processing by XWork (as 
per a ServletRedirectResult).

eg. GET /namespace/image/21

@Result("stream", type = StreamResult.class, value="image")
class ImageController {
   public InputStream getImage() {}

  public String show() {
     // create the stream
     return "stream";    
 }
}

See StreamResult for how to set the Content-type and other properties.

One that works, you could (potentially) register a ContentTypeHandler 
for each image content type you want to return (eg. register the jpg and 
png extensions).  The ContentTypeHandler should do nothing except return 
a result name for XWork that is mapped to a StreamResult.  The image 
retrieval would have to be performed in the Controller rather than the 
Handler though.  There is probably a bit of work to do in the plugin to 
handle binary results better though.

Hope that gets you started.
Jeromy Evans



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org