Implemented channel.put Rest method for renaming channels. Also renamed publishing-events-for-node.get to publishing-events.get.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@29312 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
N Smith
2011-07-25 15:06:14 +00:00
parent e4870c0afa
commit 314420ede9
8 changed files with 168 additions and 18 deletions

View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
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<String, String> 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;
}
}

View File

@@ -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
{

View File

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

View File

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