Coverage Report - org.apache.shindig.social.opensocial.service.RequestItem
 
Classes in this File Line Coverage Branch Coverage Complexity
RequestItem
74%
42/57
79%
22/28
0
 
 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.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  
  * Abstract base type for social API requests.
 40  
  */
 41  
 public abstract class RequestItem {
 42  
 
 43  
   // Common OpenSocial API fields
 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  
   // Opensocial defaults
 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  
         // Assume @me
 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  
 }