Improve the Links rest api testing for deletion, and port the delete webscript from JS to Java backed (using the service)

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@30109 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Nick Burch
2011-08-26 16:08:55 +00:00
parent c252a3e4b5
commit e921389cef
11 changed files with 251 additions and 128 deletions

View File

@@ -0,0 +1,8 @@
<webscript>
<shortname>Delete link</shortname>
<description>Deletes a link.</description>
<url>/api/links/link/site/{site}/{container}/{path}</url>
<format default="json">argument</format>
<authentication>user</authentication>
<transaction>required</transaction>
</webscript>

View File

@@ -1,119 +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">
function getRequestNodes()
{
var siteId = url.templateArgs.site;
var containerId = url.templateArgs.container;
var items = [];
var nodes = [];
if (json.has("items"))
{
var tmp = json.get("items");
for (var x=0; x < tmp.length(); x++)
{
items.push(tmp.get(x));
}
}
// fetch site
var site = siteService.getSite(siteId);
if (site === null)
{
status.setCode(status.STATUS_NOT_FOUND, "Site " + siteId + " does not exist");
return null;
}
// fetch container
var node = site.getContainer(containerId);
if (node === null)
{
node = site.createContainer(containerId);
if (node === null)
{
status.setCode(status.STATUS_NOT_FOUND, "Unable to fetch container '" + containerId + "' of site '" + siteId + "'. (No write permission?)");
return null;
}
}
for (var i in items)
{
if (i)
{
var tmpNode = node.childByNamePath(items[i]);
if (tmpNode)
{
nodes.push(tmpNode);
}
}
}
return nodes;
}
/**
* Deletes a link node
*/
function deleteLink(linkNode)
{
// delete the node
var nodeRef = linkNode.nodeRef;
var linkData = getLinksData(linkNode);
var isDeleted = linkNode.remove();
if (! isDeleted)
{
var mes = "Unable to delete node: " + nodeRef;
status.setCode(status.STATUS_INTERNAL_SERVER_ERROR, mes);
model.message = mes;
return;
}
model.message = "Node " + nodeRef + " deleted";
var siteId = url.templateArgs.site;
var data =
{
title: linkData.title,
page: "links"
};
activities.postActivity("org.alfresco.links.link-deleted", siteId, "links", jsonUtils.toJSONString(data));
}
function main()
{
// get requested node
var nodes = getRequestNodes();
if (status.getCode() != status.STATUS_OK)
{
return;
}
if (nodes == null || nodes.length == 0)
{
status.code = 404;
var mess = "No valid link names supplied";
status.message = mess;
model.message = mess;
return;
}
for (var i in nodes)
{
if (i)
{
if (!nodes[i].hasPermission("Delete"))
{
status.code = 403;
var mes = "Permission to delete is denied";
status.message = mes;
model.message = mes;
return;
}
deleteLink(nodes[i]);
}
}
}
main();

View File

@@ -1537,12 +1537,24 @@
parent="abstractLinksWebScript"> parent="abstractLinksWebScript">
</bean> </bean>
<!-- Deletes a single link -->
<bean id="webscript.org.alfresco.repository.links.link.link.delete"
class="org.alfresco.repo.web.scripts.links.LinkDelete"
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"
parent="abstractLinksWebScript"> parent="abstractLinksWebScript">
</bean> </bean>
<!-- Deletes multiple links in a site -->
<bean id="webscript.org.alfresco.repository.links.links-delete.post"
class="org.alfresco.repo.web.scripts.links.LinksDeletePost"
parent="abstractLinksWebScript">
</bean>
<!-- --> <!-- -->
<!-- Wiki Pages REST API --> <!-- Wiki Pages REST API -->

View File

