Author: enorman
Date: Tue Aug 24 04:36:06 2010
New Revision: 988381

URL: http://svn.apache.org/viewvc?rev=988381&view=rev
Log:
SLING-1676 Let UserManager POST servlets return JSON

Modified:
    sling/trunk/bundles/jcr/jackrabbit-usermanager/pom.xml
    
sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/impl/post/AbstractAuthorizablePostServlet.java
    
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/AbstractAuthenticatedTest.java
    
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/CreateGroupTest.java
    
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/CreateUserTest.java
    
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/RemoveAuthorizablesTest.java
    
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/UpdateGroupTest.java
    
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/UpdateUserTest.java

Modified: sling/trunk/bundles/jcr/jackrabbit-usermanager/pom.xml
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/jackrabbit-usermanager/pom.xml?rev=988381&r1=988380&r2=988381&view=diff
==============================================================================
--- sling/trunk/bundles/jcr/jackrabbit-usermanager/pom.xml (original)
+++ sling/trunk/bundles/jcr/jackrabbit-usermanager/pom.xml Tue Aug 24 04:36:06 
2010
@@ -68,7 +68,8 @@
                         </Private-Package>
                         <Embed-Dependency>
                             
org.apache.sling.servlets.post;inline="org/apache/sling/servlets/post/impl/helper/RequestProperty*
-                                
|org/apache/sling/servlets/post/impl/helper/DateParser*"
+                                
|org/apache/sling/servlets/post/impl/helper/DateParser*
+                                
|org/apache/sling/servlets/post/impl/helper/JSONResponse*"
                         </Embed-Dependency>
                     </instructions>
                 </configuration>
@@ -89,7 +90,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.servlets.post</artifactId>
-            <version>2.0.4-incubator</version>
+            <version>2.0.5-SNAPSHOT</version>
         </dependency>
         <dependency>
             <groupId>org.apache.jackrabbit</groupId>
@@ -109,5 +110,20 @@
             <groupId>org.osgi</groupId>
             <artifactId>org.osgi.compendium</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.jcr.resource</artifactId>
+            <version>2.0.6</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.commons.osgi</artifactId>
+            <version>2.0.6</version>
+            <scope>provided</scope>
+        </dependency>
+               <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+        </dependency>
     </dependencies>
 </project>
\ No newline at end of file

Modified: 
sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/impl/post/AbstractAuthorizablePostServlet.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/impl/post/AbstractAuthorizablePostServlet.java?rev=988381&r1=988380&r2=988381&view=diff
==============================================================================
--- 
sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/impl/post/AbstractAuthorizablePostServlet.java
 (original)
+++ 
sling/trunk/bundles/jcr/jackrabbit-usermanager/src/main/java/org/apache/sling/jackrabbit/usermanager/impl/post/AbstractAuthorizablePostServlet.java
 Tue Aug 24 04:36:06 2010
@@ -44,11 +44,12 @@ import org.apache.sling.api.servlets.Htm
 import org.apache.sling.api.servlets.SlingAllMethodsServlet;
 import org.apache.sling.api.wrappers.SlingRequestPaths;
 import org.apache.sling.commons.osgi.OsgiUtil;
-import org.apache.sling.servlets.post.impl.helper.DateParser;
-import org.apache.sling.servlets.post.impl.helper.RequestProperty;
 import 
org.apache.sling.jackrabbit.usermanager.impl.resource.AuthorizableResourceProvider;
 import org.apache.sling.servlets.post.Modification;
 import org.apache.sling.servlets.post.SlingPostConstants;
