Return an Image from a REST webservice api

View: New views
7 Messages — Rating Filter:   Alert me  

Return an Image from a REST webservice api

by cgswtsu78 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

I am brand new to CXF and somewhat new to Java.  I have a requirement to write a REST webservice that returns a streamed image.  My dynamically created image is of type BufferedImage.  I don't want to persist the image but simply stream it to the browser.  Can anyone help guide me down the right path?

I'm able to write the REST webservice, but I'm unsure what annotations to use and what return type to specify on my REST webservice method.  I've looked over the forum a bit and it looks like I should attempt to use the StreamingOutput class, but the implementation details are a little vague.

Any guidance would be greatly appreciated!

-Colin

Re: Return an Image from a REST webservice api

by Sergey Beryozkin-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi

I've looked at BufferedImage JavaDocs ant it appears that indeed, using StreamingOutput (which can be simpler than doing a custom
MessageBodyWriter) can offer the best option, at least for a start.

Ex.

@Path("/images")
public ImagesResource {
    @Path("{id}")
    public StreamingOutput getImage(@PathParam("id") int id) {

          BufferedImage image = retrieveImage(id);
          retrun new StreamingOutputImpl(image);
    }
}

private static class StreamingOutputImpl implements StreamingOutput {


private  BufferedImage image;


public StreamingOutputImplBufferedImage image ) {

this.image = image;

}


public void write(OutputStream output) throws IOException, WebApplicationException {

  // just guessing  here :-), the idea is to stream the BufferdImage instance to the output

  ImageProducer ip = image.getImageProducer();

  im.addImageConsumer(new ImageConsumerImpl(output));

  ip.startProduction();

}

hope it helps, Sergey

>
> Hello,
>
> I am brand new to CXF and somewhat new to Java.  I have a requirement to
> write a REST webservice that returns a streamed image.  My dynamically
> created image is of type BufferedImage.  I don't want to persist the image
> but simply stream it to the browser.  Can anyone help guide me down the
> right path?
>
> I'm able to write the REST webservice, but I'm unsure what annotations to
> use and what return type to specify on my REST webservice method.  I've
> looked over the forum a bit and it looks like I should attempt to use the
> StreamingOutput class, but the implementation details are a little vague.
>
> Any guidance would be greatly appreciated!
>
> -Colin
> --
> View this message in context: http://old.nabble.com/Return-an-Image-from-a-REST-webservice-api-tp26160563p26160563.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>


RE: Return an Image from a REST webservice api

by rdgrimes :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

In addition to what Sergey has presented, which is undoubtedly more directly to the point of what you asked, I wanted to suggest the following. Perhaps this doesn't fit your situation, but I have never found it actually necessary to waste the servlet framework resources to return a streamed image. Instead, I prefer to:

1) Have the servlet/web service locate the image - whether that means finding it or creating it on the server's hard drive.

2) Pass the name or path of the image back to the client, and

3) Have the client use the returned image name or path to request the image via a standard http static image request.

Since you indicated that you basically wanted it streamed to the browser, presumably to simply display it, it sounds like there is no need to tie up Java framework resources and connections to accomplish what the container can do with simple HTTP static resource retrieval mechanisms.


Ron Grimes



-----Original Message-----
From: Sergey Beryozkin [mailto:sberyozk@...]
Sent: Tuesday, November 03, 2009 10:42 AM
To: users@...
Subject: Re: Return an Image from a REST webservice api

Hi

I've looked at BufferedImage JavaDocs ant it appears that indeed, using StreamingOutput (which can be simpler than doing a custom
MessageBodyWriter) can offer the best option, at least for a start.

Ex.

@Path("/images")
public ImagesResource {
    @Path("{id}")
    public StreamingOutput getImage(@PathParam("id") int id) {

          BufferedImage image = retrieveImage(id);
          retrun new StreamingOutputImpl(image);
    }
}

private static class StreamingOutputImpl implements StreamingOutput {


private  BufferedImage image;


public StreamingOutputImplBufferedImage image ) {

this.image = image;

}


public void write(OutputStream output) throws IOException, WebApplicationException {

  // just guessing  here :-), the idea is to stream the BufferdImage instance to the output

  ImageProducer ip = image.getImageProducer();

  im.addImageConsumer(new ImageConsumerImpl(output));

  ip.startProduction();

}

hope it helps, Sergey

