You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2007/10/19 15:14:03 UTC
svn commit: r586424 - in
/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets:
DefaultSlingServlet.java SlingPostServlet.java
Author: fmeschbe
Date: Fri Oct 19 06:14:03 2007
New Revision: 586424
URL: http://svn.apache.org/viewvc?rev=586424&view=rev
Log:
SLING-67 Provide extensible default Servlets
Removed:
incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/SlingPostServlet.java
Modified:
incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/slingservlets/DefaultSlingServlet.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=586424&r1=586423&r2=586424&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:14:03 2007
@@ -16,31 +16,34 @@
*/
package org.apache.sling.microsling.slingservlets;
-import java.io.OutputStreamWriter;
+import java.io.IOException;
import java.io.PrintWriter;
+import java.util.Enumeration;
import javax.jcr.Item;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
+import javax.jcr.Session;
import javax.jcr.Value;
+import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.sling.microsling.api.SlingRequestContext;
import org.apache.sling.microsling.api.exceptions.HttpStatusCodeException;
-import org.apache.sling.microsling.helpers.servlets.AbstractSlingServlet;
+import org.apache.sling.microsling.helpers.servlets.SlingAllMethodsServlet;
/**
* The default SlingServlet, used if no other SlingServlet wants to process the
* current request.
*/
-public class DefaultSlingServlet extends AbstractSlingServlet {
+public class DefaultSlingServlet extends SlingAllMethodsServlet {
@Override
- public void doGet(HttpServletRequest req, HttpServletResponse resp)
- throws Exception {
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
+ throws ServletException, IOException {
resp.setContentType("text/plain");
final SlingRequestContext ctx = SlingRequestContext.Accessor.getSlingRequestContext(req);
@@ -51,24 +54,98 @@
final Item data = ctx.getResource().getItem();
if (data != null) {
- final PrintWriter pw = new PrintWriter(new OutputStreamWriter(
- resp.getOutputStream()));
- if (data.isNode()) {
- dump(pw, (Node) data);
- } else {
- dump(pw, (Property) data);
+ 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);
}
- pw.flush();
} else {
- throw new HttpStatusCodeException(501,
- "Not implemented: resource "
- + ctx.getResource().getURI() + " cannot be dumped by "
- + getClass().getSimpleName());
+ throw new HttpStatusCodeException(501, "Not implemented: resource "
+ + ctx.getResource().getURI() + " cannot be dumped by "
+ + getClass().getSimpleName());
+ }
+ }
+
+ @Override
+ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ final SlingRequestContext ctx = SlingRequestContext.Accessor.getSlingRequestContext(req);
+
+ // Create or update node under current path
+ final String nodePath = ctx.getRequestPathInfo().getContentPath();
+
+ Session s = null;
+ try {
+ s = ctx.getSession();
+ 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,
+ // 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);
+ }
+
+ // Copy request parameters to node properties and save
+ setPropertiesFromRequest(current,req);
+ s.save();
+ } catch (RepositoryException re) {
+ throw new ServletException("Failed modifying 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
+ }
+ }
+
+ 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();
+ final String value = req.getParameter(name);
+ n.setProperty(name, value);
}
}
- public boolean canProcess(HttpServletRequest req) throws Exception {
- return true;
+ /**
+ * 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 dump(PrintWriter pw, Node n) throws RepositoryException {