Created channels.get Rest Api webscript.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@28459 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
N Smith
2011-06-17 15:05:47 +00:00
parent bb5c53d645
commit 497c19cb59
6 changed files with 346 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
<webscript>
<shortname>Get Publishing Channels</shortname>
<description>Get the publishing channels for a Share Site.</description>
<url>/api/publishing/site/{site_id}/channels</url>
<format default="json"/>
<authentication>user</authentication>
<transaction allow="readonly">required</transaction>
<lifecycle>public_api</lifecycle>
<args>
<arg>
<shortname>site_id</shortname>
<description>The id of the site to retrieve channels for.</description>
</arg>
</args>
<responses>
<response>
<format>json</format>
<type>
<![CDATA[
{
"data":
{
[
{
"url": string,
"name": string,
"channelType":
{
"id": string,
"url": string,
"channelNodeType": string,
"contentRootNodeType": string,
"supportedContentTypes": [string, ...],
"supportedMimeTypes": [string, ...],
"canPublish": boolean,
"canPublishStatusUpdates": boolean,
"canUnpublish": boolean,
}
},
...
]
}
}
]]>
</type>
</response>
</responses>
</webscript>

View File

@@ -0,0 +1,11 @@
<#-- List Channels -->
<#import "publishing.lib.ftl" as publishLib />
{
"data":
[
<#list data as channel>
<@publishLib.channelJSON channel=channel />
<#if channel_has_next>,</#if>
</#list>
]
}

View File

@@ -0,0 +1,42 @@
<#-- Renders a Channel. -->
<#macro channelJSON channel>
<#escape x as jsonUtils.encodeJSONString(x)>
{
"url": "${channel.url}",
"name": "${channel.name}",
"channelType":
<@channelTypeJSON type=channel.channelType />
}
</#escape>
</#macro>
<#-- Renders a Channel Type. -->
<#macro channelTypeJSON type>
<#escape x as jsonUtils.encodeJSONString(x)>
{
"id": "${type.id}",
"url": "${type.url}",
"channelNodeType": "${type.channelNodeType}",
"contentRootNodeType": "${type.contentRootNodeType}",
"supportedContentTypes":
<@iterateStringsJSON strings=type.supportedContentTypes />,
"supportedMimeTypes":
<@iterateStringsJSON strings=type.supportedMimeTypes/>,
"canPublish": "${type.canPublish}",
"canPublishStatusUpdates": "${type.canPublishStatusUpdates}",
"canUnpublish": "${type.canUnpublish}"
}
</#escape>
</#macro>
<#-- Renders a List of Strings. -->
<#macro iterateStringsJSON strings>
<#escape x as jsonUtils.encodeJSONString(x)>
[
<#list strings as string>
"${string}"
<#if string_has_next>,</#if>
</#list>
]
</#escape>
</#macro>

View File

@@ -0,0 +1,30 @@
/*
* 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;
/**
* @author Nick Smith
* @since 4.0
*
*/
public class AbstractPublishingWebscript
{
}

View File

@@ -0,0 +1,95 @@
/*
* 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.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.alfresco.service.cmr.publishing.channels.Channel;
import org.alfresco.service.cmr.publishing.channels.ChannelService;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.DeclarativeWebScript;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest;
/**
* @author Nick Smith
* @since 4.0
*
*/
public class ChannelsGet extends DeclarativeWebScript
{
public static final String DATA_KEY = "data";
public static final String SITE_ID = "site_id";
private ChannelService channelService;
/**
* {@inheritDoc}
*/
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
{
PublishingModelBuilder builder = new PublishingModelBuilder();
return buildModel(builder, req, status, cache);
}
private Map<String, Object> buildModel(PublishingModelBuilder builder,
WebScriptRequest req, Status status, Cache cache)
{
Map<String, String> params = req.getServiceMatch().getTemplateVars();
String siteId = params.get(SITE_ID);
if (siteId== null)
{
throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, "A Site ID must be specified!");
}
List<Channel> channels = channelService.getChannels(siteId);
List<Map<String, Object>> results = builder.buildChannels(channels);
return createBaseModel(results);
}
protected Map<String, Object> createBaseModel(Map<String, Object> result)
{
Map<String, Object> model = new HashMap<String, Object>();
model.put(DATA_KEY, result);
return model;
}
protected Map<String, Object> createBaseModel(List<Map<String, Object>> results)
{
Map<String, Object> model = new HashMap<String, Object>();
model.put(DATA_KEY, results);
return model;
}
/**
* @param channelService the channelService to set
*/
public void setChannelService(ChannelService channelService)
{
this.channelService = channelService;
}
}

