You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ju...@apache.org on 2011/09/15 22:58:26 UTC

svn commit: r1171271 - in /sling/trunk: bundles/servlets/post/ bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/ bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/ bundles/servlets/post/src/test/java/org/apache/s...

Author: justin
Date: Thu Sep 15 20:58:25 2011
New Revision: 1171271

URL: http://svn.apache.org/viewvc?rev=1171271&view=rev
Log:
SLING-2223 - adding PostResponseCreator interface and integration test

Added:
    sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/PostResponseCreator.java
    sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostResponseCreatorTest.java
    sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/servlets/CustomPostResponseCreatorImpl.java
Modified:
    sling/trunk/bundles/servlets/post/pom.xml
    sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java
    sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/SlingPostServletTest.java
    sling/trunk/launchpad/test-services/pom.xml

Modified: sling/trunk/bundles/servlets/post/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/pom.xml?rev=1171271&r1=1171270&r2=1171271&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/post/pom.xml (original)
+++ sling/trunk/bundles/servlets/post/pom.xml Thu Sep 15 20:58:25 2011
@@ -59,7 +59,7 @@
                 <configuration>
                     <instructions>
                         <Export-Package>
-                            org.apache.sling.servlets.post;version=2.0.8
+                            org.apache.sling.servlets.post;version=2.1.0
                         </Export-Package>
                         <Private-Package>
                             org.apache.sling.servlets.post.impl.*

Added: sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/PostResponseCreator.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/PostResponseCreator.java?rev=1171271&view=auto
==============================================================================
--- sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/PostResponseCreator.java (added)
+++ sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/PostResponseCreator.java Thu Sep 15 20:58:25 2011
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package org.apache.sling.servlets.post;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+
+/**
+ * Service interface which allows for alternate implementations of the
+ * PostResponse interface to be created as needed.
+ * 
+ */
+public interface PostResponseCreator {
+    PostResponse createPostResponse(SlingHttpServletRequest req);
+}

Modified: sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java?rev=1171271&r1=1171270&r2=1171271&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java (original)
+++ sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/SlingPostServlet.java Thu Sep 15 20:58:25 2011
@@ -49,6 +49,7 @@ import org.apache.sling.servlets.post.JS
 import org.apache.sling.servlets.post.NodeNameGenerator;
 import org.apache.sling.servlets.post.PostOperation;
 import org.apache.sling.servlets.post.PostResponse;
+import org.apache.sling.servlets.post.PostResponseCreator;
 import org.apache.sling.servlets.post.SlingPostConstants;
 import org.apache.sling.servlets.post.SlingPostOperation;
 import org.apache.sling.servlets.post.SlingPostProcessor;
@@ -86,6 +87,7 @@ import org.slf4j.LoggerFactory;
     @Reference(name = "postProcessor", referenceInterface = SlingPostProcessor.class, cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE, policy = ReferencePolicy.DYNAMIC),
     @Reference(name = "postOperation", referenceInterface = PostOperation.class, cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE, policy = ReferencePolicy.DYNAMIC),
     @Reference(name = "nodeNameGenerator", referenceInterface = NodeNameGenerator.class, cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE, policy = ReferencePolicy.DYNAMIC),
+    @Reference(name = "postResponseCreator", referenceInterface = PostResponseCreator.class, cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE, policy = ReferencePolicy.DYNAMIC),
     @Reference(name = "contentImporter", referenceInterface = ContentImporter.class, cardinality = ReferenceCardinality.OPTIONAL_UNARY, policy = ReferencePolicy.DYNAMIC) })
 public class SlingPostServlet extends SlingAllMethodsServlet {
 
@@ -155,6 +157,12 @@ public class SlingPostServlet extends Sl
 
     private NodeNameGenerator[] cachedNodeNameGenerators = new NodeNameGenerator[0];
 
+    private final List<ServiceReference> delayedPostResponseCreators = new ArrayList<ServiceReference>();
+
+    private final List<ServiceReference> postResponseCreators = new ArrayList<ServiceReference>();
+
+    private PostResponseCreator[] cachedPostResponseCreators = new PostResponseCreator[0];
+
     private ComponentContext componentContext;
 
     private ImportOperation importOperation;
@@ -174,7 +182,7 @@ public class SlingPostServlet extends Sl
         request.setAttribute(VersioningConfiguration.class.getName(), localVersioningConfig);
 
         // prepare the response
-        PostResponse htmlResponse = createHtmlResponse(request);
+        PostResponse htmlResponse = createPostResponse(request);
         htmlResponse.setReferer(request.getHeader("referer"));
 
         PostOperation operation = getSlingPostOperation(request);
@@ -218,7 +226,7 @@ public class SlingPostServlet extends Sl
     }
 
     /**
-     * Creates an instance of a HtmlResponse.
+     * Creates an instance of a PostResponse.
      * @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>
@@ -226,9 +234,17 @@ public class SlingPostServlet extends Sl
      *   <li>the request is a JSON POST request (see SLING-1172)</li>
      *   <li>the request has a request parameter <code>:accept=application/json</code></li>
      * </ul>
-     * or a {@link org.apache.sling.api.servlets.HtmlResponse} otherwise
+     * or a {@link org.apache.sling.api.servlets.PostResponse} otherwise
      */
