diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/publishing/channel.put.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/repository/publishing/channel.put.desc.xml new file mode 100644 index 0000000000..ef61ff3b12 --- /dev/null +++ b/config/alfresco/templates/webscripts/org/alfresco/repository/publishing/channel.put.desc.xml @@ -0,0 +1,27 @@ + + Update Channel + Updates an existing Channel's name. + /api/publishing/channels/{channel_id} + + user + required + public_api + + + channel_id + The URL-encoded id of the channel to be updated. + + + + + json + + + + + + \ No newline at end of file diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/publishing/publishing-events-for-node.get.desc.xml b/config/alfresco/templates/webscripts/org/alfresco/repository/publishing/publishing-events.get.desc.xml similarity index 100% rename from config/alfresco/templates/webscripts/org/alfresco/repository/publishing/publishing-events-for-node.get.desc.xml rename to config/alfresco/templates/webscripts/org/alfresco/repository/publishing/publishing-events.get.desc.xml diff --git a/config/alfresco/templates/webscripts/org/alfresco/repository/publishing/publishing-events-for-node.get.json.ftl b/config/alfresco/templates/webscripts/org/alfresco/repository/publishing/publishing-events.get.json.ftl similarity index 100% rename from config/alfresco/templates/webscripts/org/alfresco/repository/publishing/publishing-events-for-node.get.json.ftl rename to config/alfresco/templates/webscripts/org/alfresco/repository/publishing/publishing-events.get.json.ftl diff --git a/config/alfresco/web-scripts-application-context.xml b/config/alfresco/web-scripts-application-context.xml index f0cda6c1c9..5e456a6dfc 100644 --- a/config/alfresco/web-scripts-application-context.xml +++ b/config/alfresco/web-scripts-application-context.xml @@ -1110,6 +1110,13 @@ + + + + + - diff --git a/source/java/org/alfresco/repo/web/scripts/publishing/ChannelPut.java b/source/java/org/alfresco/repo/web/scripts/publishing/ChannelPut.java new file mode 100644 index 0000000000..b99ee1028c --- /dev/null +++ b/source/java/org/alfresco/repo/web/scripts/publishing/ChannelPut.java @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2005-2010 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.publishing; + +import java.util.Map; + +import javax.servlet.http.HttpServletResponse; + +import org.alfresco.repo.web.scripts.WebScriptUtil; +import org.alfresco.service.cmr.publishing.channels.Channel; +import org.alfresco.service.cmr.publishing.channels.ChannelService; +import org.springframework.extensions.surf.util.URLDecoder; +import org.springframework.extensions.webscripts.AbstractWebScript; +import org.springframework.extensions.webscripts.WebScriptException; +import org.springframework.extensions.webscripts.WebScriptRequest; +import org.springframework.extensions.webscripts.WebScriptResponse; + +/** + * @author Nick Smith + * @since 4.0 + * + */ +public class ChannelPut extends AbstractWebScript +{ + private static final String CHANNEL_ID = "channel_id"; + + private final PublishingJsonParser parser = new PublishingJsonParser(); + private ChannelService channelService; + + + /** + * {@inheritDoc} + */ + @Override + public void execute(WebScriptRequest req, WebScriptResponse res) + { + Map params = req.getServiceMatch().getTemplateVars(); + String channelId = URLDecoder.decode(params.get(CHANNEL_ID)); + Channel channel = channelService.getChannelById(channelId); + if(channel == null) + { + String msg = "No channel found for ID: " + channelId; + throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, msg); + } + String content = null; + try + { + content = WebScriptUtil.getContent(req); + if (content == null || content.isEmpty()) + { + throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "No publishing event was posted!"); + } + parser.updateChannel(channel, content, channelService); + } + catch(Exception e) + { + String msg = "Failed to Rename Channel: " + channelId + ". POST body: " + content; + throw new WebScriptException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e); + } + } + + /** + * @param channelService the channelService to set + */ + public void setChannelService(ChannelService channelService) + { + this.channelService = channelService; + } +} diff --git a/source/java/org/alfresco/repo/web/scripts/publishing/PublishingJsonParser.java b/source/java/org/alfresco/repo/web/scripts/publishing/PublishingJsonParser.java index 1ccc6fc13b..058c738a20 100644 --- a/source/java/org/alfresco/repo/web/scripts/publishing/PublishingJsonParser.java +++ b/source/java/org/alfresco/repo/web/scripts/publishing/PublishingJsonParser.java @@ -20,15 +20,6 @@ package org.alfresco.repo.web.scripts.publishing; import static org.alfresco.repo.web.scripts.WebScriptUtil.getCalendar; -import static org.alfresco.repo.web.scripts.publishing.PublishingWebScriptConstants.CHANNEL_ID; -import static org.alfresco.repo.web.scripts.publishing.PublishingWebScriptConstants.CHANNEL_IDS; -import static org.alfresco.repo.web.scripts.publishing.PublishingWebScriptConstants.COMMENT; -import static org.alfresco.repo.web.scripts.publishing.PublishingWebScriptConstants.MESSAGE; -import static org.alfresco.repo.web.scripts.publishing.PublishingWebScriptConstants.NODE_REF; -import static org.alfresco.repo.web.scripts.publishing.PublishingWebScriptConstants.PUBLISH_NODES; -import static org.alfresco.repo.web.scripts.publishing.PublishingWebScriptConstants.SCHEDULED_TIME; -import static org.alfresco.repo.web.scripts.publishing.PublishingWebScriptConstants.STATUS_UPDATE; -import static org.alfresco.repo.web.scripts.publishing.PublishingWebScriptConstants.UNPUBLISH_NODES; import java.text.ParseException; import java.util.ArrayList; @@ -42,6 +33,8 @@ import org.alfresco.service.cmr.publishing.MutablePublishingPackage; import org.alfresco.service.cmr.publishing.PublishingPackage; import org.alfresco.service.cmr.publishing.PublishingQueue; import org.alfresco.service.cmr.publishing.StatusUpdate; +import org.alfresco.service.cmr.publishing.channels.Channel; +import org.alfresco.service.cmr.publishing.channels.ChannelService; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.util.collections.Function; import org.alfresco.util.collections.JsonUtils; @@ -55,7 +48,7 @@ import org.json.JSONTokener; * @since 4.0 * */ -public class PublishingJsonParser +public class PublishingJsonParser implements PublishingWebScriptConstants { public JSONObject getJson(String jsonStr) throws JSONException @@ -66,6 +59,16 @@ public class PublishingJsonParser } return new JSONObject(); } + + public void updateChannel(Channel channel, String jsonStr, ChannelService channelService) throws JSONException + { + JSONObject json = getJson(jsonStr); + String newName = json.optString(NAME); + if(newName != null && newName.isEmpty() == false) + { + channelService.renameChannel(channel, newName); + } + } public String schedulePublishingEvent(PublishingQueue queue, String jsonStr) throws ParseException, JSONException { diff --git a/source/java/org/alfresco/repo/web/scripts/publishing/PublishingModelBuilder.java b/source/java/org/alfresco/repo/web/scripts/publishing/PublishingModelBuilder.java index f2e5293ecc..bec25478ae 100644 --- a/source/java/org/alfresco/repo/web/scripts/publishing/PublishingModelBuilder.java +++ b/source/java/org/alfresco/repo/web/scripts/publishing/PublishingModelBuilder.java @@ -151,7 +151,7 @@ public class PublishingModelBuilder implements PublishingWebScriptConstants model.put(CAN_UNPUBLISH, toString(type.canUnpublish())); model.put(MAX_STATUS_LENGTH, type.getMaximumStatusLength()); - model.put(ICON, getUrl(type) + "/icon/"); + model.put(ICON, getUrl(type) + "/icon"); return model; } diff --git a/source/java/org/alfresco/repo/web/scripts/publishing/PublishingRestApiTest.java b/source/java/org/alfresco/repo/web/scripts/publishing/PublishingRestApiTest.java index fe1c89f312..5c8046da97 100644 --- a/source/java/org/alfresco/repo/web/scripts/publishing/PublishingRestApiTest.java +++ b/source/java/org/alfresco/repo/web/scripts/publishing/PublishingRestApiTest.java @@ -113,6 +113,7 @@ import org.springframework.context.ApplicationContext; import org.springframework.extensions.surf.util.URLEncoder; import org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest; import org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest; +import org.springframework.extensions.webscripts.TestWebScriptServer.PutRequest; import org.springframework.extensions.webscripts.TestWebScriptServer.Response; /** @@ -128,10 +129,11 @@ public class PublishingRestApiTest extends BaseWebScriptTest private static final int maxStatusLength = 100; private static final String CHANNELS_URL = "api/publishing/channels"; + private static final String CHANNEL_URL = "api/publishing/channels/{0}"; private static final String CHANNELS_NODE_URL = "api/publishing/{0}/{1}/{2}/channels"; private static final String CHANNEL_TYPES_URL = "api/publishing/channel-types"; private static final String PUBLISHING_QUEUE_URL = "api/publishing/queue"; - private static final String PUBLISHING_EVENTS_FOR_NODE_url = "api/publishing/{0}/{1}/{2}/events"; + private static final String PUBLISHING_EVENTS_URL = "api/publishing/{0}/{1}/{2}/events"; private static final String JSON = "application/json"; @@ -208,6 +210,31 @@ public class PublishingRestApiTest extends BaseWebScriptTest checkChannels(statusChannels, statusUpdateChannel); } + public void testChannelPut() throws Exception + { + Channel channel1 = createChannel(publishAnyType); + Channel channel2 = createChannel(publishAnyType); + + String name1 = channel1.getName(); + String name2 = channel2.getName(); + + String newName = name1 + "Foo"; + JSONObject json = new JSONObject(); + json.put(NAME, newName); + + String jsonStr = json.toString(); + + String channel1Url = MessageFormat.format(CHANNEL_URL, URLEncoder.encode(channel1.getId())); + // Post JSON content. + sendRequest(new PutRequest(channel1Url, jsonStr, JSON), 200); + + Channel renamedCH1 = channelService.getChannelById(channel1.getId()); + assertEquals("Channel1 was not renamed correctly!", newName, renamedCH1.getName()); + + Channel renamedCH2 = channelService.getChannelById(channel2.getId()); + assertEquals("Channel2 name should not have changed!", name2, renamedCH2.getName()); + } + @SuppressWarnings("unchecked") public void testPublishingQueuePost() throws Exception { @@ -224,7 +251,7 @@ public class PublishingRestApiTest extends BaseWebScriptTest String comment = "The comment"; String statusMessage = "The status message"; - JSONObject json = buildJson(textNode, publishChannel, comment, statusMessage, statusChannel); + JSONObject json = buildScheduleEventJson(textNode, publishChannel, comment, statusMessage, statusChannel); String jsonStr = json.toString(); @@ -302,7 +329,7 @@ public class PublishingRestApiTest extends BaseWebScriptTest String protocol = textNode1.getStoreRef().getProtocol(); String storeId = textNode1.getStoreRef().getIdentifier(); String nodeId1 = textNode1.getId(); - String textNode1Url = MessageFormat.format(PUBLISHING_EVENTS_FOR_NODE_url, protocol, storeId, nodeId1); + String textNode1Url = MessageFormat.format(PUBLISHING_EVENTS_URL, protocol, storeId, nodeId1); // Get events on textNode1 before any events created. Response response = sendRequest(new GetRequest(textNode1Url), 200); @@ -328,7 +355,7 @@ public class PublishingRestApiTest extends BaseWebScriptTest // Query for events on textNode2. String nodeId2 = textNode2.getId(); - String textNode2Url = MessageFormat.format(PUBLISHING_EVENTS_FOR_NODE_url, protocol, storeId, nodeId2); + String textNode2Url = MessageFormat.format(PUBLISHING_EVENTS_URL, protocol, storeId, nodeId2); response = sendRequest(new GetRequest(textNode2Url), 200); data = getDataArray(response); assertEquals(0, data.length()); @@ -469,7 +496,7 @@ public class PublishingRestApiTest extends BaseWebScriptTest assertEquals(date, actualDate); } - private JSONObject buildJson(NodeRef node, Channel publishChannel, + private JSONObject buildScheduleEventJson(NodeRef node, Channel publishChannel, String comment, String statusMessage, Channel... statusChannels) throws JSONException { @@ -560,7 +587,7 @@ public class PublishingRestApiTest extends BaseWebScriptTest check(MAX_STATUS_LENGTH, jsonType, channelType.getMaximumStatusLength()); //TODO Implement Icon URL - check(ICON, jsonType, ""); + check(ICON, jsonType, expUrl + "/icon"); } private void check(String key, JSONObject json, Object exp)