mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-14 17:58:59 +00:00
Checkin for RSOLR 031: "Remote API to get and compare models"
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@28472 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
<webscript>
|
||||
<shortname>Get Model</shortname>
|
||||
<description>Get the model for given model name.</description>
|
||||
<url>/api/solr/model/{modelShortQName}</url>
|
||||
<format default="json">argument</format>
|
||||
<authentication>admin</authentication>
|
||||
<transaction allow="readonly">required</transaction>
|
||||
<lifecycle>internal</lifecycle>
|
||||
<family>SOLR</family>
|
||||
<args>
|
||||
</args>
|
||||
</webscript>
|
@@ -0,0 +1,12 @@
|
||||
<webscript>
|
||||
<shortname>Get Model Diffs</shortname>
|
||||
<description>Get a diff of models given parameters passed in.</description>
|
||||
<url>/api/solr/modelsdiff</url>
|
||||
<format default="json">argument</format>
|
||||
<authentication>admin</authentication>
|
||||
<transaction allow="readonly">required</transaction>
|
||||
<lifecycle>internal</lifecycle>
|
||||
<family>SOLR</family>
|
||||
<args>
|
||||
</args>
|
||||
</webscript>
|
@@ -0,0 +1,14 @@
|
||||
<#import "solr.lib.ftl" as solrLib/>
|
||||
{
|
||||
"diffs" :
|
||||
[
|
||||
<#list diffs as diff>
|
||||
{
|
||||
"name": <@solrLib.qNameJSON qName=diff.modelName/>,
|
||||
"type" : "${diff.type}",
|
||||
"oldChecksum": <#if diff.oldChecksum??>${diff.oldChecksum?c}<#else>null</#if>,
|
||||
"newChecksum": <#if diff.newChecksum??>${diff.newChecksum?c}<#else>null</#if>
|
||||
}<#if diff_has_next>,</#if>
|
||||
</#list>
|
||||
]
|
||||
}
|
@@ -1355,6 +1355,19 @@
|
||||
<property name="nodeDAO" ref="nodeDAO"/>
|
||||
</bean>
|
||||
|
||||
<bean id="webscript.org.alfresco.repository.solr.model.get"
|
||||
class="org.alfresco.repo.web.scripts.solr.AlfrescoModelGet"
|
||||
parent="webscript">
|
||||
<property name="solrTrackingComponent" ref="solrTrackingComponent"/>
|
||||
<property name="namespaceService" ref="namespaceService"/>
|
||||
</bean>
|
||||
|
||||
<bean id="webscript.org.alfresco.repository.solr.modelsdiff.post"
|
||||
class="org.alfresco.repo.web.scripts.solr.AlfrescoModelsDiff"
|
||||
parent="webscript">
|
||||
<property name="solrTrackingComponent" ref="solrTrackingComponent"/>
|
||||
</bean>
|
||||
|
||||
<!-- -->
|
||||
<!-- Node Locator -->
|
||||
<!-- -->
|
||||
|
@@ -0,0 +1,76 @@
|
||||
package org.alfresco.repo.web.scripts.solr;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.repo.solr.AlfrescoModel;
|
||||
import org.alfresco.repo.solr.SOLRTrackingComponent;
|
||||
import org.alfresco.service.cmr.dictionary.ModelDefinition;
|
||||
import org.alfresco.service.namespace.NamespaceService;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.JSONException;
|
||||
import org.springframework.extensions.webscripts.AbstractWebScript;
|
||||
import org.springframework.extensions.webscripts.Status;
|
||||
import org.springframework.extensions.webscripts.WebScriptException;
|
||||
import org.springframework.extensions.webscripts.WebScriptRequest;
|
||||
import org.springframework.extensions.webscripts.WebScriptResponse;
|
||||
|
||||
/**
|
||||
* Support for SOLR: Get Alfresco model
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public class AlfrescoModelGet extends AbstractWebScript
|
||||
{
|
||||
protected static final Log logger = LogFactory.getLog(AlfrescoModelGet.class);
|
||||
|
||||
private NamespaceService namespaceService;
|
||||
private SOLRTrackingComponent solrTrackingComponent;
|
||||
|
||||
public void setSolrTrackingComponent(SOLRTrackingComponent solrTrackingComponent)
|
||||
{
|
||||
this.solrTrackingComponent = solrTrackingComponent;
|
||||
}
|
||||
|
||||
public void setNamespaceService(NamespaceService namespaceService)
|
||||
{
|
||||
this.namespaceService = namespaceService;
|
||||
}
|
||||
|
||||
public void execute(WebScriptRequest req, WebScriptResponse res)
|
||||
{
|
||||
try
|
||||
{
|
||||
handle(req, res);
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
throw new WebScriptException("IO exception parsing request", e);
|
||||
}
|
||||
catch(JSONException e)
|
||||
{
|
||||
throw new WebScriptException("Invalid JSON", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void handle(WebScriptRequest req, WebScriptResponse res) throws JSONException, IOException
|
||||
{
|
||||
// create map of template vars
|
||||
Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
|
||||
String modelName = templateVars.get("modelShortQName");
|
||||
if(modelName == null)
|
||||
{
|
||||
throw new WebScriptException(
|
||||
Status.STATUS_BAD_REQUEST,
|
||||
"URL parameter 'modelShortQName' not provided.");
|
||||
}
|
||||
|
||||
ModelDefinition.XMLBindingType bindingType = ModelDefinition.XMLBindingType.SOLR;
|
||||
AlfrescoModel model = solrTrackingComponent.getModel(QName.createQName(modelName, namespaceService));
|
||||
res.setHeader("XAlfresco-modelChecksum", String.valueOf(model.getModelDef().getChecksum(bindingType)));
|
||||
model.getModelDef().toXML(bindingType, res.getOutputStream());
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,87 @@
|
||||
package org.alfresco.repo.web.scripts.solr;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.repo.solr.AlfrescoModelDiff;
|
||||
import org.alfresco.repo.solr.SOLRTrackingComponent;
|
||||
import org.alfresco.service.namespace.QName;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.springframework.extensions.surf.util.Content;
|
||||
import org.springframework.extensions.webscripts.DeclarativeWebScript;
|
||||
import org.springframework.extensions.webscripts.Status;
|
||||
import org.springframework.extensions.webscripts.WebScriptException;
|
||||
import org.springframework.extensions.webscripts.WebScriptRequest;
|
||||
|
||||
/**
|
||||
* Support for SOLR: Track Alfresco model changes
|
||||
*
|
||||
* @since 4.0
|
||||
*/
|
||||
public class AlfrescoModelsDiff extends DeclarativeWebScript
|
||||
{
|
||||
protected static final Log logger = LogFactory.getLog(AlfrescoModelsDiff.class);
|
||||
|
||||
private SOLRTrackingComponent solrTrackingComponent;
|
||||
|
||||
public void setSolrTrackingComponent(SOLRTrackingComponent solrTrackingComponent)
|
||||
{
|
||||
this.solrTrackingComponent = solrTrackingComponent;
|
||||
}
|
||||
|
||||
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status)
|
||||
{
|
||||
try
|
||||
{
|
||||
Map<String, Object> model = buildModel(req);
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("Result: \n\tRequest: " + req + "\n\tModel: " + model);
|
||||
}
|
||||
return model;
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
throw new WebScriptException("IO exception parsing request", e);
|
||||
}
|
||||
catch(JSONException e)
|
||||
{
|
||||
throw new WebScriptException("Invalid JSON", e);
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, Object> buildModel(WebScriptRequest req) throws JSONException, IOException
|
||||
{
|
||||
Map<String, Object> model = new HashMap<String, Object>(1, 1.0f);
|
||||
|
||||
Content content = req.getContent();
|
||||
if(content == null)
|
||||
{
|
||||
throw new WebScriptException("Failed to convert request to String");
|
||||
}
|
||||
JSONObject o = new JSONObject(content.getContent());
|
||||
JSONArray jsonModels = o.getJSONArray("models");
|
||||
Map<QName, Long> models = new HashMap<QName, Long>(jsonModels.length());
|
||||
for(int i = 0; i < jsonModels.length(); i++)
|
||||
{
|
||||
JSONObject jsonModel = jsonModels.getJSONObject(i);
|
||||
models.put(QName.createQName(jsonModel.getString("name")), jsonModel.getLong("checksum"));
|
||||
}
|
||||
|
||||
List<AlfrescoModelDiff> diffs = solrTrackingComponent.getModelDiffs(models);
|
||||
model.put("diffs", diffs);
|
||||
|
||||
if (logger.isDebugEnabled())
|
||||
{
|
||||
logger.debug("Result: \n\tRequest: " + req + "\n\tModel: " + model);
|
||||
}
|
||||
|
||||
return model;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user