incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r591611 - in /incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling: servlet/ slingservlets/
Date Sat, 03 Nov 2007 13:00:25 GMT
Author: bdelacretaz
Date: Sat Nov  3 06:00:25 2007
New Revision: 591611

URL: http://svn.apache.org/viewvc?rev=591611&view=rev
Log:
SLING-92 - refactoring to prepare for the real microjax POST handler servlet

Added:
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/servlet/MicroslingServletConfig.java
  (with props)
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPostServlet.java
  (with props)
Modified:
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/servlet/MicroslingServletResolver.java
    incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java

Added: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/servlet/MicroslingServletConfig.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/servlet/MicroslingServletConfig.java?rev=591611&view=auto
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/servlet/MicroslingServletConfig.java
(added)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/servlet/MicroslingServletConfig.java
Sat Nov  3 06:00:25 2007
@@ -0,0 +1,53 @@
+/*
+ * 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.microsling.servlet;
+
+import java.util.Collections;
+import java.util.Enumeration;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
+
+/** Simple ServletConfig used to instantiate servlets from microsling code */
+public class MicroslingServletConfig implements ServletConfig {
+
+    private final String servletName;
+    private final ServletContext servletContext;
+
+    public MicroslingServletConfig(String servletName, ServletContext servletContext) {
+        this.servletName = servletName;
+        this.servletContext = servletContext;
+    }
+
+    public String getInitParameter(String name) {
+        return null;
+    }
+
+    public Enumeration<?> getInitParameterNames() {
+        return Collections.enumeration(Collections.emptyList());
+    }
+
+    public ServletContext getServletContext() {
+        return servletContext;
+    }
+
+    public String getServletName() {
+        return servletName;
+    }
+}

Propchange: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/servlet/MicroslingServletConfig.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/servlet/MicroslingServletConfig.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/servlet/MicroslingServletResolver.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/servlet/MicroslingServletResolver.java?rev=591611&r1=591610&r2=591611&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/servlet/MicroslingServletResolver.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/servlet/MicroslingServletResolver.java
Sat Nov  3 06:00:25 2007
@@ -139,8 +139,7 @@
     protected void addServlet(final String resourceType, Servlet servlet) {
 
         try {
-            ServletConfig config = new MicroslingServletConfig(resourceType, getServletContext());
-            servlet.init(config);
+            servlet.init(new MicroslingServletConfig(resourceType, getServletContext()));
 
             // only register if initialization succeeds
             servlets.put(resourceType, servlet);
@@ -154,32 +153,5 @@
 
     protected ServletContext getServletContext() {
         return servletContext;
-    }
-
-    private static class MicroslingServletConfig implements ServletConfig {
-
-        private final String servletName;
-        private final ServletContext servletContext;
-
-        public MicroslingServletConfig(String servletName, ServletContext servletContext)
{
-            this.servletName = servletName;
-            this.servletContext = servletContext;
-        }
-
-        public String getInitParameter(String name) {
-            return null;
-        }
-
-        public Enumeration<?> getInitParameterNames() {
-            return Collections.enumeration(Collections.emptyList());
-        }
-
-        public ServletContext getServletContext() {
-            return servletContext;
-        }
-
-        public String getServletName() {
-            return servletName;
-        }
     }
 }

Modified: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java?rev=591611&r1=591610&r2=591611&view=diff
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java
(original)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.java
Sat Nov  3 06:00:25 2007
@@ -21,26 +21,21 @@
 import java.io.OutputStream;
 import java.net.URL;
 import java.net.URLConnection;
-import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Map;
 
 import javax.jcr.Item;
-import javax.jcr.Node;
-import javax.jcr.RepositoryException;
-import javax.jcr.Session;
 import javax.servlet.Servlet;
 import javax.servlet.ServletException;
-import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.sling.api.HttpStatusCodeException;
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.SlingHttpServletResponse;
-import org.apache.sling.api.request.RequestPathInfo;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.servlets.HttpConstants;
 import org.apache.sling.api.servlets.SlingAllMethodsServlet;
+import org.apache.sling.microsling.servlet.MicroslingServletConfig;
 import org.apache.sling.microsling.slingservlets.renderers.DefaultHtmlRendererServlet;
 import org.apache.sling.microsling.slingservlets.renderers.JsonRendererServlet;
 import org.apache.sling.microsling.slingservlets.renderers.PlainTextRendererServlet;
@@ -51,11 +46,14 @@
  */
 public class DefaultSlingServlet extends SlingAllMethodsServlet {
     private static final long serialVersionUID = -2259461041692895761L;
-
     private Map<String, Servlet> renderingServlets = new HashMap <String, Servlet>();
+    private Servlet postServlet;
 
     @Override
     public void init() throws ServletException {
+        postServlet = new MicrojaxPostServlet();
+        postServlet.init(new MicroslingServletConfig("Microjax POST servlet",getServletContext()));
+        
         String contentType = null;
         final String ctSuffix = "; charset=UTF-8";
         
@@ -112,112 +110,14 @@
                 + " cannot be dumped by " + getClass().getSimpleName());
         }
     }
