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.opensocial.model.Activity;
21  import org.apache.shindig.social.opensocial.spi.ActivityService;
22  import org.apache.shindig.social.opensocial.spi.SocialSpiException;
23  import org.apache.shindig.social.opensocial.spi.UserId;
24  
25  import com.google.common.collect.Sets;
26  import com.google.inject.Inject;
27  
28  import java.util.List;
29  import java.util.Set;
30  import java.util.concurrent.Future;
31  
32  public class ActivityHandler extends DataRequestHandler {
33    private final ActivityService service;
34  
35    private static final String ACTIVITY_ID_PATH
36        = "/activities/{userId}+/{groupId}/{appId}/{activityId}+";
37  
38    @Inject
39    public ActivityHandler(ActivityService service) {
40      this.service = service;
41    }
42  
43    /***
44     * Allowed end-points /activities/{userId}/@self/{actvityId}+
45     *
46     * examples: /activities/john.doe/@self/1
47     */
48    @Override
49    protected Future<?> handleDelete(RequestItem request)
50        throws SocialSpiException {
51      request.applyUrlTemplate(ACTIVITY_ID_PATH);
52  
53      Set<UserId> userIds = request.getUsers();
54      Set<String> activityIds = Sets.newLinkedHashSet(request.getListParameter("activityId"));
55  
56      Preconditions.requireNotEmpty(userIds, "No userId specified");
57      Preconditions.requireSingular(userIds, "Multiple userIds not supported");
58  
59      return service.deleteActivities(userIds.iterator().next(), request.getGroup(),
60          request.getAppId(), activityIds, request.getToken());
61    }
62  
63    /***
64     * Allowed end-points /activities/{userId}/@self
65     *
66     * examples: /activities/john.doe/@self - postBody is an activity object
67     */
68    @Override
69    protected Future<?> handlePut(RequestItem request) throws SocialSpiException {
70      return handlePost(request);
71    }
72  
73    /***
74     * Allowed end-points /activities/{userId}/@self
75     *
76     * examples: /activities/john.doe/@self - postBody is an activity object
77     */
78    @Override
79    protected Future<?> handlePost(RequestItem request) throws SocialSpiException {
80      request.applyUrlTemplate(ACTIVITY_ID_PATH);
81  
82      Set<UserId> userIds = request.getUsers();
83      List<String> activityIds = request.getListParameter("activityId");
84  
85      Preconditions.requireNotEmpty(userIds, "No userId specified");
86      Preconditions.requireSingular(userIds, "Multiple userIds not supported");
87      // TODO(lryan) This seems reasonable to allow on PUT but we don't have an update verb.
88      Preconditions.requireEmpty(activityIds, "Cannot specify activityId in create");
89  
90      return service.createActivity(userIds.iterator().next(), request.getGroup(),
91          request.getAppId(), request.getFields(),
92          request.getTypedParameter("activity", Activity.class),
93          request.getToken());
94    }
95  
96    /***
97     * Allowed end-points /activities/{userId}/{groupId}/{optionalActvityId}+
98     * /activities/{userId}+/{groupId}
99     *
100    * examples: /activities/john.doe/@self/1 /activities/john.doe/@self
101    * /activities/john.doe,jane.doe/@friends
102    */
103   @Override
104   protected Future<?> handleGet(RequestItem request)
105       throws SocialSpiException {
106     request.applyUrlTemplate(ACTIVITY_ID_PATH);
107 
108     Set<UserId> userIds = request.getUsers();
109     Set<String> optionalActivityIds = Sets.newLinkedHashSet(request.getListParameter("activityId"));
110 
111     // Preconditions
112     Preconditions.requireNotEmpty(userIds, "No userId specified");
113     if (userIds.size() > 1 && !optionalActivityIds.isEmpty()) {
114       throw new IllegalArgumentException("Cannot fetch same activityIds for multiple userIds");
115     }
116 
117     if (!optionalActivityIds.isEmpty()) {
118       if (optionalActivityIds.size() == 1) {
119         return service.getActivity(userIds.iterator().next(), request.getGroup(),
120             request.getAppId(), request.getFields(), optionalActivityIds.iterator().next(),
121             request.getToken());
122       } else {
123         return service.getActivities(userIds.iterator().next(), request.getGroup(),
124             request.getAppId(), request.getFields(), optionalActivityIds, request.getToken());
125       }
126     }
127 
128     return service.getActivities(userIds, request.getGroup(),
129         request.getAppId(),
130         // TODO: add pagination and sorting support
131         // getSortBy(params), getFilterBy(params), getStartIndex(params), getCount(params),
132         request.getFields(), request.getToken());
133   }
134 
135 }