From 9278e49a1a7bb7e56ea49eab26924b9248678899 Mon Sep 17 00:00:00 2001 From: Andreas Kohn Date: Tue, 25 Feb 2014 16:20:08 +0000 Subject: [PATCH] SHINDIG-1967: Adjust parameter names in mediaItems to match up with OpenSocial 2.5.1 Review: https://reviews.apache.org/r/18431/ git-svn-id: https://svn.apache.org/repos/asf/shindig/trunk@1571724 13f79535-47bb-0310-9956-ffa450edef68 --- .../shindig/protocol/BaseRequestItem.java | 14 +++++++ .../opensocial/service/MediaItemHandler.java | 40 ++++++++++++++----- .../service/MediaItemHandlerTest.java | 37 +++++++++++++++-- 3 files changed, 76 insertions(+), 15 deletions(-) diff --git a/java/common/src/main/java/org/apache/shindig/protocol/BaseRequestItem.java b/java/common/src/main/java/org/apache/shindig/protocol/BaseRequestItem.java index 55570056e..58ac4846e 100644 --- a/java/common/src/main/java/org/apache/shindig/protocol/BaseRequestItem.java +++ b/java/common/src/main/java/org/apache/shindig/protocol/BaseRequestItem.java @@ -211,6 +211,20 @@ public T getTypedParameter(String parameterName, Class dataTypeClass) { } } + public T getOptionalTypedParameter(String parameterName, Class dataTypeClass) { + try { + String json = getParameter(parameterName); + if (json == null) { + return null; + } + return converter.convertToObject(json, dataTypeClass); + } catch (RuntimeException e) { + if (e.getCause() instanceof JSONException) + throw new ProtocolException(HttpServletResponse.SC_BAD_REQUEST, e.getMessage()); + throw e; + } + } + public T getTypedRequest(Class dataTypeClass) { try { return jsonConverter.convertToObject(new JSONObject(this.parameters).toString(), diff --git a/java/social-api/src/main/java/org/apache/shindig/social/opensocial/service/MediaItemHandler.java b/java/social-api/src/main/java/org/apache/shindig/social/opensocial/service/MediaItemHandler.java index 7bdd07e6c..52e688667 100644 --- a/java/social-api/src/main/java/org/apache/shindig/social/opensocial/service/MediaItemHandler.java +++ b/java/social-api/src/main/java/org/apache/shindig/social/opensocial/service/MediaItemHandler.java @@ -43,7 +43,7 @@ * * @since 2.0.0 */ -@Service(name = "mediaItems", path = "/{userId}+/{groupId}/{albumId}/{mediaItemId}+") +@Service(name = "mediaItems", path = "/{userId}+/{groupId}/{albumId}/{id}+") public class MediaItemHandler { @@ -63,7 +63,7 @@ public MediaItemHandler( /* * Handles GET operations. * - * Allowed end-points: /mediaItems/{userId}+/{groupId}/{albumId}/{mediaItemId}+ + * Allowed end-points: /mediaItems/{userId}+/{groupId}/{albumId}/{id}+ * * Examples: /mediaItems/john.doe/@self * /mediaItems/john.doe,jane.doe/@self @@ -75,7 +75,7 @@ public Future get(SocialRequestItem request) throws ProtocolException { // Get user, group, album IDs, and MediaItem IDs Set userIds = request.getUsers(); Set optionalAlbumIds = ImmutableSet.copyOf(request.getListParameter("albumId")); - Set optionalMediaItemIds = ImmutableSet.copyOf(request.getListParameter("mediaItemId")); + Set optionalMediaItemIds = ImmutableSet.copyOf(getRequestMediaItemIds(request)); // At least one userId must be specified HandlerPreconditions.requireNotEmpty(userIds, "No user ID specified"); @@ -133,7 +133,7 @@ public Future get(SocialRequestItem request) throws ProtocolException { /* * Handles DELETE operations. * - * Allowed end-points: /mediaItem/{userId}/@self/{albumId}/{mediaItemId} + * Allowed end-points: /mediaItem/{userId}/@self/{albumId}/{id} * * Examples: /mediaItems/john.doe/@self/1/2 */ @@ -142,7 +142,7 @@ public Future delete(SocialRequestItem request) throws ProtocolException { // Get users, Album ID, and MediaItem ID Set userIds = request.getUsers(); Set albumIds = ImmutableSet.copyOf(request.getListParameter("albumId")); - Set mediaItemIds = ImmutableSet.copyOf(request.getListParameter("mediaItemId")); + Set mediaItemIds = ImmutableSet.copyOf(getRequestMediaItemIds(request)); // Exactly one user, Album, and MediaItem must be specified HandlerPreconditions.requireNotEmpty(userIds, "No userId specified"); @@ -163,7 +163,7 @@ public Future delete(SocialRequestItem request) throws ProtocolException { * * Examples: /mediaItems/john.doe/@self/1 */ - @Operation(httpMethods = "POST", bodyParam = "mediaItem") + @Operation(httpMethods = "POST", bodyParam = "data") public Future create(SocialRequestItem request) throws ProtocolException { // Retrieve userIds and albumIds Set userIds = request.getUsers(); @@ -177,23 +177,23 @@ public Future create(SocialRequestItem request) throws ProtocolException { // Service request return service.createMediaItem(Iterables.getOnlyElement(userIds), request.getAppId(), Iterables.getOnlyElement(albumIds), - request.getTypedParameter("mediaItem", MediaItem.class), + getRequestMediaItem(request), request.getToken()); } /* * Handles PUT operations. * - * Allowed end-points: /mediaItems/{userId}/@self/{albumId}/{mediaItemId} + * Allowed end-points: /mediaItems/{userId}/@self/{albumId}/{id} * * Examples: /mediaItems/john.doe/@self/1/2 */ - @Operation(httpMethods = "PUT", bodyParam = "mediaItem") + @Operation(httpMethods = "PUT", bodyParam = "data") public Future update(SocialRequestItem request) throws ProtocolException { // Retrieve userIds, albumIds, and mediaItemIds Set userIds = request.getUsers(); Set albumIds = ImmutableSet.copyOf(request.getListParameter("albumId")); - Set mediaItemIds = ImmutableSet.copyOf(request.getListParameter("mediaItemId")); + Set mediaItemIds = ImmutableSet.copyOf(getRequestMediaItemIds(request)); // Exactly one user, Album, and MediaItem must be specified HandlerPreconditions.requireNotEmpty(userIds, "No userId specified"); @@ -205,7 +205,7 @@ public Future update(SocialRequestItem request) throws ProtocolException { return service.updateMediaItem(Iterables.getOnlyElement(userIds), request.getAppId(), Iterables.getOnlyElement(albumIds), Iterables.getOnlyElement(mediaItemIds), - request.getTypedParameter("mediaItem", MediaItem.class), + getRequestMediaItem(request), request.getToken()); } @@ -217,4 +217,22 @@ public List supportedFields(RequestItem request) { return config.getList(container, "${Cur['gadgets.features'].opensocial.supportedFields.mediaItem}"); } + + protected List getRequestMediaItemIds(SocialRequestItem request) { + List ids = request.getListParameter("id"); + if (ids.isEmpty()) { + ids = request.getListParameter("mediaItemId"); + } + return ids; + } + + protected MediaItem getRequestMediaItem(SocialRequestItem request) { + // 'data' missing is ok, but then 'mediaItem' must exist. + // 'data' or 'mediaItem' invalid will lead to errors. + MediaItem result = request.getOptionalTypedParameter("data", MediaItem.class); + if (result == null) { + result = request.getTypedParameter("mediaItem", MediaItem.class); + } + return result; + } } diff --git a/java/social-api/src/test/java/org/apache/shindig/social/opensocial/service/MediaItemHandlerTest.java b/java/social-api/src/test/java/org/apache/shindig/social/opensocial/service/MediaItemHandlerTest.java index 242f619da..321107e56 100644 --- a/java/social-api/src/test/java/org/apache/shindig/social/opensocial/service/MediaItemHandlerTest.java +++ b/java/social-api/src/test/java/org/apache/shindig/social/opensocial/service/MediaItemHandlerTest.java @@ -18,8 +18,9 @@ */ package org.apache.shindig.social.opensocial.service; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Maps; +import java.util.Arrays; +import java.util.List; + import org.apache.shindig.common.EasyMockTestCase; import org.apache.shindig.common.testing.FakeGadgetToken; import org.apache.shindig.config.ContainerConfig; @@ -36,7 +37,9 @@ import org.junit.Before; import org.junit.Test; -import java.util.List; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Maps; +import com.google.inject.Guice; public class MediaItemHandlerTest extends EasyMockTestCase { private MediaItemService mediaService; @@ -45,11 +48,12 @@ public class MediaItemHandlerTest extends EasyMockTestCase { private FakeGadgetToken token; protected HandlerRegistry registry; private BeanJsonConverter converter; + protected SocialRequestItem request; @Before public void setUp() throws Exception { token = new FakeGadgetToken(); - converter = mock(BeanJsonConverter.class); + converter = new BeanJsonConverter(Guice.createInjector()); mediaService = mock(MediaItemService.class); JSONObject config = new JSONObject('{' + ContainerConfig.DEFAULT_CONTAINER + ':' + "{'gadgets.container': ['default']," + @@ -63,6 +67,9 @@ public void setUp() throws Exception { registry = new DefaultHandlerRegistry(null, converter, new HandlerExecutionListener.NoOpHandler()); registry.addHandlers(ImmutableSet.of(handler)); + request = new SocialRequestItem( + Maps.newHashMap(), + token, converter, converter); } @Test @@ -101,4 +108,26 @@ public void testSupportedFields() throws Exception { verify(); } + + @Test + public void testGetRequestMediaItemIds() { + List expectedMediaItemIds = Arrays.asList("mediaItemId1"); + request.setParameter("mediaItemId", expectedMediaItemIds); + assertEquals(expectedMediaItemIds, handler.getRequestMediaItemIds(request)); + + List expectedIds = Arrays.asList("id1"); + request.setParameter("id", expectedIds); + assertEquals(expectedIds, handler.getRequestMediaItemIds(request)); + } + + @Test + public void testGetRequestMediaItem() { + String mediaItemJson = "{\"id\":\"mediaItem\"}"; + request.setParameter("mediaItem", mediaItemJson); + assertEquals("mediaItem", handler.getRequestMediaItem(request).getId()); + + String dataJson = "{\"id\":\"data\"}"; + request.setParameter("data", dataJson); + assertEquals("data", handler.getRequestMediaItem(request).getId()); + } }