-
+    
     @Override
-    protected void doPost(SlingHttpServletRequest req, SlingHttpServletResponse resp)
-            throws ServletException, IOException {
-
-        // require a node resource
-        Resource r = req.getResource();
-        if (!(req.getResource().getRawData() instanceof Node)
-            && !Resource.RESOURCE_TYPE_NON_EXISTING.equals(r.getResourceType()))
{
-            throw new HttpStatusCodeException(HttpServletResponse.SC_NOT_FOUND,
-                "Resource not found: " + r.getURI() + " must be missing or a Node");
-        }
-
-        String redirectPath = req.getPathInfo();
-        Session s = null;
-        try {
-            Node current = (Node) req.getResource().getRawData();
-            if (current == null) {
-                Resource root = req.getResourceResolver().getResource("/");
-                if (root != null) {
-                    current = (Node) root.getRawData();
-                } else {
-                    throw new ServletException("Cannot get resource for root node");
-                }
-            }
-            s = current.getSession();
-
-            // Decide whether to create or update a node
-            // TODO: this is a simplistic way of deciding, for now: if we have
-            // no Resource or if the Node that it points to already has child nodes,
-            // we create a new node. Else we update the current node.
-            if(current.hasNodes()) {
-                final RequestPathInfo pathInfo = req.getRequestPathInfo();
-                final String parentPath = pathInfo.getResourcePath();
-                final String newNodePath = (pathInfo.getSuffix() == null)
-                        ? String.valueOf(System.currentTimeMillis())
-                        : pathInfo.getSuffix();
-                current = deepCreateNode(s, parentPath + "/" + newNodePath);
-            }
-
-            // Copy request parameters to node properties and save
-            setPropertiesFromRequest(current, req);
-            s.save();
-            redirectPath = current.getPath();
-
-        } catch (RepositoryException re) {
-            throw new ServletException("Failed to modify content: "
-                + re.getMessage(), re);
-
-        } finally {
-            try {
-                if (s != null && s.hasPendingChanges()) {
-                    s.refresh(false);
-                }
-            } catch (RepositoryException re) {
-                // TODO: might want to log, but don't further care
-            }
-        }
-
-        // redirect to the created node, so that it is displayed using a user-supplied extension
-        String redirectExtension = req.getParameter("slingDisplayExtension");
-        final String redirectUrl =
-            req.getContextPath() + req.getServletPath() + redirectPath
-            + (redirectExtension == null ? "" : "." + redirectExtension)
-        ;
-        resp.sendRedirect(redirectUrl);
+    /** delegate to our postServlet */
+    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)

+    throws ServletException, IOException 
+    {
+        postServlet.service(request, response);
     }