View File

@@ -0,0 +1,120 @@
/*
* 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 static org.alfresco.util.collections.CollectionUtils.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.alfresco.service.cmr.publishing.channels.Channel;
import org.alfresco.service.cmr.publishing.channels.ChannelType;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.util.collections.Function;
import org.springframework.extensions.surf.util.URLEncoder;
/**
* @author Nick Smith
* @since 4.0
*
*/
public class PublishingModelBuilder
{
// General Keys
public static final String ID = "id";
public static final String URL = "url";
// Channel Type Keys
public static final String CHANNEL_NODE_TYP = "channelNodeType";
public static final String CONTENT_ROOT_NODE_TYP = "contentRootNodeType";
public static final String SUPPORTED_CONTENT_TYPES = "supportedContentTypes";
public static final String SUPPORTED_MIME_TYPES = "supportedMimeTypes";
public static final String CAN_PUBLISH = "canPublish";
public static final String CAN_PUBLISH_STATUS_UPDATES = "canPublishStatusUpdates";
public static final String CAN_UNPUBLISH = "canUnpublish";
// Channel Keys
public static final String CHANNEL_TYPE = "channelType";
public static final String NAME = "name";
public Map<String, Object> buildChannel(Channel channel)
{
Map<String, Object> model = new HashMap<String, Object>();
model.put(NAME, channel.getName());
model.put(URL, getUrl(channel));
model.put(CHANNEL_TYPE, buildChannelType(channel.getChannelType()));
return model;
}
public List<Map<String, Object>> buildChannels(List<Channel> channels)
{
return transform(channels, new Function<Channel, Map<String, Object>>()
{
public Map<String, Object> apply(Channel value)
{
return buildChannel(value);
}
});
}
public Map<String, Object> buildChannelType(ChannelType type)
{
Map<String, Object> model = new HashMap<String, Object>();
model.put(ID, type.getId());
model.put(URL, getUrl(type));
model.put(CHANNEL_NODE_TYP, type.getChannelNodeType().toString());
model.put(CONTENT_ROOT_NODE_TYP, type.getContentRootNodeType().toString());
model.put(SUPPORTED_CONTENT_TYPES, toListOfStrings(type.getSupportedContentTypes()));
model.put(SUPPORTED_MIME_TYPES, type.getSupportedMimetypes());
model.put(CAN_PUBLISH, toString(type.canPublish()));
model.put(CAN_PUBLISH_STATUS_UPDATES, toString(type.canPublishStatusUpdates()));
model.put(CAN_UNPUBLISH, toString(type.canUnpublish()));
return model;
}
public static String getUrl(ChannelType type)
{
return "api/publishing/channelTypes/"+URLEncoder.encode(type.getId());
}
public static String getUrl(Channel channel)
{
NodeRef node = channel.getNodeRef();
StoreRef storeRef = node.getStoreRef();
StringBuilder sb = new StringBuilder("api/publishing/channels/");
sb.append(storeRef.getProtocol()).append("/")
.append(storeRef.getIdentifier()).append("/")
.append(node.getId());
return sb.toString();
}
private String toString(boolean b)
{
return Boolean.toString(b);
}
}