View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations under the License.
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      // Preconditions
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          // TODO: Add some crazy concept to handle the userId?
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         // Every other case is a collection response of optional person ids
101         return personService.getPeople(personIds, new GroupId(GroupId.Type.self, null),
102             options, fields, request.getToken());
103       }
104     }
105 
106     // Every other case is a collection response.
107     return personService.getPeople(userIds, groupId, options, fields, request.getToken());
108   }
109 }