-     PostResponse createHtmlResponse(SlingHttpServletRequest req) {
+    PostResponse createPostResponse(SlingHttpServletRequest req) {
+        for (final PostResponseCreator creator : cachedPostResponseCreators) {
+            PostResponse response = creator.createPostResponse(req);
+            if (response != null) {
+                return response;
+            }
+        }
+
+        // Fall through to default behavior
         @SuppressWarnings({"MismatchedQueryAndUpdateOfCollection"})
         MediaRangeList mediaRangeList = new MediaRangeList(req);
         if (JSONResponse.RESPONSE_CONTENT_TYPE.equals(mediaRangeList.prefer("text/html", JSONResponse.RESPONSE_CONTENT_TYPE))) {
@@ -351,6 +367,13 @@ public class SlingPostServlet extends Sl
             this.delayedNodeNameGenerators.clear();
         }
 
+        synchronized ( this.delayedPostResponseCreators ) {
+            for(final ServiceReference ref : this.delayedPostResponseCreators) {
+                this.registerPostResponseCreator(ref);
+            }
+            this.delayedPostResponseCreators.clear();
+        }
+
         // default operation: create/modify
         modifyOperation = new ModifyOperation();
         modifyOperation.setExtraNodeNameGenerators(cachedNodeNameGenerators);
@@ -580,6 +603,53 @@ public class SlingPostServlet extends Sl
         }
     }
 
+    protected void bindPostResponseCreator(ServiceReference ref) {
+        synchronized ( this.delayedPostResponseCreators ) {
+            if ( this.componentContext == null ) {
+                this.delayedPostResponseCreators.add(ref);
+            } else {
+                this.registerPostResponseCreator(ref);
+            }
+        }
+    }
+
+    protected void unbindPostResponseCreator(ServiceReference ref) {
+        synchronized ( this.delayedPostResponseCreators ) {
+            this.delayedPostResponseCreators.remove(ref);
+            this.postResponseCreators.remove(ref);
+        }
+    }
+
+    protected void registerPostResponseCreator(ServiceReference ref) {
+        final int ranking = OsgiUtil.toInteger(ref.getProperty(Constants.SERVICE_RANKING), 0);
+        int index = 0;
+        while ( index < this.postResponseCreators.size() &&
+            ranking < OsgiUtil.toInteger(this.postResponseCreators.get(index).getProperty(Constants.SERVICE_RANKING), 0)) {
+            index++;
+        }
+        if ( index == this.postResponseCreators.size() ) {
+            this.postResponseCreators.add(ref);
+        } else {
+            this.postResponseCreators.add(index, ref);
+        }
+        this.cachedPostResponseCreators = new PostResponseCreator[this.postResponseCreators.size()];
+        index = 0;
+        for(final ServiceReference current : this.postResponseCreators) {
+            final PostResponseCreator creator = (PostResponseCreator) this.componentContext.locateService("postResponseCreator", current);
+            if ( creator != null ) {
+                this.cachedPostResponseCreators[index] = creator;
+                index++;
+            }
+        }
+        if ( index < this.cachedPostResponseCreators.length ) {
+            PostResponseCreator[] oldArray = this.cachedPostResponseCreators;
+            this.cachedPostResponseCreators = new PostResponseCreator[index];
+            for(int i=0;i<index;i++) {
+                this.cachedPostResponseCreators[i] = oldArray[i];
+            }
+        }
+    }
+
     protected void bindContentImporter(ContentImporter importer) {
         this.contentImporter = importer;
         if (importOperation != null) {

Modified: sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/SlingPostServletTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/SlingPostServletTest.java?rev=1171271&r1=1171270&r2=1171271&view=diff
==============================================================================
--- sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/SlingPostServletTest.java (original)
+++ sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/SlingPostServletTest.java Thu Sep 15 20:58:25 2011
@@ -68,7 +68,7 @@ public class SlingPostServletTest extend
             }
         };
         SlingPostServlet servlet = new SlingPostServlet();
