Coverage Report - org.apache.shindig.social.opensocial.service.RpcRequestItem
 
Classes in this File Line Coverage Branch Coverage Complexity
RpcRequestItem
68%
34/50
92%
11/12
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
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.shindig.social.opensocial.service;
 20  
 
 21  
 import org.apache.shindig.auth.SecurityToken;
 22  
 import org.apache.shindig.social.opensocial.spi.SocialSpiException;
 23  
 import org.apache.shindig.social.ResponseError;
 24  
 
 25  
 import com.google.common.collect.Lists;
 26  
 
 27  
 import org.json.JSONArray;
 28  
 import org.json.JSONException;
 29  
 import org.json.JSONObject;
 30  
 
 31  
 import java.util.Collections;
 32  
 import java.util.List;
 33  
 
 34  
 /**
 35  
  * A JSON-RPC specific implementation of RequestItem
 36  
  */
 37  
 public class RpcRequestItem extends RequestItem {
 38  
 
 39  
   private JSONObject data;
 40  
 
 41  
   static String getService(String rpcMethod) {
 42  23
     return rpcMethod.substring(0, rpcMethod.indexOf('.'));
 43  
   }
 44  
 
 45  
   static String getOperation(String rpcMethod) {
 46  23
     return rpcMethod.substring(rpcMethod.indexOf('.') + 1);
 47  
   }
 48  
 
 49  
   public RpcRequestItem(JSONObject rpc, SecurityToken token,
 50  
       BeanConverter converter) throws JSONException {
 51  23
     super(getService(rpc.getString("method")),
 52  
         getOperation(rpc.getString("method")),
 53  
         token, converter);
 54  23
     if (rpc.has("params")) {
 55  18
       this.data = rpc.getJSONObject("params");
 56  18
     } else {
 57  5
       this.data = new JSONObject();
 58  
     }
 59  23
   }
 60  
 
 61  
   @Override
 62  
   public String getParameter(String paramName) {
 63  
     try {
 64  16
       if (data.has(paramName)) {
 65  9
         return data.getString(paramName);
 66  
       } else {
 67  7
         return null;
 68  
       }
 69  0
     } catch (JSONException je) {
 70  0
       throw new SocialSpiException(ResponseError.BAD_REQUEST, je.getMessage(), je);
 71  
     }
 72  
   }
 73  
 
 74  
   @Override
 75  
   public String getParameter(String paramName, String defaultValue) {
 76  
     try {
 77  1
       if (data.has(paramName)) {
 78  1
         return data.getString(paramName);
 79  
       } else {
 80  0
         return defaultValue;
 81  
       }
 82  0
     } catch (JSONException je) {
 83  0
       throw new SocialSpiException(ResponseError.BAD_REQUEST, je.getMessage(), je);
 84  
     }
 85  
   }
 86  
 
 87  
   @Override
 88  
   public List<String> getListParameter(String paramName) {
 89  
     try {
 90  5
       if (data.has(paramName)) {
 91  4
         if (data.get(paramName) instanceof JSONArray) {
 92  3
           JSONArray jsonArray = data.getJSONArray(paramName);
 93  3
           List<String> returnVal = Lists.newArrayListWithExpectedSize(jsonArray.length());
 94  9
           for (int i = 0; i < jsonArray.length(); i++) {
 95  6
             returnVal.add(jsonArray.getString(i));
 96  
           }
 97  3
           return returnVal;
 98  
         } else {
 99  
           // Allow up-conversion of non-array to array params.
 100  1
           return Lists.newArrayList(data.getString(paramName));
 101  
         }
 102  
       } else {
 103  1
         return Collections.emptyList();
 104  
       }
 105  0
     } catch (JSONException je) {
 106  0
       throw new SocialSpiException(ResponseError.BAD_REQUEST, je.getMessage(), je);
 107  
     }
 108  
   }
 109  
 
 110  
   @Override
 111  
   public <T> T getTypedParameter(String parameterName, Class<T> dataTypeClass) {
 112  
     try {
 113  1
       return converter.convertToObject(data.get(parameterName).toString(), dataTypeClass);
 114  0
     } catch (JSONException je) {
 115  0
       throw new SocialSpiException(ResponseError.BAD_REQUEST, je.getMessage(), je);
 116  
     }
 117  
   }
 118  
   
 119  
   @Override
 120  
   public<T> T getTypedParameters(Class<T> dataTypeClass) {
 121  1
     return converter.convertToObject(data.toString(), dataTypeClass);
 122  
   }
 123  
 
 124  
   @Override
 125  
   public void applyUrlTemplate(String urlTemplate) {
 126  
     // No params in the URL
 127  0
   }
 128  
 
 129  
   /** Method used only by tests */
 130  
   void setParameter(String paramName, String param) {
 131  
     try {
 132  20
       data.put(paramName, param);
 133  0
     } catch (JSONException je) {
 134  0
       throw new IllegalArgumentException(je);
 135  20
     }
 136  20
   }
 137  
 
 138  
   /** Method used only by tests */
 139  
   void setJsonParameter(String paramName, JSONObject param) {
 140  
     try {
 141  1
       data.put(paramName, param);
 142  0
     } catch (JSONException je) {
 143  0
       throw new IllegalArgumentException(je);
 144  1
     }
 145  1
   }
 146  
 
 147  
   /** Method used only by tests */
 148  
   void setListParameter(String paramName, List<String> params) {
 149  
     try {
 150  2
       JSONArray arr = new JSONArray(params);
 151  2
       data.put(paramName, arr);
 152  0
     } catch (JSONException je) {
 153  0
       throw new IllegalArgumentException(je);
 154  2
     }
 155  2
   }
 156  
 }