Skip to content

Commit

Permalink
SHINDIG-1967: Adjust parameter names in mediaItems to match up with O…
Browse files Browse the repository at this point in the history
…penSocial 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
  • Loading branch information
ankon committed Feb 25, 2014
1 parent 4825a6f commit 9278e49
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,20 @@ public <T> T getTypedParameter(String parameterName, Class<T> dataTypeClass) {
}
}

public <T> T getOptionalTypedParameter(String parameterName, Class<T> 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> T getTypedRequest(Class<T> dataTypeClass) {
try {
return jsonConverter.convertToObject(new JSONObject(this.parameters).toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {


Expand All @@ -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
Expand All @@ -75,7 +75,7 @@ public Future<?> get(SocialRequestItem request) throws ProtocolException {
// Get user, group, album IDs, and MediaItem IDs
Set<UserId> userIds = request.getUsers();
Set<String> optionalAlbumIds = ImmutableSet.copyOf(request.getListParameter("albumId"));
Set<String> optionalMediaItemIds = ImmutableSet.copyOf(request.getListParameter("mediaItemId"));
Set<String> optionalMediaItemIds = ImmutableSet.copyOf(getRequestMediaItemIds(request));

// At least one userId must be specified
HandlerPreconditions.requireNotEmpty(userIds, "No user ID specified");
Expand Down Expand Up @@ -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
*/
Expand All @@ -142,7 +142,7 @@ public Future<?> delete(SocialRequestItem request) throws ProtocolException {
// Get users, Album ID, and MediaItem ID
Set<UserId> userIds = request.getUsers();
Set<String> albumIds = ImmutableSet.copyOf(request.getListParameter("albumId"));
Set<String> mediaItemIds = ImmutableSet.copyOf(request.getListParameter("mediaItemId"));
Set<String> mediaItemIds = ImmutableSet.copyOf(getRequestMediaItemIds(request));

// Exactly one user, Album, and MediaItem must be specified
HandlerPreconditions.requireNotEmpty(userIds, "No userId specified");
Expand All @@ -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<UserId> userIds = request.getUsers();
Expand All @@ -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<UserId> userIds = request.getUsers();
Set<String> albumIds = ImmutableSet.copyOf(request.getListParameter("albumId"));
Set<String> mediaItemIds = ImmutableSet.copyOf(request.getListParameter("mediaItemId"));
Set<String> mediaItemIds = ImmutableSet.copyOf(getRequestMediaItemIds(request));

// Exactly one user, Album, and MediaItem must be specified
HandlerPreconditions.requireNotEmpty(userIds, "No userId specified");
Expand All @@ -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());
}

Expand All @@ -217,4 +217,22 @@ public List<Object> supportedFields(RequestItem request) {
return config.getList(container,
"${Cur['gadgets.features'].opensocial.supportedFields.mediaItem}");
}

protected List<String> getRequestMediaItemIds(SocialRequestItem request) {
List<String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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']," +
Expand All @@ -63,6 +67,9 @@ public void setUp() throws Exception {
registry = new DefaultHandlerRegistry(null, converter,
new HandlerExecutionListener.NoOpHandler());
registry.addHandlers(ImmutableSet.<Object>of(handler));
request = new SocialRequestItem(
Maps.<String, String[]>newHashMap(),
token, converter, converter);
}

@Test
Expand Down Expand Up @@ -101,4 +108,26 @@ public void testSupportedFields() throws Exception {

verify();
}

@Test
public void testGetRequestMediaItemIds() {
List<String> expectedMediaItemIds = Arrays.asList("mediaItemId1");
request.setParameter("mediaItemId", expectedMediaItemIds);
assertEquals(expectedMediaItemIds, handler.getRequestMediaItemIds(request));

List<String> 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());
}
}

0 comments on commit 9278e49

Please sign in to comment.