Subscription service REST API and a glimpse of UI

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@28460 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Florian Mü
2011-06-17 16:17:32 +00:00
parent 497c19cb59
commit 28ad07846e
14 changed files with 502 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
<webscript>
<shortname>Follow</shortname>
<description>Follow a user</description>
<url>/api/subscriptions/{userid}/follow/{otheruserid}</url>
<format default="json"/>
<authentication>user</authentication>
<transaction>required</transaction>
</webscript>

View File

@@ -0,0 +1,8 @@
<webscript>
<shortname>Followers</shortname>
<description>Returns followers</description>
<url>/api/subscriptions/{userid}/followers</url>
<format default="json"/>
<authentication>user</authentication>
<transaction>required</transaction>
</webscript>

View File

@@ -0,0 +1,8 @@
<webscript>
<shortname>Followers Count</shortname>
<description>Returns the size of followers list</description>
<url>/api/subscriptions/{userid}/followers/count</url>
<format default="json"/>
<authentication>user</authentication>
<transaction>required</transaction>
</webscript>

View File

@@ -0,0 +1,8 @@
<webscript>
<shortname>Following</shortname>
<description>Returns the following list</description>
<url>/api/subscriptions/{userid}/following</url>
<format default="json"/>
<authentication>user</authentication>
<transaction>required</transaction>
</webscript>

View File

@@ -0,0 +1,8 @@
<webscript>
<shortname>Following Count</shortname>
<description>Returns size of the following list</description>
<url>/api/subscriptions/{userid}/following/count</url>
<format default="json"/>
<authentication>user</authentication>
<transaction>required</transaction>
</webscript>

View File

@@ -0,0 +1,8 @@
<webscript>
<shortname>Follow</shortname>
<description>Follow a user</description>
<url>/api/subscriptions/{userid}/unfollow/{otheruserid}</url>
<format default="json"/>
<authentication>user</authentication>
<transaction>required</transaction>
</webscript>

View File

@@ -595,6 +595,45 @@
</property> </property>
</bean> </bean>
<!-- -->
<!-- Subscriptions Web Scripts -->
<!-- -->
<bean id="webscript.org.alfresco.repository.subscriptions.followers.get" class="org.alfresco.repo.web.scripts.subscriptions.SubscriptionServiceFollowersGet" parent="webscript">
<property name="subscriptionService" ref="SubscriptionService"/>
<property name="nodeService" ref="NodeService"/>
<property name="personService" ref="PersonService"/>
</bean>
<bean id="webscript.org.alfresco.repository.subscriptions.followerscount.get" class="org.alfresco.repo.web.scripts.subscriptions.SubscriptionServiceFollowersCountGet" parent="webscript">
<property name="subscriptionService" ref="SubscriptionService"/>
<property name="nodeService" ref="NodeService"/>
<property name="personService" ref="PersonService"/>
</bean>
<bean id="webscript.org.alfresco.repository.subscriptions.following.get" class="org.alfresco.repo.web.scripts.subscriptions.SubscriptionServiceFollowingGet" parent="webscript">
<property name="subscriptionService" ref="SubscriptionService"/>
<property name="nodeService" ref="NodeService"/>
<property name="personService" ref="PersonService"/>
</bean>
<bean id="webscript.org.alfresco.repository.subscriptions.followingcount.get" class="org.alfresco.repo.web.scripts.subscriptions.SubscriptionServiceFollowingCountGet" parent="webscript">
<property name="subscriptionService" ref="SubscriptionService"/>
<property name="nodeService" ref="NodeService"/>
<property name="personService" ref="PersonService"/>
</bean>
<bean id="webscript.org.alfresco.repository.subscriptions.follow.get" class="org.alfresco.repo.web.scripts.subscriptions.SubscriptionServiceFollowGet" parent="webscript">
<property name="subscriptionService" ref="SubscriptionService"/>
<property name="nodeService" ref="NodeService"/>
<property name="personService" ref="PersonService"/>
</bean>
<bean id="webscript.org.alfresco.repository.subscriptions.unfollow.get" class="org.alfresco.repo.web.scripts.subscriptions.SubscriptionServiceUnfollowGet" parent="webscript">
<property name="subscriptionService" ref="SubscriptionService"/>
<property name="nodeService" ref="NodeService"/>
<property name="personService" ref="PersonService"/>
</bean>
<!-- --> <!-- -->
<!-- Invite Service REST API --> <!-- Invite Service REST API -->

View File

