You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by sn...@apache.org on 2014/08/08 21:05:15 UTC

[09/15] git commit: Eliminate use of problematic EntityHolder class from ServiceResource and UserResource.

Eliminate use of problematic EntityHolder class from ServiceResource and UserResource.


Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/696e0fbd
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/696e0fbd
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/696e0fbd

Branch: refs/heads/two-dot-o
Commit: 696e0fbd9fb57bbf63ac1d1e16ae29911bb4af5c
Parents: 223a9c2
Author: Dave Johnson <dm...@apigee.com>
Authored: Tue Aug 5 09:25:36 2014 -0400
Committer: Dave Johnson <dm...@apigee.com>
Committed: Tue Aug 5 09:25:36 2014 -0400

----------------------------------------------------------------------
 .../rest/applications/ServiceResource.java      | 100 +++++++++++++++----
 .../applications/assets/AssetsResource.java     |   9 +-
 .../rest/applications/users/UserResource.java   |   9 +-
 .../rest/applications/users/UsersResource.java  |  65 ++++++------
 4 files changed, 131 insertions(+), 52 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/696e0fbd/stack/rest/src/main/java/org/apache/usergrid/rest/applications/ServiceResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/main/java/org/apache/usergrid/rest/applications/ServiceResource.java b/stack/rest/src/main/java/org/apache/usergrid/rest/applications/ServiceResource.java
index 9aee695..6defbaa 100644
--- a/stack/rest/src/main/java/org/apache/usergrid/rest/applications/ServiceResource.java
+++ b/stack/rest/src/main/java/org/apache/usergrid/rest/applications/ServiceResource.java
@@ -17,6 +17,9 @@
 package org.apache.usergrid.rest.applications;
 
 
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Date;
@@ -64,11 +67,11 @@ import org.apache.usergrid.utils.InflectionUtils;
 import org.apache.commons.lang.StringUtils;
 
 import com.sun.jersey.api.json.JSONWithPadding;
-import com.sun.jersey.core.provider.EntityHolder;
 import com.sun.jersey.multipart.BodyPart;
 import com.sun.jersey.multipart.BodyPartEntity;
 import com.sun.jersey.multipart.FormDataBodyPart;
 import com.sun.jersey.multipart.FormDataMultiPart;
+import java.io.IOException;
 
 import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;
 