>
> Hello,
>
> I am brand new to CXF and somewhat new to Java.  I have a requirement to
> write a REST webservice that returns a streamed image.  My dynamically
> created image is of type BufferedImage.  I don't want to persist the image
> but simply stream it to the browser.  Can anyone help guide me down the
> right path?
>
> I'm able to write the REST webservice, but I'm unsure what annotations to
> use and what return type to specify on my REST webservice method.  I've
> looked over the forum a bit and it looks like I should attempt to use the
> StreamingOutput class, but the implementation details are a little vague.
>
> Any guidance would be greatly appreciated!
>
> -Colin
> --
> View this message in context: http://old.nabble.com/Return-an-Image-from-a-REST-webservice-api-tp26160563p26160563.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>


RE: Return an Image from a REST webservice api

by Sergey Beryozkin-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Sounds like a really good advice. By the way, CXF servlets will support
the redirection shortly, so, as far as serving the static resources is
concerned, one would have the option to redirect to a default servlet
(Tomcat/Jetty/etc) or indeed to some other servlet capable of streaming
static resources back to the client

Cheers, Sergey

-----Original Message-----
From: Ron Grimes [mailto:rgrimes@...]
Sent: 03 November 2009 18:27
To: users@...
Subject: RE: Return an Image from a REST webservice api

Hi,

In addition to what Sergey has presented, which is undoubtedly more
directly to the point of what you asked, I wanted to suggest the
following. Perhaps this doesn't fit your situation, but I have never
found it actually necessary to waste the servlet framework resources to
return a streamed image. Instead, I prefer to:

1) Have the servlet/web service locate the image - whether that means
finding it or creating it on the server's hard drive.

2) Pass the name or path of the image back to the client, and

3) Have the client use the returned image name or path to request the
image via a standard http static image request.

Since you indicated that you basically wanted it streamed to the
browser, presumably to simply display it, it sounds like there is no
need to tie up Java framework resources and connections to accomplish
what the container can do with simple HTTP static resource retrieval
mechanisms.


Ron Grimes



-----Original Message-----
From: Sergey Beryozkin [mailto:sberyozk@...]
Sent: Tuesday, November 03, 2009 10:42 AM
To: users@...
Subject: Re: Return an Image from a REST webservice api

Hi

I've looked at BufferedImage JavaDocs ant it appears that indeed, using
StreamingOutput (which can be simpler than doing a custom
MessageBodyWriter) can offer the best option, at least for a start.

Ex.

@Path("/images")
public ImagesResource {
    @Path("{id}")
    public StreamingOutput getImage(@PathParam("id") int id) {

          BufferedImage image = retrieveImage(id);
          retrun new StreamingOutputImpl(image);
    }
}

private static class StreamingOutputImpl implements StreamingOutput {


private  BufferedImage image;


public StreamingOutputImplBufferedImage image ) {

this.image = image;

}


public void write(OutputStream output) throws IOException,
WebApplicationException {

  // just guessing  here :-), the idea is to stream the BufferdImage
instance to the output

  ImageProducer ip = image.getImageProducer();

  im.addImageConsumer(new ImageConsumerImpl(output));

  ip.startProduction();

}

hope it helps, Sergey

>
> Hello,
>
> I am brand new to CXF and somewhat new to Java.  I have a requirement
to
> write a REST webservice that returns a streamed image.  My dynamically
> created image is of type BufferedImage.  I don't want to persist the
image
> but simply stream it to the browser.  Can anyone help guide me down
the
> right path?
>
> I'm able to write the REST webservice, but I'm unsure what annotations
to
> use and what return type to specify on my REST webservice method.
I've
> looked over the forum a bit and it looks like I should attempt to use
the
> StreamingOutput class, but the implementation details are a little
vague.
>
> Any guidance would be greatly appreciated!
>
> -Colin
> --
> View this message in context:
http://old.nabble.com/Return-an-Image-from-a-REST-webservice-api-tp26160
563p26160563.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>


RE: Return an Image from a REST webservice api

by cgswtsu78 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thank both of you for your replies, but I'm afraid I'm confused.  

If I create a BufferedImage, how can I return it to the client without streaming it?  Since the rest web service isn't storing it anywhere but in memory, I don't understand how to get it back to the client who called the rest web service.  I've tried to simply stream it from the rest web service, but I can't get access to the HttpServletResponse object as its always null.  I've tried the below to get the HttpServletResponse without success.  I'm calling the rest web service url directly from a link on jsp page that does a get on the rest web service url.  Thanks for all of your help, greatly appreciated!


@Resource
private WebServiceContext context;
MessageContext ctx = context.getMessageContext();
HttpServletRequest request = (HttpServletRequest) ctx.get(AbstractHTTPDestination.HTTP_REQUEST);
HttpServletResponse response = (HttpServletResponse) ctx.get(AbstractHTTPDestination.HTTP_RESPONSE);


rdgrimes wrote:
Hi,