@@ -0,0 +1,165 @@
/*
* 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.subscriptions;
import java.io.IOException;
import java.io.Writer;
import java.util.Date;
import java.util.List;
import org.alfresco.model.ContentModel;
import org.alfresco.query.PagingRequest;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.security.NoSuchPersonException;
import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.service.cmr.subscriptions.PrivateSubscriptionListException;
import org.alfresco.service.cmr.subscriptions.SubscriptionService;
import org.alfresco.util.ISO8601DateFormat;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.extensions.webscripts.AbstractWebScript;
import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse;
public abstract class AbstractSubscriptionServiceWebScript extends AbstractWebScript
{
protected SubscriptionService subscriptionService;
protected NodeService nodeService;
protected PersonService personService;
public void setSubscriptionService(SubscriptionService subscriptionService)
{
this.subscriptionService = subscriptionService;
}
public void setNodeService(NodeService nodeService)
{
this.nodeService = nodeService;
}
public void setPersonService(PersonService personService)
{
this.personService = personService;
}
public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException
{
try
{
String userId = req.getServiceMatch().getTemplateVars().get("userid");
JSONObject obj = executeImpl(userId, req, res);
if (obj == null)
{
res.setStatus(204);
} else
{
Writer writer = res.getWriter();
obj.write(writer);
writer.flush();
}
} catch (NoSuchPersonException nspe)
{
throw new WebScriptException(400, "Unknown user '" + nspe.getUserName() + "'!", nspe);
} catch (PrivateSubscriptionListException psle)
{
throw new WebScriptException(403, "Subscription list is private!", psle);
} catch (JSONException je)
{
throw new WebScriptException(500, "Unable to serialize JSON!", je);
}
}
public abstract JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res)
throws IOException, JSONException;
protected int parseNumber(String name, String number, int def)
{
if (number != null && number.length() > 0)
{
try
{
return Integer.parseInt(number);
} catch (NumberFormatException e)
{
throw new WebScriptException(400, name + " is not a number!", e);
}
} else
{
return def;
}
}
protected PagingRequest createPagingRequest(WebScriptRequest req)
{
int skipCount = parseNumber("skipCount", req.getParameter("skipCount"), 0);
int maxItems = parseNumber("maxItems", req.getParameter("maxItems"), -1);
PagingRequest result = new PagingRequest(skipCount, maxItems, null);
result.setRequestTotalCountMax(Integer.MAX_VALUE);
return result;
}
protected JSONObject getUserDetails(String username) throws JSONException
{
NodeRef node = personService.getPerson(username);
JSONObject result = new JSONObject();
result.put("userName", username);
result.put("firstName", nodeService.getProperty(node, ContentModel.PROP_FIRSTNAME));
result.put("lastName", nodeService.getProperty(node, ContentModel.PROP_LASTNAME));
String status = (String) nodeService.getProperty(node, ContentModel.PROP_USER_STATUS);
if (status != null)
{
result.put("userStatus", status);
}
Date statusTime = (Date) nodeService.getProperty(node, ContentModel.PROP_USER_STATUS_TIME);
if (statusTime != null)
{
JSONObject statusTimeJson = new JSONObject();
statusTimeJson.put("iso8601", ISO8601DateFormat.format(statusTime));
result.put("userStatusTime", statusTimeJson);
}
return result;
}
protected JSONArray getUserArray(List<String> usernames) throws JSONException
{
JSONArray result = new JSONArray();
if (usernames != null)
{
for (String username : usernames)
{
result.put(getUserDetails(username));
}
}
return result;
}
}

View File

@@ -0,0 +1,39 @@
/*
* 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.subscriptions;
import java.io.IOException;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse;
public class SubscriptionServiceFollowGet extends AbstractSubscriptionServiceWebScript
{
public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException,
JSONException
{
String otherUser = req.getServiceMatch().getTemplateVars().get("otheruserid");
subscriptionService.follow(userId, otherUser);
return null;
}
}

View File

@@ -0,0 +1,40 @@
/*
* 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.subscriptions;
import java.io.IOException;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse;
public class SubscriptionServiceFollowersCountGet extends AbstractSubscriptionServiceWebScript
{
public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException,
JSONException
{
int count = subscriptionService.getFollowersCount(userId);
JSONObject obj = new JSONObject();
obj.put("count", count);
return obj;
}
}

View File

@@ -0,0 +1,46 @@
/*
* 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.subscriptions;
import java.io.IOException;
import org.alfresco.service.cmr.subscriptions.PagingFollowingResults;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse;
public class SubscriptionServiceFollowersGet extends AbstractSubscriptionServiceWebScript
{
public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException,
JSONException
{
PagingFollowingResults result = subscriptionService.getFollowers(userId, createPagingRequest(req));
JSONObject obj = new JSONObject();
obj.put("people", getUserArray(result.getPage()));
obj.put("hasMoreItems", result.hasMoreItems());
if (result.getTotalResultCount() != null)
{
obj.put("totalCount", result.getTotalResultCount().getFirst());
}
return obj;
}
}

View File

@@ -0,0 +1,40 @@
/*
* 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.subscriptions;
import java.io.IOException;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse;
public class SubscriptionServiceFollowingCountGet extends AbstractSubscriptionServiceWebScript
{
public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException,
JSONException
{
int count = subscriptionService.getFollowingCount(userId);
JSONObject obj = new JSONObject();
obj.put("count", count);
return obj;
}
}

View File

@@ -0,0 +1,46 @@
/*
* 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.subscriptions;
import java.io.IOException;
import org.alfresco.service.cmr.subscriptions.PagingFollowingResults;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse;
public class SubscriptionServiceFollowingGet extends AbstractSubscriptionServiceWebScript
{
public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException,
JSONException
{
PagingFollowingResults result = subscriptionService.getFollowing(userId, createPagingRequest(req));
JSONObject obj = new JSONObject();
obj.put("people", getUserArray(result.getPage()));
obj.put("hasMoreItems", result.hasMoreItems());
if (result.getTotalResultCount() != null)
{
obj.put("totalCount", result.getTotalResultCount().getFirst());
}
return obj;
}
}

View File

@@ -0,0 +1,39 @@
/*
* 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.subscriptions;
import java.io.IOException;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse;
public class SubscriptionServiceUnfollowGet extends AbstractSubscriptionServiceWebScript
{
public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException,
JSONException
{
String otherUser = req.getServiceMatch().getTemplateVars().get("otheruserid");
subscriptionService.unfollow(userId, otherUser);
return null;
}
}