+import org.apache.sling.servlets.post.impl.helper.DateParser;
+import org.apache.sling.servlets.post.impl.helper.JSONResponse;
+import org.apache.sling.servlets.post.impl.helper.RequestProperty;
 import org.osgi.service.component.ComponentContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -103,7 +104,7 @@ public abstract class AbstractAuthorizab
             SlingHttpServletResponse httpResponse) throws ServletException,
             IOException {
         // prepare the response
-        HtmlResponse htmlResponse = new HtmlResponse();
+        HtmlResponse htmlResponse = createHtmlResponse(request);
         htmlResponse.setReferer(request.getHeader("referer"));
 
         // calculate the paths
@@ -191,6 +192,23 @@ public abstract class AbstractAuthorizab
     }
 
     /**
+     * Creates an instance of a HtmlResponse.
+     * @param req The request being serviced
+     * @return a {...@link 
org.apache.sling.servlets.post.impl.helper.JSONResponse} if any of these 
conditions are true:
+     * <ul>
+     *   <li>the response content type is application/json
+     * </ul>
+     * or a {...@link org.apache.sling.api.servlets.HtmlResponse} otherwise
+     */
+    protected HtmlResponse createHtmlResponse(SlingHttpServletRequest req) {
+       if 
(JSONResponse.RESPONSE_CONTENT_TYPE.equals(req.getResponseContentType())) {
+               return new JSONResponse();
+       } else {
+            return new HtmlResponse();
+       }
+    }
+    
+    /**
      * Extending Servlet should implement this operation to do the work
      * 
      * @param request the sling http request to process

Modified: 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/AbstractAuthenticatedTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/AbstractAuthenticatedTest.java?rev=988381&r1=988380&r2=988381&view=diff
==============================================================================
--- 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/AbstractAuthenticatedTest.java
 (original)
+++ 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/AbstractAuthenticatedTest.java
 Tue Aug 24 04:36:06 2010
@@ -154,6 +154,62 @@ public abstract class AbstractAuthentica
         }
     }
 
+    /** retrieve the contents of given URL and assert its content type
+     * @param expectedContentType use CONTENT_TYPE_DONTCARE if must not be 
checked 
+     * @throws IOException
+     * @throws HttpException */
+    protected String getAuthenticatedPostContent(Credentials creds, String 
url, String expectedContentType, List<NameValuePair> postParams, int 
expectedStatusCode) throws IOException {
+        final PostMethod post = new PostMethod(url);
+
+        URL baseUrl = new URL(HTTP_BASE_URL);
+        AuthScope authScope = new AuthScope(baseUrl.getHost(), 
baseUrl.getPort(), AuthScope.ANY_REALM);
+        post.setDoAuthentication(true);
+        Credentials oldCredentials = 
httpClient.getState().getCredentials(authScope);
+       try {
+                       httpClient.getState().setCredentials(authScope, creds);
+                       
+               if(postParams!=null) {
+                   final NameValuePair [] nvp = {};
+                   post.setRequestBody(postParams.toArray(nvp));
+               }
+
+               final int status = httpClient.executeMethod(post);
+               final InputStream is = post.getResponseBodyAsStream();
+               final StringBuffer content = new StringBuffer();
+               final String charset = post.getResponseCharSet();
+               final byte [] buffer = new byte[16384];
+               int n = 0;
+               while( (n = is.read(buffer, 0, buffer.length)) > 0) {
+                   content.append(new String(buffer, 0, n, charset));
+               }
+               assertEquals("Expected status " + expectedStatusCode + " for " 
+ url + " (content=" + content + ")",
+                       expectedStatusCode,status);
+               final Header h = post.getResponseHeader("Content-Type");
+               if(expectedContentType == null) {
+                   if(h!=null) {
+                       fail("Expected null Content-Type, got " + h.getValue());
+                   }
+               } else if(CONTENT_TYPE_DONTCARE.equals(expectedContentType)) {
+                   // no check
+               } else if(h==null) {
+                   fail(
+                           "Expected Content-Type that starts with '" + 
expectedContentType
+                           +" but got no Content-Type header at " + url
+                   );
+               } else {
+                   assertTrue(
+                       "Expected Content-Type that starts with '" + 
expectedContentType
+                       + "' for " + url + ", got '" + h.getValue() + "'",
+                       h.getValue().startsWith(expectedContentType)
+                   );
+               }
+               return content.toString();
+                       
+       } finally {
+               httpClient.getState().setCredentials(authScope, oldCredentials);
+       }
+    }
+    
 
     private static Random random = new Random(System.currentTimeMillis());
 

