Hello,I have a web script in Share that calls a repo web script which returns some JSON. I want to use this JSON in a FreeMarker template to build up a page but I'm having some problems. The repo side seems to be working absolutely fine. Here is an example of the JSON it returns:
[{"Name":"Matrix - Connectivity problems","Problem":"Terminal won't connect\nConnection error"}]
This is my Share side Java controller which calls the repo web script and puts the results into the model.
package com.x.y.z;
import java.util.HashMap;
import java.util.Map;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.util.ParameterCheck;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.springframework.extensions.surf.RequestContext;
import org.springframework.extensions.surf.ServletUtil;
import org.springframework.extensions.surf.exception.ConnectorServiceException;
import org.springframework.extensions.surf.support.ThreadLocalRequestContext;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.DeclarativeWebScript;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptSession;
import org.springframework.extensions.webscripts.connector.Connector;
import org.springframework.extensions.webscripts.connector.Response;
public class ShareWebScript extends DeclarativeWebScript {
@Override
protected Map<String,Object> executeImpl(WebScriptRequest req, Status status) {
return executeImpl(req, status, null);
}
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
String query = getParameterAsString(req, "query");
Map<String, Object> model = new HashMap<>();
try {
final RequestContext rc = ThreadLocalRequestContext.getRequestContext();
final String userId = rc.getUserId();
final Connector conn = rc.getServiceRegistry().getConnectorService().getConnector("alfresco", userId, ServletUtil.getSession());
final String url = "/repo/search?query=" + query;
final Response response = conn.call(url);
if (response.getStatus().getCode() == Status.STATUS_OK) {
JSONArray resultsResponse = new JSONArray(response.getResponse());
model.put("results", resultsResponse);
}
} catch (final ConnectorServiceException e)
{
throw new AlfrescoRuntimeException("Failed to connect repository", e);
} catch (final JSONException e)
{
throw new AlfrescoRuntimeException("Failed to parse JSON string", e);
}
return model;
}
private String getParameterAsString(final WebScriptRequest req, final String paramName) {
ParameterCheck.mandatory("req", req);
ParameterCheck.mandatoryString("paramName", paramName);
String paramVal = StringUtils.trimToNull(req.getParameter(paramName));
if (StringUtils.isBlank(paramVal)) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Missing " + paramName + " parameter");
}
return paramVal;
}
}
This is the FTL:
<#escape x as jsonUtils.encodeJSONString(x)>
{
"Results" : [
<#list results as result>
{
"Title" : "${result.Name}",
"Problem : "${result.Problem}"
},
</#list>
]
}
</#escape>
And the error I'm getting is:
freemarker.template.TemplateException: Expected collection or sequence. results evaluated instead to freemarker.ext.beans.StringModel on line 4
I have remotely debugged the application and can see that the JSONArray I create from the response in the controller is correctly formed. I have attached a screenshot showing this.I'm sure I'm doing something simple wrong but I can't figure it out.