diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/links/links.put.json.js b/config/alfresco/templates/webscripts/org/alfresco/repository/links/links.put.json.js deleted file mode 100644 index bf0d5b7686..0000000000 --- a/config/alfresco/templates/webscripts/org/alfresco/repository/links/links.put.json.js +++ /dev/null @@ -1,82 +0,0 @@ - - - -/** - * 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(); diff --git a/config/alfresco/web-scripts-application-context.xml b/config/alfresco/web-scripts-application-context.xml index 38ebb68d63..e2daeb8b50 100644 --- a/config/alfresco/web-scripts-application-context.xml +++ b/config/alfresco/web-scripts-application-context.xml @@ -1525,6 +1525,12 @@ parent="abstractLinksWebScript"> + + + + getTags(JSONObject json) throws JSONException + { + List 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(); + JSONArray jsTags = json.getJSONArray("tags"); + for(int i=0; i. + */ +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 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"; + + 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 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; + } +}