Wrong content type in REST-API

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2015 06:19 AM
Hello at all,
when you invoke the Activiti REST-API
Shouln´t it be
I need to display the diagram with the current state in a browser. Any idea without changing the rest api?
Best regards
Ben
when you invoke the Activiti REST-API
runtime/process-instances/{process-instance-id}/diagram
, the content-type set by the REST-API is text/html
. That leads to the problem that the diagram (png) can not be displayed in the browser because the browser only displays the raw-content of the png-diagram.Shouln´t it be
image/png
?I need to display the diagram with the current state in a browser. Any idea without changing the rest api?
Best regards
Ben
Labels:
- Labels:
-
Archive
4 REPLIES 4

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2015 12:05 AM
Spring MVC uses the request's Accept header to set the response content type. When a request to the diagram is made from an IMG HTML tag the header will look something like
Which will produce a response of content type image/png. But when trying to access the image directly the request's Accept header will look something like
Which produces a response of content type text/html. Although, the code in getProcessInstanceDiagram sets the content type of the response to image/png
[java]
response.setContentType("image/png");
[/java]
That won't work since Spring MVC will change it afterwards depending on the request's Accept header. To force the response header to always be image/png the produces attribute can be used in the @RequestMapping annotation as follows
[java]
@RequestMapping(value="/runtime/process-instances/{processInstanceId}/diagram", method = RequestMethod.GET, produces="image/png")
[/java]
Accept: image/png,image/*;q=0.8,*/*;q=0.5
Which will produce a response of content type image/png. But when trying to access the image directly the request's Accept header will look something like
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Which produces a response of content type text/html. Although, the code in getProcessInstanceDiagram sets the content type of the response to image/png
[java]
response.setContentType("image/png");
[/java]
That won't work since Spring MVC will change it afterwards depending on the request's Accept header. To force the response header to always be image/png the produces attribute can be used in the @RequestMapping annotation as follows
[java]
@RequestMapping(value="/runtime/process-instances/{processInstanceId}/diagram", method = RequestMethod.GET, produces="image/png")
[/java]

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2015 03:58 AM
Wow, thanks for this detailed solution!

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2015 08:36 PM
Actually, I'm glad that you brought up this issue. I faced this issue a couple of months ago and was planning to submit a pull for a fix but forgot to do that. So, thanks for reminding me 
This issue doesn't appear in tests since requests do not include Accept headers, therefore, Spring MVC will not alter the response content type.
Additionally, using the produces attribute of @RequestMapping annotation doesn't handle cases when trying to access a non-existing process instance diagram or when an exception is thrown. The response in those cases will be an ugly 'HTTP Status 500' page error instead of a json formatted response. A better solution could be using Spring MVC's ResponseEntity class.
I am working on a fix and will submit it a pull request soon.

This issue doesn't appear in tests since requests do not include Accept headers, therefore, Spring MVC will not alter the response content type.
Additionally, using the produces attribute of @RequestMapping annotation doesn't handle cases when trying to access a non-existing process instance diagram or when an exception is thrown. The response in those cases will be an ugly 'HTTP Status 500' page error instead of a json formatted response. A better solution could be using Spring MVC's ResponseEntity class.
I am working on a fix and will submit it a pull request soon.

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2015 10:41 PM