@@ -93,6 +96,9 @@ public class ServiceResource extends AbstractContextResource {
     protected static final Logger LOG = LoggerFactory.getLogger( ServiceResource.class );
     private static final String FILE_FIELD_NAME = "file";
 
+    protected static TypeReference<Map<String, Object>> mapTypeReference = new TypeReference<Map<String, Object>>() {};
+    protected static TypeReference<List<Object>> listTypeReference = new TypeReference<List<Object>>() {};
+
     @Autowired
     private BinaryStore binaryStore;
 
@@ -323,16 +329,31 @@ public class ServiceResource extends AbstractContextResource {
     }
 
 
-    @POST
-    @RequireApplicationAccess
-    @Consumes(MediaType.APPLICATION_JSON)
-    public JSONWithPadding executePost( @Context UriInfo ui, EntityHolder<Object> body,
-                                        @QueryParam("callback") @DefaultValue("callback") String callback )
-            throws Exception {
+    /**
+     * Next three new methods necessary to work around inexplicable problems with EntityHolder.
+     * This problem happens consistently when you deploy "two-dot-o" to Tomcat:
+     * https://groups.google.com/forum/#!topic/usergrid/yyAJdmsBfig
+     */
+    protected Object readJsonToObject( String content ) throws IOException {
+        ObjectMapper mapper = new ObjectMapper();
+        JsonNode jsonNode = mapper.readTree( content );
+        Object jsonObject;
+        if ( jsonNode.isArray() ) {
+            jsonObject = mapper.readValue( content, listTypeReference );
+        } else {
+            jsonObject = mapper.readValue( content, mapTypeReference );
+        }
+        return jsonObject;
+    }
 
-        LOG.debug( "ServiceResource.executePost" );
+    /**
+     * Necessary to work around inexplicable problems with EntityHolder.
+     * See above.
+     */
+    public JSONWithPadding executePostWithObject( @Context UriInfo ui, Object json, 
+            @QueryParam("callback") @DefaultValue("callback") String callback ) throws Exception {
 
-        Object json = body.getEntity();
+        LOG.debug( "ServiceResource.executePostWithMap" );
 
         ApiResponse response = createApiResponse();
 
@@ -349,14 +370,12 @@ public class ServiceResource extends AbstractContextResource {
     }
 
 
-    @PUT
-    @RequireApplicationAccess
-    @Consumes(MediaType.APPLICATION_JSON)
-    public JSONWithPadding executePut( @Context UriInfo ui, Map<String, Object> json,
-                                       @QueryParam("callback") @DefaultValue("callback") String callback )
-            throws Exception {
-
-        LOG.debug( "ServiceResource.executePut" );
+    /**
+     * Necessary to work around inexplicable problems with EntityHolder.
+     * See above.
+     */
+    public JSONWithPadding executePutWithMap( @Context UriInfo ui, Map<String, Object> json,
+            @QueryParam("callback") @DefaultValue("callback") String callback ) throws Exception {
 
         ApiResponse response = createApiResponse();
         response.setAction( "put" );
@@ -373,6 +392,53 @@ public class ServiceResource extends AbstractContextResource {
     }
 
 
+    @POST
+    @RequireApplicationAccess
+    @Consumes(MediaType.APPLICATION_JSON)
+    public JSONWithPadding executePost( @Context UriInfo ui, String body, 
+            @QueryParam("callback") @DefaultValue("callback") String callback ) throws Exception {
+
+        LOG.debug( "ServiceResource.executePost: body = " + body );
+
+        Object json;
+        if ( StringUtils.isEmpty( body ) ) {
+            json = null;
+        } else {
+            json = readJsonToObject( body );
+        }
+
+        ApiResponse response = createApiResponse();
+
+
+        response.setAction( "post" );
+        response.setApplication( services.getApplication() );
+        response.setParams( ui.getQueryParameters() );
+
+        ServicePayload payload = getPayload( json );
+
+        executeServiceRequest( ui, response, ServiceAction.POST, payload );
+
+        return new JSONWithPadding( response, callback );
+    }
+
+
+
+    @PUT
+    @RequireApplicationAccess
+    @Consumes(MediaType.APPLICATION_JSON)
+    public JSONWithPadding executePut( @Context UriInfo ui, String body,
+                                       @QueryParam("callback") @DefaultValue("callback") String callback )
+            throws Exception {
+
+        LOG.debug( "ServiceResource.executePut" );
+
+        ObjectMapper mapper = new ObjectMapper();
+        Map<String, Object> json = mapper.readValue( body, mapTypeReference );
+
+        return executePutWithMap(ui, json, callback);
+    }
+
+
     @DELETE
     @RequireApplicationAccess
     public JSONWithPadding executeDelete( @Context UriInfo ui,

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/696e0fbd/stack/rest/src/main/java/org/apache/usergrid/rest/applications/assets/AssetsResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/main/java/org/apache/usergrid/rest/applications/assets/AssetsResource.java b/stack/rest/src/main/java/org/apache/usergrid/rest/applications/assets/AssetsResource.java
index 9370d7f..b43827b 100644
--- a/stack/rest/src/main/java/org/apache/usergrid/rest/applications/assets/AssetsResource.java
+++ b/stack/rest/src/main/java/org/apache/usergrid/rest/applications/assets/AssetsResource.java
@@ -17,6 +17,7 @@
 package org.apache.usergrid.rest.applications.assets;
 
 
+import com.fasterxml.jackson.databind.ObjectMapper;
 import java.io.InputStream;
 import java.util.Date;
 import java.util.Map;
@@ -53,7 +54,6 @@ import org.apache.usergrid.services.assets.data.BinaryStore;
 import org.apache.usergrid.utils.StringUtils;
 
 import com.sun.jersey.api.json.JSONWithPadding;
-import com.sun.jersey.core.header.FormDataContentDisposition;
 import com.sun.jersey.multipart.FormDataParam;
 
 
@@ -96,11 +96,14 @@ public class AssetsResource extends ServiceResource {
     @PUT
     @RequireApplicationAccess
     @Consumes(MediaType.APPLICATION_JSON)
-    public JSONWithPadding executePut( @Context UriInfo ui, Map<String, Object> json,
+    public JSONWithPadding executePut( @Context UriInfo ui, String body,
                                        @QueryParam("callback") @DefaultValue("callback") String callback )
             throws Exception {
 
-        return super.executePut( ui, json, callback );
+        ObjectMapper mapper = new ObjectMapper();
+        Map<String, Object> json = mapper.readValue( body, mapTypeReference );
+
+        return super.executePutWithMap( ui, json, callback );
     }
 
 

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/696e0fbd/stack/rest/src/main/java/org/apache/usergrid/rest/applications/users/UserResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/main/java/org/apache/usergrid/rest/applications/users/UserResource.java b/stack/rest/src/main/java/org/apache/usergrid/rest/applications/users/UserResource.java
index 4f1d1c3..a8b0f81 100644
--- a/stack/rest/src/main/java/org/apache/usergrid/rest/applications/users/UserResource.java
+++ b/stack/rest/src/main/java/org/apache/usergrid/rest/applications/users/UserResource.java
@@ -18,6 +18,7 @@ package org.apache.usergrid.rest.applications.users;
 
 
 import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import java.util.Map;
 import java.util.UUID;
 
@@ -104,15 +105,19 @@ public class UserResource extends ServiceResource {
     @PUT
     @RequireApplicationAccess
     @Consumes(MediaType.APPLICATION_JSON)
-    public JSONWithPadding executePut( @Context UriInfo ui, Map<String, Object> json,
+    public JSONWithPadding executePut( @Context UriInfo ui, String body,
                                        @QueryParam("callback") @DefaultValue("callback") String callback )
             throws Exception {
+
+        ObjectMapper mapper = new ObjectMapper();
+        Map<String, Object> json = mapper.readValue( body, mapTypeReference );
+
         if ( json != null ) {
             json.remove( "password" );
             json.remove( "pin" );
         }
 
-        return super.executePut( ui, json, callback );
+        return super.executePutWithMap( ui, json, callback );
     }
 
 

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/696e0fbd/stack/rest/src/main/java/org/apache/usergrid/rest/applications/users/UsersResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/main/java/org/apache/usergrid/rest/applications/users/UsersResource.java b/stack/rest/src/main/java/org/apache/usergrid/rest/applications/users/UsersResource.java
index b9b2df6..ba32ffe 100644
--- a/stack/rest/src/main/java/org/apache/usergrid/rest/applications/users/UsersResource.java
+++ b/stack/rest/src/main/java/org/apache/usergrid/rest/applications/users/UsersResource.java
@@ -17,10 +17,14 @@
 package org.apache.usergrid.rest.applications.users;
 
 
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.sun.jersey.api.json.JSONWithPadding;
+import com.sun.jersey.api.view.Viewable;
+import com.sun.jersey.core.provider.EntityHolder;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
-
 import javax.ws.rs.Consumes;
 import javax.ws.rs.DefaultValue;
 import javax.ws.rs.FormParam;
@@ -35,32 +39,25 @@ import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.PathSegment;
 import javax.ws.rs.core.UriInfo;
-
-import org.apache.usergrid.rest.RootResource;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.context.annotation.Scope;
-import org.springframework.stereotype.Component;
+import net.tanesha.recaptcha.ReCaptchaImpl;
+import net.tanesha.recaptcha.ReCaptchaResponse;
+import static org.apache.commons.lang.StringUtils.isBlank;
+import static org.apache.commons.lang.StringUtils.isNotBlank;
 import org.apache.usergrid.persistence.Entity;
+import org.apache.usergrid.persistence.entities.User;
 import org.apache.usergrid.persistence.index.query.Identifier;
 import org.apache.usergrid.persistence.index.query.Query;
-import org.apache.usergrid.persistence.entities.User;
 import org.apache.usergrid.rest.AbstractContextResource;
 import org.apache.usergrid.rest.ApiResponse;
+import org.apache.usergrid.rest.RootResource;
 import org.apache.usergrid.rest.applications.ServiceResource;
 import org.apache.usergrid.rest.exceptions.RedirectionException;
 import org.apache.usergrid.rest.security.annotations.RequireApplicationAccess;
-
-import com.sun.jersey.api.json.JSONWithPadding;
-import com.sun.jersey.api.view.Viewable;
-import com.sun.jersey.core.provider.EntityHolder;
-
-import net.tanesha.recaptcha.ReCaptchaImpl;
-import net.tanesha.recaptcha.ReCaptchaResponse;
-
-import static org.apache.commons.lang.StringUtils.isBlank;
-import static org.apache.commons.lang.StringUtils.isNotBlank;
 import static org.apache.usergrid.services.ServiceParameter.addParameter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.context.annotation.Scope;
+import org.springframework.stereotype.Component;
 
 
 @Component("org.apache.usergrid.rest.applications.users.UsersResource")
@@ -191,37 +188,45 @@ public class UsersResource extends ServiceResource {
     @PUT
     @Override
     @RequireApplicationAccess
-    public JSONWithPadding executePut( @Context UriInfo ui, Map<String, Object> json,
+    public JSONWithPadding executePut( @Context UriInfo ui, String body,
                                        @QueryParam("callback") @DefaultValue("callback") String callback )
             throws Exception {
+
+        ObjectMapper mapper = new ObjectMapper();
+        Map<String, Object> json = mapper.readValue( body, mapTypeReference );
+
         User user = getUser();
         if ( user == null ) {
-            return executePost( ui, new EntityHolder( json ), callback );
+            return executePost( ui, body, callback );
         }
         if ( json != null ) {
             json.remove( "password" );
             json.remove( "pin" );
         }
-        return super.executePut( ui, json, callback );
+        return super.executePutWithMap( ui, json, callback );
     }
 
 
     @POST
     @Override
     @RequireApplicationAccess
-    public JSONWithPadding executePost( @Context UriInfo ui, EntityHolder<Object> body,
+    public JSONWithPadding executePost( @Context UriInfo ui, String body,
                                         @QueryParam("callback") @DefaultValue("callback") String callback )
             throws Exception {
-        Object json = body.getEntity();
+
+        LOG.debug( "UsersResource.executePost: body = " + body);
+
+        Object json = readJsonToObject( body );
+
         String password = null;
         String pin = null;
 
-        Boolean registration_requires_email_confirmation = ( Boolean ) this.getServices().getEntityManager()
-                                                                           .getProperty( this.getServices()
-                                                                                             .getApplicationRef(),
-                                                                                   "registration_requires_email_confirmation" );
-        boolean activated =
-                !( ( registration_requires_email_confirmation != null ) && registration_requires_email_confirmation );
+        Boolean confRequred = (Boolean)this.getServices().getEntityManager().getProperty( 
+            this.getServices().getApplicationRef(), "registration_requires_email_confirmation" );
+
+        boolean activated = !( ( confRequred != null ) && confRequred );
+
+        LOG.debug("Confirmation required: {} Activated: {}", confRequred, activated );
 
         if ( json instanceof Map ) {
             @SuppressWarnings("unchecked") Map<String, Object> map = ( Map<String, Object> ) json;
@@ -242,7 +247,7 @@ public class UsersResource extends ServiceResource {
             }
         }
 
-        ApiResponse response = ( ApiResponse ) super.executePost( ui, body, callback ).getJsonSource();
+        ApiResponse response = ( ApiResponse ) super.executePostWithObject( ui, json, callback ).getJsonSource();
 
         if ( ( response.getEntities() != null ) && ( response.getEntities().size() == 1 ) ) {