| 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 | 17 | public PersonHandler(PersonService personService) { |
| 43 | 17 | this.personService = personService; |
| 44 | 17 | } |
| 45 | |
|
| 46 | |
@Override |
| 47 | |
protected Future<?> handleDelete(RequestItem request) throws SocialSpiException { |
| 48 | 1 | throw new SocialSpiException(ResponseError.BAD_REQUEST, "You can't delete people."); |
| 49 | |
} |
| 50 | |
|
| 51 | |
@Override |
| 52 | |
protected Future<?> handlePut(RequestItem request) throws SocialSpiException { |
| 53 | 1 | 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 | 1 | throw new SocialSpiException(ResponseError.NOT_IMPLEMENTED, "You can't add people right now."); |
| 59 | |
} |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
@Override |
| 67 | |
protected Future<?> handleGet(RequestItem request) throws SocialSpiException { |
| 68 | 14 | request.applyUrlTemplate(PEOPLE_PATH); |
| 69 | |
|
| 70 | 14 | GroupId groupId = request.getGroup(); |
| 71 | 14 | Set<String> optionalPersonId = Sets.newLinkedHashSet(request.getListParameter("personId")); |
| 72 | 14 | Set<String> fields = request.getFields(Person.Field.DEFAULT_FIELDS); |
| 73 | 14 | Set<UserId> userIds = request.getUsers(); |
| 74 | |
|
| 75 | |
|
| 76 | 14 | Preconditions.requireNotEmpty(userIds, "No userId specified"); |
| 77 | 14 | if (userIds.size() > 1 && !optionalPersonId.isEmpty()) { |
| 78 | 0 | throw new IllegalArgumentException("Cannot fetch personIds for multiple userIds"); |
| 79 | |
} |
| 80 | |
|
| 81 | 14 | CollectionOptions options = new CollectionOptions(request); |
| 82 | |
|
| 83 | 14 | if (userIds.size() == 1) { |
| 84 | 13 | if (optionalPersonId.isEmpty()) { |
| 85 | 12 | if (groupId.getType() == GroupId.Type.self) { |
| 86 | 3 | return personService.getPerson(userIds.iterator().next(), fields, request.getToken()); |
| 87 | |
} else { |
| 88 | 9 | return personService.getPeople(userIds, groupId, options, fields, request.getToken()); |
| 89 | |
} |
| 90 | 1 | } else if (optionalPersonId.size() == 1) { |
| 91 | |
|
| 92 | 1 | return personService.getPerson(new UserId(UserId.Type.userId, |
| 93 | |
optionalPersonId.iterator().next()), |
| 94 | |
fields, request.getToken()); |
| 95 | |
} else { |
| 96 | 0 | Set<UserId> personIds = Sets.newLinkedHashSet(); |
| 97 | 0 | for (String pid : optionalPersonId) { |
| 98 | 0 | personIds.add(new UserId(UserId.Type.userId, pid)); |
| 99 | 0 | } |
| 100 | |
|
| 101 | 0 | return personService.getPeople(personIds, new GroupId(GroupId.Type.self, null), |
| 102 | |
options, fields, request.getToken()); |
| 103 | |
} |
| 104 | |
} |
| 105 | |
|
| 106 | |
|
| 107 | 1 | return personService.getPeople(userIds, groupId, options, fields, request.getToken()); |
| 108 | |
} |
| 109 | |
} |