sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r691249 - in /incubator/sling/trunk/servlets/get/src/main/java/org/apache/sling/servlets/get/helpers: HtmlRendererServlet.java PlainTextRendererServlet.java
Date Tue, 02 Sep 2008 14:01:09 GMT
Author: cziegeler
Date: Tue Sep  2 07:01:07 2008
New Revision: 691249

URL: http://svn.apache.org/viewvc?rev=691249&view=rev
Log:
Return no content if content can't be rendered, only render surrounding html if the request
is not included.

Modified:
    incubator/sling/trunk/servlets/get/src/main/java/org/apache/sling/servlets/get/helpers/HtmlRendererServlet.java
    incubator/sling/trunk/servlets/get/src/main/java/org/apache/sling/servlets/get/helpers/PlainTextRendererServlet.java

Modified: incubator/sling/trunk/servlets/get/src/main/java/org/apache/sling/servlets/get/helpers/HtmlRendererServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/servlets/get/src/main/java/org/apache/sling/servlets/get/helpers/HtmlRendererServlet.java?rev=691249&r1=691248&r2=691249&view=diff
==============================================================================
--- incubator/sling/trunk/servlets/get/src/main/java/org/apache/sling/servlets/get/helpers/HtmlRendererServlet.java
(original)
+++ incubator/sling/trunk/servlets/get/src/main/java/org/apache/sling/servlets/get/helpers/HtmlRendererServlet.java
Tue Sep  2 07:01:07 2008
@@ -22,7 +22,9 @@
 import java.util.Map;
 
 import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletResponse;
 
+import org.apache.sling.api.SlingConstants;
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.SlingHttpServletResponse;
 import org.apache.sling.api.resource.Resource;
@@ -56,22 +58,44 @@
 
         final PrintWriter pw = resp.getWriter();
 
-        pw.println("<html><body>");
-        pw.println("<h1>Resource dumped by " + getClass().getSimpleName() + "</h1>");
-        pw.println("<p>Resource path: <b>" + r.getPath() + "</b></p>");
-        pw.println("<p>Resource metadata: <b>" + r.getResourceMetadata()
-            + "</b></p>");
+        final boolean isIncluded = req.getAttribute(SlingConstants.ATTR_REQUEST_SERVLET)
!= null;
 
         @SuppressWarnings("unchecked")
         final Map map = r.adaptTo(Map.class);
         if ( map != null ) {
+            printProlog(pw, isIncluded);
+            printResourceInfo(pw, r);
             render(pw, r, map);
+            printEpilog(pw, isIncluded);
         } else if ( r.adaptTo(String.class) != null ) {
+            printProlog(pw, isIncluded);
+            printResourceInfo(pw, r);
             render(pw, r, r.adaptTo(String.class));
+            printEpilog(pw, isIncluded);
         } else {
-            pw.println("<p>Resource can't be adapted to a map or a string.</p>");
+            if ( !isIncluded ) {
+                resp.sendError(HttpServletResponse.SC_NO_CONTENT); // NO Content
+            }
         }
-        pw.println("</body></html>");
+    }
+
+    private void printProlog(PrintWriter pw, boolean isIncluded) {
+        if ( !isIncluded ) {
+            pw.println("<html><body>");
+        }
+    }
+
+    private void printEpilog(PrintWriter pw, boolean isIncluded) {
+        if ( !isIncluded ) {
+            pw.println("</body></html>");
+        }
+    }
+
+    private void printResourceInfo(PrintWriter pw, Resource r) {
+        pw.println("<h1>Resource dumped by " + getClass().getSimpleName() + "</h1>");
+        pw.println("<p>Resource path: <b>" + r.getPath() + "</b></p>");
+        pw.println("<p>Resource metadata: <b>" + r.getResourceMetadata()
+            + "</b></p>");
     }
 
     @SuppressWarnings("unchecked")

Modified: incubator/sling/trunk/servlets/get/src/main/java/org/apache/sling/servlets/get/helpers/PlainTextRendererServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/servlets/get/src/main/java/org/apache/sling/servlets/get/helpers/PlainTextRendererServlet.java?rev=691249&r1=691248&r2=691249&view=diff
==============================================================================
--- incubator/sling/trunk/servlets/get/src/main/java/org/apache/sling/servlets/get/helpers/PlainTextRendererServlet.java
(original)
+++ incubator/sling/trunk/servlets/get/src/main/java/org/apache/sling/servlets/get/helpers/PlainTextRendererServlet.java
Tue Sep  2 07:01:07 2008
@@ -22,6 +22,7 @@
 import java.util.Map;
 
 import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletResponse;
 
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.SlingHttpServletResponse;
@@ -60,12 +61,6 @@
         resp.setCharacterEncoding("UTF-8");
 
         final PrintWriter pw = resp.getWriter();
-        renderResource(pw, r);
-    }
-
-    /** Render a resource (map or string) */
-    private void renderResource(PrintWriter pw, Resource r)
-            throws ServletException {
         @SuppressWarnings("unchecked")
         final Map map = r.adaptTo(Map.class);
         if ( map != null ) {
@@ -73,8 +68,7 @@
         } else if ( r.adaptTo(String.class) != null ) {
             printPropertyValue(pw, ResourceUtil.getName(r), r.adaptTo(String.class), false);
         } else {
-            throw new ServletException("Resource " + r
-                + " does not adapt to a map or string.");
+            resp.sendError(HttpServletResponse.SC_NO_CONTENT);
         }
     }
 



Mime
View raw message