@@ -54,6 +54,9 @@ public abstract class AbstractLinksWebScript extends DeclarativeWebScript
{ {
public static final String LINKS_SERVICE_ACTIVITY_APP_NAME = "links"; public static final String LINKS_SERVICE_ACTIVITY_APP_NAME = "links";
protected static final String PARAM_MESSAGE = "message";
protected static final String PARAM_ITEM = "item";
private static Log logger = LogFactory.getLog(AbstractLinksWebScript.class); private static Log logger = LogFactory.getLog(AbstractLinksWebScript.class);
// Injected services // Injected services

View File

@@ -0,0 +1,69 @@
/*
* 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.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.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 deleting link.delete webscript.
*
* @author Nick Burch
* @since 4.0
*/
public class LinkDelete extends AbstractLinksWebScript
{
@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";
throw new WebScriptException(Status.STATUS_NOT_FOUND, message);
}
// Delete it
try
{
linksService.deleteLink(link);
}
catch(AccessDeniedException e)
{
String message = "You don't have permission to delete that link";
throw new WebScriptException(Status.STATUS_FORBIDDEN, message);
}
// Mark it as gone
status.setCode(Status.STATUS_NO_CONTENT);
return model;
}
}

View File

@@ -51,7 +51,7 @@ public class LinkGet extends AbstractLinksWebScript
} }
// Build the model // Build the model
model.put("item", renderLink(link)); model.put(PARAM_ITEM, renderLink(link));
model.put("node", link.getNodeRef()); model.put("node", link.getNodeRef());
model.put("link", link); model.put("link", link);
model.put("site", site); model.put("site", site);

View File

