incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r586456 - in /incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets: DefaultSlingServlet.java StreamServlet.java
Date Fri, 19 Oct 2007 13:53:09 GMT
Author: fmeschbe
Date: Fri Oct 19 06:53:08 2007
New Revision: 586456

URL: http://svn.apache.org/viewvc?rev=586456&view=rev
Log:
Handle Exception currently declared for getResource()

Modified:
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/StreamServlet.java

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java?rev=586456&r1=586455&r2=586456&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java
Fri Oct 19 06:53:08 2007
@@ -33,6 +33,7 @@
 
 import org.apache.sling.microsling.api.SlingRequestContext;
 import org.apache.sling.microsling.api.exceptions.HttpStatusCodeException;
+import org.apache.sling.microsling.api.exceptions.SlingException;
 import org.apache.sling.microsling.helpers.servlets.SlingAllMethodsServlet;
 
 /**
@@ -46,34 +47,43 @@
             throws ServletException, IOException {
         resp.setContentType("text/plain");
 
-        final SlingRequestContext ctx = SlingRequestContext.Accessor.getSlingRequestContext(req);
-        if (ctx.getResource() == null) {
-            throw new HttpStatusCodeException(404, "Resource not found: "
-                + req.getPathInfo());
-        }
+        try {
+            final SlingRequestContext ctx = SlingRequestContext.Accessor.getSlingRequestContext(req);
+            if (ctx.getResource() == null) {
+                throw new HttpStatusCodeException(404, "Resource not found: "
+                    + req.getPathInfo());
+            }
 
-        final Item data = ctx.getResource().getItem();
-        if (data != null) {
-            final PrintWriter pw = resp.getWriter();
-            try {
-                if (data.isNode()) {
-                    dump(pw, (Node) data);
-                } else {
-                    dump(pw, (Property) data);
+            final Item data = ctx.getResource().getItem();
+            if (data != null) {
+                final PrintWriter pw = resp.getWriter();
+                try {
+                    if (data.isNode()) {
+                        dump(pw, (Node) data);
+                    } else {
+                        dump(pw, (Property) data);
+                    }
+                } catch (RepositoryException re) {
+                    throw new ServletException("Cannot dump contents of "
+                        + ctx.getResource().getURI(), re);
                 }
-            } catch (RepositoryException re) {
-                throw new ServletException("Cannot dump contents of "
-                    + ctx.getResource().getURI(), re);
+            } else {
+                throw new HttpStatusCodeException(501,
+                    "Not implemented: resource " + ctx.getResource().getURI()
+                        + " cannot be dumped by " + getClass().getSimpleName());
             }
-        } else {
-            throw new HttpStatusCodeException(501, "Not implemented: resource "
-                + ctx.getResource().getURI() + " cannot be dumped by "
-                + getClass().getSimpleName());
+        } catch (ServletException se) {
+            throw se;
+        } catch (IOException ioe) {
+            throw ioe;
+        } catch (Exception e) {
+            throw new SlingException(e.getMessage(), e);
         }
     }
 
     @Override
-    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
IOException {
+    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
+            throws ServletException, IOException {
         final SlingRequestContext ctx = SlingRequestContext.Accessor.getSlingRequestContext(req);
 
         // Create or update node under current path
@@ -82,30 +92,32 @@
         Session s = null;
         try {
             s = ctx.getSession();
-            Node current  = null;
+            Node current = null;
             boolean createNewNode = false;
-            if(s.itemExists(nodePath)) {
-                current = (Node)s.getItem(nodePath);
-                // TODO this is simplistic...if the node has children we assume it's a folder,
+            if (s.itemExists(nodePath)) {
+                current = (Node) s.getItem(nodePath);
+                // TODO this is simplistic...if the node has children we assume
+                // it's a folder,
                 // so we create a new node under it instead of updating it
                 createNewNode = current.hasNodes();
             } else {
                 createNewNode = true;
             }
 
-            if(createNewNode) {
-                final String newNodePath = nodePath + "/" + System.currentTimeMillis();
-                current = deepCreateNode(s,newNodePath);
+            if (createNewNode) {
+                final String newNodePath = nodePath + "/"
+                    + System.currentTimeMillis();
+                current = deepCreateNode(s, newNodePath);
             }
 
             // Copy request parameters to node properties and save
-            setPropertiesFromRequest(current,req);
+            setPropertiesFromRequest(current, req);
             s.save();
         } catch (RepositoryException re) {
             throw new ServletException("Failed modifying content: "
                 + re.getMessage(), re);
         } finally {
-            try  {
+            try {
                 if (s != null && s.hasPendingChanges()) {
                     s.refresh(false);
                 }
@@ -116,12 +128,13 @@
 
         final String redirectPath = req.getServletPath() + req.getPathInfo();
         resp.sendRedirect(redirectPath);
-     }
+    }
 
     /** Set node properties from current request (only handles Strings for now) */
-    protected void setPropertiesFromRequest(Node n,HttpServletRequest req) throws RepositoryException
{
-        for(Enumeration e = req.getParameterNames(); e.hasMoreElements(); ) {
-            final String name = (String)e.nextElement();
+    protected void setPropertiesFromRequest(Node n, HttpServletRequest req)
+            throws RepositoryException {
+        for (Enumeration e = req.getParameterNames(); e.hasMoreElements();) {
+            final String name = (String) e.nextElement();
             final String value = req.getParameter(name);
             n.setProperty(name, value);
         }
@@ -129,9 +142,11 @@
 
     /**
      * Deep creates a node, parent-padding with nt:unstructured nodes
+     *
      * @param path absolute path to node that needs to be deep-created
      */
-    protected Node deepCreateNode(Session s,String path) throws RepositoryException {
+    protected Node deepCreateNode(Session s, String path)
+            throws RepositoryException {
         String[] pathelems = path.substring(1).split("/");
         int i = 0;
         String mypath = "";

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/StreamServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/StreamServlet.java?rev=586456&r1=586455&r2=586456&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/StreamServlet.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/StreamServlet.java
Fri Oct 19 06:53:08 2007
@@ -64,9 +64,10 @@
 
         SlingRequestContext ctx = SlingRequestContext.Accessor.getSlingRequestContext(request);
 
-        Item item = ctx.getResource().getItem();
 
         try {
+            Item item = ctx.getResource().getItem();
+
             // otherwise handle nt:file/nt:resource specially
             Node node = (Node) item;
             if (node.isNodeType("nt:file")) {
@@ -105,6 +106,9 @@
 
         } catch (RepositoryException re) {
             throw new SlingException(re.getMessage(), re);
+
+        } catch (Exception e) {
+            throw new SlingException(e.getMessage(), e);
         }
     }
 



Mime
View raw message