Modified: 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/CreateGroupTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/CreateGroupTest.java?rev=988381&r1=988380&r2=988381&view=diff
==============================================================================
--- 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/CreateGroupTest.java
 (original)
+++ 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/CreateGroupTest.java
 Tue Aug 24 04:36:06 2010
@@ -107,4 +107,23 @@ public class CreateGroupTest extends Abs
                assertEquals("My Test Group", jsonObj.getString("displayName"));
                assertEquals("http://www.apache.org";, jsonObj.getString("url"));
        }
+       
+       
+       /**
+        * Test for SLING-1677
+        */
+       public void testCreateGroupResponseAsJSON() throws IOException, 
JSONException {
+        String postUrl = HTTP_BASE_URL + 
"/system/userManager/group.create.json";
+
+               testGroupId = "testGroup" + random.nextInt();
+               List<NameValuePair> postParams = new ArrayList<NameValuePair>();
+               postParams.add(new NameValuePair(":name", testGroupId));
+               postParams.add(new NameValuePair("marker", testGroupId));
+               Credentials creds = new UsernamePasswordCredentials("admin", 
"admin");
+               String json = getAuthenticatedPostContent(creds, postUrl, 
CONTENT_TYPE_JSON, postParams, HttpServletResponse.SC_OK);
+
+               //make sure the json response can be parsed as a JSON object
+               JSONObject jsonObj = new JSONObject(json);
+               assertNotNull(jsonObj);
+       }       
 }

Modified: 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/CreateUserTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/CreateUserTest.java?rev=988381&r1=988380&r2=988381&view=diff
==============================================================================
--- 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/CreateUserTest.java
 (original)
+++ 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/CreateUserTest.java
 Tue Aug 24 04:36:06 2010
@@ -177,4 +177,25 @@ public class CreateUserTest extends Abst
                httpClient.getState().clearCredentials();
                assertPostStatus(postUrl, 
HttpServletResponse.SC_INTERNAL_SERVER_ERROR, postParams, null);
        }
+       
+       
+       /**
+        * Test for SLING-1677
+        */
+       public void testCreateUserResponseAsJSON() throws IOException, 
JSONException {
+        String postUrl = HTTP_BASE_URL + 
"/system/userManager/user.create.json";
+
+               testUserId = "testUser" + random.nextInt();
+               List<NameValuePair> postParams = new ArrayList<NameValuePair>();
+               postParams.add(new NameValuePair(":name", testUserId));
+               postParams.add(new NameValuePair("marker", testUserId));
+               postParams.add(new NameValuePair("pwd", "testPwd"));
+               postParams.add(new NameValuePair("pwdConfirm", "testPwd"));
+               Credentials creds = new UsernamePasswordCredentials("admin", 
"admin");
+               String json = getAuthenticatedPostContent(creds, postUrl, 
CONTENT_TYPE_JSON, postParams, HttpServletResponse.SC_OK);
+
+               //make sure the json response can be parsed as a JSON object
+               JSONObject jsonObj = new JSONObject(json);
+               assertNotNull(jsonObj);
+       }       
 }

Modified: 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/RemoveAuthorizablesTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/RemoveAuthorizablesTest.java?rev=988381&r1=988380&r2=988381&view=diff
==============================================================================
--- 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/RemoveAuthorizablesTest.java
 (original)
+++ 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/RemoveAuthorizablesTest.java
 Tue Aug 24 04:36:06 2010
@@ -25,6 +25,8 @@ import javax.servlet.http.HttpServletRes
 import org.apache.commons.httpclient.Credentials;
 import org.apache.commons.httpclient.NameValuePair;
 import org.apache.commons.httpclient.UsernamePasswordCredentials;
