| 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.auth.SecurityToken; |
| 21 | |
import org.apache.shindig.social.ResponseError; |
| 22 | |
import org.apache.shindig.social.opensocial.spi.GroupId; |
| 23 | |
import org.apache.shindig.social.opensocial.spi.PersonService; |
| 24 | |
import org.apache.shindig.social.opensocial.spi.SocialSpiException; |
| 25 | |
import org.apache.shindig.social.opensocial.spi.UserId; |
| 26 | |
|
| 27 | |
import org.joda.time.DateTime; |
| 28 | |
|
| 29 | |
import com.google.common.collect.ImmutableSet; |
| 30 | |
import com.google.common.collect.Lists; |
| 31 | |
import com.google.common.collect.Sets; |
| 32 | |
|
| 33 | |
import java.util.Collections; |
| 34 | |
import java.util.Date; |
| 35 | |
import java.util.List; |
| 36 | |
import java.util.Set; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
public abstract class RequestItem { |
| 42 | |
|
| 43 | |
|
| 44 | |
public static final String APP_ID = "appId"; |
| 45 | |
|
| 46 | |
public static final String USER_ID = "userId"; |
| 47 | |
|
| 48 | |
public static final String GROUP_ID = "groupId"; |
| 49 | |
|
| 50 | |
public static final String START_INDEX = "startIndex"; |
| 51 | |
|
| 52 | |
public static final String COUNT = "count"; |
| 53 | |
|
| 54 | |
public static final String SORT_BY = "sortBy"; |
| 55 | |
public static final String SORT_ORDER = "sortOrder"; |
| 56 | |
|
| 57 | |
public static final String FILTER_BY = "filterBy"; |
| 58 | |
public static final String FILTER_OPERATION = "filterOp"; |
| 59 | |
public static final String FILTER_VALUE = "filterValue"; |
| 60 | |
|
| 61 | |
public static final String FIELDS = "fields"; |
| 62 | |
|
| 63 | |
|
| 64 | |
public static final int DEFAULT_START_INDEX = 0; |
| 65 | |
|
| 66 | |
public static final int DEFAULT_COUNT = 20; |
| 67 | |
|
| 68 | |
public static final String APP_SUBSTITUTION_TOKEN = "@app"; |
| 69 | |
|
| 70 | |
private final SecurityToken token; |
| 71 | |
|
| 72 | |
protected final BeanConverter converter; |
| 73 | |
|
| 74 | |
private final String operation; |
| 75 | |
|
| 76 | |
private final String service; |
| 77 | |
|
| 78 | |
public RequestItem(String service, String operation, SecurityToken token, |
| 79 | 121 | BeanConverter converter) { |
| 80 | 121 | this.service = service; |
| 81 | 121 | this.operation = operation; |
| 82 | 121 | this.token = token; |
| 83 | 121 | this.converter = converter; |
| 84 | 121 | } |
| 85 | |
|
| 86 | |
public String getAppId() { |
| 87 | 54 | String appId = getParameter(APP_ID); |
| 88 | 54 | if (appId != null && appId.equals(APP_SUBSTITUTION_TOKEN)) { |
| 89 | 9 | return token.getAppId(); |
| 90 | |
} else { |
| 91 | 45 | return appId; |
| 92 | |
} |
| 93 | |
} |
| 94 | |
|
| 95 | |
public Date getUpdatedSince() { |
| 96 | 14 | String updatedSince = getParameter("updatedSince"); |
| 97 | 14 | if (updatedSince == null) |
| 98 | 14 | return null; |
| 99 | |
|
| 100 | 0 | DateTime date = new DateTime(updatedSince); |
| 101 | 0 | if (date == null) return null; |
| 102 | |
|
| 103 | 0 | return date.toDate(); |
| 104 | |
} |
| 105 | |
|
| 106 | |
public Set<UserId> getUsers() { |
| 107 | 68 | List<String> ids = getListParameter(USER_ID); |
| 108 | 68 | if (ids.isEmpty()) { |
| 109 | 0 | if (token.getViewerId() != null) { |
| 110 | |
|
| 111 | 0 | ids = Lists.newArrayList("@me"); |
| 112 | 0 | } else { |
| 113 | 0 | throw new IllegalArgumentException("No userId provided and viewer not available"); |
| 114 | |
} |
| 115 | |
} |
| 116 | 68 | Set<UserId> userIds = Sets.newLinkedHashSet(); |
| 117 | 68 | for (String id : ids) { |
| 118 | 71 | userIds.add(UserId.fromJson(id)); |
| 119 | 71 | } |
| 120 | 68 | return userIds; |
| 121 | |
} |
| 122 | |
|
| 123 | |
|
| 124 | |
public GroupId getGroup() { |
| 125 | 66 | return GroupId.fromJson(getParameter(GROUP_ID, "@self")); |
| 126 | |
} |
| 127 | |
|
| 128 | |
public int getStartIndex() { |
| 129 | 18 | String startIndex = getParameter(START_INDEX); |
| 130 | |
try { |
| 131 | 18 | return startIndex == null ? DEFAULT_START_INDEX |
| 132 | |
: Integer.valueOf(startIndex); |
| 133 | 0 | } catch (NumberFormatException nfe) { |
| 134 | 0 | throw new SocialSpiException(ResponseError.BAD_REQUEST, |
| 135 | |
"Parameter " + START_INDEX + " (" + startIndex + ") is not a number."); |
| 136 | |
} |
| 137 | |
} |
| 138 | |
|
| 139 | |
public int getCount() { |
| 140 | 18 | String count = getParameter(COUNT); |
| 141 | |
try { |
| 142 | 18 | return count == null ? DEFAULT_COUNT : Integer.valueOf(count); |
| 143 | 0 | } catch (NumberFormatException nfe) { |
| 144 | 0 | throw new SocialSpiException(ResponseError.BAD_REQUEST, |
| 145 | |
"Parameter " + COUNT + " (" + count + ") is not a number."); |
| 146 | |
} |
| 147 | |
} |
| 148 | |
|
| 149 | |
public String getSortBy() { |
| 150 | 16 | String sortBy = getParameter(SORT_BY); |
| 151 | 16 | return sortBy == null ? PersonService.TOP_FRIENDS_SORT : sortBy; |
| 152 | |
} |
| 153 | |
|
| 154 | |
public PersonService.SortOrder getSortOrder() { |
| 155 | 18 | String sortOrder = getParameter(SORT_ORDER); |
| 156 | |
try { |
| 157 | 18 | return sortOrder == null |
| 158 | |
? PersonService.SortOrder.ascending |
| 159 | |
: PersonService.SortOrder.valueOf(sortOrder); |
| 160 | 0 | } catch (IllegalArgumentException iae) { |
| 161 | 0 | throw new SocialSpiException(ResponseError.BAD_REQUEST, |
| 162 | |
"Parameter " + SORT_ORDER + " (" + sortOrder + ") is not valid."); |
| 163 | |
} |
| 164 | |
} |
| 165 | |
|
| 166 | |
public String getFilterBy() { |
| 167 | 16 | return getParameter(FILTER_BY); |
| 168 | |
} |
| 169 | |
|
| 170 | |
public PersonService.FilterOperation getFilterOperation() { |
| 171 | 16 | String filterOp = getParameter(FILTER_OPERATION); |
| 172 | |
try { |
| 173 | 16 | return filterOp == null |
| 174 | |
? PersonService.FilterOperation.contains |
| 175 | |
: PersonService.FilterOperation.valueOf(filterOp); |
| 176 | 0 | } catch (IllegalArgumentException iae) { |
| 177 | 0 | throw new SocialSpiException(ResponseError.BAD_REQUEST, |
| 178 | |
"Parameter " + FILTER_OPERATION + " (" + filterOp + ") is not valid."); |
| 179 | |
} |
| 180 | |
} |
| 181 | |
|
| 182 | |
public String getFilterValue() { |
| 183 | 16 | String filterValue = getParameter(FILTER_VALUE); |
| 184 | 16 | return filterValue == null ? "" : filterValue; |
| 185 | |
} |
| 186 | |
|
| 187 | |
public Set<String> getFields() { |
| 188 | 53 | return getFields(Collections.<String>emptySet()); |
| 189 | |
} |
| 190 | |
|
| 191 | |
public Set<String> getFields(Set<String> defaultValue) { |
| 192 | 67 | Set<String> result = ImmutableSet.copyOf(getListParameter(FIELDS)); |
| 193 | 67 | if (result.isEmpty()) { |
| 194 | 46 | return defaultValue; |
| 195 | |
} |
| 196 | 21 | return result; |
| 197 | |
} |
| 198 | |
|
| 199 | |
public String getOperation() { |
| 200 | 97 | return operation; |
| 201 | |
} |
| 202 | |
|
| 203 | |
public String getService() { |
| 204 | 68 | return service; |
| 205 | |
} |
| 206 | |
|
| 207 | |
public SecurityToken getToken() { |
| 208 | 64 | return token; |
| 209 | |
} |
| 210 | |
|
| 211 | |
public abstract <T> T getTypedParameter(String parameterName, Class<T> dataTypeClass); |
| 212 | |
|
| 213 | |
public abstract <T> T getTypedParameters(Class<T> dataTypeClass); |
| 214 | |
|
| 215 | |
public abstract void applyUrlTemplate(String urlTemplate); |
| 216 | |
|
| 217 | |
public abstract String getParameter(String paramName); |
| 218 | |
|
| 219 | |
public abstract String getParameter(String paramName, String defaultValue); |
| 220 | |
|
| 221 | |
public abstract List<String> getListParameter(String paramName); |
| 222 | |
} |