usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From g...@apache.org
Subject [3/9] incubator-usergrid git commit: small refactor.
Date Thu, 16 Apr 2015 18:09:27 GMT
small refactor.


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

Branch: refs/heads/master
Commit: 27757b3292c97af81ca8e38a93a0c949811ee362
Parents: c15fcc8
Author: Dave Johnson <dmjohnson@apigee.com>
Authored: Tue Apr 14 10:00:44 2015 -0400
Committer: Dave Johnson <dmjohnson@apigee.com>
Committed: Tue Apr 14 10:00:44 2015 -0400

----------------------------------------------------------------------
 .../rest/management/ManagementResource.java     | 75 +++++++++++++-------
 1 file changed, 48 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/27757b32/stack/rest/src/main/java/org/apache/usergrid/rest/management/ManagementResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/main/java/org/apache/usergrid/rest/management/ManagementResource.java
b/stack/rest/src/main/java/org/apache/usergrid/rest/management/ManagementResource.java
index 0dfd7a7..27dda53 100644
--- a/stack/rest/src/main/java/org/apache/usergrid/rest/management/ManagementResource.java
+++ b/stack/rest/src/main/java/org/apache/usergrid/rest/management/ManagementResource.java
@@ -32,6 +32,7 @@ import com.sun.jersey.api.client.config.ClientConfig;
 import com.sun.jersey.api.client.config.DefaultClientConfig;
 import com.sun.jersey.api.json.JSONConfiguration;
 import org.apache.commons.lang.RandomStringUtils;
+import org.apache.usergrid.exception.NotImplementedException;
 import org.apache.usergrid.management.ApplicationCreator;
 import org.apache.usergrid.management.OrganizationInfo;
 import org.apache.usergrid.management.OrganizationOwnerInfo;
@@ -471,12 +472,15 @@ public class ManagementResource extends AbstractContextResource {
      */
     @POST
     @Path( "/externaltoken" )
-    //@RequireSystemAccess TODO: should this be required
     public Response validateExternalToken(
             @Context UriInfo ui,
             Map<String, Object> json,
             @QueryParam( "callback" ) @DefaultValue( "" ) String callback )  throws Exception
{
 
+        if ( StringUtils.isEmpty( properties.getProperty( USERGRID_CENTRAL_URL ))) {
+            throw new NotImplementedException( "External Token Validation Service is not
configured" );
+        }
+
         Object extAccessTokenObj = json.get("ext_access_token");
         if ( extAccessTokenObj == null ) {
             throw new IllegalArgumentException("ext_access_token must be specified");
@@ -520,7 +524,6 @@ public class ManagementResource extends AbstractContextResource {
      */
     @GET
     @Path( "/externaltoken" )
-    //@RequireSystemAccess TODO: should this be required
     public Response validateExternalToken(
                                 @Context UriInfo ui,
                                 @QueryParam( "ext_access_token" ) String extAccessToken,
@@ -529,6 +532,10 @@ public class ManagementResource extends AbstractContextResource {
             throws Exception {
 
 
+        if ( StringUtils.isEmpty( properties.getProperty( USERGRID_CENTRAL_URL ))) {
+            throw new NotImplementedException( "External Token Validation Service is not
configured" );
+        }
+
         if ( extAccessToken == null ) {
             throw new IllegalArgumentException("ext_access_token must be specified");
         }
@@ -537,31 +544,9 @@ public class ManagementResource extends AbstractContextResource {
             throw new IllegalArgumentException("ttl must be specified");
         }
 
-        // create URL of central Usergrid's /management/me endpoint
-
-        String externalUrl = properties.getProperty( USERGRID_CENTRAL_URL ).trim();
-        // be lenient about trailing slash
-        externalUrl = !externalUrl.endsWith( "/" ) ? externalUrl + "/" : externalUrl;
-        String me = externalUrl + "management/me?access_token=" + extAccessToken;
-
-        // use our favorite HTTP client to GET /management/me
-
-        ClientConfig clientConfig = new DefaultClientConfig();
-        clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
-        Client client = Client.create(clientConfig);
-        final JsonNode accessInfoNode;
-        try {
-            accessInfoNode = client.resource( me )
-                    .type(MediaType.APPLICATION_JSON_TYPE)
-                    .get(JsonNode.class);
-
-        } catch ( Exception e ) {
-            // user not found 404
-            String msg = "Cannot find Admin User associated with " + extAccessToken;
-            throw new EntityNotFoundException( msg, e );
-        }
+        // look up user via UG Central's /management/me endpoint.
 
-        // good. user was found in central Usergrid
+        JsonNode accessInfoNode = getMeFromUgCentral( extAccessToken );
 
         JsonNode userNode = accessInfoNode.get( "user" );
         String username = userNode.get( "username" ).getTextValue();
@@ -575,8 +560,8 @@ public class ManagementResource extends AbstractContextResource {
         // if user does not exist locally then we need to fix that
 
         final UUID userId;
-
         final OrganizationInfo organizationInfo = management.getOrganizationByName(username);
+
         if ( organizationInfo == null ) {
 
             // create local user and personal organization, activate user.
@@ -605,6 +590,42 @@ public class ManagementResource extends AbstractContextResource {
     }
 
 
+    /**
+     * Look up Admin User via UG Central's /management/me endpoint.
+     *
+     * @param extAccessToken Access token issued by UG Central of Admin User
+     * @return JsonNode representation of AccessInfo object for Admin User
+     * @throws EntityNotFoundException if access_token is not valid.
+     */
+    private JsonNode getMeFromUgCentral( String extAccessToken )  throws EntityNotFoundException
{
+
+        // create URL of central Usergrid's /management/me endpoint
+
+        String externalUrl = properties.getProperty( USERGRID_CENTRAL_URL ).trim();
+        // be lenient about trailing slash
+        externalUrl = !externalUrl.endsWith( "/" ) ? externalUrl + "/" : externalUrl;
+        String me = externalUrl + "management/me?access_token=" + extAccessToken;
+
+        // use our favorite HTTP client to GET /management/me
+
+        ClientConfig clientConfig = new DefaultClientConfig();
+        clientConfig.getFeatures().put( JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
+        Client client = Client.create(clientConfig);
+        final JsonNode accessInfoNode;
+        try {
+            accessInfoNode = client.resource( me )
+                    .type( MediaType.APPLICATION_JSON_TYPE)
+                    .get(JsonNode.class);
+
+        } catch ( Exception e ) {
+            // user not found 404
+            String msg = "Cannot find Admin User associated with " + extAccessToken;
+            throw new EntityNotFoundException( msg, e );
+        }
+        return accessInfoNode;
+    }
+
+
     String errorMsg = "";
     String responseType;
     String clientId;


Mime
View raw message