From e921389cef46995fae355a29e7a84999eaf27924 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Fri, 26 Aug 2011 16:08:55 +0000 Subject: [PATCH] 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 --- .../links/link/link.delete.desc.xml | 8 ++ .../links/link/link.delete.json.ftl | 0 .../links/links-delete.post.json.js | 119 ----------------- .../web-scripts-application-context.xml | 12 ++ .../scripts/links/AbstractLinksWebScript.java | 3 + .../repo/web/scripts/links/LinkDelete.java | 69 ++++++++++ .../repo/web/scripts/links/LinkGet.java | 2 +- .../repo/web/scripts/links/LinkPut.java | 2 - .../web/scripts/links/LinksDeletePost.java | 121 ++++++++++++++++++ .../repo/web/scripts/links/LinksPost.java | 4 +- .../web/scripts/links/LinksRestApiTest.java | 39 +++++- 11 files changed, 251 insertions(+), 128 deletions(-) create mode 100644 config/alfresco/templates/webscripts/org/alfresco/repository/links/link/link.delete.desc.xml create mode 100644 config/alfresco/templates/webscripts/org/alfresco/repository/links/link/link.delete.json.ftl delete mode 100644 config/alfresco/templates/webscripts/org/alfresco/repository/links/links-delete.post.json.js create mode 100644 source/java/org/alfresco/repo/web/scripts/links/LinkDelete.java create mode 100644 source/java/org/alfresco/repo/web/scripts/links/LinksDeletePost.java diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/links/link/link.delete.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/repository/links/link/link.delete.desc.xml new file mode 100644 index 0000000000..ef980eb4b2 --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/links/link/link.delete.desc.xml @@ -0,0 +1,8 @@ + + Delete link + Deletes a link. + /api/links/link/site/{site}/{container}/{path} + argument + user + required + diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/links/link/link.delete.json.ftl b/config/alfresco/templates/webscripts/org/alfresco/repository/links/link/link.delete.json.ftl new file mode 100644 index 0000000000..e69de29bb2 diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/links/links-delete.post.json.js b/config/alfresco/templates/webscripts/org/alfresco/repository/links/links-delete.post.json.js deleted file mode 100644 index 22a202d408..0000000000 --- a/config/alfresco/templates/webscripts/org/alfresco/repository/links/links-delete.post.json.js +++ /dev/null @@ -1,119 +0,0 @@ - - - - -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(); diff --git a/config/alfresco/web-scripts-application-context.xml b/config/alfresco/web-scripts-application-context.xml index 8d7922afed..b486d57005 100644 --- a/config/alfresco/web-scripts-application-context.xml +++ b/config/alfresco/web-scripts-application-context.xml @@ -1537,11 +1537,23 @@ parent="abstractLinksWebScript"> + + + + + + + + diff --git a/source/java/org/alfresco/repo/web/scripts/links/AbstractLinksWebScript.java b/source/java/org/alfresco/repo/web/scripts/links/AbstractLinksWebScript.java index ac6df0ecdf..d63342fbc5 100644 --- a/source/java/org/alfresco/repo/web/scripts/links/AbstractLinksWebScript.java +++ b/source/java/org/alfresco/repo/web/scripts/links/AbstractLinksWebScript.java @@ -53,6 +53,9 @@ import org.springframework.extensions.webscripts.WebScriptRequest; public abstract class AbstractLinksWebScript extends DeclarativeWebScript { 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); diff --git a/source/java/org/alfresco/repo/web/scripts/links/LinkDelete.java b/source/java/org/alfresco/repo/web/scripts/links/LinkDelete.java new file mode 100644 index 0000000000..2e7bda2fba --- /dev/null +++ b/source/java/org/alfresco/repo/web/scripts/links/LinkDelete.java @@ -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 . + */ +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 executeImpl(SiteInfo site, String linkName, + WebScriptRequest req, JSONObject json, Status status, Cache cache) { + Map model = new HashMap(); + + // 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; + } +} diff --git a/source/java/org/alfresco/repo/web/scripts/links/LinkGet.java b/source/java/org/alfresco/repo/web/scripts/links/LinkGet.java index 1cd2f66792..1a202e45d9 100644 --- a/source/java/org/alfresco/repo/web/scripts/links/LinkGet.java +++ b/source/java/org/alfresco/repo/web/scripts/links/LinkGet.java @@ -51,7 +51,7 @@ public class LinkGet extends AbstractLinksWebScript } // Build the model - model.put("item", renderLink(link)); + model.put(PARAM_ITEM, renderLink(link)); model.put("node", link.getNodeRef()); model.put("link", link); model.put("site", site); diff --git a/source/java/org/alfresco/repo/web/scripts/links/LinkPut.java b/source/java/org/alfresco/repo/web/scripts/links/LinkPut.java index 78837b5c2a..52ebe8b6cc 100644 --- a/source/java/org/alfresco/repo/web/scripts/links/LinkPut.java +++ b/source/java/org/alfresco/repo/web/scripts/links/LinkPut.java @@ -40,8 +40,6 @@ import org.springframework.extensions.webscripts.WebScriptRequest; */ public class LinkPut extends AbstractLinksWebScript { - private static final String PARAM_MESSAGE = "message"; - @Override protected Map executeImpl(SiteInfo site, String linkName, WebScriptRequest req, JSONObject json, Status status, Cache cache) { diff --git a/source/java/org/alfresco/repo/web/scripts/links/LinksDeletePost.java b/source/java/org/alfresco/repo/web/scripts/links/LinksDeletePost.java new file mode 100644 index 0000000000..25a2e8f198 --- /dev/null +++ b/source/java/org/alfresco/repo/web/scripts/links/LinksDeletePost.java @@ -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 . + */ +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 executeImpl(SiteInfo site, String linkName, + WebScriptRequest req, JSONObject json, Status status, Cache cache) { + Map model = new HashMap(); + + // Get the requested nodes from the JSON + // Silently skips over any invalid ones specified + List links = new ArrayList(); + try + { + if(json.has("items")) + { + JSONArray items = json.getJSONArray("items"); + for(int i=0; i executeImpl(SiteInfo site, String linkName, WebScriptRequest req, JSONObject json, Status status, Cache cache) { @@ -101,7 +99,7 @@ public class LinksPost extends AbstractLinksWebScript // Build the model 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("link", link); model.put("site", site); diff --git a/source/java/org/alfresco/repo/web/scripts/links/LinksRestApiTest.java b/source/java/org/alfresco/repo/web/scripts/links/LinksRestApiTest.java index 560d685f94..0681633dcf 100644 --- a/source/java/org/alfresco/repo/web/scripts/links/LinksRestApiTest.java +++ b/source/java/org/alfresco/repo/web/scripts/links/LinksRestApiTest.java @@ -18,7 +18,9 @@ */ package org.alfresco.repo.web.scripts.links; +import java.util.Arrays; import java.util.Date; +import java.util.List; import javax.transaction.UserTransaction; @@ -43,6 +45,7 @@ import org.apache.commons.logging.LogFactory; import org.json.JSONArray; import org.json.JSONObject; 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.PostRequest; import org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest; @@ -298,9 +301,29 @@ public class LinksRestApiTest extends BaseWebScriptTest * Deletes the link */ 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 names, int expectedStatus) throws Exception { JSONArray items = new JSONArray(); - items.put(name); + for(String name : names) + { + items.put(name); + } JSONObject json = new JSONObject(); json.put("items", items); @@ -482,7 +505,7 @@ public class LinksRestApiTest extends BaseWebScriptTest // Delete - link = deleteLink(name, Status.STATUS_OK); + link = deleteLinks(Arrays.asList(new String[]{name}), Status.STATUS_OK); assertEquals( "Incorrect JSON: " + link.toString(), true, link.has("message") @@ -498,11 +521,21 @@ public class LinksRestApiTest extends BaseWebScriptTest // 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 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); } /**