10-31-2014 05:57 AM
[{"Name":"Matrix - Connectivity problems","Problem":"Terminal won't connect\nConnection error"}]
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;
}
}
<#escape x as jsonUtils.encodeJSONString(x)>
{
"Results" : [
<#list results as result>
{
"Title" : "${result.Name}",
"Problem : "${result.Problem}"
},
</#list>
]
}
</#escape>
freemarker.template.TemplateException: Expected collection or sequence. results evaluated instead to freemarker.ext.beans.StringModel on line 4
10-31-2014 08:52 AM
10-31-2014 10:41 AM
package com.x.y.z;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
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.json.JSONObject;
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) {
Map<String, Object> model = new HashMap<>();
List<Map<String, String>> results = new ArrayList<>();
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 = "/mySearch?query=" + query;
final Response response = conn.call(url);
if (response.getStatus().getCode() == Status.STATUS_OK) {
JSONArray resultsResponse = new JSONArray(response.getResponse());
for (int i = 0; i < resultsResponse.length(); i++) {
JSONObject result = resultsResponse.getJSONObject(i);
Map<String, String> resultObject = new HashMap<>();
String title = result.getString("Name");
resultObject.put("ShareTitle", title);
String problem = result.getString("Problem");
resultObject.put("ShareProblem", problem);
results.add(resultObject);
}
model.put("results", results);
}
} 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;
}
}
}
<#escape x as jsonUtils.encodeJSONString(x)>
{
"Results" : [
<#list results as result>
{
"Title" : "${result.ShareTitle}",
"Problem : "${result.ShareProblem}"
},
</#list>
]
}
</#escape>
05-19-2016 12:18 PM
05-20-2016 03:38 AM
05-20-2016 10:15 AM
07-21-2016 07:09 AM
Tags
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.