-
-    /** Set node properties from current request (only handles Strings for now) */
-    protected void setPropertiesFromRequest(Node n, HttpServletRequest req)
-            throws RepositoryException {
-        // TODO ignore sling-specific properties like slingDisplayExtension
-        for (Enumeration e = req.getParameterNames(); e.hasMoreElements();) {
-            final String name = (String) e.nextElement();
-            final String[] values = req.getParameterValues(name);
-            if (values.length==1) {
-            	n.setProperty(name, values[0]);
-            } else {
-            	n.setProperty(name, values);
-            }
-        }
-    }
-
-    /**
-     * 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 {
-        String[] pathelems = path.substring(1).split("/");
-        int i = 0;
-        String mypath = "";
-        Node parent = s.getRootNode();
-        while (i < pathelems.length) {
-            String name = pathelems[i];
-            mypath += "/" + name;
-            if (!s.itemExists(mypath)) {
-                parent.addNode(name);
-            }
-            parent = (Node) s.getItem(mypath);
-            i++;
-        }
-        return (parent);
-    }
-
 
     protected void spool(URL url, SlingHttpServletResponse res) throws IOException {
         URLConnection conn = url.openConnection();

Added: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPostServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPostServlet.java?rev=591611&view=auto
==============================================================================
--- incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPostServlet.java
(added)
+++ incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPostServlet.java
Sat Nov  3 06:00:25 2007
@@ -0,0 +1,144 @@
+/*
+ * 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.microsling.slingservlets;
+
+import java.io.IOException;
+import java.util.Enumeration;
+
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.sling.api.HttpStatusCodeException;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.request.RequestPathInfo;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.servlets.SlingAllMethodsServlet;
+
+/** Servlet that implements the microjax POST "protocol", see SLING-92 */
+public class MicrojaxPostServlet extends SlingAllMethodsServlet {
+    private static final long serialVersionUID = 1837674988291697074L;
+
+    @Override
+    protected void doPost(SlingHttpServletRequest req, SlingHttpServletResponse resp)
+            throws ServletException, IOException {
+
+        // require a node resource
+        Resource r = req.getResource();
+        if (!(req.getResource().getRawData() instanceof Node)
+            && !Resource.RESOURCE_TYPE_NON_EXISTING.equals(r.getResourceType()))
{
+            throw new HttpStatusCodeException(HttpServletResponse.SC_NOT_FOUND,
+                "Resource not found: " + r.getURI() + " must be missing or a Node");
+        }
+
+        String redirectPath = req.getPathInfo();
+        Session s = null;
+        try {
+            Node current = (Node) req.getResource().getRawData();
+            if (current == null) {
+                Resource root = req.getResourceResolver().getResource("/");
+                if (root != null) {
+                    current = (Node) root.getRawData();
+                } else {
+                    throw new ServletException("Cannot get resource for root node");
+                }
+            }
+            s = current.getSession();
+
+            // Decide whether to create or update a node
+            // TODO: this is a simplistic way of deciding, for now: if we have
+            // no Resource or if the Node that it points to already has child nodes,
+            // we create a new node. Else we update the current node.
+            if(current.hasNodes()) {
+                final RequestPathInfo pathInfo = req.getRequestPathInfo();
+                final String parentPath = pathInfo.getResourcePath();
+                final String newNodePath = (pathInfo.getSuffix() == null)
+                        ? String.valueOf(System.currentTimeMillis())
+                        : pathInfo.getSuffix();
+                current = deepCreateNode(s, parentPath + "/" + newNodePath);
+            }
+
+            // Copy request parameters to node properties and save
+            setPropertiesFromRequest(current, req);
+            s.save();
+            redirectPath = current.getPath();
+
+        } catch (RepositoryException re) {
+            throw new ServletException("Failed to modify content: "
+                + re.getMessage(), re);
+
+        } finally {
+            try {
+                if (s != null && s.hasPendingChanges()) {
+                    s.refresh(false);
+                }
+            } catch (RepositoryException re) {
+                // TODO: might want to log, but don't further care
+            }
+        }
+
+        // redirect to the created node, so that it is displayed using a user-supplied extension
+        String redirectExtension = req.getParameter("slingDisplayExtension");
+        final String redirectUrl =
+            req.getContextPath() + req.getServletPath() + redirectPath
+            + (redirectExtension == null ? "" : "." + redirectExtension)
+        ;
+        resp.sendRedirect(redirectUrl);
+    }
+
+    /** Set node properties from current request (only handles Strings for now) */
+    protected void setPropertiesFromRequest(Node n, HttpServletRequest req)
+            throws RepositoryException {
+        // TODO ignore sling-specific properties like slingDisplayExtension
+        for (Enumeration<?> e = req.getParameterNames(); e.hasMoreElements();) {
+            final String name = (String) e.nextElement();
+            final String[] values = req.getParameterValues(name);
+            if (values.length==1) {
+                n.setProperty(name, values[0]);
+            } else {
+                n.setProperty(name, values);
+            }
+        }
+    }
+
+    /**
+     * 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 {
+        String[] pathelems = path.substring(1).split("/");
+        int i = 0;
+        String mypath = "";
+        Node parent = s.getRootNode();
+        while (i < pathelems.length) {
+            String name = pathelems[i];
+            mypath += "/" + name;
+            if (!s.itemExists(mypath)) {
+                parent.addNode(name);
+            }
+            parent = (Node) s.getItem(mypath);
+            i++;
+        }
+        return (parent);
+    }
+}

Propchange: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPostServlet.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/microsling/microsling-core/src/main/java/org/apache/sling/microsling/slingservlets/MicrojaxPostServlet.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL



Mime
View raw message