+import org.apache.sling.commons.json.JSONException;
+import org.apache.sling.commons.json.JSONObject;
 
 /**
  * Tests for the 'removeAuthorizable' Sling Post Operation
@@ -111,5 +113,25 @@ public class RemoveAuthorizablesTest ext
                getUrl = HTTP_BASE_URL + "/system/userManager/group/" + groupId 
+ ".json";
                assertAuthenticatedHttpStatus(creds, getUrl, 
HttpServletResponse.SC_NOT_FOUND, null); //make sure the profile request 
returns some data
        }
+
        
+       /**
+        * Test for SLING-1677
+        */
+       public void testRemoveAuthorizablesResponseAsJSON() throws IOException, 
JSONException {
+               String userId = createTestUser();
+               String groupId = createTestGroup();
+               
+        Credentials creds = new UsernamePasswordCredentials("admin", "admin");
+               
+               String postUrl = HTTP_BASE_URL + 
"/system/userManager.delete.json";
+               List<NameValuePair> postParams = new ArrayList<NameValuePair>();
+               postParams.add(new NameValuePair(":applyTo", "group/" + 
groupId));
+               postParams.add(new NameValuePair(":applyTo", "user/" + userId));
+               String json = getAuthenticatedPostContent(creds, postUrl, 
CONTENT_TYPE_JSON, postParams, HttpServletResponse.SC_OK);
+
+               //make sure the json response can be parsed as a JSON object
+               JSONObject jsonObj = new JSONObject(json);
+               assertNotNull(jsonObj);
+       }       
 }

Modified: 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/UpdateGroupTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/UpdateGroupTest.java?rev=988381&r1=988380&r2=988381&view=diff
==============================================================================
--- 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/UpdateGroupTest.java
 (original)
+++ 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/UpdateGroupTest.java
 Tue Aug 24 04:36:06 2010
@@ -141,5 +141,24 @@ public class UpdateGroupTest extends Abs
         return members;
     }
 
+       /**
+        * Test for SLING-1677
+        */
+       public void testUpdateGroupResponseAsJSON() throws IOException, 
JSONException {
+               testGroupId = createTestGroup();
+
+        String postUrl = HTTP_BASE_URL + "/system/userManager/group/" + 
testGroupId + ".update.json";
+
+               List<NameValuePair> postParams = new ArrayList<NameValuePair>();
+               postParams.add(new NameValuePair("displayName", "My Updated 
Test Group"));
+               postParams.add(new NameValuePair("url", 
"http://www.apache.org/updated";));
+
+               Credentials creds = new UsernamePasswordCredentials("admin", 
"admin");
+               String json = getAuthenticatedPostContent(creds, postUrl, 
CONTENT_TYPE_JSON, postParams, HttpServletResponse.SC_OK);
+
+               //make sure the json response can be parsed as a JSON object
+               JSONObject jsonObj = new JSONObject(json);
+               assertNotNull(jsonObj);
+       }       
 }
 

Modified: 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/UpdateUserTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/UpdateUserTest.java?rev=988381&r1=988380&r2=988381&view=diff
==============================================================================
--- 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/UpdateUserTest.java
 (original)
+++ 
sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/userManager/UpdateUserTest.java
 Tue Aug 24 04:36:06 2010
@@ -112,5 +112,23 @@ public class UpdateUserTest extends Abst
                Credentials creds = new UsernamePasswordCredentials("admin", 
"admin");
                assertAuthenticatedPostStatus(creds, postUrl, 
HttpServletResponse.SC_INTERNAL_SERVER_ERROR, postParams, null);
        }
-       
+
+       /**
+        * Test for SLING-1677
+        */
+       public void testUpdateUserResponseAsJSON() throws IOException, 
JSONException {
+               testUserId = createTestUser();
+               
+        String postUrl = HTTP_BASE_URL + "/system/userManager/user/" + 
testUserId + ".update.json";
+
+               List<NameValuePair> postParams = new ArrayList<NameValuePair>();
+               postParams.add(new NameValuePair("displayName", "My Updated 
Test User"));
+               postParams.add(new NameValuePair("url", 
"http://www.apache.org/updated";));
+               Credentials creds = new UsernamePasswordCredentials(testUserId, 
"testPwd");
+               String json = getAuthenticatedPostContent(creds, postUrl, 
CONTENT_TYPE_JSON, postParams, HttpServletResponse.SC_OK);
+
+               //make sure the json response can be parsed as a JSON object
+               JSONObject jsonObj = new JSONObject(json);
+               assertNotNull(jsonObj);
+       }       
 }


Reply via email to