-        PostResponse result = servlet.createHtmlResponse(req);
+        PostResponse result = servlet.createPostResponse(req);
         assertTrue(result instanceof JSONResponse);
     }
 

Added: sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostResponseCreatorTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostResponseCreatorTest.java?rev=1171271&view=auto
==============================================================================
--- sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostResponseCreatorTest.java (added)
+++ sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostResponseCreatorTest.java Thu Sep 15 20:58:25 2011
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package org.apache.sling.launchpad.webapp.integrationtest.servlets.post;
+
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.sling.commons.testing.integration.HttpTestBase;
+import org.apache.sling.servlets.post.SlingPostConstants;
+
+public class PostResponseCreatorTest extends HttpTestBase {
+    public static final String TEST_BASE_PATH = "/sling-tests";
+    private String postUrl;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        postUrl = HTTP_BASE_URL + TEST_BASE_PATH + "/" + System.currentTimeMillis();
+    }
+
+    public void testCustomPostResponseCreator() throws Exception {
+        final PostMethod post = new PostMethod(postUrl + SlingPostConstants.DEFAULT_CREATE_SUFFIX);
+        post.addParameter(":responseType", "custom");
+        
+        post.setFollowRedirects(false);
+
+        final int status = httpClient.executeMethod(post);
+        assertEquals("Unexpected status response", 201, status);
+
+        assertEquals("Thanks!", post.getResponseBodyAsString());
+
+        post.releaseConnection();
+    }
+}

Modified: sling/trunk/launchpad/test-services/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/test-services/pom.xml?rev=1171271&r1=1171270&r2=1171271&view=diff
==============================================================================
--- sling/trunk/launchpad/test-services/pom.xml (original)
+++ sling/trunk/launchpad/test-services/pom.xml Thu Sep 15 20:58:25 2011
@@ -139,6 +139,11 @@
             <groupId>javax.jcr</groupId>
             <artifactId>jcr</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.servlets.post</artifactId>
+            <version>2.1.1-SNAPSHOT</version>
+        </dependency>
     </dependencies>
 
 </project>

Added: sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/servlets/CustomPostResponseCreatorImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/servlets/CustomPostResponseCreatorImpl.java?rev=1171271&view=auto
==============================================================================
--- sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/servlets/CustomPostResponseCreatorImpl.java (added)
+++ sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/servlets/CustomPostResponseCreatorImpl.java Thu Sep 15 20:58:25 2011
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package org.apache.sling.launchpad.testservices.servlets;
+
+import java.io.IOException;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.servlets.post.AbstractPostResponse;
+import org.apache.sling.servlets.post.PostResponse;
+import org.apache.sling.servlets.post.PostResponseCreator;
+
+/**
+ * Sample implementation of the PostResponseCreator interface.
+ */
+@Component
+@Service
+public class CustomPostResponseCreatorImpl implements PostResponseCreator {
+
+    public PostResponse createPostResponse(SlingHttpServletRequest req) {
+        if ("custom".equals(req.getParameter(":responseType"))) {
+            return new AbstractPostResponse() {
+                
+                public void onChange(String type, String... arguments) {
+                    // NO-OP
+                }
+                
+                @Override
+                protected void doSend(HttpServletResponse response) throws IOException {
+                    response.setContentType("text/html");
+                    response.setCharacterEncoding("UTF-8");
+                    
+                    response.getWriter().write("Thanks!");
+                    response.getWriter().flush();
+                }
+                
+            };
+        } else {
+            return null;
+        }
+    }
+}