1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shindig.social.opensocial.service;
19
20 import org.apache.shindig.social.ResponseError;
21 import org.apache.shindig.social.opensocial.model.Person;
22 import org.apache.shindig.social.opensocial.spi.CollectionOptions;
23 import org.apache.shindig.social.opensocial.spi.GroupId;
24 import org.apache.shindig.social.opensocial.spi.PersonService;
25 import org.apache.shindig.social.opensocial.spi.SocialSpiException;
26 import org.apache.shindig.social.opensocial.spi.UserId;
27
28 import com.google.common.collect.Sets;
29 import com.google.inject.Inject;
30
31 import java.util.Set;
32 import java.util.concurrent.Future;
33
34
35 public class PersonHandler extends DataRequestHandler {
36
37 private final PersonService personService;
38
39 private static final String PEOPLE_PATH = "/people/{userId}+/{groupId}/{personId}+";
40
41 @Inject
42 public PersonHandler(PersonService personService) {
43 this.personService = personService;
44 }
45
46 @Override
47 protected Future<?> handleDelete(RequestItem request) throws SocialSpiException {
48 throw new SocialSpiException(ResponseError.BAD_REQUEST, "You can't delete people.");
49 }
50
51 @Override
52 protected Future<?> handlePut(RequestItem request) throws SocialSpiException {
53 throw new SocialSpiException(ResponseError.NOT_IMPLEMENTED, "You can't update right now.");
54 }
55
56 @Override
57 protected Future<?> handlePost(RequestItem request) throws SocialSpiException {
58 throw new SocialSpiException(ResponseError.NOT_IMPLEMENTED, "You can't add people right now.");
59 }
60
61 /***
62 * Allowed end-points /people/{userId}+/{groupId} /people/{userId}/{groupId}/{optionalPersonId}+
63 *
64 * examples: /people/john.doe/@all /people/john.doe/@friends /people/john.doe/@self
65 */
66 @Override
67 protected Future<?> handleGet(RequestItem request) throws SocialSpiException {
68 request.applyUrlTemplate(PEOPLE_PATH);
69
70 GroupId groupId = request.getGroup();
71 Set<String> optionalPersonId = Sets.newLinkedHashSet(request.getListParameter("personId"));
72 Set<String> fields = request.getFields(Person.Field.DEFAULT_FIELDS);
73 Set<UserId> userIds = request.getUsers();
74
75
76 Preconditions.requireNotEmpty(userIds, "No userId specified");
77 if (userIds.size() > 1 && !optionalPersonId.isEmpty()) {
78 throw new IllegalArgumentException("Cannot fetch personIds for multiple userIds");
79 }
80
81 CollectionOptions options = new CollectionOptions(request);
82
83 if (userIds.size() == 1) {
84 if (optionalPersonId.isEmpty()) {
85 if (groupId.getType() == GroupId.Type.self) {
86 return personService.getPerson(userIds.iterator().next(), fields, request.getToken());
87 } else {
88 return personService.getPeople(userIds, groupId, options, fields, request.getToken());
89 }
90 } else if (optionalPersonId.size() == 1) {
91
92 return personService.getPerson(new UserId(UserId.Type.userId,
93 optionalPersonId.iterator().next()),
94 fields, request.getToken());
95 } else {
96 Set<UserId> personIds = Sets.newLinkedHashSet();
97 for (String pid : optionalPersonId) {
98 personIds.add(new UserId(UserId.Type.userId, pid));
99 }
100
101 return personService.getPeople(personIds, new GroupId(GroupId.Type.self, null),
102 options, fields, request.getToken());
103 }
104 }
105
106
107 return personService.getPeople(userIds, groupId, options, fields, request.getToken());
108 }
109 }