/** * {@inheritDoc} * * Post a message for a set of users. */ public Future<Void> createMessage(UserId userId, String appId, String msgCollId, Message message, SecurityToken token) throws ProtocolException { for (String recipient : message.getRecipients()) { try { JSONArray outbox = db.getJSONObject(MESSAGE_TABLE).getJSONArray(recipient); if (outbox == null) { outbox = new JSONArray(); db.getJSONObject(MESSAGE_TABLE).put(recipient, outbox); } outbox.put(message); } catch (JSONException je) { throw new ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(), je); } } return ImmediateFuture.newInstance(null); }
public Future<?> call(Object handler, RequestItem item) { try { Object result; if (inputClass == null) { result = method.invoke(handler); } else if (RequestItem.class.isAssignableFrom(inputClass)) { result = method.invoke(handler, item); } else { result = method.invoke(handler, item.getTypedRequest(inputClass)); } if (result instanceof Future<?>) { return (Future<?>) result; } return ImmediateFuture.newInstance(result); } catch (IllegalAccessException e) { return ImmediateFuture.errorInstance(e); } catch (InvocationTargetException e) { // Unwrap the internal exception return ImmediateFuture.errorInstance(e.getTargetException()); } } }
public Future<?> execute(Map<String, String[]> parameters, Reader body, SecurityToken token, BeanConverter converter) { return ImmediateFuture.errorInstance(error); } }
public Future<?> execute(Map<String, String[]> parameters, Reader body, SecurityToken token, BeanConverter converter) { return ImmediateFuture.errorInstance(error); } }
/** {@inheritDoc} */ public Future<Void> updatePersonData(UserId userId, GroupId groupId, String appId, Set<String> fields, Map<String, String> values, SecurityToken token) throws ProtocolException { // TODO: this seems redundant. No need to pass both fields and a map of // field->value // TODO: According to rest, yes there is. If a field is in the param list // but not in the map // that means it is a delete try { JSONObject personData = db.getJSONObject(DATA_TABLE).getJSONObject(userId.getUserId(token)); if (personData == null) { personData = new JSONObject(); db.getJSONObject(DATA_TABLE).put(userId.getUserId(token), personData); } for (Map.Entry<String, String> entry : values.entrySet()) { personData.put(entry.getKey(), entry.getValue()); } return ImmediateFuture.newInstance(null); } catch (JSONException je) { throw new ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(), je); } }
public Future<?> call(Object handler, RequestItem item) { try { Object result; if (inputClass == null) { result = method.invoke(handler); } else if (RequestItem.class.isAssignableFrom(inputClass)) { result = method.invoke(handler, item); } else { result = method.invoke(handler, item.getTypedRequest(inputClass)); } if (result instanceof Future<?>) { return (Future<?>) result; } return ImmediateFuture.newInstance(result); } catch (IllegalAccessException e) { return ImmediateFuture.errorInstance(e); } catch (InvocationTargetException e) { // Unwrap the internal exception return ImmediateFuture.errorInstance(e.getTargetException()); } } }
public Future<?> execute(Map<String, FormDataItem> formItems, SecurityToken token, BeanConverter converter) { return ImmediateFuture.errorInstance(error); } }
/** {@inheritDoc} */ public Future<Void> deletePersonData(UserId userId, GroupId groupId, String appId, Set<String> fields, SecurityToken token) throws ProtocolException { try { String user = userId.getUserId(token); if (!db.getJSONObject(DATA_TABLE).has(user)) { return null; } JSONObject newPersonData = new JSONObject(); JSONObject oldPersonData = db.getJSONObject(DATA_TABLE).getJSONObject(user); // JSONObject keys are always strings @SuppressWarnings("unchecked") Iterator<String> keys = oldPersonData.keys(); while (keys.hasNext()) { String key = keys.next(); if (!fields.contains(key)) { newPersonData.put(key, oldPersonData.getString(key)); } } db.getJSONObject(DATA_TABLE).put(user, newPersonData); return ImmediateFuture.newInstance(null); } catch (JSONException je) { throw new ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(), je); } }
public Future<?> execute(Map<String, FormDataItem> formItems, SecurityToken token, BeanConverter converter) { return ImmediateFuture.errorInstance(error); } }
/** {@inheritDoc} */ public Future<RestfulCollection<MessageCollection>> getMessageCollections(UserId userId, Set<String> fields, CollectionOptions options, SecurityToken token) throws ProtocolException { try { List<MessageCollection> result = Lists.newArrayList(); JSONObject messageCollections = db.getJSONObject(MESSAGE_TABLE).getJSONObject( userId.getUserId(token)); for (String msgCollId : JSONObject.getNames(messageCollections)) { JSONObject msgColl = messageCollections.getJSONObject(msgCollId); msgColl.put("id", msgCollId); JSONArray messages = msgColl.getJSONArray("messages"); int numMessages = (messages == null) ? 0 : messages.length(); msgColl.put("total", String.valueOf(numMessages)); msgColl.put("unread", String.valueOf(numMessages)); result.add(filterFields(msgColl, fields, MessageCollection.class)); } return ImmediateFuture.newInstance(new RestfulCollection<MessageCollection>(result)); } catch (JSONException je) { throw new ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(), je); } }
public Future<?> execute(JSONObject rpc, Map<String, FormDataItem> formItems, SecurityToken token, BeanConverter converter) { RequestItem item; try { JSONObject params = rpc.has("params") ? (JSONObject)rpc.get("params") : new JSONObject(); item = methodCaller.getRpcRequestItem(params, formItems, token, beanJsonConverter); } catch (Exception e) { return ImmediateFuture.errorInstance(e); } try { listener.executing(item); return methodCaller.call(handlerProvider.get(), item); } catch (Exception e) { return ImmediateFuture.errorInstance(e); } finally { listener.executed(item); } } }
return ImmediateFuture.newInstance(new DataCollection(idToData)); } catch (JSONException je) { throw new ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(),
public Future<?> execute(JSONObject rpc, Map<String, FormDataItem> formItems, SecurityToken token, BeanConverter converter) { RequestItem item; try { JSONObject params = rpc.has("params") ? (JSONObject)rpc.get("params") : new JSONObject(); item = methodCaller.getRpcRequestItem(params, formItems, token, beanJsonConverter); } catch (Exception e) { return ImmediateFuture.errorInstance(e); } try { listener.executing(item); return methodCaller.call(handlerProvider.get(), item); } catch (Exception e) { return ImmediateFuture.errorInstance(e); } finally { listener.executed(item); } } }
/** {@inheritDoc} */ public Future<RestfulCollection<MediaItem>> getMediaItems( Set<UserId> userIds, GroupId groupId, String appId, Set<String> fields, CollectionOptions options, SecurityToken token) throws ProtocolException { try { List<MediaItem> result = Lists.newArrayList(); Set<String> idSet = getIdSet(userIds, groupId, token); // Gather MediaItems for all user IDs for (String id : idSet) { if (db.getJSONObject(MEDIAITEMS_TABLE).has(id)) { JSONArray userMediaItems = db.getJSONObject(MEDIAITEMS_TABLE).getJSONArray(id); for (int i = 0; i < userMediaItems.length(); i++) { result.add(filterFields(userMediaItems.getJSONObject(i), fields, MediaItem.class)); } } } return ImmediateFuture.newInstance(new RestfulCollection<MediaItem>(result)); } catch (JSONException je) { throw new ProtocolException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(), je); } }
public Future<?> execute(Map<String, String[]> parameters, Reader body, SecurityToken token, BeanConverter converter) { RequestItem item; try { // bind the body contents if available if (body != null) { parameters.put(operation.bodyParam(), new String[]{IOUtils.toString(body)}); } item = methodCaller.getRestRequestItem(parameters, token, converter, beanJsonConverter); } catch (Exception e) { return ImmediateFuture.errorInstance(e); } try { listener.executing(item); return methodCaller.call(handlerProvider.get(), item); } catch (Exception e) { return ImmediateFuture.errorInstance(e); } finally { listener.executed(item); } } }
/** {@inheritDoc} */ public Future<RestfulCollection<Activity>> getActivities(UserId userId, GroupId groupId, String appId, Set<String> fields, CollectionOptions options, Set<String> activityIds, SecurityToken token) throws ProtocolException { List<Activity> result = Lists.newArrayList(); try { String user = userId.getUserId(token); if (db.getJSONObject(ACTIVITIES_TABLE).has(user)) { JSONArray activities = db.getJSONObject(ACTIVITIES_TABLE).getJSONArray(user); for (int i = 0; i < activities.length(); i++) { JSONObject activity = activities.getJSONObject(i); if (activity.get(Activity.Field.USER_ID.toString()).equals(user) && activityIds.contains(activity.getString(Activity.Field.ID.toString()))) { result.add(filterFields(activity, fields, Activity.class)); } } } return ImmediateFuture.newInstance(new RestfulCollection<Activity>(result)); } catch (JSONException je) { throw new ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(), je); } }
public Future<?> execute(Map<String, String[]> parameters, Reader body, SecurityToken token, BeanConverter converter) { RequestItem item; try { // bind the body contents if available if (body != null) { parameters.put(operation.bodyParam(), new String[]{IOUtils.toString(body)}); } item = methodCaller.getRestRequestItem(parameters, token, converter, beanJsonConverter); } catch (Exception e) { return ImmediateFuture.errorInstance(e); } try { listener.executing(item); return methodCaller.call(handlerProvider.get(), item); } catch (Exception e) { return ImmediateFuture.errorInstance(e); } finally { listener.executed(item); } } }
/** {@inheritDoc} */ public Future<Void> deleteActivities(UserId userId, GroupId groupId, String appId, Set<String> activityIds, SecurityToken token) throws ProtocolException { try { String user = userId.getUserId(token); if (db.getJSONObject(ACTIVITIES_TABLE).has(user)) { JSONArray activities = db.getJSONObject(ACTIVITIES_TABLE).getJSONArray(user); if (activities != null) { JSONArray newList = new JSONArray(); for (int i = 0; i < activities.length(); i++) { JSONObject activity = activities.getJSONObject(i); if (!activityIds.contains(activity.getString(Activity.Field.ID.toString()))) { newList.put(activity); } } db.getJSONObject(ACTIVITIES_TABLE).put(user, newList); // TODO. This seems very odd that we return no useful response in this // case // There is no way to represent not-found // if (found) { ?? // } } } // What is the appropriate response here?? return ImmediateFuture.newInstance(null); } catch (JSONException je) { throw new ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(), je); } }
@Test public void testErrorInstance() throws Exception { RuntimeException re = new RuntimeException(); Future<String> errorFuture = ImmediateFuture.errorInstance(re); try { errorFuture.get(); fail(); } catch (ExecutionException ee) { assertSame(re, ee.getCause()); } } }
/** {@inheritDoc} */ public Future<Void> createActivity(UserId userId, GroupId groupId, String appId, Set<String> fields, Activity activity, SecurityToken token) throws ProtocolException { // Are fields really needed here? try { JSONObject jsonObject = convertFromActivity(activity, fields); if (!jsonObject.has(Activity.Field.ID.toString())) { jsonObject.put(Activity.Field.ID.toString(), System.currentTimeMillis()); } JSONArray jsonArray = db.getJSONObject(ACTIVITIES_TABLE) .getJSONArray(userId.getUserId(token)); if (jsonArray == null) { jsonArray = new JSONArray(); db.getJSONObject(ACTIVITIES_TABLE).put(userId.getUserId(token), jsonArray); } // TODO (woodser): if used with PUT, duplicate activity would be created? jsonArray.put(jsonObject); return ImmediateFuture.newInstance(null); } catch (JSONException je) { throw new ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(), je); } }