In addition to what Sergey has presented, which is undoubtedly more directly to the point of what you asked, I wanted to suggest the following. Perhaps this doesn't fit your situation, but I have never found it actually necessary to waste the servlet framework resources to return a streamed image. Instead, I prefer to:

1) Have the servlet/web service locate the image - whether that means finding it or creating it on the server's hard drive.

2) Pass the name or path of the image back to the client, and

3) Have the client use the returned image name or path to request the image via a standard http static image request.

Since you indicated that you basically wanted it streamed to the browser, presumably to simply display it, it sounds like there is no need to tie up Java framework resources and connections to accomplish what the container can do with simple HTTP static resource retrieval mechanisms.


Ron Grimes



-----Original Message-----
From: Sergey Beryozkin [mailto:sberyozk@progress.com]
Sent: Tuesday, November 03, 2009 10:42 AM
To: users@cxf.apache.org
Subject: Re: Return an Image from a REST webservice api

Hi

I've looked at BufferedImage JavaDocs ant it appears that indeed, using StreamingOutput (which can be simpler than doing a custom
MessageBodyWriter) can offer the best option, at least for a start.

Ex.

@Path("/images")
public ImagesResource {
    @Path("{id}")
    public StreamingOutput getImage(@PathParam("id") int id) {

          BufferedImage image = retrieveImage(id);
          retrun new StreamingOutputImpl(image);
    }
}

private static class StreamingOutputImpl implements StreamingOutput {


private  BufferedImage image;


public StreamingOutputImplBufferedImage image ) {

this.image = image;

}


public void write(OutputStream output) throws IOException, WebApplicationException {

  // just guessing  here :-), the idea is to stream the BufferdImage instance to the output

  ImageProducer ip = image.getImageProducer();

  im.addImageConsumer(new ImageConsumerImpl(output));

  ip.startProduction();

}

hope it helps, Sergey

>
> Hello,
>
> I am brand new to CXF and somewhat new to Java.  I have a requirement to
> write a REST webservice that returns a streamed image.  My dynamically
> created image is of type BufferedImage.  I don't want to persist the image
> but simply stream it to the browser.  Can anyone help guide me down the
> right path?
>
> I'm able to write the REST webservice, but I'm unsure what annotations to
> use and what return type to specify on my REST webservice method.  I've
> looked over the forum a bit and it looks like I should attempt to use the
> StreamingOutput class, but the implementation details are a little vague.
>
> Any guidance would be greatly appreciated!
>
> -Colin
> --
> View this message in context: http://old.nabble.com/Return-an-Image-from-a-REST-webservice-api-tp26160563p26160563.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>

RE: Return an Image from a REST webservice api

by rdgrimes :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

With regards to my suggestion, I typically write these images to a directory that can be accessed through a standard http request or simple servlet. You can name these images something dynamic and not easily guessed. Maybe customer number + time stamp + .png, or user Id + timestamp + .png. Anyway, you get the idea. Return the image name/path to the client so they can request it either via standard http static resource request or via a servlet that extends HttpServlet. I tend to do the latter as it's just a simple Java servlet that handles the request for the download of resources. I do it this way because there is no need to use the power of Spring, JAX-WS, JAXB, CXF to return a simple image to a client. I can offload that work to a simple process and free up the mentioned frameworks to do what they're uniquely good at.

Oh, and btw, I run a regularly scheduled process to clean up that folder where I write the above mentioned images.

Ron Grimes


________________________________________
From: cgswtsu78 [cgray@...]
Sent: Wednesday, November 04, 2009 9:06 AM
To: users@...
Subject: RE: Return an Image from a REST webservice api

Thank both of you for your replies, but I'm afraid I'm confused.

If I create a BufferedImage, how can I return it to the client without
streaming it?  Since the rest web service isn't storing it anywhere but in
memory, I don't understand how to get it back to the client who called the
rest web service.  I've tried to simply stream it from the rest web service,
but I can't get access to the HttpServletResponse object as its always null.
I've tried the below to get the HttpServletResponse without success.  I'm
calling the rest web service url directly from a link on jsp page that does
a get on the rest web service url.  Thanks for all of your help, greatly
appreciated!

Re: Return an Image from a REST webservice api

by Sergey Beryozkin-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi

sorry for a delay...

> @Resource
> private WebServiceContext context;

I think you might be using a deprecated CXF HTTP binding as opposed to its JAXRS implementation ?
If you'd like to return an in-memory BufferedImage then the simplest option would be to return  a StreamingOutput implementation and
start writing to the output stream when its write() method is called. Other options are available too like returning a pointer to
byte array containg this BufferedImage's data, etc... If you do prefer to write directly to the HttpServletResponse then you can
access it from

