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

@@ -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);

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
model.put("item", renderLink(link));
model.put(PARAM_ITEM, renderLink(link));
model.put("node", link.getNodeRef());
model.put("link", link);
model.put("site", site);

View File

@@ -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<String, Object> executeImpl(SiteInfo site, String linkName,
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
{
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) {
@@ -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);

View File

@@ -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<String> 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);
}
/**