Author: vramdal
Date: Mon Apr 26 09:04:34 2010
New Revision: 937970

URL: http://svn.apache.org/viewvc?rev=937970&view=rev
Log:
SLING-1501 Let JSONResponse set the correct HTTP response code (applying path 
from Simon Gaeremynck - thanks!)

Modified:
    
sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/helper/JSONResponse.java
    
sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/helper/JsonResponseTest.java
    
sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletOutputContentTypeTest.java

Modified: 
sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/helper/JSONResponse.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/helper/JSONResponse.java?rev=937970&r1=937969&r2=937970&view=diff
==============================================================================
--- 
sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/helper/JSONResponse.java
 (original)
+++ 
sling/trunk/bundles/servlets/post/src/main/java/org/apache/sling/servlets/post/impl/helper/JSONResponse.java
 Mon Apr 26 09:04:34 2010
@@ -144,6 +144,20 @@ public class JSONResponse extends HtmlRe
         setReferer(referer);
         response.setContentType(RESPONSE_CONTENT_TYPE);
         response.setCharacterEncoding(RESPONSE_CHARSET);
+        
+        // Status code
+        if (setStatus) {
+          Object status = getProperty(PN_STATUS_CODE);
+          if (status instanceof Number) {
+              int statusCode = ((Number) status).intValue();
+              response.setStatus(statusCode);
+
+              // special treatment of 201/CREATED: Requires Location
+              if (statusCode == HttpServletResponse.SC_CREATED) {
+                  response.setHeader("Location", getLocation());
+              }
+          }
+      }
 
         try {
             json.write(response.getWriter());

Modified: 
sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/helper/JsonResponseTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/helper/JsonResponseTest.java?rev=937970&r1=937969&r2=937970&view=diff
==============================================================================
--- 
sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/helper/JsonResponseTest.java
 (original)
+++ 
sling/trunk/bundles/servlets/post/src/test/java/org/apache/sling/servlets/post/impl/helper/JsonResponseTest.java
 Mon Apr 26 09:04:34 2010
@@ -171,11 +171,9 @@ public class JsonResponseTest extends Te
         }
 
         public void setStatus(int i) {
-            throw new UnsupportedOperationException("Not implemented: " + 
getClass().getName() + ".setStatus");
         }
 
         public void setStatus(int i, String s) {
-            throw new UnsupportedOperationException("Not implemented: " + 
getClass().getName() + ".setStatus");
         }
 
         public String getCharacterEncoding() {

Modified: 
sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletOutputContentTypeTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletOutputContentTypeTest.java?rev=937970&r1=937969&r2=937970&view=diff
==============================================================================
--- 
sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletOutputContentTypeTest.java
 (original)
+++ 
sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/servlets/post/PostServletOutputContentTypeTest.java
 Mon Apr 26 09:04:34 2010
@@ -17,8 +17,15 @@
 package org.apache.sling.launchpad.webapp.integrationtest.servlets.post;
 
 import org.apache.commons.httpclient.Header;
+import org.apache.commons.httpclient.NameValuePair;
 import org.apache.commons.httpclient.methods.PostMethod;
 import org.apache.sling.commons.testing.integration.HttpTestBase;
+import org.apache.sling.servlets.post.SlingPostConstants;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.servlet.http.HttpServletResponse;
 
 /** Test switching the output content-type of the POST servlet using
  *  either an Accept header or :http-equiv-accept parameter */
@@ -75,4 +82,24 @@ public class PostServletOutputContentTyp
     public void testJsonContentTypeWithQ() throws Exception {
         runTest("text/plain; q=0.5, text/html; q=0.8, application/json; 
q=0.9", CONTENT_TYPE_JSON);         
     }
+    
+    public void testJsonContentTypeException() throws Exception {
+      final String testPath = MY_TEST_PATH + "/abs/" + 
System.currentTimeMillis();
+      final String url = HTTP_BASE_URL + "/" + MY_TEST_PATH;
+
+      // create dest as parent
+      testClient.createNode(HTTP_BASE_URL + testPath + "/dest", null);
+
+      // Perform a POST that should fail.
+      final PostMethod post = new PostMethod(url);
+      post.setFollowRedirects(false);
+      post.addParameter(new NameValuePair(SlingPostConstants.RP_DEST, testPath 
+ "/dest/"));
+      post.addParameter(new NameValuePair(SlingPostConstants.RP_OPERATION,
+          SlingPostConstants.OPERATION_COPY));
+      post.addRequestHeader("Accept", CONTENT_TYPE_JSON);
+
+      final int status = httpClient.executeMethod(post);
+      assertEquals(500, status);
+    }
+
 }


Reply via email to