Dear All,

I am trying to implement a web service whose mime type is dynamically
determined. Suppose that it is an image; if I set the entity to the image's
InputStream bytes like so:

@Path(...)
public class MyServiceImpl
{
        @Path("/{id}/storage")
        public Response getImageById(@PathParam("id") Long id) 
        {
                // Retrieve bytes of the image with the specified ID here
...
                bytes[] bytes = myImageInputStreamBytes();
                try
                {
                        final ResponseBuilder builder = Response.ok();
                        builder.entity(bytes);
                        builder.type(WsUtilService.newMediaType(mimeType));
                        final Response response = builder.build();
                        // Attach storage code as custom response header
                        
response.getMetadata().add(HttpHeader.RESOURCE_STORAGE_CODE.getName(),
                                        storageCode.toString());

 // builder.entity() doesn't work properly. What if we comment that line and
use instead:            
//httpResponse.getOutputStream().write(contentFactory.getResourceContent(resource));
// Well, I can't get the httpResponse to be spring-injected into my REST
service in the first place so the above line doesn't work
                        return response;
                }
                catch (IOException e)
                {
                        throw new WsException(ErrorCodes.INTERNAL_ERROR, 
e.getMessage());
                }

... then I get a weird response when hitting the WS in Firefox. Instead of
the actual image, I see the URL and can't find the image file in Firebug,
even though it says it's there (of size 144kb in this case). However, I
*can* read back the response in a JUnit test case using HttpClient and
converting  response.getResponseBody() back to an image in my Java code.

- What is the proper way to stream back an image in CXF 2.2 JAX-RS?
- My next use case retrieves a database resource that can be an image, xml,
etc. Should I set the builder's type() as above with the appropriate
MIME-TYPE? How do I stream an XML document without CXF generating an
<xsd:string> tag around it? (so far I've been trying to set the builder's
entity() to be the XML document as a string)

Thank you so much in advance,
Oren
-- 
View this message in context: 
http://www.nabble.com/How-to-treat-images-in-CXF-JAX-RS--tp22976985p22976985.html
Sent from the cxf-user mailing list archive at Nabble.com.

Reply via email to