Completed subscriptions REST API and added a bit more UI logic

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@28488 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Florian Mü
2011-06-20 18:08:36 +00:00
parent 5daf79d986
commit f0acf43970
13 changed files with 250 additions and 29 deletions

View File

@@ -31,6 +31,7 @@ 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.service.cmr.subscriptions.SubscriptionsDisabledException;
import org.alfresco.util.ISO8601DateFormat;
import org.json.JSONArray;
import org.json.JSONException;
@@ -66,17 +67,28 @@ public abstract class AbstractSubscriptionServiceWebScript extends AbstractWebSc
try
{
String userId = req.getServiceMatch().getTemplateVars().get("userid");
JSONObject obj = executeImpl(userId, req, res);
Object obj = executeImpl(userId, req, res);
if (obj == null)
if (obj instanceof JSONObject || obj instanceof JSONArray)
{
res.setStatus(204);
res.setContentEncoding("UTF-8");
Writer writer = res.getWriter();
if (obj instanceof JSONObject)
{
((JSONObject) obj).write(writer);
} else
{
((JSONArray) obj).write(writer);
}
writer.flush();
} else
{
Writer writer = res.getWriter();
obj.write(writer);
writer.flush();
res.setStatus(204);
}
} catch (SubscriptionsDisabledException sde)
{
throw new WebScriptException(400, "Subscription service is disabled!", sde);
} catch (NoSuchPersonException nspe)
{
throw new WebScriptException(400, "Unknown user '" + nspe.getUserName() + "'!", nspe);
@@ -85,12 +97,12 @@ public abstract class AbstractSubscriptionServiceWebScript extends AbstractWebSc
throw new WebScriptException(403, "Subscription list is private!", psle);
} catch (JSONException je)
{
throw new WebScriptException(500, "Unable to serialize JSON!", je);
throw new WebScriptException(500, "Unable to parse or serialize JSON!", je);
}
}
public abstract JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res)
throws IOException, JSONException;
public abstract Object executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException,
JSONException;
protected int parseNumber(String name, String number, int def)
{
@@ -141,7 +153,7 @@ public abstract class AbstractSubscriptionServiceWebScript extends AbstractWebSc
if (statusTime != null)
{
JSONObject statusTimeJson = new JSONObject();
statusTimeJson.put("iso8601", ISO8601DateFormat.format(statusTime));
statusTimeJson.put("iso8601", ISO8601DateFormat.format(statusTime));
result.put("userStatusTime", statusTimeJson);
}

View File

@@ -19,20 +19,32 @@
package org.alfresco.repo.web.scripts.subscriptions;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
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 class SubscriptionServiceFollowPost extends AbstractSubscriptionServiceWebScript
{
public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException,
JSONException
{
String otherUser = req.getServiceMatch().getTemplateVars().get("otheruserid");
JSONArray jsonUsers = new JSONArray(req.getContent().getContent());
subscriptionService.unfollow(userId, otherUser);
List<String> users = new ArrayList<String>(jsonUsers.length());
for (int i = 0; i < jsonUsers.length(); i++)
{
users.add(jsonUsers.getString(i));
}
for (String user : users)
{
subscriptionService.follow(userId, user);
}
return null;
}

View File

@@ -0,0 +1,54 @@
/*
* 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.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse;
public class SubscriptionServiceFollowsPost extends AbstractSubscriptionServiceWebScript
{
public JSONArray executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException,
JSONException
{
JSONArray jsonUsers = new JSONArray(req.getContent().getContent());
List<String> users = new ArrayList<String>(jsonUsers.length());
for (int i = 0; i < jsonUsers.length(); i++)
{
users.add(jsonUsers.getString(i));
}
JSONArray result = new JSONArray();
for (String user : users)
{
JSONObject item = new JSONObject();
item.put(user, subscriptionService.follows(userId, user));
result.put(item);
}
return result;
}
}

View File

@@ -25,15 +25,14 @@ import org.json.JSONObject;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse;
public class SubscriptionServiceFollowGet extends AbstractSubscriptionServiceWebScript
public class SubscriptionServicePrivateListGet extends AbstractSubscriptionServiceWebScript
{
public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException,
JSONException
{
String otherUser = req.getServiceMatch().getTemplateVars().get("otheruserid");
JSONObject obj = new JSONObject();
obj.put("private", subscriptionService.isSubscriptionListPrivate(userId));
subscriptionService.follow(userId, otherUser);
return null;
return obj;
}
}

View File

@@ -0,0 +1,50 @@
/*
* 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 SubscriptionServicePrivateListPut extends SubscriptionServicePrivateListGet
{
public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException,
JSONException
{
JSONObject obj = new JSONObject(req.getContent().getContent());
String setPrivate = obj.getString("private");
if (setPrivate != null)
{
if (setPrivate.equalsIgnoreCase("true"))
{
subscriptionService.setSubscriptionListPrivate(userId, true);
} else if (setPrivate.equalsIgnoreCase("false"))
{
subscriptionService.setSubscriptionListPrivate(userId, false);
}
}
return super.executeImpl(userId, req, res);
}
}

View File

@@ -0,0 +1,51 @@
/*
* 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.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse;
public class SubscriptionServiceUnfollowPost extends AbstractSubscriptionServiceWebScript
{
public JSONObject executeImpl(String userId, WebScriptRequest req, WebScriptResponse res) throws IOException,
JSONException
{
JSONArray jsonUsers = new JSONArray(req.getContent().getContent());
List<String> users = new ArrayList<String>(jsonUsers.length());
for (int i = 0; i < jsonUsers.length(); i++)
{
users.add(jsonUsers.getString(i));
}
for (String user : users)
{
subscriptionService.unfollow(userId, user);
}
return null;
}
}