| 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.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 | 18 | public ActivityHandler(ActivityService service) { |
| 40 | 18 | this.service = service; |
| 41 | 18 | } |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
@Override |
| 49 | |
protected Future<?> handleDelete(RequestItem request) |
| 50 | |
throws SocialSpiException { |
| 51 | 1 | request.applyUrlTemplate(ACTIVITY_ID_PATH); |
| 52 | |
|
| 53 | 1 | Set<UserId> userIds = request.getUsers(); |
| 54 | 1 | Set<String> activityIds = Sets.newLinkedHashSet(request.getListParameter("activityId")); |
| 55 | |
|
| 56 | 1 | Preconditions.requireNotEmpty(userIds, "No userId specified"); |
| 57 | 1 | Preconditions.requireSingular(userIds, "Multiple userIds not supported"); |
| 58 | |
|
| 59 | 1 | return service.deleteActivities(userIds.iterator().next(), request.getGroup(), |
| 60 | |
request.getAppId(), activityIds, request.getToken()); |
| 61 | |
} |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
@Override |
| 69 | |
protected Future<?> handlePut(RequestItem request) throws SocialSpiException { |
| 70 | 1 | return handlePost(request); |
| 71 | |
} |
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
@Override |
| 79 | |
protected Future<?> handlePost(RequestItem request) throws SocialSpiException { |
| 80 | 4 | request.applyUrlTemplate(ACTIVITY_ID_PATH); |
| 81 | |
|
| 82 | 4 | Set<UserId> userIds = request.getUsers(); |
| 83 | 4 | List<String> activityIds = request.getListParameter("activityId"); |
| 84 | |
|
| 85 | 4 | Preconditions.requireNotEmpty(userIds, "No userId specified"); |
| 86 | 4 | Preconditions.requireSingular(userIds, "Multiple userIds not supported"); |
| 87 | |
|
| 88 | 4 | Preconditions.requireEmpty(activityIds, "Cannot specify activityId in create"); |
| 89 | |
|
| 90 | 4 | 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 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
@Override |
| 104 | |
protected Future<?> handleGet(RequestItem request) |
| 105 | |
throws SocialSpiException { |
| 106 | 13 | request.applyUrlTemplate(ACTIVITY_ID_PATH); |
| 107 | |
|
| 108 | 13 | Set<UserId> userIds = request.getUsers(); |
| 109 | 13 | Set<String> optionalActivityIds = Sets.newLinkedHashSet(request.getListParameter("activityId")); |
| 110 | |
|
| 111 | |
|
| 112 | 13 | Preconditions.requireNotEmpty(userIds, "No userId specified"); |
| 113 | 13 | if (userIds.size() > 1 && !optionalActivityIds.isEmpty()) { |
| 114 | 0 | throw new IllegalArgumentException("Cannot fetch same activityIds for multiple userIds"); |
| 115 | |
} |
| 116 | |
|
| 117 | 13 | if (!optionalActivityIds.isEmpty()) { |
| 118 | 3 | if (optionalActivityIds.size() == 1) { |
| 119 | 3 | return service.getActivity(userIds.iterator().next(), request.getGroup(), |
| 120 | |
request.getAppId(), request.getFields(), optionalActivityIds.iterator().next(), |
| 121 | |
request.getToken()); |
| 122 | |
} else { |
| 123 | 0 | return service.getActivities(userIds.iterator().next(), request.getGroup(), |
| 124 | |
request.getAppId(), request.getFields(), optionalActivityIds, request.getToken()); |
| 125 | |
} |
| 126 | |
} |
| 127 | |
|
| 128 | 10 | return service.getActivities(userIds, request.getGroup(), |
| 129 | |
request.getAppId(), |
| 130 | |
|
| 131 | |
|
| 132 | |
request.getFields(), request.getToken()); |
| 133 | |
} |
| 134 | |
|
| 135 | |
} |