| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 127 | 0 | } |
| 128 | |
|
| 129 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
} |