Hi Tom,
This is going back a while but the key things are that you:
*  have the right annotations at the head of your controller class
(e...@result(name = "image", type = StreamResult.class, value = "image")),
* implement a getImage() method (assuming it's images you're wanting) that
does something like:
      ByteArrayInputStream result = null;
      ByteArrayOutputStream interim = new ByteArrayOutputStream();
      if (null != this.model)
      {
        ImageIO.write(model.getImage(), imageType, interim);
      }
      result = new ByteArrayInputStream(interim.toByteArray());
      return result;
* implement a custom ContentTypeHandler if you're doing anything funky and
remember to reference it from your struts.xml
e.g.:   <bean name="myCustomHandler"
 type="org.apache.struts2.rest.handler.ContentTypeHandler"
        class="org.someoutfit.rest.handler.ImageHandler" />
* Specify the action for your image controller in a package def in
struts.xml
e.g.:   <package name="imagePackage" extends="rest-h">
    <action name="image" class="org.someoutfit.rest.action.ImageController">
      <result name="image" type="stream">
        <param name="contentType">${contentType}</param>
        <param name="inputName">image</param>
        <param name="bufferSize">${bufferSize}</param>
      </result>
    </action>

I know that probably sounds a bit cryptic but if you get in to it it should
make sense.

Mike
2009/4/29 <vfr...@yahoo.com>

> Hello Mike and Jeromy,
>
> I am new to Struts 2 plugin and trying to implement rest ws for binary
> data.
>
> Do you have some sample codes for me to get started?  If you could provide
> some details on how to implement the controller, I would really appreciate
> it.
>
> Thank you in advance.
>
> -Tom
>
> Mike Watson-2 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
> >
> > 2008/6/20 Jeromy Evans <jeromy.ev...@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-unsubscr...@struts.apache.org
> >> For additional commands, e-mail: user-h...@struts.apache.org
> >>
> >>
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
> > For additional commands, e-mail: user-h...@struts.apache.org
> >
> >
> >
> Quoted from:
> http://www.nabble.com/Rest-plugin-and-binary-data-tp18022059p18085702.html
>
>

Reply via email to