@@ -40,8 +40,6 @@ import org.springframework.extensions.webscripts.WebScriptRequest;
*/ */
public class LinkPut extends AbstractLinksWebScript public class LinkPut extends AbstractLinksWebScript
{ {
private static final String PARAM_MESSAGE = "message";
@Override @Override
protected Map<String, Object> executeImpl(SiteInfo site, String linkName, protected Map<String, Object> executeImpl(SiteInfo site, String linkName,
WebScriptRequest req, JSONObject json, Status status, Cache cache) { WebScriptRequest req, JSONObject json, Status status, Cache cache) {

View File

@@ -0,0 +1,121 @@
/*
* 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.ArrayList;
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.JSONArray;
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 links deleting links-delete.post webscript.
*
* @author Nick Burch
* @since 4.0
*/
public class LinksDeletePost extends AbstractLinksWebScript
{
protected static final int RECENT_SEARCH_PERIOD_DAYS = 7;
protected static final long ONE_DAY_MS = 24*60*60*1000;
@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>();
// Get the requested nodes from the JSON
// Silently skips over any invalid ones specified
List<LinkInfo> links = new ArrayList<LinkInfo>();
try
{
if(json.has("items"))
{
JSONArray items = json.getJSONArray("items");
for(int i=0; i<items.length(); i++)
{
String name = items.getString(i);
LinkInfo link = linksService.getLink(site.getShortName(), name);
if(link != null)
{
links.add(link);
}
}
}
}
catch(JSONException je)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Invalid JSON: " + je.getMessage());
}
// Check we got at least one link, and bail if not
if(links.size() == 0)
{
String message = "No valid link names supplied";
status.setCode(Status.STATUS_NOT_FOUND);
status.setMessage(message);
model.put(PARAM_MESSAGE, message);
return model;
}
// Delete each one in turn
for(LinkInfo link : links)
{
// Do the delete
try
{
linksService.deleteLink(link);
}
catch(AccessDeniedException e)
{
String message = "You don't have permission to delete the link with name '" + link.getSystemName() + "'";
status.setCode(Status.STATUS_FORBIDDEN);
status.setMessage(message);
model.put(PARAM_MESSAGE, message);
return model;
}
// Generate the activity entry for it
addActivityEntry("deleted", link, site, req, json);
// Record a message (only the last one is used though!)
model.put(PARAM_MESSAGE, "Node " + link.getNodeRef() + " deleted");
}
// All done
model.put("siteId", site.getShortName());
model.put("site", site);
return model;
}
}

View File

@@ -40,8 +40,6 @@ import org.springframework.extensions.webscripts.WebScriptRequest;
*/ */
public class LinksPost extends AbstractLinksWebScript public class LinksPost extends AbstractLinksWebScript
{ {
private static final String PARAM_MESSAGE = "message";
@Override @Override
protected Map<String, Object> executeImpl(SiteInfo site, String linkName, protected Map<String, Object> executeImpl(SiteInfo site, String linkName,
WebScriptRequest req, JSONObject json, Status status, Cache cache) { WebScriptRequest req, JSONObject json, Status status, Cache cache) {
@@ -101,7 +99,7 @@ public class LinksPost extends AbstractLinksWebScript
// Build the model // Build the model
model.put(PARAM_MESSAGE, link.getSystemName()); // Really! model.put(PARAM_MESSAGE, link.getSystemName()); // Really!
model.put("item", renderLink(link)); model.put(PARAM_ITEM, renderLink(link));
model.put("node", link.getNodeRef()); model.put("node", link.getNodeRef());
model.put("link", link); model.put("link", link);
model.put("site", site); model.put("site", site);

View File

@@ -18,7 +18,9 @@
*/ */
package org.alfresco.repo.web.scripts.links; package org.alfresco.repo.web.scripts.links;
import java.util.Arrays;
import java.util.Date; import java.util.Date;
import java.util.List;
import javax.transaction.UserTransaction; import javax.transaction.UserTransaction;
@@ -43,6 +45,7 @@ import org.apache.commons.logging.LogFactory;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import org.springframework.extensions.webscripts.Status; import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.TestWebScriptServer.DeleteRequest;
import org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest; import org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest;
import org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest; import org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest;
import org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest; import org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest;
@@ -298,9 +301,29 @@ public class LinksRestApiTest extends BaseWebScriptTest
* Deletes the link * Deletes the link
*/ */
private JSONObject deleteLink(String name, int expectedStatus) throws Exception private JSONObject deleteLink(String name, int expectedStatus) throws Exception
{
Response response = sendRequest(new DeleteRequest(URL_LINKS_FETCH+name), expectedStatus);
if (expectedStatus == Status.STATUS_OK)
{
JSONObject result = new JSONObject(response.getContentAsString());
return result;
}
else
{
return null;
}
}
/**
* Deletes the links
*/
private JSONObject deleteLinks(List<String> names, int expectedStatus) throws Exception
{ {
JSONArray items = new JSONArray(); JSONArray items = new JSONArray();
for(String name : names)
{
items.put(name); items.put(name);
}
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("items", items); json.put("items", items);
@@ -482,7 +505,7 @@ public class LinksRestApiTest extends BaseWebScriptTest
// Delete // Delete
link = deleteLink(name, Status.STATUS_OK); link = deleteLinks(Arrays.asList(new String[]{name}), Status.STATUS_OK);
assertEquals( assertEquals(
"Incorrect JSON: " + link.toString(), "Incorrect JSON: " + link.toString(),
true, link.has("message") true, link.has("message")
@@ -498,11 +521,21 @@ public class LinksRestApiTest extends BaseWebScriptTest
// Can't delete again // Can't delete again
deleteLink(name, Status.STATUS_NOT_FOUND); deleteLinks(Arrays.asList(new String[]{name}), Status.STATUS_NOT_FOUND);
// Can't edit it when it's deleted // Can't edit it when it's deleted
sendRequest(new PutRequest(URL_LINKS_UPDATE + name, "{}", "application/json"), Status.STATUS_NOT_FOUND); sendRequest(new PutRequest(URL_LINKS_UPDATE + name, "{}", "application/json"), Status.STATUS_NOT_FOUND);
// Do a single delete
link = createLink(LINK_TITLE_ONE, "Thing 1", LINK_URL_ONE, false, Status.STATUS_OK);
name = getNameFromLink(link);
getLink(name, Status.STATUS_OK);
deleteLink(name, Status.STATUS_NO_CONTENT);
getLink(name, Status.STATUS_NOT_FOUND);
deleteLink(name, Status.STATUS_NOT_FOUND);
} }
/** /**