@Context
org.apache.cxf.jaxrs.ext.MessageContext context;

or

@Context
private HttpServletResponse context;

cheers, Sergey

>
> Thank both of you for your replies, but I'm afraid I'm confused.
>
> If I create a BufferedImage, how can I return it to the client without
> streaming it?  Since the rest web service isn't storing it anywhere but in
> memory, I don't understand how to get it back to the client who called the
> rest web service.  I've tried to simply stream it from the rest web service,
> but I can't get access to the HttpServletResponse object as its always null.
> I've tried the below to get the HttpServletResponse without success.  I'm
> calling the rest web service url directly from a link on jsp page that does
> a get on the rest web service url.  Thanks for all of your help, greatly
> appreciated!
>
>
> @Resource
> private WebServiceContext context;
> MessageContext ctx = context.getMessageContext();
> HttpServletRequest request = (HttpServletRequest)
> ctx.get(AbstractHTTPDestination.HTTP_REQUEST);
> HttpServletResponse response = (HttpServletResponse)
> ctx.get(AbstractHTTPDestination.HTTP_RESPONSE);
>
>
>
> rdgrimes wrote:
>>
>> Hi,
>>
>> In addition to what Sergey has presented, which is undoubtedly more
>> directly to the point of what you asked, I wanted to suggest the
>> following. Perhaps this doesn't fit your situation, but I have never found
>> it actually necessary to waste the servlet framework resources to return a
>> streamed image. Instead, I prefer to:
>>
>> 1) Have the servlet/web service locate the image - whether that means
>> finding it or creating it on the server's hard drive.
>>
>> 2) Pass the name or path of the image back to the client, and
>>
>> 3) Have the client use the returned image name or path to request the
>> image via a standard http static image request.
>>
>> Since you indicated that you basically wanted it streamed to the browser,
>> presumably to simply display it, it sounds like there is no need to tie up
>> Java framework resources and connections to accomplish what the container
>> can do with simple HTTP static resource retrieval mechanisms.
>>
>>
>> Ron Grimes
>>
>>
>>
>> -----Original Message-----
>> From: Sergey Beryozkin [mailto:sberyozk@...]
>> Sent: Tuesday, November 03, 2009 10:42 AM
>> To: users@...
>> Subject: Re: Return an Image from a REST webservice api
>>
>> Hi
>>
>> I've looked at BufferedImage JavaDocs ant it appears that indeed, using
>> StreamingOutput (which can be simpler than doing a custom
>> MessageBodyWriter) can offer the best option, at least for a start.
>>
>> Ex.
>>
>> @Path("/images")
>> public ImagesResource {
>>     @Path("{id}")
>>     public StreamingOutput getImage(@PathParam("id") int id) {
>>
>>           BufferedImage image = retrieveImage(id);
>>           retrun new StreamingOutputImpl(image);
>>     }
>> }
>>
>> private static class StreamingOutputImpl implements StreamingOutput {
>>
>>
>> private  BufferedImage image;
>>
>>
>> public StreamingOutputImplBufferedImage image ) {
>>
>> this.image = image;
>>
>> }
>>
>>
>> public void write(OutputStream output) throws IOException,
>> WebApplicationException {
>>
>>   // just guessing  here :-), the idea is to stream the BufferdImage
>> instance to the output
>>
>>   ImageProducer ip = image.getImageProducer();
>>
>>   im.addImageConsumer(new ImageConsumerImpl(output));
>>
>>   ip.startProduction();
>>
>> }
>>
>> hope it helps, Sergey
>>
>>>
>>> Hello,
>>>
>>> I am brand new to CXF and somewhat new to Java.  I have a requirement to
>>> write a REST webservice that returns a streamed image.  My dynamically
>>> created image is of type BufferedImage.  I don't want to persist the
>>> image
>>> but simply stream it to the browser.  Can anyone help guide me down the
>>> right path?
>>>
>>> I'm able to write the REST webservice, but I'm unsure what annotations to
>>> use and what return type to specify on my REST webservice method.  I've
>>> looked over the forum a bit and it looks like I should attempt to use the
>>> StreamingOutput class, but the implementation details are a little vague.
>>>
>>> Any guidance would be greatly appreciated!
>>>
>>> -Colin
>>> --
>>> View this message in context:
>>> http://old.nabble.com/Return-an-Image-from-a-REST-webservice-api-tp26160563p26160563.html
>>> Sent from the cxf-user mailing list archive at Nabble.com.
>>>
>>
>>
>>
>
> --
> View this message in context: http://old.nabble.com/Return-an-Image-from-a-REST-webservice-api-tp26160563p26199183.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>