0

I wrote an Alfresco Web Script that renders CSV, it works well.

In a particular case, I want the Web Script to return an error 500 as JSON, so I wrote a .json.500.ftl template for it.

PROBLEM: The error 500 always shows up as HTML (the default Web Script error template).
What did I do wrong?

My files:

auditlog.get.desc.xml
auditlog.get.csv.ftl
auditlog.get.json.500.ftl

auditlog.get.json.500.ftl just contains {"error": "abc is not correct"}

auditlog.get.desc.xml contains:

<webscript>
    <shortname>Audit Log Web Script</shortname>
    <description>Returns audit data for a given day</description>
    <url>/theapp/auditlog</url>
    <authentication>admin</authentication>
    <format default="csv"></format>
</webscript>

All of these files are in tomcat/webapps/alfresco/WEB-INF/classes/alfresco/extension/templates/webscripts/ and I have restarted Alfresco.

Community
  • 1
  • 1
Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
  • Try to call like this, http://localhost:8080/alfresco/service/theapp/auditlog?format=json I added the format=json at the last. But it may affect the original request param of format. – Muralidharan Deenathayalan Mar 08 '17 at 07:39
  • @MuralidharanDeenathayalan: That correctly returns the JSON. I was hoping to get CSV if everything goes well, but get JSON only in case of error 500... maybe Web Script technology is not meant to do that? – Nicolas Raoul Mar 08 '17 at 07:54
  • 1
    If you're using Java backed webscript, IIRC, you can override the webscript return format to JSON. Refer `org.springframework.extensions.webscripts.DeclarativeWebScript` and `org.springframework.extensions.webscripts.DeclarativeWebScript.renderFormatTemplate(String, Map, Writer)` method. – Muralidharan Deenathayalan Mar 08 '17 at 08:02
  • @MuralidharanDeenathayalan: `DeclarativeWebScri‌​pt.renderFormatTempl‌​ate` is `final`, so I guess I must extend `AbstractWebScript`? (currently my Java code extends `DeclarativeWebScript`) – Nicolas Raoul Mar 08 '17 at 08:46

1 Answers1

2
<format default="csv"></format>

Above line will try to fetch template with "auditlog.get.csv.500.ftl" name,which is not there in your case.

It will only try to fetch different response template if you are passing different parameter in url, something like below.

"http://localhost:8080/share/service/demo/webscript?format=json"

Krutik Jayswal
  • 3,165
  • 1
  • 15
  • 38
  • So your conclusion is that my requirement (same request returns CSV is OK, JSON is KO) is not implementable without tweaking the Web Scripts system, right? – Nicolas Raoul Mar 09 '17 at 04:41
  • didn't get you. – Krutik Jayswal Mar 09 '17 at 05:37
  • First, you didn't mentions anything anout html template,2nd is if server return 500 error it will render 500.ftl, as you defined that.I think you should read about how tag works in descriptor. – Krutik Jayswal Mar 09 '17 at 06:17
  • Indeed writing `` was my mistake. Thank you! – Nicolas Raoul Mar 09 '17 at 06:36
  • The same Web Script must return CSV if things go well, but JSON if things go wrong. I can not ask people to use a different URL, because when launching the request they don't know yet whether things will go right or wrong. – Nicolas Raoul Mar 09 '17 at 06:37