Hi,


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));

You can also pass a string directly to builder.type() or use MediaType.valueOf()

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

Why builder.entity() does not work ? Can you clarify please...

use instead:
//httpResponse.getOutputStream().write(contentFactory.getResourceContent(resource));

believe it or not I was actually about to get it fixed today, I have a list of various things which need to be done for TCK to work, so ensuring
users can use HttpServletResponse directly is on the map.

// 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

If you do
@Context HttpServletResponse httpResponse

then you should get it injected by the JAXRS runtime...

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.

Lets try to capture the response on the wire. Suppose that service is listening on 8080 port, you can start a tcp trace utility on port 8081 and tell it to redirect to 8080 and direct the browser and the test code to invoke through 8081. We can then see why the unit test code is working and the browser is not....


- What is the proper way to stream back an image in CXF 2.2 JAX-RS?

Perhaps returning JAXRS StreamingOutput is what would help ?

- 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)

can you explain a bit more ? Are you using some CXF utility code may be that adds 
<xsd:string> tag ?

thanks. Sergey

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