Switch the Links updating webscript to be Java backed using the new service

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@30103 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Nick Burch
2011-08-26 15:14:50 +00:00
parent 43169b08e6
commit 5cd521cf65
4 changed files with 168 additions and 82 deletions

View File

@@ -1,82 +0,0 @@
<import resource="classpath:alfresco/templates/webscripts/org/alfresco/repository/requestutils.lib.js">
<import resource="classpath:alfresco/templates/webscripts/org/alfresco/repository/links/links.lib.js">
/**
* Update a link
*/
function updateLink(linkNode)
{
var tags = [];
if (json.has("tags"))
{
// get the tags JSONArray and copy it into a real javascript array object
var tmp = json.get("tags");
for (var x = 0; x < tmp.length(); x++)
{
tags.push(tmp.get(x));
}
}
var isInternal = isLinkInternal();
if (!linkNode.hasAspect("lnk:internal") && isInternal)
{
var pr = [];
pr["lnk:isInternal"] = "true";
linkNode.addAspect("lnk:internal", pr);
}
else if (linkNode.hasAspect("lnk:internal") && !isInternal)
{
linkNode.removeAspect("lnk:internal");
}
var prs = getLinkProperties();
for (var propName in prs)
{
if (propName)
{
linkNode.properties[propName] = prs[propName];
}
}
linkNode.mimetype = "text/html";
linkNode.content = linkNode.properties["lnk:url"];
linkNode.tags = tags;
linkNode.save();
model.message = "Node " + linkNode.nodeRef + " updated";
var siteId = url.templateArgs.site;
var data =
{
title: json.get("title"),
page: json.get("page") + "?linkId=" + linkNode.name
};
activities.postActivity("org.alfresco.links.link-updated", siteId, "links", jsonUtils.toJSONString(data));
return linkNode;
}
function main()
{
// get requested node
var node = getRequestNode();
if (node == null)
{
status.code = 404;
var mes = "Link not found with the supplied name";
status.message = mes;
model.message = mes;
return;
}
if (!node.hasPermission("WriteProperties") || !node.hasPermission("WriteContent"))
{
status.code = 403;
var mes = "Permission to update is denied";
status.message = mes;
model.message = mes;
return;
}
updateLink(node);
}
main();

View File

@@ -1525,6 +1525,12 @@
parent="abstractLinksWebScript"> parent="abstractLinksWebScript">
</bean> </bean>
<!-- Updates the details of one link -->
<bean id="webscript.org.alfresco.repository.links.links.put"
class="org.alfresco.repo.web.scripts.links.LinkPut"
parent="abstractLinksWebScript">
</bean>
<!-- Lists the Links for a site --> <!-- Lists the Links for a site -->
<bean id="webscript.org.alfresco.repository.links.links.get" <bean id="webscript.org.alfresco.repository.links.links.get"
class="org.alfresco.repo.web.scripts.links.LinksListGet" class="org.alfresco.repo.web.scripts.links.LinksListGet"

View File

@@ -19,7 +19,9 @@
package org.alfresco.repo.web.scripts.links; package org.alfresco.repo.web.scripts.links;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.alfresco.query.PagingRequest; import org.alfresco.query.PagingRequest;
@@ -34,6 +36,7 @@ import org.alfresco.service.cmr.site.SiteInfo;
import org.alfresco.service.cmr.site.SiteService; import org.alfresco.service.cmr.site.SiteService;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONTokener; import org.json.JSONTokener;
@@ -95,6 +98,42 @@ public abstract class AbstractLinksWebScript extends DeclarativeWebScript
return null; return null;
} }
protected List<String> getTags(JSONObject json) throws JSONException
{
List<String> tags = null;
if(json.has("tags"))
{
// Is it "tags":"" or "tags":[...] ?
if(json.get("tags") instanceof String)
{
// This is normally an empty string, skip
String tagsS = json.getString("tags");
if("".equals(tagsS))
{
// No tags were given
return null;
}
else
{
// Log, and treat as empty
// (We don't support "tags":"a,b,c" in these webscripts)
logger.warn("Unexpected tag data: " + tagsS);
return null;
}
}
else
{
tags = new ArrayList<String>();
JSONArray jsTags = json.getJSONArray("tags");
for(int i=0; i<jsTags.length(); i++)
{
tags.add( jsTags.getString(i) );
}
}
}
return tags;
}
/** /**
* Builds up a listing Paging request, based on the arguments * Builds up a listing Paging request, based on the arguments
* specified in the URL * specified in the URL

View File

@@ -0,0 +1,123 @@
/*
* Copyright (C) 2005-2011 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.web.scripts.links;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.alfresco.repo.security.permissions.AccessDeniedException;
import org.alfresco.service.cmr.links.LinkInfo;
import org.alfresco.service.cmr.site.SiteInfo;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest;
/**
* This class is the controller for the link fetching links.put webscript.
*
* @author Nick Burch
* @since 4.0
*/
public class LinkPut extends AbstractLinksWebScript
{
private static final String PARAM_MESSAGE = "message";
@Override
protected Map<String, Object> executeImpl(SiteInfo site, String linkName,
WebScriptRequest req, JSONObject json, Status status, Cache cache) {
Map<String, Object> model = new HashMap<String, Object>();
// Try to find the link
LinkInfo link = linksService.getLink(site.getShortName(), linkName);
if(link == null)
{
String message = "No link found with that name";
status.setCode(Status.STATUS_NOT_FOUND);
status.setMessage(message);
model.put(PARAM_MESSAGE, message);
return model;
}
// Get the new link details from the JSON
try
{
// Update the main properties
link.setTitle(getOrNull(json, "title"));
link.setDescription(getOrNull(json, "description"));
link.setURL(getOrNull(json, "url"));
// Handle internal / not internal
if(json.has("internal"))
{
link.setInternal(true);
}
else
{
link.setInternal(false);
}
// Do the tags
link.getTags().clear();
List<String> tags = getTags(json);
if(tags != null && tags.size() > 0)
{
link.getTags().addAll(tags);
}
}
catch(JSONException je)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Invalid JSON: " + je.getMessage());
}
// Update the link
try
{
link = linksService.updateLink(link);
}
catch(AccessDeniedException e)
{
String message = "You don't have permission to update that link";
status.setCode(Status.STATUS_FORBIDDEN);
status.setMessage(message);
model.put(PARAM_MESSAGE, message);
return model;
}
// Generate an activity for the change
addActivityEntry("updated", link, site, req, json);
// Build the model
model.put(PARAM_MESSAGE, "Node " + link.getNodeRef() + " updated");
model.put("link", link);
model.put("site", site);
model.put("siteId", site.getShortName());
// All done
return model;
}
}